Blame contrib/mrtgindex.cgi/mrtgindx.cgi

Packit 667938
#!/usr/local/bin/perl
Packit 667938
Packit 667938
# mrtgindex.cgi v 1.1
Packit 667938
# (c) 1997, Mick Ghazey mick@lowdown.com
Packit 667938
# Thanks to Dave Rand, Peter W. Osel and Tobias Oetiker.
Packit 667938
Packit 667938
# Why CGI?
Packit 667938
# Mrtgindx.cgi has features that only CGI can provide. 
Packit 667938
Packit 667938
# Runtime index:
Packit 667938
# The index page is built every time the page is requested.
Packit 667938
# Changes to config files are visible on the next update.
Packit 667938
Packit 667938
# Clickable graphs:
Packit 667938
# Each graph is a clickable hyperlink to more information.
Packit 667938
Packit 667938
# Automatic updates:
Packit 667938
# The index page automatically updates every 5 minutes.
Packit 667938
# Modify $interval to adjust this period.
Packit 667938
Packit 667938
# Timely updates:
Packit 667938
# Mrtgindx.cgi predicts when to update based on when the
Packit 667938
# current graphs were completed.
Packit 667938
# Updates occurs shortly after the graphs are completed
Packit 667938
# regardless of when the page was initially requested.
Packit 667938
# Modify $guardband to adjust this period.
Packit 667938
# If the guardband is too small you run the risk of attempting
Packit 667938
# an update while a graph is under construction. 
Packit 667938
Packit 667938
# Predictable graph order:
Packit 667938
# Graphs appear in the same order as Titles in the config files.
Packit 667938
# If there are multiple config files graphs from the first config
Packit 667938
# file appear before those from the second, etc.
Packit 667938
Packit 667938
# Customizable graph order:
Packit 667938
# If you want your index ordered differently than your
Packit 667938
# config files you can use a dummy config file. Mrtgindex.cgi is
Packit 667938
# only interested in "Title" statements. You could, for example,
Packit 667938
# have different dummy config files for different arangements
Packit 667938
# of graphs.
Packit 667938
Packit 667938
# Web based graph order selection:
Packit 667938
# Click Top, Up, Down, or Bot to change the position of a graph.
Packit 667938
Packit 667938
# Multiple config files:
Packit 667938
# Mrtgindex.cgi supports multiple config files. Some users
Packit 667938
# run mrtg with more than one config file. The reason may be
Packit 667938
# because certain events are run on a different schedules, 
Packit 667938
# 5 and 10 minute intervals for example.
Packit 667938
# Or perhaps multiple instances of mrtg are run
Packit 667938
# to assure completion within a 5 minute interval.
Packit 667938
# Ping-probes have longer run times than measuring traffic
Packit 667938
# on local routers, for example. However, CPU load is similar.
Packit 667938
# Modify the @config_files array below to suit your environment.
Packit 667938
Packit 667938
# Runs fast:
Packit 667938
# Mrtgindx.cgi is fast because it doesn't create graph files or
Packit 667938
# maintain log files. In addition, web based graph order changes
Packit 667938
# benefit from browser cacheing of graphs.
Packit 667938
Packit 667938
# Index.cgi:
Packit 667938
# You might want to rename this file "index.cgi". Then it
Packit 667938
# will load automatically when when the directory is browsed -
Packit 667938
# much as index.html loads automatically. You might have to
Packit 667938
# modify "DirectoryIndex" in srm.conf if you're using Apache
Packit 667938
# to allow CGI programs to be index files.
Packit 667938
Packit 667938
# CGI.pm required:
Packit 667938
# Mrtgindex.cgi requires CGI.pm by Lincoln Stein
Packit 667938
# http://www-genome.wi.mit.edu/ftp/pub/software/WWW/cgi_docs.html
Packit 667938
Packit 667938
#-------------------------------------------------------
Packit 667938
# Modify this statement to match your configuration
Packit 667938
@config_files = ('mrtg.cfg'); # Single config file
Packit 667938
Packit 667938
#@config_files = ('mrtg.cfg', 'mrtg-ping.cfg'); # Two config files
Packit 667938
#@config_files = ('mrtg.cfg', 'yahoo.ping', 'netscape.ping', 'msn..ping', 
Packit 667938
                   'att.ping'); # anal retentive
