moltenbar/mod/network
fbt fd3bca11c5 my_config
Signed-off-by: fbt <fbt@fleshless.org>
2016-10-04 15:34:25 +03:00

42 lines
744 B
Ruby

# vim: ft=ruby
class Network < Worker
require 'open3'
def mainloop()
self.write "checking..."
while true do
self.write network_status
sleep(10)
end
end
def network_status
hosts = @my_config['hosts']
net_fail = 0
hosts.each do |h|
i,o,t = Open3.popen2("ping", "-c", "1", h)
t.join
net_fail += 1 unless t.value.exitstatus == 0
end
if net_fail == 0
status = "ok"
fg_colour = @my_config['ok_colour']
elsif net_fail == hosts.length
status = "CRIT: all hosts failed!"
fg_colour = @my_config['crit_colour']
else
status = "WARN: #{net_fail} host(s) failed"
fg_colour = @my_config['warn_colour']
end
return "%{F#{fg_colour}}#{status}%{F-}"
end
end
Modules.add("network", "Network")