moltenbar/mod/network

42 lines
804 B
Plaintext
Raw Normal View History

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