Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks.

#!/usr/bin/perl -w use strict; use Net::Ping; print "Starting Network Monitor...\n\n"; open FH, ">", "net.dat" or die "Error: $!\n"; my $p = Net::Ping->new(); for(;;) { $p->ping(xxx.xx.x.xx); } print FH "Lost connection at ", scalar localtime; close FH;


My goal is to monitor my internet connection and when it disconnects, I'd like the date/time the network failure occured printed to a file.

I wasn't sure how to say 'if the ping resulted in 100% packet loss' since !$p->ping(xxx.xx.x.xx); did not function as expected.

Thank you.

Replies are listed 'Best First'.
Re: Need help writing a basic network monitor.
by McDarren (Abbot) on Sep 16, 2006 at 07:15 UTC
    Still keeping it fairly simple, how about something like this:
    #!/usr/bin/perl -w use strict; use Net::Ping; my $test_host = 'www.google.com'; my $sleepy_time = 30; print "Network Monitor started at ", scalar localtime, ". (Testing $test_host every $sleepy_time seconds)\n"; my $p = Net::Ping->new(); while (1) { sleep $sleepy_time; next if $p->ping($test_host); print "$test_host appears to be down at ", scalar localtime, "\n"; }

    If you wanted to do this properly, then there are plenty of (open source) off-the-shelf Network Monitoring Systems available. Big Brother is one that I have used and would recommend.

    Cheers,
    Darren :)

      Hi McDarren++,
      we use Nagios. Uses loads of plugin scripts for the different test. The plugins are small standalone scripts or programs. I use perl massively to write plugins for Nagios.
      Furthermore, we use some perl scripts to generate the Nagios config files from a machine database that we maintain.
      Regards,
      svenXY
        "we use Nagios."

        I've heard a lot of good stuff about Nagios, but I've never used it myself. I've been using BB for almost 6 years now, and it pretty much does everything I need.

        "I use perl massively to write plugins for Nagios."

        Same here with regards BB. At the previous company I worked for, we were running about a dozen customised tests - all written in Perl :)
        Where I am now, most of the servers we monitor are windoze boxen. I would have liked to have written our customised tests in Perl, but it wasn't really practical. It would have meant either installing ActivePerl on about 300 machines, or doing something with PAR. We've actually resorted to (ugh!) VBScript at the moment - which is (sortof) working okay, except for some of the incredibly ridiculous hoops you have to jump though to do the most basic things with VBScript - such as parsing a bit of text ;) So I would still like to migrate everything to Perl if I get the chance :)

        "Furthermore, we use some perl scripts to generate the Nagios config files from a machine database that we maintain."

        Yup, again we do the same with BB. In fact, we've gone a step further and completely dispensed with the standard BB display interface, and built one of our own using Perl/CGI. A bit more about that is here.

        Cheers, Darren :)

Re: Need help writing a basic network monitor.
by aquarium (Curate) on Sep 16, 2006 at 14:27 UTC
    the state (and lots of other useful info) for your adapter is available via a MIB lookup. use Net::Snmp or similar, rather than wasting bandwidth with pings. also note that for ping you're also testing the destination server and all (internet) circuits that run up to it...not just your connection to the internet.
    the hardest line to type correctly is: stty erase ^H