#!/usr/athena/bin/perl

#
# Configuration
#
$n = 5000;
$serverlog = "/var/log/apache2/access_log";
$servername = "stuff.mit.edu";
$uptime = "/usr/bin/uptime";
$tail = "/usr/bin/tail";


#
# Main
#
$lastblock = `$tail -$n $serverlog`;
($nowsec, $nowmin, $nowhour, @rest) = localtime(time);
$lastblock =~ /\[\d+\/...\/\d\d\d\d:(\d\d):(\d\d):(\d\d)/;
$hour = $1; $min = $2; $sec = $3;
$# = '%.2g';
print("Content-type: text/html\n\n");

$nowhour += 24 if $hour > $nowhour;

$time = $hour*3600+$min*60+$sec;
$nowtime = $nowhour*3600+$nowmin*60+$nowsec;
$diff = $nowtime-$time;
$connpersec = int($n*100/$diff)/100;
print("<h1>$servername Web server load</h1>\n");
$connpermin = $connpersec*60;
print("<ul>\n");
printf("<li>%.2f connections per second (%.2f conn/minute)\n", $connpersec, $connpermin);
print("<li>To be exact, 1000 connections in the last $diff seconds\n");
$uptime = `$uptime`;
($uptime1, $uptime2) = split(/load average: /, $uptime);
print("<li><tt>$uptime1</tt>\n");
print("<li>Load values: <tt>$uptime2</tt>\n");
print("</ul>\n");
print("<pre>\n");
@lb = split("\n", $lastblock);
for $l (@lb){
    $l =~ /\[\d+\/...\/\d\d\d\d:(\d\d):(\d\d):(\d\d)/;
    $hour = $1; $min = $2; $sec = $3;
    $now = "$hour:$min";
    if($now ne $lnow){
        if(($min > $lmin)|| ($hour>$lhour)){
	    if(!$notfirst){
		$notfirst = 1;
		$skip{$now} = 1;
	    }
	    elsif(!$skip{$lnow}){
		$ticks = "-" x ($count{$lnow}/6);
		print("$lnow: $ticks [$count{$lnow}]\n") unless $did{$lnow}||!$count{$lnow};;
		$count = 1;
	    }
        }
    }
    else{
        $count{$now}++;
    }
    $lnow = $now;
    $lmin = $min; $lhour = $hour;
}
print("</pre>\n");

