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

$showruntimestats="/opt/open-xchange/sbin/showruntimestats";
$showexec="$showruntimestats -i";

if ( $ARGV[0] and $ARGV[0] eq "autoconf") {
    if (-x $showruntimestats) {
        print "yes\n";
        exit 0;
    } else {
        print "no\n";
        exit 0;
    }
}

if ( $ARGV[0] and $ARGV[0] eq "config") {
    my @shortvals;
    my @longvals;
    open(SHOWRUNTIME, "$showexec |") || die "can not read monitoring output";
    while (<SHOWRUNTIME>) {
        chomp;
        if ( $_ =~ /SessionD Toolkit,NumberOfShortTermSessions/ ) {
            s/.*\= \[(.*)\]/$1/;
            @shortvals = split(/,[\s]/, $_);
        }
        if ( $_ =~ /SessionD Toolkit,NumberOfLongTermSessions/ ) {
            s/.*\= \[(.*)\]/$1/;
            @longvals = split(/,[\s]/, $_);
        }
    }
    close(SHOWRUNTIME);
    print "graph_title Sessions per Container\n";
    print "graph_args --base 1000 -l 0\n";
    print "graph_category Open Xchange\n";
    print "graph_vlabel Number\n";
    for ($i = 0; $i < @shortvals; $i++) {
        print "s$i.label Short term session container $i\n";
        print "s$i.draw LINE1\n";
    }
    for ($i = 0; $i < @longvals; $i++) {
        print "l$i.label Long term session container $i\n";
        print "l$i.draw LINE1\n";
    }
    exit 0
}

my @shortvals;
my @longvals;
open(SHOWRUNTIME, "$showexec |") || die "can not read monitoring output";
while (<SHOWRUNTIME>) {
    chomp;
    if ( $_ =~ /SessionD Toolkit,NumberOfShortTermSessions/ ) {
        s/.*\= \[(.*)\]/$1/;
        @shortvals = split(/,[\s]/, $_);
    }
    if ( $_ =~ /SessionD Toolkit,NumberOfLongTermSessions/ ) {
        s/.*\= \[(.*)\]/$1/;
        @longvals = split(/,[\s]/, $_);
    }
}
close (SHOWRUNTIME);
for ($i = 0; $i < @shortvals; $i++) {
    print "s$i.value $shortvals[$i]\n";
}
for ($i = 0; $i < @longvals; $i++) {
    print "l$i.value $longvals[$i]\n";
}
