Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Network Link Status Report Generator

by Big Willy (Scribe)
on Mar 27, 2001 at 04:00 UTC ( [id://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
Domain Nodelet?
Node Status?
node history
Node Type: sourcecode [id://67388]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2024-03-29 00:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found