#!/usr/bin/perl -w
#%# family=auto
#%# capabilities=autoconf suggest

$showruntimestats="/opt/open-xchange/sbin/showruntimestats";
$showexec="$showruntimestats -x";

if ( $ARGV[0] and $ARGV[0] eq "autoconf")
{
	if (-x $showruntimestats) {
		print "yes\n";
		exit 0;
	}
	else {
		print "no\n";
		exit 0;
	}
}

if ( $ARGV[0] and $ARGV[0] eq "suggest")
{
	open(SHOWRUNTIME,"$showexec |") || die "can not read monitoring output";
	while (<SHOWRUNTIME>)
	{
		if ( $_ =~  /DB Pool/ and $_ =~ /PoolSize/ ) {
		s/.*Pool //;
		s/,.*//;
		$val=$_+0;
		print "$val\n";
		}
	}
	exit 0;
}

$Database=$0;
$Database =~ s/.*_([0-9]+)/$1/;
$Database += 0;

if ( $ARGV[0] and $ARGV[0] eq "config")
{
	print "graph_title Database $Database\n";
	print "graph_args --base 1000 -l 0\n";
	print "graph_category Open Xchange\n";
	print "graph_vlabel Number\n";
	print "a.label NumActive\n";
	print "a.draw AREA\n";
	print "b.label NumIdle\n";
	print "b.draw STACK\n";
	print "c.label NumWaiting\n";
	print "c.draw LINE1\n";
	print "d.label PoolSize\n";
	print "d.draw LINE1\n";
	exit 0;
}


open(SHOWRUNTIME,"$showexec |") || die "can not read monitoring output";   

while (<SHOWRUNTIME>)
{
	if ( $_ =~ /DB Pool $Database,NumActive/) {
		s/.*\=//; 
		$val=$_+0;
		print "a.value $val\n";
		next;
	}
	if ( $_ =~ /DB Pool $Database,NumIdle/) {
		s/.*\=//; 
		$val=$_+0;
		print "b.value $val\n";
		next;
	}
	if ( $_ =~ /DB Pool $Database,NumWaiting/) {
		s/.*\=//; 
		$val=$_+0;
		print "c.value $val\n";
		next;
	}
	if ( $_ =~ /DB Pool $Database,PoolSize/) {
		s/.*\=//; 
		$val=$_+0;
		print "d.value $val\n";
		next;
	}
}

close (SHOWRUNTIME);
