Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
Per zeno's request, here's my butchering of his cool code.   I don't see that mine's better - is just tweaked to suit my own porpoises.
    cheers,
    Don
    striving for Perl Adept
    (it's pronounced "why-bick")

Update:
perldoc inet_log.pl for details.

  • 2001-04-25   06:00   And more re-rewriting.
  • 2001-04-23     Substantial re-rewrite.
  • 2001-02-13     Initial fork
#!/usr/bin/perl -wT # inet_log.pl # pod at tail use strict; use LWP::UserAgent; use Time::localtime; ### Begin Config Parameters ### my %parm = ( log => 'inet_log.log', interval => 300, timeout => 120, browser => '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 => 0, auth => 0, host => '', port => '', id => '', pass => '', ); ### End Config Parameters ### open (LOG, "> $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->new; $ua -> agent("$parm{browser}"); $ua -> timeout("$parm{timeout}"); if ($proxy{use} == 1) { $ua -> proxy('http',"http://$proxy{host}:$proxy{port}"); $ua -> no_proxy($proxy{host}); } if ($proxy{auth} == 1) { $request -> proxy_authorization_basic ("$proxy{id}", "$proxy{pass}"); } $request = HTTP::Request->new('GET',"http://$_"); my $response = $ua->request($request); if ($response->is_success()) {$success = 1;} elsif ($response->is_error()) {$success = 0;} PrintCon("$success", "$_"); if ($success != $old_success) { # has connection status cha +nged? open (LOG, ">> $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->year()+1900, localtime->mon()+1, localtime->mday(), localtime->hour(), localtime->min(), localtime->sec(), ; print " $print_state, $host,\n"; } ###################################################################### +#### sub PrintLog { my $print_state = $_[0]; my $host = $_[1]; printf LOG " %4d-%2d-%2d, %2d:%2d:%2d, ", localtime->year()+1900, localtime->mon()+1, localtime->mday(), localtime->hour(), localtime->min(), localtime->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 webhos +ts. 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+comma +s. 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 & 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) ca +lls. 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 ho +st. 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 launch +ing. If connection fails, immediately try next host. =cut

In reply to (code) Re: Internet Connection Uptime Logger (mucked over) by ybiC
in thread 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 making s'mores by the fire in the courtyard of the Monastery: (11)
    As of 2014-09-16 16:43 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      How do you remember the number of days in each month?











      Results (35 votes), past polls