Packit 667938
Packit 667938
#-------------------------------------------------------
Packit 667938
Packit 667938
require 'stat.pl';
Packit 667938
use CGI ':all';
Packit 667938
use CGI::Carp qw(fatalsToBrowser);
Packit 667938
#use diagnostics;
Packit 667938
Packit 667938
$gifdone = 0; # Scan for newest graph and save info for later
Packit 667938
while(@config_files > 0){
Packit 667938
    open(In, $cfg = shift @config_files) ||
Packit 667938
	die "Can't open $cfg. Check \@config_files array.\n";
Packit 667938
    while(<In>){
Packit 667938
	next unless /^Title\[(.*)\]:\s*(.+)$/; # Look for a title keyword
Packit 667938
Packit 667938
	$router = lc $1;
Packit 667938
	Stat("$router-day.png");
Packit 667938
	@$router = ($st_mtime, $2); # Save the mod date and title
Packit 667938
	push @routers, $router; # Remember the router name so we can find above info
Packit 667938
	$gifdone = $st_mtime if $st_mtime > $gifdone; # Find the newest file
Packit 667938
    }
Packit 667938
    close In;
Packit 667938
}
Packit 667938
Packit 667938
# Time the next update to occur a little while after the next interval completes
Packit 667938
$interval = 300; # 5 min update interval
Packit 667938
$guardband = 15; # updates occur this many seconds after predicted gif completion
Packit 667938
$refresh = $interval + $guardband + $gifdone - time; # predict how long until next update
Packit 667938
$refresh = $interval if $refresh <= $guardband;
Packit 667938
$expires = gmtime (time + $interval * 2 + $guardband);
Packit 667938
Packit 667938
print header, start_html(-TITLE=>'Daily Stats', -BGCOLOR=>'#e6e6e6'),
Packit 667938
    "\n",
Packit 667938
    "<meta http-equiv=\"expires\" content=\"$expires GMT\">\n",
Packit 667938
    "<meta http-equiv=\"refresh\" content=$refresh>\n",
Packit 667938
Packit 667938
    table({-width=>"100\%"}, TR(
Packit 667938
#-------------------------------------------------------
Packit 667938
    # Uncomment the following line if you have Count.cgi installed.
Packit 667938
    td({-align=>left, width=>"25\%"}, img({-src=>"/cgi-bin/Count.cgi?display=clock"})),
Packit 667938
#-------------------------------------------------------
Packit 667938
Packit 667938
    td("Click graph for more info")));
Packit 667938
Packit 667938
@router_order = (0..$#routers);
Packit 667938
@router_order = split /:/, param('ord') if defined param('ord');
Packit 667938
$selfurl = url;
Packit 667938
$selfurl =~ s/\?.*//; # Remove arguments
Packit 667938
Packit 667938
for $index (0..$#routers){
Packit 667938
    @spliced = @router_order;
Packit 667938
    $router_num = splice @spliced, $index, 1; # Router removed
Packit 667938
    $router = $routers[$router_num];
Packit 667938
    $time = localtime $$router[0]; # $st_mtime saved in above loop
Packit 667938
    ($time) = $time =~ /(\d+:\d+:\d+)/; # Just the time
Packit 667938
    print hr, "\n";
Packit 667938
Packit 667938
    print a({-name=>$index});
Packit 667938
    # Print re-ordering links top, bot, up, dn
Packit 667938
    $mv_dn = $mv_up = "";
Packit 667938
    $" = ':';
Packit 667938
    if($index > 0){
Packit 667938
	@top = ($router_num, @spliced);
Packit 667938
	@up = @spliced;
Packit 667938
	$indxup = $index - 1;
Packit 667938
	splice @up, $indxup, 0, $router_num;
Packit 667938
Packit 667938
	$mv_up = sprintf "%s %s ", a({-href=>"$selfurl?ord=@top#0"}, "Top"), # move to top
Packit 667938
	a({-href=>"$selfurl?ord=@up#$indxup"}, "Up"); # move up
Packit 667938
    }
Packit 667938
    if($index < $#routers){
Packit 667938
	@bot = (@spliced, $router_num);
Packit 667938
	@down = @spliced;
Packit 667938
	$indxdn = $index + 1;
Packit 667938
	splice @down, $indxdn, 0, $router_num;
Packit 667938
Packit 667938
	$indxbot = @routers - 3;
Packit 667938
	while($indxbot < 0){$indxbot += 1}
Packit 667938
	$mv_dn = sprintf "%s %s ", a({-href=>"$selfurl?ord=@down#$indxdn"}, "Down"), # move down
Packit 667938
	a({-href=>"$selfurl?ord=@bot#$indxbot"}, "Bot"); # move to bottom
Packit 667938
    }
Packit 667938
    undef $";
Packit 667938
Packit 667938
    print table({-width=>"100\%"}, 
Packit 667938
		TR(td({-align=>"left",-width=>"20\%"}, "$mv_up $mv_dn"),
Packit 667938
		   td({-align=>"left"}, b($$router[1]), " $time")));
Packit 667938
Packit 667938
    print a({-href=>"$router.html"}, img{-src=>"$router-day.png"});
Packit 667938
Packit 667938
}
Packit 667938
Packit 667938
print "\n",hr,"Direct questions and feedback to Mick Ghazey: ",
Packit 667938
    a({-href=>"mailto:mick\@lowdown.com"}, "mick\@lowdown.com"),
Packit 667938
    end_html;