Create a gist now

Instantly share code, notes, and snippets.

simple network monitor with ping and growl
#! /usr/bin/env ruby
# -*- coding: utf-8 -*-
require 'logger'
=begin
WHATIS
* ping monitoring once every 30 secs
* growl and log if network is down
REQUIREMENT
* ruby :)
* ping
* which
* growl ( supplied with ruby-growl gem ) or growlnotify ( supplied with Growl )
GETTING STARTED
configure
@ip_base your network segment
@ipaddrs host => addr Hash
@growl_listener your machine's ip addr
$ ./ping_and_growl.rb &
$ tail -f ping_monitor.log
=end
def main
init()
log = Logger.new( @logfile )
log.level = Logger::INFO
@ipaddrs.each_pair { |name, ip|
Thread.new( log, name, ip ) {
while true
if ( !ping( ip ) )
growl( name )
log.warn( "#{name} きれてる!" )
end
sleep 30
end
}
}
sleep
end
def time
Time.now.strftime( '%H:%M:%S' )
end
def ping( ip )
system( "ping -c 1 #{@ip_base}.#{ip} > /dev/null" )
end
def growl( name )
system( "#{@growl} -s -H #{@growl_listener} -m '#{time()} #{name} きれてる!'" )
end
def init
@logfile = File.join( File.dirname( __FILE__ ), 'ping_monitor.log' )
cmds = %w(
growl
growlnotify
)
@growl = Hash[*cmds.zip( cmds.map { |e|
`which #{e}`.chomp
} ).flatten
].find { |k, v|
v.size > 0
}.last
@ip_base = '192.168.1'
@ipaddrs = {
'foo' => 1,
'bar' => 2,
'baz' => 3
}
@growl_listener = xxx.xxx.xxx.xxx
end
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment