#!/usr/bin/perl
#
# apache-stats
#
# A utility for use with cricket to pull some interesting information from 
# the Apache web server (http://www.apache.org) and graph it.
#
# Copyright (C) 2000  Andrew Williams
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
#
# Output is as follows (use the line #'s in the exec calls from cricket)
#
# 0-8:  are pulled directly from the output of the mod_status output
# 9-16: are derived from the scoreboard output.  They are counts of
#       processes in the named state.
# 
# 0: Total hits
# 1: Total kB served
# 2: CPU Load (in %)
# 3: Uptime (in seconds)
# 4: Requests per Second
# 5: Bytes per request
# 6: Busy Servers
# 7: Idle Servers
# 8: Scoreboard
# 9: Waiting
# 10: Starting
# 11: Reading
# 12: Writing
# 13: Keepalive
# 14: DNS Lookup
# 15: Logging
# 16: Gracefully Finishing



use LWP::UserAgent;


# Set the URL to the server status page
$URL = shift(@ARGV);

if($URL eq "")
{
    die("Usage: $0 <URL>\n");
}
if($URL !~ /^http:/i)
{
    die("Must provide a full url you sent: $URL\n");
}

# How long to wait for a reply
$timeout = 30;




## BEGIN ##

# Do some basic setup of the UserAgent package
$ua = new LWP::UserAgent;
$ua->agent("apache-stats/1.0" . $ua->agent);
$ua->timeout($timeout);



# Setup the request ?auto is appended to the URL to get the result in a 
# more machine readable format

$request = new HTTP::Request('GET', $URL . "?auto");
$response = $ua->request($request);

foreach $line (split("\n",$response->content))
{
    if($line =~ /Total Accesses:\s*(.*)/)
    {
	$Total_Accesses = $1;
    }
    elsif($line =~ /Total kBytes:\s*(.*)/)
    {
	$Total_kBytes = $1;
    }
    elsif($line =~ /CPULoad:\s*(.*)/)
    {
	$CPULoad = $1;
    }
    elsif($line =~ /Uptime:\s*(.*)/)
    {
	$Uptime = $1;
    }
    elsif($line =~ /ReqPerSec:\s*(.*)/)
    {
	$ReqPerSec = $1;
    }
    elsif($line =~ /BytesPerSec:\s*(.*)/)
    {
	$BytesPerSec = $1;
    }
    elsif($line =~ /BytesPerReq:\s*(.*)/)
    {
	$BytesPerReq = $1;
    }
    elsif($line =~ /BusyServers:\s*(.*)/)
    {
	$BusyServers = $1;
    }
    elsif($line =~ /IdleServers:\s*(.*)/)
    {
	$IdleServers = $1;
    }
    elsif($line =~ /Scoreboard:\s*(.*)/)
    {
	# actually processed later
	$Scoreboard = $1;
    }
    else
    {
	warn "odd, shouldn't have hit this\n";
    }
}

#
# Process the scoreboard file counts
# _ waiting for connection
# S Starting up
# R Reading Request
# W Sending Reply
# K Keepalive (read)
# D DNS Lookup
# L Logging
# G Gracefully finishing
# . Open slot
#

@states = split(//, $Scoreboard);
$Waiting = grep(/\_/, @states);
$Starting = grep(/S/, @states);
$Reading = grep(/R/, @states);
$Writing = grep(/W/, @states);
$Keepalive = grep(/K/, @states);
$DNS = grep(/D/, @states);
$Logging = grep(/L/, @states);
$Grace_finish = grep(/G/, @states);

print "$Total_Accesses\n";
print "$Total_kBytes\n";
print "$CPULoad\n";
print "$Uptime\n";
print "$ReqPerSec\n";
print "$BytesPerSec\n";
print "$BytesPerReq\n";
print "$BusyServers\n";
print "$IdleServers\n";
print "$Waiting\n";
print "$Starting\n";
print "$Reading\n";
print "$Writing\n";
print "$Keepalive\n";
print "$DNS\n";
print "$Logging\n";
print "$Grace_finish\n";
