#!/usr/bin/perl
#
# Plugin to monitor the number of apache-processes running on the
# machine, and (in addition to a simple process count), separate then
# into "busy" or "idle" servers.
#
# 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
#
# $Log: apache_processes.in,v $
# Revision 1.2  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/server-status?auto";

if ( exists $ARGV[0] and $ARGV[0] eq "autoconf" )
{
	my $ua = LWP::UserAgent->new(timeout => 30);

	my @badports;
	my $response = $ua->get($URL);
	if ($response->is_success and $response->content =~ /IdleServers/) {
		print "yes\n";
		exit 0;
	} else {
		print "no (no apache server-status on ports @badports)\n";
		exit 1;
	}
}

if ( exists $ARGV[0] and $ARGV[0] eq "config" )
{
        print "graph_title Apache processes\n";
        print "graph_args --base 1000\n";
        print "graph_order busy idle\n";
        print "graph_vlabel processes\n";
        print "graph_total total\n";
        print "busy.label busy servers\n";
        print "busy.draw AREA\n";
        print "idle.label idle servers\n";
        print "idle.draw STACK\n";
	exit 0;
}

my $ua = LWP::UserAgent->new(timeout => 30);
my $response = $ua->get($URL);
if ($response->content =~ /^BusyServers:\s+(.+)$/im) {
	print "busy.value $1\n";
} else {
	print "busy.value U\n";
}
if ($response->content =~ /^IdleServers:\s+(.+)$/im) {
	print "idle.value $1\n";
} else {
	print "idle.value U\n";
}

# vim:syntax=perl
