#!@@PERL@@
#
# Plugin to monitor the volume of data sent from Apache servers. It handles
# a list of ports passed in from a plugin configuration file.
#
# Requirements:
# 	- Needs access to http://localhost/server-status?auto (or modify the
# 	  address for another host). See your apache documentation on how to
# 	  set up this url in your httpd.conf. Apache needs ExtendedStatus
# 	  enabled for this plugin to work.
#
# Tip: To see if it's already set up correctly, just run this plugin
# with the parameter "autoconf". If you get a "yes", everything should
# work like a charm already.
#
# Parameters supported:
#
# 	config
# 	autoconf
#
# Configurable variables
#
# 	url      - Override default status-url
# 	ports    - HTTP port numbers
#
# $Log$
# Revision 1.12  2004/12/10 18:51:43  jimmyo
# linux/apt* has been forced to LANG=C, to get predictable output.
#
# Revision 1.11  2004/12/10 10:47:47  jimmyo
# Change name from ${scale} to ${graph_period}, to be more consistent.
#
# Revision 1.10  2004/12/09 22:12:54  jimmyo
# Added "graph_period" option, to make "graph_sums" usable.
#
# Revision 1.9  2004/09/26 22:14:39  jimmyo
# Changd COUNTER -> DERIVE for some plugins. Set min/max values.
#
# Revision 1.8  2004/05/20 13:57:12  jimmyo
# Set categories to some of the plugins.
#
# Revision 1.7  2004/05/14 21:16:46  jimmyo
# "Upped" som plugins from contrib/manual to auto.
#
# Revision 1.6  2004/04/27 21:32:06  jimmyo
# Clarified the vlabels in the apache-plugins (Deb#238594).
#
# Revision 1.5  2004/04/27 08:46:57  jimmyo
# Fixed broken autoconf in apache-* plugins (Deb#236144).
#
# Revision 1.4  2004/02/18 15:47:35  jimmyo
# The generic/apache_* plugins now have defined max values.
#
# Revision 1.3  2004/02/03 17:17:25  jimmyo
# Generic/apache-plugins have been modified to properly to report the correct autoconf value. Also, bugfixes in _processes and _volume.
#
# Revision 1.2  2004/01/29 18:47:30  jimmyo
# Made plugins apache_* compatible with older versions of LWP::UserAgent (SF#881411).
#
# Revision 1.1  2004/01/02 18:50:00  jimmyo
# Renamed occurrances of lrrd -> munin
#
# Revision 1.1.1.1  2004/01/02 15:18:07  jimmyo
# Import of LRRD CVS tree after renaming to Munin
#
# Revision 1.4  2003/12/18 16:35:33  jimmyo
# fail more gracefully when using uninstalled perl modules.
#
# Revision 1.3  2003/11/07 17:43:16  jimmyo
# Cleanups and log entries
#
#
#
# Magic markers:
#%# family=auto
#%# capabilities=autoconf

my $ret = undef;
if (! eval "require LWP::UserAgent;")
{
	$ret = "LWP::UserAgent not found";
}

my $URL = exists $ENV{'url'} ? $ENV{'url'} : "http://127.0.0.1:%d/server-status?auto";
my @PORTS = exists $ENV{'ports'} ? split(' ', $ENV{'ports'}) : (80);

if ( exists $ARGV[0] and $ARGV[0] eq "autoconf" )
{
	if ($ret)
	{
		print "no ($ret)\n";
		exit 1;
	}
	
	my $ua = LWP::UserAgent->new(timeout => 30);

	my @badports;
	foreach my $port (@PORTS) {
		my $url = sprintf $URL, $port;
		my $response = $ua->request(HTTP::Request->new('GET',$url));
		push @badports, $port unless $response->is_success and $response->content =~ /^Total Accesses:/im;
	}
	if (@badports) {
		print "no (no apache server-status or ExtendedStatus missing on ports @badports)\n";
		exit 1;
	} else {
		print "yes\n";
		exit 0;
	}
}

if ( exists $ARGV[0] and $ARGV[0] eq "config" )
{
	print "graph_title Apache volume\n";
	print "graph_args --base 1000\n";
	print "graph_vlabel bytes per \${graph_period}\n";
	print "graph_category apache\n";
	foreach my $port (@PORTS) {
		print "volume$port.label port $port\n";
		print "volume$port.type DERIVE\n";
		print "volume$port.max 1000000000\n";
		print "volume$port.min 0\n";
	}
	exit 0;
}

my $ua = LWP::UserAgent->new(timeout => 30);

foreach my $port (@PORTS) {
	my $url = sprintf $URL, $port;
	my $response = $ua->request(HTTP::Request->new('GET',$url));
	if ($response->content =~ /^Total kBytes:\s+(.+)$/im) {
		print "volume$port.value ", ($1*1024), "\n";
	} else {
		print "volume$port.value U\n";
	}
}

# vim:syntax=perl
