#!/usr/bin/perl
#
# Magic markers:
#%# family=auto
#%# capabilities=autoconf
#
# Parameters supported:
#
# 	config
# 	autoconf

=head1 NAME

apache_volume - Munin plugin to monitor the volume of data sent from Apache servers.

=head1 APPLICABLE SYSTEMS

Apache HTTP servers with C</server-status> enabled.

=head1 CONFIGURATION

The plugin needs access to http://localhost/server-status?auto (or
modify the URL 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.

This configuration section shows the defaults of the plugin:

  [apache_*]
     env.url   http://127.0.0.1:%d/server-status?auto
     env.ports 80

The %d in the url will be replaced with the port.  The default port is
80 as snown.

The port list is a space separated list of ports.  NOTE that one
single Apache can have several open ports, and the plugin needs only
to contact one to get the servers global status.  The list of ports is
only needed if you have several B<different> Apaches configured on
your host.

If you need authenticated access to the URL you can specify the
username and password in the URL.  For example:

  [apache_volume]
     env.url http://munin:spamalot@localhost/server-status?auto

This will provide for HTTP basic authentication.

=head1 INTERPRETATION

The plugin shows the Apache HTTP servers global data volume in
bytes. I.e. how many bytes the server has served.

If there is a flat ceiling effect on the graph you may have reached
some kind of bandwidth limit on your outgoing connection.

=head1 VERSION

  $Id: apache_volume.in 1488 2008-02-20 19:11:33Z matthias $

=head1 BUGS

Does not support digest authentication.

=head1 AUTHOR

Unknown

=head1 LICENSE

GPLv2

=cut


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 ( defined $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 ( defined $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
