Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

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.

It works by using LWP to download the header of a reliable site every $interval seconds. It makes a log entry every time the service goes up or down.

Many thanks to tye and arhuman, who helped me with a problem of autoflushing the log in open, sleep, & print together cause an error.

Update: ybiC has posted a muchly improved version of this same program here.

An improvement would be to tell it to check a second site if the first one fails.

-zeno

=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 < 5.6 compatibility (thanks ybiC) open (OUT, ">>$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,">>",$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; } }

In reply to Internet Connection Uptime Logger by zeno

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others romping around the Monastery: (6)
    As of 2014-10-22 02:02 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      For retirement, I am banking on:










      Results (112 votes), past polls