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

use strict;

use LWP;
use JSON;

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

if ( $ARGV[0] and $ARGV[0] eq "config")
{
	print "graph_title Sessions in Storage\n";
	print "graph_args --base 1000 -l 0\n";
	print "graph_category Open Xchange\n";
	print "graph_vlabel Number\n";
	print "a.label Locally owned\n";
	print "a.draw LINE1\n";
	print "b.label Locally backed up\n";
	print "b.draw LINE1\n";
	exit 0;
}

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

testHazelcast();
callUrl("a","/read/com.hazelcast:*,type=IMap,name=sessions-*/localBackupEntryCount","localBackupEntryCount");
callUrl("b","/read/com.hazelcast:*,type=IMap,name=sessions-*/localOwnedEntryCount","localOwnedEntryCount",);

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}){
			my $jsonValues = $json->{value};
			foreach my $val (values %$jsonValues){
				print "$_[0].value ";
				print $val->{$_[2]},"\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;
		}
	}
}

sub testHazelcast {
	testHazelcastgetPartitionOwner();
	testHazelusesCustomPartitioning();	
}

sub testHazelcastgetPartitionOwner{
	
	my $ua = LWP::UserAgent->new();
	my $req = HTTP::Request->new(GET => "$url"."/exec/com.openexchange.hazelcast:name=Hazelcast%20Toolkit/getPartitionOwner/probe");
	$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}){
			if ( $json->{value} eq "null" ) {
				die "No partition owners detected, unable to retrieve map statistics. value=".$json->{value}."\n";
			}
		} else {
			die "No partition owners detected, unable to retrieve map statistics.\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;
		}
	}
}

sub testHazelcastsupportsPartitionReplicas{
	
	my $ua = LWP::UserAgent->new();
	my $req = HTTP::Request->new(GET => "$url"."/exec/com.openexchange.hazelcast:name=Hazelcast%20Toolkit/supportsPartitionReplicas/");
	$req->header('Accept', => 'text/html');
	$req->authorization_basic($username,$password);
	
	my $response = $ua->request($req);
	
	if ($response->is_success) {
		my $json = decode_json($response->decoded_content);
		if (defined $json->{value}){
			if ( "true" ne $json->{value} ) {
				die "No owner for all configured partition replicas detected, unable to retrieve map statistic. value=".$json->{value}."\n";
			} else {
			}
		} else {
			die "No owner for all configured partition replicas detected, unable to retrieve map statistics.\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;
		}
	}
}

sub testHazelusesCustomPartitioning{
	
	my $ua = LWP::UserAgent->new();
	my $req = HTTP::Request->new(GET => "$url"."/exec/com.openexchange.hazelcast:name=Hazelcast%20Toolkit/usesCustomPartitioning/");
	$req->header('Accept', => 'text/html');
	$req->authorization_basic($username,$password);
	
	my $response = $ua->request($req);
	
	if ($response->is_success) {
		my $json = decode_json($response->decoded_content);
		if (defined $json->{value}){
			if ( "false" eq $json->{value}) {
			} else {
				testHazelcastsupportsPartitionReplicas();
			}
		} else {
			testHazelcastsupportsPartitionReplicas();
		}
	}
	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;
		}
	}
}