#!/usr/bin/perl # Host(s) Monitor (using fping) #### Pagefile Usage ########### # # hostname:pagelist # # Example: # goliath:steve@yahoo.com,mike@yahoo.com # hercules:steve@yahoo.com,sarah@yahoo.com # ############################### #----------------------------------------------------------- # Environment Variables | #----------------------------------------------------------- $pagefile="pagelist"; # Config file for hostname:pagelist $default_mail="root"; $timeout=90000; # Adjusts Sensitivity Level for Network traffi +c(milliseconds) $fping="/usr/local/bin/fping"; $count=0; $cnt=0; $lines=0; $check=0; #------------------------------------------------------------- #------------------------------------------------------------- # Logging Options | #------------------------------------------------------------- $log="hostlog"; $response_times="response_times"; $outages="outages"; #------------------------------------------------------------- #------------------------------------------------------------- # Required Perl Modules | #------------------------------------------------------------- require Mail::Send; # From Mail::Tools #------------------------------------------------------------- #------------------------------------------------------------- # Make or notify user of required files | #------------------------------------------------------------- if ( ! -e $response_times ){ `touch $response_times`; } if ( ! -e $log ){ `touch $log`; } #-------------------------------------------------------------- #-------------------------------------------------------------- # Verify integrity of config file - loosely | #-------------------------------------------------------------- open(FILE1, "<$pagefile") || die "Cannot find $pagefile\n"; while (<FILE1>){ @letters= split(//); { $lines=$lines+1; foreach (@letters){ if(/:/){ $cnt=$cnt+1 } } } } if ($lines ne $cnt){ print("Error in config file\n"); exit; } close (FILE1); #--------------------------------------------------------------- # Start while (1){ open(FILE1, "<$pagefile") || die "Cannot open $pagefile\n"; while (<FILE1>){ @data = split(/:/, $_); $hostname=@data[0]; $mailto=@data[1]; $mailstrl = length($mailto); if ( $mailstrl < 1 ){ $mailto="$default_mail"; chomp($mailto, $hostname); } foreach ($hostname){ $date=localtime(time); $check = `$fping -e -r 10 -t $timeout $hostname`; open(RESPONSE,">>$response_times"); print RESPONSE "$date $check"; close(RESPONSE); if ( $check =~ /unreachable|down|not found/i ){ $machine {"$hostname"}++; if ($machine {"$hostname"} == 1){ open(RESPONSE,">>$outages"); print RESPONSE "$hostname went down at $date\n"; close(RESPONSE); $msg=new Mail::Send Subject=>"$hostname went down at $ +date", To=>"$mailto"; $fh=$msg->open; $fh->close; open(LOG,">> $log"); print LOG "$hostname went down at $date\n"; close(LOG); } } } sleep 30; # Are we still down? Only send page if host is now alive # Send only one page, log to file foreach ($hostname){ if ($machine {"$hostname"} >= 1){ $date=localtime(time); $check2 = `$fping -e -r 10 -t $timeout $hostname`; if ( $check2 =~ /alive/i ){ open(RESPONSE,">>$outages"); print RESPONSE "$hostname alive at $date\n"; close(RESPONSE); $msg=new Mail::Send Subject=>"$hostname alive at $date +", To=>"$mailto"; $fh=$msg->open; $fh->close; open(LOG,">> $log"); print LOG "$hostname alive at $date\n"; close(LOG); $machine{"$hostname"}=0; } } } } }

In reply to Host Monitor by cgaff

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.