I've never tried to write a monitoring script before, but thought I would give it a go. This is what I have so far:
#!/usr/bin/perl # # unless($ARGV[0] && $ARGV[1] && $ARGV[2]) { print "Please supply the following information:\n"; print "ping-test.pl <host address> <notification sensitivity> +<email address>.\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 $em +ail\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"; } } }
I know it is probably really ugly :) But basically I am trying to get it to send out a recovery email too and then start all over, but it has failed so far. It seems to work up until the call to &recheck, maybe I am going about it the wrong way. Thanks for any help

In reply to Perl question, icmp script. by eldestructo

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.