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

###
# Infos about Web Sockets:
#   count:                The number of open Web Sockets on target node.
#   buffered-messages:    The number of buffered messages that are supposed to be transmitted to remote cluster members. 
###

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

if ( $ARGV[0] and $ARGV[0] eq "autoconf")
{
    if (-e "/opt/open-xchange/bundles/com.openexchange.websockets.grizzly.jar") {
        print "yes\n";
        exit 0;
    }
    else {
        print "no\n";
        exit 0;
    }
}

if ( $ARGV[0] and $ARGV[0] eq "config")
{
    print "graph_title Web Socket\n";
    print "graph_args --base 1000 -l 0\n";
    print "graph_category Open Xchange\n";
    print "graph_vlabel Number\n";
    
    print "count.label count\n";
    print "count.draw LINE1\n";
    print "count.type GAUGE\n";
    print "count.min 0\n";
    
    print "buffered-message.label buffered-message\n";
    print "buffered-message.draw LINE1\n";
    print "buffered-message.type GAUGE\n";
    print "buffered-message.min 0\n";

    exit 0
}

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

while (<SHOWRUNTIME>)
{
    if ( $_ =~ /WebSocketMBean,NumberOfWebSockets/) {
        s/.*\=//;
        $val=$_+0;
        print "count.value $val\n";
        next;
    }
    if ( $_ =~ /WebSocketMBean,NumberOfBufferedMessages/) {
        s/.*\=//;
        $val=$_+0;
        print "buffered-message.value $val\n";
        next;
    }
}

close (SHOWRUNTIME);
