#!/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 Median Conversion Time\n";
    print "graph_args -l 0\n";
    print "graph_scale no\n";
    print "graph_category Open Xchange\n";
    print "graph_vlabel Median conversion time (seconds)\n";

    print "conversiontime_total.cdef conversiontime_total,0.001,*\n";
    print "conversiontime_total.label Median conversion time - Total\n";
    print "conversiontime_total.draw LINE1\n";

    print "conversiontime_background.cdef conversiontime_background,0.001,*\n";
    print "conversiontime_background.label Median conversion time - Background priority\n";
    print "conversiontime_background.draw LINE1\n";

    print "conversiontime_low.cdef conversiontime_low,0.001,*\n";
    print "conversiontime_low.label Median conversion time - Low priority\n";
    print "conversiontime_low.draw LINE1\n";

    print "conversiontime_medium.cdef conversiontime_medium,0.001,*\n";
    print "conversiontime_medium.label Median conversion time - Medium priority\n";
    print "conversiontime_medium.draw LINE1\n";

    print "conversiontime_high.cdef conversiontime_high,0.001,*\n";
    print "conversiontime_high.label Median conversion time - High priority\n";
    print "conversiontime_high.draw LINE1\n";

    print "conversiontime_instant.cdef conversiontime_instant,0.001,*\n";
    print "conversiontime_instant.label Median conversion time - Instant priority\n";
    print "conversiontime_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("conversiontime_background","/read/com.openexchange.documentconverter:name=DocumentConverterInformation/MedianConversionTimeMillis_Background");
    callUrl("conversiontime_low","/read/com.openexchange.documentconverter:name=DocumentConverterInformation/MedianConversionTimeMillis_Low");
    callUrl("conversiontime_medium","/read/com.openexchange.documentconverter:name=DocumentConverterInformation/MedianConversionTimeMillis_Medium");
    callUrl("conversiontime_high","/read/com.openexchange.documentconverter:name=DocumentConverterInformation/MedianConversionTimeMillis_High");
    callUrl("conversiontime_instant","/read/com.openexchange.documentconverter:name=DocumentConverterInformation/MedianConversionTimeMillis_Instant");
    callUrl("conversiontime_total","/read/com.openexchange.documentconverter:name=DocumentConverterInformation/MedianConversionTimeMillis_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,MedianConversionTimeMillis_Background/) {
            s/.*\=//;
            $val=$_+0.0;
            print "conversiontime_background.value $val\n";
            next;
        }
        elsif ($_ =~ /DocumentConverterInformation,MedianConversionTimeMillis_Low/) {
            s/.*\=//;
            $val=$_+0.0;
            print "conversiontime_low.value $val\n";
            next;
        }
        elsif ($_ =~ /DocumentConverterInformation,MedianConversionTimeMillis_Medium/) {
            s/.*\=//;
            $val=$_+0.0;
            print "conversiontime_medium.value $val\n";
            next;
        }
        elsif ($_ =~ /DocumentConverterInformation,MedianConversionTimeMillis_High/) {
            s/.*\=//;
            $val=$_+0.0;
            print "conversiontime_high.value $val\n";
            next;
        }
        elsif ($_ =~ /DocumentConverterInformation,MedianConversionTimeMillis_Instant/) {
            s/.*\=//;
            $val=$_+0.0;
            print "conversiontime_instant.value $val\n";
            next;
        }
        elsif ($_ =~ /DocumentConverterInformation,MedianConversionTimeMillis_Total/) {
            s/.*\=//;
            $val=$_+0.0;
            print "conversiontime_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;
        }
    }
}
