#!/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/documentconverter/bundles/com.openexchange.documentconverter.server/com.openexchange.documentconverter.server.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 DocumentConverter Peak Job Count\n";
    print "graph_args -l 0\n";
    print "graph_scale no\n";
    print "graph_category Open Xchange\n";
    print "graph_vlabel Peak job count\n";

    print "queuejobs_total.label Peak job count - Total\n";
    print "queuejobs_total.draw LINE1\n";

    print "queuejobs_background.label Peak job count - Background priority\n";
    print "queuejobs_background.draw LINE1\n";

    print "queuejobs_low.label Peak job count - Low priority\n";
    print "queuejobs_low.draw LINE1\n";

    print "queuejobs_medium.label Peak job count - Medium priority\n";
    print "queuejobs_medium.draw LINE1\n";

    print "queuejobs_high.label Peak job count - High priority\n";
    print "queuejobs_high.draw LINE1\n";

    print "queuejobs_instant.label Peak job count - Instant priority\n";
    print "queuejobs_instant.draw LINE1\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("queuejobs_background","/read/com.openexchange.documentconverter:name=DocumentConverterInformation/PeakJobCountInQueue_Background");
    callUrl("queuejobs_low","/read/com.openexchange.documentconverter:name=DocumentConverterInformation/PeakJobCountInQueue_Low");
    callUrl("queuejobs_medium","/read/com.openexchange.documentconverter:name=DocumentConverterInformation/PeakJobCountInQueue_Medium");
    callUrl("queuejobs_high","/read/com.openexchange.documentconverter:name=DocumentConverterInformation/PeakJobCountInQueue_High");
    callUrl("queuejobs_instant","/read/com.openexchange.documentconverter:name=DocumentConverterInformation/PeakJobCountInQueue_Instant");
    callUrl("queuejobs_total","/read/com.openexchange.documentconverter:name=DocumentConverterInformation/PeakJobCountInQueue_Total");

} else {
    my $showruntimestats="/opt/open-xchange/sbin/showruntimestats";
    my $showexec="$showruntimestats --port 9998 -y";
    my $val;

    open(SHOWRUNTIME,"$showexec |") || die "can not read monitoring output";

    while (<SHOWRUNTIME>)
    {
        if ($_ =~ /DocumentConverterInformation,PeakJobCountInQueue_Background/) {
            s/.*\=//;
            $val=$_+0;
            print "queuejobs_background.value $val\n";
            next;
        }
        elsif ($_ =~ /DocumentConverterInformation,PeakJobCountInQueue_Low/) {
            s/.*\=//;
            $val=$_+0;
            print "queuejobs_low.value $val\n";
            next;
        }
        elsif ($_ =~ /DocumentConverterInformation,PeakJobCountInQueue_Medium/) {
            s/.*\=//;
            $val=$_+0;
            print "queuejobs_medium.value $val\n";
            next;
        }
        elsif ($_ =~ /DocumentConverterInformation,PeakJobCountInQueue_High/) {
            s/.*\=//;
            $val=$_+0;
            print "queuejobs_high.value $val\n";
            next;
        }
        elsif ($_ =~ /DocumentConverterInformation,PeakJobCountInQueue_Instant/) {
            s/.*\=//;
            $val=$_+0;
            print "queuejobs_instant.value $val\n";
            next;
        }
        elsif ($_ =~ /DocumentConverterInformation,PeakJobCountInQueue_Total/) {
            s/.*\=//;
            $val=$_+0;
            print "queuejobs_total.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;
        }
    }
}
