diff --git a/index.lua b/index.lua index 078ac12..aa834e8 100644 --- a/index.lua +++ b/index.lua @@ -1,30 +1,21 @@ -- [boundary.com] Redis Lua Plugin -- [author] Ivano Picco --- Requires. +-- Common requires. local utils = require('utils') -local uv_native = require ('uv_native') -local string = require('string') -local split = require('split') -local redis = require('luvit-redis') local timer = require('timer') -local ffi = require ('ffi') local fs = require('fs') local json = require('json') +local os = require ('os') +local tools = require ('tools') + local success, boundary = pcall(require,'boundary') if (not success) then boundary = nil end --- portable gethostname syscall -ffi.cdef [[ - int gethostname (char *, int); -]] -function gethostname() - local buf = ffi.new("uint8_t[?]", 256) - ffi.C.gethostname(buf,256); - return ffi.string(buf) -end +-- Business requires. +local redis = require('luvit-redis') -- Default parameters. local pollInterval = 10000 @@ -41,7 +32,7 @@ _parameters.pollInterval = _parameters.source = (type(_parameters.source) == 'string' and _parameters.source:gsub('%s+', '') ~= '' and _parameters.source ~= nil and _parameters.source) or - gethostname() + os.hostname() _parameters.host = (type(_parameters.host) == 'string' and _parameters.host:gsub('%s+', '') ~= '' and _parameters.host) or @@ -76,7 +67,7 @@ end -- Parse line (i.e. line: "connected_clients : "). function parseEachLine(line) - local t = split(line,':') + local t = tools.split(line,':') if (#t == 2) then currentValues[t[1]]=t[2]; end @@ -91,7 +82,7 @@ function outputs() utils.print('REDIS_KEY_EVICTIONS', diffvalues('evicted_keys'), _parameters.source) utils.print('REDIS_COMMANDS_PROCESSED', diffvalues('total_commands_processed'), _parameters.source) utils.print('REDIS_CONNECTIONS_RECEIVED', diffvalues('total_connections_received'), _parameters.source) - utils.print('REDIS_USED_MEMORY', currentValues.used_memory_rss / uv_native.getTotalMemory(), _parameters.source) + utils.print('REDIS_USED_MEMORY', currentValues.used_memory_rss / os.totalmem(), _parameters.source) end -- Get current values. diff --git a/modules/split/package.lua b/modules/split/package.lua deleted file mode 100644 index 119a318..0000000 --- a/modules/split/package.lua +++ /dev/null @@ -1,7 +0,0 @@ -return { - name = 'split', - version = '0.0.1', - description = 'split string to a table', - author = 'Ivano Picco ', - main = 'src/init.lua' -} diff --git a/modules/split/src/init.lua b/modules/split/src/init.lua deleted file mode 100644 index af52e92..0000000 --- a/modules/split/src/init.lua +++ /dev/null @@ -1,15 +0,0 @@ -local string=require('string'); - -exports = function (inputstr, sep) - if sep == nil then - sep = "%s" - end - local t={} ; local i=1 - for str in string.gmatch(inputstr, "([^"..sep.."]+)") do - t[i] = str - i = i + 1 - end - return t -end - -return exports \ No newline at end of file diff --git a/modules/tools.lua b/modules/tools.lua new file mode 100644 index 0000000..6430598 --- /dev/null +++ b/modules/tools.lua @@ -0,0 +1,65 @@ +-- +-- Module. +-- +local tools = {} + + +-- Requires. +local string = require('string') + +-- +-- Limit a given number x between two boundaries. +-- Either min or max can be nil, to fence on one side only. +-- +tools.fence = function(x, min, max) + return (min and x < min and min) or (max and x > max and max) or x +end + +-- +-- Encode data in Base64 format. +-- +tools.base64 = function(data) + local _lookup = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/' + return ((data:gsub('.', function(x) + local r, b = '', x:byte() + for i = 8, 1, -1 do + r = r .. (b % 2 ^ i - b % 2 ^ (i - 1) > 0 and '1' or '0') + end + return r + end) .. '0000'):gsub('%d%d%d?%d?%d?%d?', function(x) + if #x < 6 then + return '' + end + local c = 0 + for i = 1, 6 do + c = c + (x:sub(i, i) == '1' and 2 ^ (6 - i) or 0) + end + return _lookup:sub(c + 1, c + 1) + end) .. ({ + '', + '==', + '=' + })[#data % 3 + 1]) +end + + +-- +-- Split a string into a table +-- + +tools.split = function (inputstr, sep) + if sep == nil then + sep = "%s" + end + local t={} ; local i=1 + for str in string.gmatch(inputstr, "([^"..sep.."]+)") do + t[i] = str + i = i + 1 + end + return t +end + +-- +-- Export. +-- +return tools \ No newline at end of file