<?xml version="1.0" encoding="windows-1252"?>
<node id="57865" title="Internet Connection Uptime Logger" created="2001-02-12 09:29:23" updated="2005-07-19 14:08:39">
<type id="1042">
CUFP</type>
<author id="49126">
zeno</author>
<data>
<field name="doctext">
&lt;p&gt;Our ISP at work is a bit unreliable, so we started keeping a log of how often our service goes up and down.  I wrote this script to automate the task.&lt;/p&gt;
&lt;p&gt;It works by using LWP to download the header of a reliable site every &lt;i&gt;$interval&lt;/i&gt; seconds.  It makes a log entry every time the service goes up or down.&lt;/p&gt;
&lt;p&gt;Many thanks to [tye] and [arhuman], who helped me with a problem of autoflushing the log in [id://57525].&lt;br&gt;
&lt;p&gt;&lt;b&gt;Update:&lt;/b&gt; [ybiC] has posted a muchly improved version of this same program [id://59841|here].&lt;/p&gt;
&lt;READMORE&gt;
&lt;p&gt;An improvement would be to tell it to check a second site if the first one fails.&lt;/p&gt;
-[zeno]
&lt;/p&gt;
&lt;code&gt;
=head1 NAME 

inet_log.pl

=head1 DESCRIPTION

Keeps a log of internet up and down time

=head1 AUTHOR

zeno

=head1 SYNOPSIS

perl inet_log.pl [name_of_logfile interval url_of_website]

=head1 ARGUMENTS

 name_of_logfile - text file to use as log file 
   [display (-) by default]
 interval - interval in seconds between checks 
   [600 seconds by default]
 url_of_website - URL of website to try 
   [http://www.perlmonks.com by default]
 
=cut

#!/usr/bin/perl -w
use LWP::Simple;
use strict;
use warnings;

# log file (display by default)
my $log =      $ARGV[0] ||= '-';
# interval between checks (600)
my $interval = $ARGV[1] ||= 600;
# site to try (perlmonks by default)
my $site =     $ARGV[2] ||= 'http://www.perlmonks.com';  

my $old_state;
my $state;

print "Creating Internet Uptime Log at $log ";
print "testing $site every $interval seconds.\n";

#changed this to the two arg version for 
#version &lt; 5.6 compatibility (thanks ybiC)
open (OUT, "&gt;&gt;$log") or die "can't open $log: $!";

#this way of setting AUTOFLUSH on the log I got
#from tye and arhuman
my $selected = select(OUT);
$|++;
select($selected);

$old_state = (defined head($site));
print OUT (localtime).($old_state ? ": up\n" : ": down\n");
close OUT or die;
print "".(localtime).($old_state ? ": up\n" : ": down\n");

while (1) {
  sleep $interval;
  $state = (defined head($site));
  if ($state != $old_state) {
    # state changed
    open OUT,"&gt;&gt;",$log or die "can't open $log: $!";
    print OUT (localtime).($state ? ": up\n" : ": down\n");
    close OUT or die;
    print "".(localtime).($state ? ": up\n" : ": down\n");    
    $old_state = $state;
  }
}
&lt;/code&gt;</field>
</data>
</node>
