3 things:
  1. send all failures in one email message
  2. instantiate only one Net::Ping and re-use it
  3. get rid of the tryagain sub
You have coded tryagain to accept and iterate through a list of hosts, but you always pass it one scalar. But like i said, it's not really necessary anyway. Here is a re-write that uses Getopt::Long and Pod::Usage. However, Pod::Usage and "icmp" pings don't play together in the sandbox very well. By that i mean that "Superuser must not run perldoc without security audit and taint checks." (perldoc error message) and "specifying the 'icmp' protocol requires that the program be run as root or that the program be setuid to root" (from the Net::Ping docs). What this boils down to is unless you set up those "security audit and taint checks", there is no reason to use a Pod::Usage -verbose setting higher than 1 (one), but i did anyway for the help message.
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)

In reply to (jeffa) Re: Ping Monitor with Email Notification by jeffa
in thread Ping Monitor with Email Notification by linebacker

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.