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

use strict;
no strict "subs";

use Module::Load::Conditional qw[check_install];

### 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 Operations\n";
    print "graph_args -l 0\n";
    print "graph_scale no\n";
    print "graph_category Open Xchange\n";
    print "graph_vlabel Documents Spreadsheet Operations per second\n";
    
    print "spreadsheetdocsoperations_incoming.label Documents Spreadsheet Operations - Incoming\n";
    print "spreadsheetdocsoperations_incoming.draw LINE1\n";
    print "spreadsheetdocsoperations_incoming.type DERIVE\n";
    print "spreadsheetdocsoperations_incoming.min 0\n";
    
    print "spreadsheetdocsoperations_distributed.label Documents Spreadsheet Operations - Distributed\n";
    print "spreadsheetdocsoperations_distributed.draw LINE1\n";
    print "spreadsheetdocsoperations_distributed.type DERIVE\n";
    print "spreadsheetdocsoperations_distributed.min 0\n";

    exit 0
}

### using Jmx4Perl or the going the classic way

my $jolokiaUrl = $ENV{oxJolokiaUrl};

### check if the Jolokia Url is set and if the jmx4perl module is available
if ( $jolokiaUrl && (length($jolokiaUrl) > 0) && check_install(module => 'JMX::Jmx4Perl') ) {
    ### Settings and connection Testing
    require JMX::Jmx4Perl;
    require JMX::Jmx4Perl::Request;
    
    my $jmx = new JMX::Jmx4Perl(url => $jolokiaUrl,
    	user => $ENV{oxJolokiaUser},
    	password => $ENV{oxJolokiaPassword});
    
    ### test if a connection can be made
    my $request = new JMX::Jmx4Perl::Request({type => "version"});
    my $response = $jmx->request($request);
    
    if ($response->is_error()){
        my $status = $response->status();
        
    	if ($status == 404) {
            die "Link to servlet might be wrong";
        }
        elsif ($status == 401) {
            die "Credentials to login might be wrong";
        }
        else {
            die "Something went wrong ",$status;
        }
    }
    
    ### sending the request
    
    $request = new JMX::Jmx4Perl::Request("read",
        "com.openexchange.office:name=OfficeMonitoring",
        "DocumentsOperations_Spreadsheet_Incoming");
    $response = $jmx->request($request);
    
    if ($response->is_ok()){
        print "spreadsheetdocsoperations_incoming.value ",$response->value(),"\n";
    }

    ###

    $request = new JMX::Jmx4Perl::Request("read",
        "com.openexchange.office:name=OfficeMonitoring",
        "DocumentsOperations_Spreadsheet_Distributed");
    $response = $jmx->request($request);
    
    if ($response->is_ok()){
        print "spreadsheetdocsoperations_distributed.value ",$response->value(),"\n";
    }
} 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,DocumentsOperations_Spreadsheet_Incoming/) {
            s/.*\=//; 
            $val=$_+0;
            print "spreadsheetdocsoperations_incoming.value $val\n";
            next;
        } elsif ($_ =~ /OfficeMonitoring,DocumentsOperations_Spreadsheet_Distributed/) {
            s/.*\=//; 
            $val=$_+0;
            print "spreadsheetdocsoperations_distributed.value $val\n";
            next;
        } 
    }
    
    close (SHOWRUNTIME);
}
