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

use strict;
no strict "subs";

use LWP;
use JSON;

### Munin conf init

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

### Graph settings

if ( $ARGV[0] and $ARGV[0] eq "config") {
    print "graph_title Documents Text Opened\n";
    print "graph_args -l 0\n";
    print "graph_scale no\n";
    print "graph_category Open Xchange\n";
    print "graph_vlabel Documents Text Opened per second\n";
    
    print "textdocsopened_total.label Documents Text Opened - Total\n";
    print "textdocsopened_total.draw LINE1\n";
    print "textdocsopened_total.type DERIVE\n";
    print "textdocsopened_total.min 0\n";

    print "textdocsopened_ooxml.label Documents Text Opened - OOXML\n";
    print "textdocsopened_ooxml.draw LINE1\n";
    print "textdocsopened_ooxml.type DERIVE\n";
    print "textdocsopened_ooxml.min 0\n";

    print "textdocsopened_binary.label Documents Text Opened - Binary\n";
    print "textdocsopened_binary.draw LINE1\n";
    print "textdocsopened_binary.type DERIVE\n";
    print "textdocsopened_binary.min 0\n";

    print "textdocsopened_odf.label Documents Text Opened - ODF\n";
    print "textdocsopened_odf.draw LINE1\n";
    print "textdocsopened_odf.type DERIVE\n";
    print "textdocsopened_odf.min 0\n";

    exit 0
}

### using Jmx4Perl or the going the classic way

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

### check if the Jolokia Url is set and if the username is not the standard value
if ( $jolokiaUrl && (length($jolokiaUrl) > 0) && ( $username && $username ne "changeMe!Now") ) {
    
    callUrl("textdocsopened_total","/read/com.openexchange.office:name=OfficeMonitoring/DocumentsOpened_Text_Total");
    callUrl("textdocsopened_ooxml","/read/com.openexchange.office:name=OfficeMonitoring/DocumentsOpened_Text_OOXML");
    callUrl("textdocsopened_binary","/read/com.openexchange.office:name=OfficeMonitoring/DocumentsOpened_Text_Binary");
    callUrl("textdocsopened_odf","/read/com.openexchange.office:name=OfficeMonitoring/DocumentsOpened_Text_ODF");
    
} else {
    my $showruntimestats="/opt/open-xchange/sbin/showruntimestats";
    my $showexec="$showruntimestats -f";
    my $val;

    open(SHOWRUNTIME,"$showexec |") || die "can not read monitoring output";   
    
    while (<SHOWRUNTIME>)
    {
        if ($_ =~ /OfficeMonitoring,DocumentsOpened_Text_Total/) {
            s/.*\=//; 
            $val=$_+0;
            print "textdocsopened_total.value $val\n";
            next;
        } elsif ($_ =~ /OfficeMonitoring,DocumentsOpened_Text_OOXML/) {
            s/.*\=//; 
            $val=$_+0;
            print "textdocsopened_ooxml.value $val\n";
            next;
        } elsif ($_ =~ /OfficeMonitoring,DocumentsOpened_Text_Binary/) {
            s/.*\=//; 
            $val=$_+0;
            print "textdocsopened_binary.value $val\n";
            next;
        } elsif ($_ =~ /OfficeMonitoring,DocumentsOpened_Text_ODF/) {
            s/.*\=//; 
            $val=$_+0;
            print "textdocsopened_odf.value $val\n";
            next;
        }
    }
    
    close (SHOWRUNTIME);
}

sub callUrl {
	my $ua = LWP::UserAgent->new();
	my $req = HTTP::Request->new(GET => "$jolokiaUrl$_[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;
		}
	}
}
