#!/usr/bin/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.
#
# 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: apache_volume.in,v $
# Revision 1.3  2003/11/07 17:43:16  jimmyo
# Cleanups and log entries
#
#
#
# Magic markers:
#%# family=manual
#%# capabilities=autoconf

use LWP::UserAgent;

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" )
{
	my $ua = LWP::UserAgent->new(timeout => 30);

	my @badports;
	foreach my $port (@PORTS) {
		my $url = sprintf $URL, $port;
		my $response = $ua->get($url);
		push @badports, $port unless $response->is_success and $response->content =~ /IdleServers/;
	}
	if (@badports) {
		print "no (no apache server-status 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 volume\n";
	foreach my $port (@PORTS) {
		print "volume$port.label port $port\n";
		print "volume$port.type COUNTER\n";
	}
	exit 0;
}

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

foreach my $port (@PORTS) {
	my $url = sprintf $URL, $port;
	my $response = $ua->get($url);
	if ($response->content =~ /^Total Accesses:\s+(.+)$/im) {
		print "volume$port.value $1\n";
	} else {
		print "volume$port.value U\n";
	}
}

# vim:syntax=perl
