#!/usr/bin/perl # # unless($ARGV[0] && $ARGV[1] && $ARGV[2]) { print "Please supply the following information:\n"; print "ping-test.pl .\n"; exit; } while (1) { &monitor; } sub monitor { use Net::Ping; $sendmail="/usr/sbin/sendmail"; $host=$ARGV[0]; $sensitivty=$ARGV[1]; $email=$ARGV[2]; $p = Net::Ping->new("icmp", 2); $up=$p->ping($host); $p->close(); if ($up) { print "Host $host alive.\n"; $count{$_}=0; } else { $count{$_}++; print "$count{$_}\n"; if ($count{$_} eq $sensitivty) { print "Server is down - notifying admin at $email\n"; # open (MAIL,"|$sendmail -t -f \"$email\""); # print MAIL "To: $email\n"; # print MAIL "From: $email\n"; # print MAIL "Subject: Server down\n"; &recheck; } } } sub recheck { if ($up) { $recovered{$_}++; if ($recovered{$_} eq $sensitivty) { print "Notifying admin at $email of recovery.\n"; # open (MAIL,"|$sendmail -t -f \"$email\""); # print MAIL "To: $email\n"; # print MAIL "From: $email\n"; # print MAIL "Subject: Server down\n"; $recovered{$_}=0; $count{$_}=0; } else { $count{$_}++; print "Still trying to recover.\n"; } } }