-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathinit.lua
More file actions
86 lines (67 loc) · 2.57 KB
/
init.lua
File metadata and controls
86 lines (67 loc) · 2.57 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
local framework = require('framework')
local CommandOutputDataSource = framework.CommandOutputDataSource
local PollerCollection = framework.PollerCollection
local DataSourcePoller = framework.DataSourcePoller
local Plugin = framework.Plugin
local os = require('os')
local table = require('table')
local string = require('string')
local isEmpty = framework.string.isEmpty
local clone = framework.table.clone
local params = framework.params
local commands = {
linux = { path = '/bin/ping', args = {'-n', '-w 2', '-c 1'} },
win32 = { path = 'C:/windows/system32/ping.exe', args = {'-n', '1', '-w', '3000'} },
darwin = { path = '/sbin/ping', args = {'-n', '-t 2', '-c 1'} }
}
local ping_command = commands[string.lower(os.type())]
if ping_command == nil then
print("_bevent:"..(Plugin.name or params.name)..":"..(Plugin.version or params.version)..":Your platform is not supported. We currently support Linux, Windows and OSX|t:error|tags:lua,plugin"..(Plugin.tags and framework.string.concat(Plugin.tags, ',') or params.tags))
process:exit(-1)
end
local function createPollers (params, cmd)
local pollers = PollerCollection:new()
for _, item in ipairs(params.items) do
cmd = clone(cmd)
table.insert(cmd.args, item.host)
cmd.info = item.source
local data_source = CommandOutputDataSource:new(cmd)
local poll_interval = tonumber(item.pollInterval or params.pollInterval) * 1000
local poller = DataSourcePoller:new(poll_interval, data_source)
pollers:add(poller)
end
return pollers
end
local function parseOutput(context, output)
assert(output ~= nil, 'parseOutput expect some data')
if isEmpty(output) then
context:emit('error', 'Unable to obtain any output.')
return
end
if (string.find(output, "unknown host") or string.find(output, "could not find host.")) then
context:emit('error', 'The host ' .. context.args[#context.args] .. ' was not found.')
return
end
local index
local prevIndex = 0
while true do
index = string.find(output, '\n', prevIndex+1)
if not index then break end
local line = string.sub(output, prevIndex, index-1)
local _, _, time = string.find(line, "time=([0-9]*%.?[0-9]+)")
if time then
return tonumber(time)
end
prevIndex = index
end
return -1
end
local pollers = createPollers(params, ping_command)
local plugin = Plugin:new(params, pollers)
function plugin:onParseValues(data)
local result = {}
local value = parseOutput(self, data['output'])
result['PING_RESPONSETIME'] = { value = value, source = data['info'] }
return result
end
plugin:run()