<?xml version="1.0" encoding="windows-1252"?>
<node id="59841" title="(code) Re: Internet Connection Uptime Logger (mucked over)" created="2001-02-21 02:29:12" updated="2005-07-21 06:05:03">
<type id="11">
note</type>
<author id="14909">
ybiC</author>
<data>
<field name="doctext">
Per [zeno]'s request, here's my butchering of his [id://57865|cool code]. &amp;nbsp; 
I don't see that mine's better - is just tweaked to suit my own porpoises.
&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;cheers,
&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Don
&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;i&gt;striving for &lt;a href="http://prometheus.frii.com/~gnat/yapc/2000-stages/"&gt;Perl Adept&lt;/a&gt;&lt;/i&gt;
&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font size="-2"&gt;(it's pronounced &amp;quot;why-bick&amp;quot;)&lt;/font&gt;

&lt;p&gt;
&lt;b&gt;Update: &lt;/b&gt;
&lt;br&gt;perldoc inet_log.pl for details.
&lt;ul&gt;
  &lt;li&gt;2001-04-25 &amp;nbsp; 06:00 &amp;nbsp; And more re-rewriting.
  &lt;li&gt;2001-04-23 &amp;nbsp; &amp;nbsp; Substantial re-rewrite.
  &lt;li&gt;2001-02-13 &amp;nbsp; &amp;nbsp; Initial fork
&lt;/ul&gt;

&lt;code&gt;#!/usr/bin/perl -wT

# inet_log.pl
# pod at tail

use strict;
use LWP::UserAgent;
use Time::localtime;


### Begin Config Parameters ###
my %parm = (
    log      =&gt; 'inet_log.log',
    interval =&gt; 300,
    timeout  =&gt; 120,
    browser  =&gt; 'Mozilla/5.0 (X11; U; Linux 2.2.17 i586; en-US; m18)',
    );
my @hosts = qw(
    www.w3c.org   www.cpan.org   www.perl.com   freshmeat.net
    www.cisco.com   www.useit.com   www.google.com   alistapart.com
    www.debian.org   ftp.debian.org   www.weather.com
    www.oreilly.com   www.slashdot.org   www.perlmonks.org
    www.linuxgazette.com
    );
my %proxy = (
    use  =&gt; 0,
    auth =&gt; 0,
    host =&gt; '',
    port =&gt; '',
    id   =&gt; '',
    pass =&gt; '',
    );
### End Config Parameters ###


open (LOG, "&gt; $parm{log}")   or die "\nError opening $parm{log}:\n  $!";
my $selected = select(LOG);
$|++;
select($selected);


print "\n",
      "  Query a web host every $parm{interval} seconds\n",
      "    Log:        $parm{log}\n",
      "    Program:    $0\n",
      "    LWP:        $LWP::VERSION\n",
      "    Perl:       $]\n",
      "    OS:         $^O\n";
print "    Proxy:      $proxy{host}:$proxy{port}\n"  if ($proxy{use} == 1);  
print "    Proxy user: $proxy{id}\n"                 if ($proxy{auth} == 1);
print "\n",
      "  Target hosts:\n";
print "    $_\n" for (@hosts);


=cut

if ($proxy{use} == 1) {
    Net::Ping::Improved
    unless (...) {
        print"  Proxy not accessible\n\n";
        exit;
        }
    }

=cut


print "\n  Querying web hosts  (1 = success, 0 = no response)\n";
my $old_success = 3;                       # first contact gets logged
while (1) {
    for (@hosts) {
        my ($success, $request);
        my $ua =  LWP::UserAgent-&gt;new;
        $ua    -&gt; agent("$parm{browser}");
        $ua -&gt; timeout("$parm{timeout}");

        if ($proxy{use} == 1) {
            $ua -&gt; proxy('http',"http://$proxy{host}:$proxy{port}");
            $ua -&gt; no_proxy($proxy{host});
            }
        if ($proxy{auth} == 1) {
            $request -&gt; proxy_authorization_basic
                ("$proxy{id}", "$proxy{pass}");
            }

        $request     =  HTTP::Request-&gt;new('GET',"http://$_");
        my $response =  $ua-&gt;request($request);

        if    ($response-&gt;is_success()) {$success = 1;}
        elsif ($response-&gt;is_error())   {$success = 0;}
        PrintCon("$success", "$_");

        if ($success != $old_success) {    # has connection status changed?
            open (LOG, "&gt;&gt; $parm{log}") or die "\nError opening $parm{log}: $!";
            PrintLog("$success", "$_");
            $old_success = $success;
            }
        sleep $parm{interval};
        }
    }


##########################################################################
sub PrintCon {
    my $print_state = $_[0];
    my $host        = $_[1];
    printf "    %4d-%2d-%2d,  %2d:%2d:%2d, ",
        localtime-&gt;year()+1900,
        localtime-&gt;mon()+1,
        localtime-&gt;mday(),
        localtime-&gt;hour(),
        localtime-&gt;min(),
        localtime-&gt;sec(),
        ;
    print "  $print_state,  $host,\n";
    }
##########################################################################
sub PrintLog {
    my $print_state = $_[0];
    my $host        = $_[1];
    printf LOG "  %4d-%2d-%2d,  %2d:%2d:%2d, ",
        localtime-&gt;year()+1900,
        localtime-&gt;mon()+1,
        localtime-&gt;mday(),
        localtime-&gt;hour(),
        localtime-&gt;min(),
        localtime-&gt;sec(),
        ;
    print LOG "  $print_state,  $host,\n";
    close LOG            or die "\nError closing $parm{log}: $!";
    }
##########################################################################


=head1 Name

 inet_log.pl

=head1 Description

 Log Internet connection drops, or nail up a dial connection.
 Use LWP to periodically request a page from configured list of webhosts.
 Doesn't use simple ping because:
   - ISP routers often not configured for ICMP as "interesting traffic"
       where HTTP generally is.
   - Some sites block ICMP at their outside DMZ router.
   - Allows you to run this script from inside a firewalled location 
       for other purposes.
 Logs only instances of change in response (pass|fail).

=head1 Usage

 No switches or syntax.  Config parameters near head of script.

=head1 Author

 ybiC

=head1 Credits

 Started from good monk zeno's cool inet_log.pl
 http://www.perlmonks.org/index.pl?node_id=57865

=head1 Tested

 LWP    5.51 
 Perl   5.00503
 Debian 2.2r3

=head1 Updates

 2001-04-25   06:00
   Add module and Perl versions display.
   Simplify code for opening messages.
   Add qw() to declaration of @hosts to eliminate mess o' quotes+commas.
   Unsubify LWP - move Fetch code to main body of script.
   Use $_ with @hosts to eliminate global $host.
   Fix 'uninitialized value' re $print_state
     by quoting properly in Print(Log|Con) calls.
   Add support for authenticated proxy.
   Break %global out into %proxy and %parm.
   Add if(){...} loops for proxy{use} and proxy{auth}
   Minor format &amp; pod tweaks.
 2001-04-23   22:40
   Hashamify passel o' scalar vars.
   Un-subify to reduce number of global vars.
   Replace 'use vars qw()' with 'my (,)' so lexical not global.
   Mixed-case names for remaining subroutines, and no ampersan.
   Remove unecessary doublequotes around variables.
   Fix 'scalar found where' errors by adding qq// to Print(Con|Log) calls.
   Reformat for max 75 chars/line.
 2001-02-14
   Replace LWP::Simple with LWP::UserAgent.
   Add proxy, timeout, useragent.
   Move $ua section to sub since is called twice.
   Add Time::localtime, printf.
     because no comprendo good monk zeno's time syntax.
   Move sleep to end of while(1) section so no wait after hit proxy.
 2001-02-13
   Fork from zeno's cool script.
   Minor style stuff to personal taste.
   Multiple hosts target rotation - avoid excessive hits on any one host.
   Move redundant code to subroutines.
   Output format to CSV for reporting.
   Add "use vars qw()" instead of empty 'my blah's'.
   Hit local proxy first before hitting Internet hosts.
     assumes proxy is also HTTP server.

=head1 Todos

 Ping proxy with Net::Ping(|::(Improved|External)).
 Test $proxy{user} and $proxy{pass}.
 Test on Win32 - ActiveState, Cygwin, etc.
 Randomize @hosts selection, and $parm{interval} ~1-4 minutes.
 Background with (fork|Proc::Daemon|??) so can kill xterm after launching.
 If connection fails, immediately try next host.

=cut

&lt;/code&gt;</field>
<field name="root_node">
57865</field>
<field name="parent_node">
57865</field>
</data>
</node>
