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

use strict;
use JMX::Jmx4Perl;
use JMX::Jmx4Perl::Request;   # Type constants are exported here

if ( $ARGV[0] and $ARGV[0] eq "autoconf")
{
    if (-e "/opt/open-xchange/bundles/com.openexchange.ajp.jar") {
        print "yes\n";
        exit 0;
    } else {
        print "no\n";
        exit 0;
    }
}

if ( $ARGV[0] and $ARGV[0] eq "config")
{
	print "graph_title AJPv13Listener Processing Time\n";
	print "graph_args --base 1000 -l 0\n";
	print "graph_category Open Xchange\n";
	print "graph_vlabel time in ms\n";
	print "a.label MinProcessingTime\n";
	print "a.draw LINE1\n";
	print "a.max 9223372036854775806\n";
	print "b.label AvgProcessingTime\n";
	print "b.draw LINE1\n";
	print "c.label MaxProcessingTime\n";
	print "c.draw LINE1\n";
	exit 0
}
									

### Settings and connection Testing

my $jmx = new JMX::Jmx4Perl(url => $ENV{oxJolokiaUrl},
user => $ENV{oxJolokiaUser},
password => $ENV{oxJolokiaPassword});

### Test if a connection can be made
my $request = new JMX::Jmx4Perl::Request({type => AGENT_VERSION});
my $response = $jmx->request($request);
if ($response->is_error()){
	my $status = $response->status();
	if ($status == 404) {
		die "Link to servlet might not be set correctly, this can be done by altering /etc/munin/plugin-conf.d/ox and setting the correct path to your jolokia servlet";
	}
	elsif ($status == 401) {
		die "Credentials to login might be not set correctly. The credentials are set inside /opt/open-xchange/etc/jolokia.properties on the OX, for munin, those need to be altered at /etc/munin/plugin-conf.d/ox";
	}
	else {
		die "Something went wrong ",$status;
	}
}

###

$request = new JMX::Jmx4Perl::Request({type => READ,
	mbean => "com.openexchange.monitoring:name=AJPv13TaskMonitor",
	attribute => "MinProcessingTime"});
$response = $jmx->request($request);
if ($response->is_ok()){
	print "a.value ",$response->value(),"\n";
}

$request = new JMX::Jmx4Perl::Request({type => READ,
	mbean => "com.openexchange.monitoring:name=AJPv13TaskMonitor",
	attribute => "AvgProcessingTime"});
$response = $jmx->request($request);
if ($response->is_ok()){
	print "b.value ",$response->value(),"\n";
}

$request = new JMX::Jmx4Perl::Request({type => READ,
	mbean => "com.openexchange.monitoring:name=AJPv13TaskMonitor",
	attribute => "MaxProcessingTime"});
$response = $jmx->request($request);
if ($response->is_ok()){
	print "c.value ",$response->value(),"\n";
}
