Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Network Link Status Report Generator

by Big Willy (Scribe)
on Mar 27, 2001 at 04:00 UTC ( #67388=sourcecode: print w/replies, xml ) Need Help??
Category: Networking Code
Author/Contact Info Will Stockwell (waldo@cyberspace.org) Big Willy at perlmonks.org
Description: Generates an HTML report of ping tests for connectivity to the hosts in 'hostfile,' while is formatted as such:

{host}:{description}\n

Useful for admins who want to have a cron job intermittently check link status for router-router links, etc. Realize that if you can't ping the host, but can access it by other means this script will not work.
#!/usr/bin/perl -w
# coded by Will Stockwell(waldo@cyberspace.org)

use CGI qw(:standard :html3 -no_debug);
use Time::CTime;

$dir = "."; #Directory location of output files
$refresh = 120; # Refresh rate of HTML page for browser
$hostfile = "hostlist";
$htmlfile = "linkstat.html";
$pingcnt = "3";

open(HOSTLIST, $hostfile) || die "Couldn't open $hostfile: $!";
@hostlist = <HOSTLIST>;
close(HOSTLIST);

open(HTMLOUT, ">$dir/$htmlfile") || die "Couldn't open $dir/$htmlfile:
+ $!";

print HTMLOUT "<HTML><HEAD><TITLE>Link Status</TITLE>",
              "<META HTTP-EQUIV='refresh' CONTENT='$refresh; '>",
              "</HEAD><BODY BGCOLOR='lightblue' TEXT='black'>",
              h1("Link Status");

@headings = ('Node', 'Description', 'Status');
@rows = th(\@headings);

foreach (@hostlist) {
        chomp;
        /:/ || next; # Check for colon to be sure this is not a blank 
+line
        ($host, $desc) = split(/:/);
        $pingout = `/sbin/ping -c $pingcnt $host 2>&1`; # grab the pin
+g STDOUT and STDERR
        
        open(PINGOUT, ">$dir/$host.html") || die "Couldn't open $dir/$
+host.html: $!";
        $time = ctime(time());
        print PINGOUT start_html(-BGCOLOR=>'lightblue',
                                -TEXT=>'black',
                                -title=>"Results of ping execution for
+ $host"),
                                h1("Results for $host at $time"),
                                p($desc);
        foreach (split(/\n/, $pingout)) { print PINGOUT $_ . br; }
        print PINGOUT end_html();
        close(PINGOUT); 
        
        $host = a({-href=>"$host.html"}, "$host");

        if($pingout =~ /unknown host/) { # This never shows when an IP
+ is not resolved
            push(@rows, td([$host, $desc, "<FONT COLOR='brown'>Unresol
+ved IP</FONT>"])); 
        } elsif($pingout =~ /100% packet loss/) {
            push(@rows, td([$host, $desc, "<FONT COLOR='red'>Down</FON
+T>"])); 
        } else {
            push(@rows, td([$host, $desc, "<FONT COLOR='green'>Running
+</FONT>"])); 
        }
}

$email = a({-href=>"mailto:waldo\@cyberspace.org"}, "waldo\@cyberspace
+.org");
$time = ctime(time());

print HTMLOUT table({-border=>undef}, caption("Status check as of $tim
+e"), Tr(\@rows));
print HTMLOUT h5("Generated by linkstat.pl, coded by Will Stockwell($e
+mail)");
print HTMLOUT end_html();

close(HTMLOUT);
Replies are listed 'Best First'.
Re: Network Link Status Report Generator
by idnopheq (Chaplain) on Apr 04, 2001 at 14:46 UTC
    Interesting. I would, however, seriously consider removing the dependency on the external ping command and use Net::Ping. I think it would be safer, likely faster, and takes care of ping's root requirement on some platforms.

    Try liberating the appropriate parts of the ping script from PPT.

    You need to ensure you have a current version of Net::Ping, so you can choose your ping type (TCP, UDP, or ICMP).

    HTH
    Dex

      As I see it with Net::Ping you'd still be forced to run it as su (unless you run echo service from (x)inetd).
      It's all in the documentation... Also, Net::Ping doesn't provide the same kind of output as system ping, unless you want to twiddle with timers and timeouts.
      I'd say that it would be neater here to use "open(|/sbin/ping <opts>)".
Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://67388]
help
Chatterbox?
[holli]: you know, monk days are fun and all. but too many of them are bad for your health
[1nickt]: ...modern car. Suggests using TLA+ or other software modeling language to autogenerate code.
1nickt is not sure more and more layers separating the programmer from the program is the right way to go...
[holli]: happy one, shmem ;)
[1nickt]: I had an interview couople of weeks ago, heard from a principal engineer that a friend of his who works at The Google told him that the self-teaching software that determines the dynamic pricing for their adverts...
[1nickt]: ... has become so complex that there is no one at the Goole who knows what it does.
[marto]: William Gibson was right
[moritz]: a classical problem with machine learning
[1nickt]: moritz isn;t the difference today that the machine learning algos are in charge of most stuffs?
[moritz]: depends on what you mean by "most stuff"

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (13)
As of 2017-11-21 13:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:













    Results (301 votes). Check out past polls.

    Notices?