#!/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 Created\n";
    print "graph_args -l 0\n";
    print "graph_scale no\n";
    print "graph_category Open Xchange\n";
    print "graph_vlabel Documents Created per second\n";
    
    print "docscreated_total.label Documents Created - Total\n";
    print "docscreated_total.draw LINE1\n";
    print "docscreated_total.type DERIVE\n";
    print "docscreated_total.min 0\n";

    print "docscreated_text.label Documents Created - Text\n";
    print "docscreated_text.draw LINE1\n";
    print "docscreated_text.type DERIVE\n";
    print "docscreated_text.min 0\n";

    print "docscreated_spreadsheet.label Documents Created - Spreadsheet\n";
    print "docscreated_spreadsheet.draw LINE1\n";
    print "docscreated_spreadsheet.type DERIVE\n";
    print "docscreated_spreadsheet.min 0\n";

#    print "docscreated_presentation.label Documents Created - Presentation\n";
#    print "docscreated_presentation.draw LINE1\n";
#    print "docscreated_presentation.type DERIVE\n";
#    print "docscreated_presentation.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("docscreated_total","/read/com.openexchange.office:name=OfficeMonitoring/DocumentsCreated_Total");
    callUrl("docscreated_text","/read/com.openexchange.office:name=OfficeMonitoring/DocumentsCreated_Text");
    callUrl("docscreated_spreadsheet","/read/com.openexchange.office:name=OfficeMonitoring/DocumentsCreated_Spreadsheet");
    callUrl("docscreated_presentation","/read/com.openexchange.office:name=OfficeMonitoring/DocumentsCreated_Presentation");
    
} 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,DocumentsCreated_Total/) {
            s/.*\=//; 
            $val=$_+0;
            print "docscreated_total.value $val\n";
            next;
        } elsif ($_ =~ /OfficeMonitoring,DocumentsCreated_Text/) {
            s/.*\=//; 
            $val=$_+0;
            print "docscreated_text.value $val\n";
            next;
        } elsif ($_ =~ /OfficeMonitoring,DocumentsCreated_Spreadsheet/) {
            s/.*\=//; 
            $val=$_+0;
            print "docscreated_spreadsheet.value $val\n";
            next;
        } elsif ($_ =~ /OfficeMonitoring,DocumentsCreated_Presentation/) {
            s/.*\=//; 
            $val=$_+0;
            print "docscreated_presentation.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;
		}
	}
}
