#!/usr/bin/perl -w ## check websites connection.msg you whenever spot ## a difficulty or downtime. ## A cron job should be set too. check every 15minutes. ## -v shows output of prob time. ## A db file is saved into the directory where ## this perl file exist as default. You should choose ## a place to save it as saving the config/info file at ## the same place with the exe file is bad !! use strict; use DB_File; use LWP; my (%states,$slow_sites,$down_sites,$back_sites); # turn verbose on which show more info. my $verbose = 1 if (@ARGV); my ($path) = `which $0` =~ m#(.*)/#; my $dbfile = "$path/site_state.db"; tie %states, 'DB_File', $dbfile, O_CREAT|O_RDWR|O_EXCL, 0664; #my ($day,$mon,$date) = split / /,scalar(localtime); my %sites = ( 'perl.com' => 'http://www.perl.com', 'easyya.com' => 'http://www.easyya.com', ); my ($slow,$down) = ('slow','down'); my $br = new LWP::UserAgent; $br->agent( 'CheckAlive/0.01' ); CHECK: while (my ($site,$url) = each %sites) { my ($total,$resp) = request_it( $url ); # if we fetch the site successfully if ($resp->is_success) { print "request $site ,consumed $total sec\n" if($verbose); # take more than 10sec to fetch ? too slow. MORE: if ($total > 10) { # skip if it takes long to acess google, # possibly we have a network problem my ($time) = request_it('http://www.google.com'); next CHECK if ($time > 10); # we may want to do a double check-up # ($total) = request_it ( $site ); next MORE; # case Good connection --> slow connection. # case down connection --> slow connection. if ( !(exists $states{$site}) || defined $states{$site} && $states{$site} eq $down) { $states{$site} = $slow; $slow_sites .= $site." "; next CHECK; } # case ignored.slow connection --> slow connection. } else { # case good connection --> good connection next unless (exists $states{$site}); # case down connection --> good connection # case slow connection --> good connection # clean server bad record. $back_sites .= $site." "; delete $states{$site}; } } else { # skip if it takes long to acess google, # possibly we have a network problem my ($t,$r) = request_it('http://www.google.com'); next CHECK unless ( $r->is_success ); print "Failed: $site. Consumed $total sec\n" if($verbose); # case slow connection --> down connection # case good connection --> down connection if ( !( exists $states{$site} ) || defined $states{$site} && $states{$site} eq $slow ) { $states{$site} = $down; $down_sites .= $site; next CHECK; } # case ignored.down connection --> down connection } } if ($back_sites) { send_msg( $back_sites,'come back to work. :)' ); } if ($slow_sites) { send_msg("hmmm..$slow_sites","too slow,maybe a reboot or hotlink?"); } if ($down_sites) { send_msg( $down_sites,'down, please check.' ); } sub request_it { my $start = time(); my $res = $br->head( $_[0] ); my $end = time(); return ($end-$start, $res); } sub send_msg { my ( $site,$msg ) = @_; print "\nsending server difficulty msg thr icq ....\n"; `echo "msg james/$site $msg" | /usr/local/bin/vicq -b -o`; } untie %states; __END__ %states is hash in form of states = ( site_1 => site_status )