Files
moltenbar/mod/network
T

42 lines
705 B
Ruby
Raw Normal View History

2016-09-27 11:13:42 +03:00
# vim: ft=ruby
2016-09-28 23:30:07 +03:00
class Network < Worker
2016-09-27 16:06:04 +03:00
require 'open3'
2016-09-28 23:30:07 +03:00
def mainloop()
self.write "checking..."
2016-09-27 11:13:42 +03:00
while true do
2016-09-28 23:30:07 +03:00
self.write network_status
2016-09-27 11:13:42 +03:00
sleep(10)
end
end
def network_status
2016-10-04 15:34:25 +03:00
hosts = @my_config['hosts']
2016-09-27 11:13:42 +03:00
2016-10-04 15:26:21 +03:00
net_fail = 0
2016-10-14 14:27:47 +03:00
i,o,t = Open3.popen2("fping", *hosts)
t.join
2016-09-27 16:06:04 +03:00
2016-10-14 14:27:47 +03:00
net_ok = o.each_line.count
2016-09-27 11:13:42 +03:00
2016-10-14 14:35:53 +03:00
i.close; o.close
2016-10-14 14:27:47 +03:00
if net_ok == hosts.count
2016-09-27 11:13:42 +03:00
status = "ok"
2016-10-04 15:34:25 +03:00
fg_colour = @my_config['ok_colour']
2016-10-14 14:27:47 +03:00
elsif net_ok == 0
2016-10-04 15:26:21 +03:00
status = "CRIT: all hosts failed!"
2016-10-04 15:34:25 +03:00
fg_colour = @my_config['crit_colour']
2016-10-04 15:26:21 +03:00
else
2016-10-14 14:27:47 +03:00
status = "WARN: #{net_ok} host(s) failed"
2016-10-04 15:34:25 +03:00
fg_colour = @my_config['warn_colour']
2016-09-27 11:13:42 +03:00
end
return "%{F#{fg_colour}}#{status}%{F-}"
end
end
2016-09-28 23:30:07 +03:00
Modules.add("network", "Network")