-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathinit.lua
More file actions
105 lines (81 loc) · 2.98 KB
/
init.lua
File metadata and controls
105 lines (81 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
local timer = require('timer')
local boundary = require('boundary')
local io = require('io')
local net = require('net')
local __pgk = "BOUNDARY MEMCACHED"
local client
local _previous = {}
local host = "127.0.0.1"
local port = 11211
local pollInterval = 1000
if (boundary.param ~= nil) then
pollInterval = boundary.param.pollInterval or pollInterval
host = boundary.param.host or host
port = boundary.param.port or port
source = (type(boundary.param.source) == 'string' and boundary.param.source:gsub('%s+', '') ~= '' and boundary.param.source) or
io.popen("uname -n"):read('*line')
end
function berror(err)
if err then print(string.format("%s ERROR: %s", __pgk, tostring(err))) return err end
end
function diff(a, b)
if a == nil or b == nil then return 0 end
return math.max(a - b, 0)
end
-- accumulate a value and return the difference from the previous value
function accumulate(key, newValue)
local oldValue = _previous[key] or newValue
local difference = diff(newValue, oldValue)
_previous[key] = newValue
return difference
end
-- init client
function init()
if client == nil then
client = net.createConnection(port, host, function (err)
if berror(err) then end
end)
end
client:on("error", function(err)
berror(err)
end)
client:on("data", function(data)
local d = {}
for _, v in pairs(split(data, "\r\n")) do
local s = v:gsub("STAT ", "")
local t = {}
for w in s:gmatch("%S+") do
table.insert(t, w)
end
d[t[1]] = tonumber(t[2])
end
print(string.format('MEMCACHED_ALLOCATED %d %s', d.bytes / d.limit_maxbytes, source))
print(string.format('MEMCACHED_CONNECTIONS %d %s', d.curr_connections, source))
print(string.format('MEMCACHED_HITS %d %s', accumulate('get_hits', d.get_hits), source))
print(string.format('MEMCACHED_MISSES %d %s', accumulate('get_misses', d.get_misses), source))
print(string.format('MEMCACHED_ITEMS %d %s', d.curr_items, source))
print(string.format('MEMCACHED_REQUESTS %d %s', accumulate('cmd_get', d.cmd_get) + accumulate('cmd_set', d.cmd_set), source))
print(string.format('MEMCACHED_NETWORK_IN %d %s', accumulate('bytes_read', d.bytes_read), source))
print(string.format('MEMCACHED_NETWORK_OUT %d %s', accumulate('bytes_written', d.bytes_written), source))
end)
end
-- get the natural difference between a and b
function diff(a, b)
if not a or not b then return 0 end
return math.max(a - b, 0)
end
function split(str, delim)
local res = {}
local pattern = string.format("([^%s]+)%s()", delim, delim)
while (true) do
line, pos = str:match(pattern, pos)
if line == nil then break end
table.insert(res, line)
end
return res
end
print("_bevent:MEMCACHED plugin up : version 1.0|t:info|tags:memcached, lua, plugin")
init()
timer.setInterval(pollInterval, function ()
client:write("stats " .. "\r\n")
end)