-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpinky_ping.lua
63 lines (55 loc) · 1.78 KB
/
pinky_ping.lua
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
local p = require 'pinky'
local pinky_main;
local report_ping;
local function pinky_main(uri,ps)
-- This function is the entry point.
local args = p.split(uri,"/")
-- Arguments:
-- 0: /port we return usage
if #args == 1 then
ps.ip = p.get_ip(tostring(args[1]))
if ps.ip then
ps = report_ping(ps)
else
ps.status.value,ps.status.error = "FAIL", "Could not resolve the host provided "
end
else
ps.status.value,ps.status.error = "FAIL", "Usage: /pinky/ping/<hostname>"
end
return ps
end
function report_ping(ps)
local host_os = p.get_os()
local host = ps.ip
if host_os == "Darwin" then
ping = "/sbin/ping -n -c 1 -W 1 " .. host
elseif host_os == "Linux" then
ping = "/bin/ping -n -c 1 -w 1 " .. host
end
if ping then
local ping_out = p.exec_command(ping) -- ,nil,nil," +", true)
local unknown = 'ping: unknown host'
local failure = '100%% packet loss,'
local success = ' 0%% packet loss,'
local success_osx = ' 0.0%% packet loss'
ps.data = ping_out
ps.ping_time = ping_out:match("time=(%d+%.?%d+)")
if ping_out then
if string.find(ping_out,failure) then
ps.status.value,ps.status.error = "FAIL", failure
elseif string.find(ping_out,success) then
ps.status.value = "OK"
elseif string.find(ping_out,success_osx) then
ps.status.value = "OK"
elseif string.find(ping_out,unknown) then
ps.status.value,ps.status.error = "FAIL", unknown
else
ps.status.value,ps.status.error = "FAIL", "fall through"
end
else
ps.status.value,ps.status.error = "FAIL", "ping returned nil"
end
end
return ps
end
return { pinky_main = pinky_main }