Network Mapping at a Glance, Part 4
Doing it visually with your own Perl script.
by Kent Cearley
The <IMG> script
The script called by the <IMG> tag has to complete three tasks:
- Check the status of each node on the map.
- Indicate the status by filling the nodes with an appropriate color.
- Send the modified GIF back to the browser.
To check each node on the map, the script must parse the coordinate file created earlier and ping each coordinate's associated IP address. Parsing lines from a file into tokens is one of Perl's strengths; the script easily accomplishes this with the following snippet:
open(NODES,'nodes.dat');
while() {
chop;
($xy, $ipaddr) = split(" "); # parse into two values
push(@check,$ipaddr); # add ip address to list
$hosts{$ipaddr} = $xy; # add both to lookup table
}
close(NODES);
This code reads all the nodes from NODES.DAT into an associative array ($hosts), where the x,y coordinates can be checked by using their associated IP addresses. In addition, the IP addresses alone are added to a Perl list (@check), which can be passed in toto to the ping routine as follows:
open(RESULTS, "/usr/local/bin/fping -u @check 2>/dev/null |");
The line above opens a pipe to the fping command (see How to Install Fping
and sends all the IP addresses that you want checked. Fping will return only the nodes that didn't respond to the pings, as set by the -u flag. These failing nodes can be read from the RESULTS handle, which returns them one per line. Using the associative array created earlier, the IP addresses returned from RESULTS act as keys for looking up the x,y coordinates. These coordinates are then used to fill the node's area on the map with a red alert. To change the color of the node, we'll need to modify the GIF on-the-fly with some routines from the GD library (see sidebar on "Using GD.pm").
To manipulate a GIF image, GD must first load and convert the GIF to an internal format. The following code fragment loads your network map:
open(GIF,"network.gif");
$image->newFromGif GD::Image(GIF);
[Move on to the next part of the article]
Web Developer® Site Feedback
Web Developer®
Copyright © 2000 internet.com Corporation. All rights reserved.