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

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

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

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

    exit 0
}

### using Jmx4Perl only

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("spreadsheetdocsopened_total","/read/com.openexchange.office:name=OfficeMonitoring/DocumentsOpened_Spreadsheet_Total");
    callUrl("spreadsheetdocsopened_ooxml","/read/com.openexchange.office:name=OfficeMonitoring/DocumentsOpened_Spreadsheet_OOXML");
    callUrl("spreadsheetdocsopened_binary","/read/com.openexchange.office:name=OfficeMonitoring/DocumentsOpened_Spreadsheet_Binary");
    callUrl("spreadsheetdocsopened_odf","/read/com.openexchange.office:name=OfficeMonitoring/DocumentsOpened_Spreadsheet_ODF");
    
} else {
    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";
}

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;
		}
	}
}
