All pastes #229833 Raw Edit

Miscellany

public text v1 · immutable
#229833 ·published 2006-10-30 23:26 UTC
rendered paste body
#!/usr/bin/perl
#
# This is the line that confirms we got a response back to our ping.
$PING_RESULT_STRING = "time=";

# The log file, where we save failed pings, and the perl file
# we use to update the web page. you could work it in to one file.
$LOG_FILE         = "/home/noodles/status_file.txt";

# The list of hosts we try to ping comma seperated, in ""
@HOSTS              = ();
$DUMP_FREQUENCY     = 1;  # every number of pings to update file
$PING_FREQUENCY     = 300;# how frequently to ping, in seconds

use Getopt::Std;

getopts('e');              # command-line arg; if e, then extend log 
$dump_count         = 0;  # counter for dump frequency
$TOTAL_TRIES        = 0;  # counter for number of tries so far

# open log file to write current start time. Fail if the log
# already exists; don't want to write over it and wipe out
# collected data. If the user passes in a '-e' flag, leave
# the log file as is and extend it with new data. The -e
# command is different; this checks for the existence of the
# log file.

if($opt_e != 1)  # didn't get option passed in
{
  -e $LOG_FILE and die "move the current log file, $LOG_FILE!\n";
  # log doesn't exist; prime the log file with the start time 
  open(LOG, ">$LOG_FILE");
  $current_time = time;
  print LOG "$current_time\n";
  close LOG;
}
# loop forever
while(1)
{
  $TOTAL_TRIES = $TOTAL_TRIES +1;
  # loop thru our list of hosts, pinging each
  foreach $host (@HOSTS)
   {
     # get the current time
     ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst)  = localtime(time);
      # we assume no response until otherwise proven for this host 
      $got_response = 0;
      # Run the ping command, get results back in stdin
      open PING_RESULTS, "/bin/ping -c 1 $host 2>&1 |"
              or die "cant fork";
      while($line = <PING_RESULTS>)
       {
        if($line =~ /$PING_RESULT_STRING/) 
         {
           $got_response = 1;
           open LOG, ">> $LOG_FILE";
           $myTime = time;
           printf(LOG "$myTime $line\n");
        close LOG;}
       }
      # OK, we looped through all the data we got back from ping.
      # was there anything that indicated we got a response back?
      if($got_response == 0)
      {
        open LOG, ">> $LOG_FILE";
        $myTime = time;
        printf(LOG "$myTime $TOTAL_TRIES $year/$mon/$mday $hour:$min\n");
        close LOG;
      }
  } # end of loop through hostnames
  # Go to sleep for the indicated time
  sleep $PING_FREQUENCY;
} #end of forever loop