#!/usr/bin/perl use strict; use Net::Ping; use Mail::Send; die "Usage: svcheck \n\n" unless defined ($ARGV[0]); my @email; # Array of email addresses from command line my @ip; # Array of hosts from command line (IP or hostname) # Break down command args... foreach my $arg (@ARGV) { if ($arg=~/(\S+@\S+)/) { push(@email, $arg); } else { push(@ip, $arg); } } my $time; my @svcheck; if ( -f "/tmp/time_svcheck" ) { open F, "/tmp/time_svcheck"; @svcheck=; } # Loop through hosts my $ip; my $last; foreach $ip (@ip) { # Ping the host... my $p = Net::Ping->new("icmp", 6); my $ret=$p->ping($ip,6); $p->close(); # If ping was unsuccessful, send mail to all email recipients. if (! $ret) { # Check to see if host is in /tmp/time_svcheck $last=0; foreach my $l (@svcheck) { chomp $l; if(!$last && $l =~ /^\d+$/) { $time=$l; } if ($ip eq $l) { $last=1; } } print "Last: $last\nTime: $time\ntime: ", time, "\n"; if ($last && time - $time < 1800) { print $_=localtime(time), ": Throttling email output.\n"; } else { print $_=localtime(time), ": Host $ip is down?\n"; my $msg=new Mail::Send; #$msg->to('kenw@cruzoid.com'); foreach my $to (@email) { $msg->to($msg->to, $to); print $_=localtime(time), ": Mailing $to\n"; } $msg->subject("$ip: Host not responding to ping"); my $fh=$msg->open; print $fh $_=localtime(time), ": $ip is down?"; $fh->close; # Close fires off the email push @svcheck, $ip; } } else { # Usually unnecessary output, but can be dumped into a log file or something... print $_=localtime(time), ": Host $ip is okay\n"; # Remove host from /tmp/time_svcheck file my $i; my $l; for ($i=0; $i<=$#svcheck; $i++) { $l=$svcheck[$i]; chomp($l); if($l eq $ip) { splice(@svcheck, $i, 1); } } } } open F, ">/tmp/time_svcheck"; print F time, "\n"; foreach my $l (@svcheck) { print F $l, "\n"; } close F;