paulgorman.org

< ^ txt

Tue Mar 15 09:39:48 EDT 2016 Slept from ten-something to after seven. Fifty-four and mostly cloudy today. Ides of March. So, it seems that Ruby doesn't have a ++ operator. Fine, but it also doesn't complain if you try to use it. Goals: Work: - Finish up Ruby stuff Done. - Play with Docker A little. https://paulgorman.org/technical/linux-docker.txt Went with Kristen, Scott, Kari, and Karina to a new place for lunch: Bigalora. Good pizza, mediocre gelato. Home: - Read LotFP rules No. - SEMIBUG meeting Done. Michael Lucas gave a talk about FreeBSD filesystems, mostly about GEOM and ZFS. #!/usr/bin/env ruby # A simple Ruby network monitoring script # Paul Gorman 2016 require 'colorize' require 'net/ping' class Health def initialize @history = Array.new for i in 0..99 do @history[i] = 1 end @i = 0 end def up @history[@i] = 1 @i = @i + 1 if @i > 99 @i = 0 end end def down @history[@i] = 0 @i = @i + 1 if @i > 99 @i = 0 end end def get return @history.inject(0, :+) end end class Site def initialize(name, vpn_ip, public_ip, gateway_ip) @name = name @vpn = Net::Ping::External.new(vpn_ip) @public = Net::Ping::External.new(public_ip) @gateway = Net::Ping::External.new(gateway_ip) @health = Health.new end def check if @vpn.ping? @health.up printf "%-16s OK (%d)\n".colorize(:green), @name, @health.get elsif @public.ping? @health.down printf "%-16s VPN unresponsive (%d)\n".colorize(:yellow), @name, @health.get elsif @gateway.ping? @health.down printf "%-16s router unresponsive (%d)\n".colorize(:red), @name, @health.get else @health.down printf "%-16s gateway unresponsive (%d)\n".colorize(:red), @name, @health.get end end end class Server def initialize(name, ip) @name = name @ip = Net::Ping::External.new(ip) @health = Health.new end def check if @ip.ping? @health.up printf "%-16s OK (%d)\n".colorize(:green), @name, @health.get else @health.down printf "%-16s unresponsive (%d)\n".colorize(:red), @name, @health.get end end end sites = Array.new sites.push(Site.new('BV', '10.0.25.1', 'nnn.nnn.210.234', 'nnn.nnn.210.233')) sites.push(Site.new('CL', '10.0.1.1', 'nn.nn.233.189', 'nn.nn.233.190')) sites.push(Site.new('WN', '10.0.10.1', 'nnn.nnn.217.230', 'nnn.nnn.194.114')) servers = Array.new servers.push(Server.new('server1', '10.0.0.1')) servers.push(Server.new('server2', '10.0.0.70')) servers.push(Server.new('server3', '10.0.0.10')) # Trap ^C Signal.trap('INT') { puts ' Bye.' exit } # Trap `Kill ` Signal.trap('TERM') { puts ' Bye.' exit } while true sites.each do |site| site.check sleep(1) end puts '----'.colorize(:cyan) servers.each do |server| server.check sleep(1) end puts '----'.colorize(:cyan) end

< ^ txt