-
-
Save mrandreastoth/b7284d1fbd891dafa494d32cdc8482cc to your computer and use it in GitHub Desktop.
simple network monitor with ping and growl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /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