in reply to Ping Monitor with Email Notification
use strict; use warnings; use Data::Dumper; use Net::Ping; use MIME::Lite; use Getopt::Long; use Pod::Usage; use vars qw( $ping $first $delay $second @host $file $help $to $from ); parse_args(); $to ||= 'linebacker@perlmonks.org'; $from ||= 'ICMP Monitor <joeblow@i.com>'; $first ||= 5; $delay ||= 60; $second ||= 30; $ping = Net::Ping->new('icmp'); foreach my $host (@host) { print "$host is "; unless ($ping->ping($host, $first)) { sleep ($delay); unless ($ping->ping($host, $second)) { notify($host); print 'NOT '; } } print "reachable.\n"; } $ping->close(); sub notify { my @host = @_; my $data = "Ping failures:\n\t" . join("\n\t",@host) . "\n" . local +time; my $msg=MIME::Lite-> new( To => $to, From => $from, Subject => 'Ping Failure', Data => $data, ); $msg->send; } sub parse_args { GetOptions( 'host=s' => \@host, 'file=s' => \$file, 'to=s' => \$to, 'from=s' => \$from, 'first=i' => \$first, 'delay=i' => \$delay, 'second=i' => \$second, 'help|h|?' => \$help, ); pod2usage(-verbose=>2) if $help; pod2usage(-verbose=>1) unless @host or $file; @host = file2list($file) unless @host; } sub file2list { my $file = shift; open FH, $file or die "can't open $file: $!"; my @list = <FH>; chomp @list; return @list; } __END__ =head1 NAME ping_mon.pl - Ping Monitor with Email Notification =head1 SYNOPSIS ping_mon.pl -host <host> | -file <file> Options: -file read list of hosts from file -host provide host via command line -to email recepient -from email sender -first no. seconds for first time-out -delay no. seconds between first and second ping -second no. seconds for second time-out -help -h brief help message =head1 DESCRIPTION B<This program> will ping the provided hosts and send a email notifying which hosts (if any) were not reachable. Each host will first be pinged for an initial amount (set with the -first option). If that ping fails, the program will sleep for an amount of time (set with the -delay option) before a second ping attempt. The second ping's time-out is set with the -second option. You can also set the to and from fields for the email on the command line. Modify the source code to add default values. =head1 EXAMPLES 1 - ping contents of /etc/hosts ./ping_mon.pl -file=/etc/hosts 2 - ping Google ./ping_mon.pl -host=google.com 2 - ping Google and Perlmonks ./ping_mon.pl -host=google.com -host=perlmonks.org =cut
jeffa
L-LL-L--L-LL-L--L-LL-L-- -R--R-RR-R--R-RR-R--R-RR B--B--B--B--B--B--B--B-- H---H---H---H---H---H--- (the triplet paradiddle with high-hat)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: (jeffa) Re: Ping Monitor with Email Notification
by Tomte (Priest) on Feb 13, 2003 at 14:30 UTC | |
by jeffa (Bishop) on Feb 13, 2003 at 15:04 UTC | |
by Aristotle (Chancellor) on Feb 13, 2003 at 18:52 UTC | |
by Tomte (Priest) on Feb 13, 2003 at 15:15 UTC | |
by tachyon (Chancellor) on Feb 13, 2003 at 14:58 UTC |