#!/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.http.grizzly/com.openexchange.http.grizzly.jar") {
        print "yes\n";
        exit 0;
    } else {
        print "no\n";
        exit 0;
    }
}

if ( $ARGV[0] and $ARGV[0] eq "config") {
    print "graph_title Grizzly HttpCodecFilter\n";
    print "graph_args --base 1000 -l 0\n";
    print "graph_category Open Xchange\n";
    print "graph_vlabel Number\n";
    
    print "total-bytes-written.label total-bytes-written\n";
    print "total-bytes-written.draw LINE1\n";
    print "total-bytes-written.type DERIVE\n";
    print "total-bytes-written.min 0\n";
    
    print "total-bytes-received.label total-bytes-received\n";
    print "total-bytes-received.draw LINE1\n";
    print "total-bytes-received.type DERIVE\n";
    print "total-bytes-received.min 0\n";
    
    print "http-codec-error-count.label http-codec-error-count\n";
    print "http-codec-error-count.draw LINE1\n";
    print "http-codec-error-count.type DERIVE\n";
    print "http-codec-error-count.min 0\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(READ,
	"org.glassfish.grizzly:name=HttpCodecFilter,pp=/gmbal-root/HttpServer[HttpServer]/NetworkListener[NetworkListener[http-listener]],type=HttpCodecFilter",
	"total-bytes-written");
$response = $jmx->request($request);
if ($response->is_ok()){
	print "total-bytes-written.value ",$response->value(),"\n";
}

$request = new JMX::Jmx4Perl::Request(READ,
	"org.glassfish.grizzly:name=HttpCodecFilter,pp=/gmbal-root/HttpServer[HttpServer]/NetworkListener[NetworkListener[http-listener]],type=HttpCodecFilter",
	"total-bytes-received");
$response = $jmx->request($request);
if ($response->is_ok()){
	print "total-bytes-received.value ",$response->value(),"\n";
}

$request = new JMX::Jmx4Perl::Request(READ,
	"org.glassfish.grizzly:name=HttpCodecFilter,pp=/gmbal-root/HttpServer[HttpServer]/NetworkListener[NetworkListener[http-listener]],type=HttpCodecFilter",
	"http-codec-error-count");
$response = $jmx->request($request);
if ($response->is_ok()){
	print "http-codec-error-count.value ",$response->value(),"\n";
}

