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

use strict;

use LWP;
use JSON;

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

if ( $ARGV[0] and $ARGV[0] eq "config")
{
	print "graph_title Cache objects\n";
	print "graph_args --base 1000 -l 0\n";
	print "graph_category Open Xchange\n";
	print "graph_vlabel Number of objects in caches\n";
	print "a.label Context\n";
	print "a.draw LINE1\n";
    print "b.label Filestore\n";
    print "b.draw LINE1\n";
    print "c.label DBPool\n";
    print "c.draw LINE1\n";
    print "d.label User\n";
    print "d.draw LINE1\n";
    print "e.label UserConfiguration\n";
    print "e.draw LINE1\n";
    print "f.label UserSettingMail\n";
    print "f.draw LINE1\n";
    print "g.label OXFolder\n";
    print "g.draw LINE1\n";
    print "h.label OXFolderQuery\n";
    print "h.draw LINE1\n";
    print "i.label IMAP Connection\n";
    print "i.draw LINE1\n";
    print "j.label Message\n";
    print "j.draw LINE1\n";
    print "k.label Mail Message\n";
    print "k.draw LINE1\n";
    print "l.label Mail Connection\n";
    print "l.draw LINE1\n";
    print "m.label Calendar\n";
    print "m.draw LINE1\n";
    print "n.label Session\n";
    print "n.draw LINE1\n";
	exit 0
}

my $url = $ENV{oxJolokiaUrl};
my $username = $ENV{oxJolokiaUser};
my $password = $ENV{oxJolokiaPassword};

callUrl("a","/exec/com.openexchange.caching:name=JCSCacheInformation/getMemoryCacheCount/Context");
callUrl("b","/exec/com.openexchange.caching:name=JCSCacheInformation/getMemoryCacheCount/Filestore");
callUrl("c","/exec/com.openexchange.caching:name=JCSCacheInformation/getMemoryCacheCount/OXDBPoolCache");
callUrl("d","/exec/com.openexchange.caching:name=JCSCacheInformation/getMemoryCacheCount/User");
callUrl("e","/exec/com.openexchange.caching:name=JCSCacheInformation/getMemoryCacheCount/UserConfiguration");
callUrl("f","/exec/com.openexchange.caching:name=JCSCacheInformation/getMemoryCacheCount/UserSettingMail");
callUrl("g","/exec/com.openexchange.caching:name=JCSCacheInformation/getMemoryCacheCount/OXFolderCache");
callUrl("h","/exec/com.openexchange.caching:name=JCSCacheInformation/getMemoryCacheCount/OXFolderQueryCache");
callUrl("i","/exec/com.openexchange.caching:name=JCSCacheInformation/getMemoryCacheCount/OXIMAPConCache");
callUrl("j","/exec/com.openexchange.caching:name=JCSCacheInformation/getMemoryCacheCount/OXMessageCache");
callUrl("k","/exec/com.openexchange.caching:name=JCSCacheInformation/getMemoryCacheCount/MailMessageCache");
callUrl("l","/exec/com.openexchange.caching:name=JCSCacheInformation/getMemoryCacheCount/MailConnectionCache");
callUrl("m","/exec/com.openexchange.caching:name=JCSCacheInformation/getMemoryCacheCount/CalendarCache");
callUrl("n","/exec/com.openexchange.caching:name=JCSCacheInformation/getMemoryCacheCount/SessionCache");

sub callUrl {
	my $ua = LWP::UserAgent->new();
	my $req = HTTP::Request->new(GET => "$url$_[1]");
	$req->authorization_basic($username,$password);
	
	$req->header('Accept', => 'text/html');
	
	my $response = $ua->request($req);
	
	if ($response->is_success) {
		my $json = decode_json($response->decoded_content);
		if (defined $json->{value}){
			print "$_[0].value ";
			print $json->{value},"\n";
		}
	}
	else {
		my $status = $response->status_line;
		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:\n",$status;
		}
	}
}