#!/usr/bin/env perl
# -*- perl -*-

=head1 NAME

openfire_ a Plugin for displaying Openfire Stats

=head1 INTERPRETATION

This plugin displays the following charts:

1) db 				Displaying DB Connections Stats.
2) threads 			Displaying Stats about Openfire Stats.
3) tasks 			Displaying Stats about Openfire Background Tasks.
4) sessions 		Displaying the Number of current Jabber Sessions.
5) nio 				Displaying some Network I/O Stats.
6) queue	Displaying the Number of queued Events
7) timestamp 		Displaying the Log Timestamp (should be increasing - for Debugging)

You can set the modes with naming the softlink:

1) openfire_db
2) openfire_threads
3) openfire_tasks
4) openfire_sessions
5) openfire_nio
6) openfire_queue
7) openfire_timestamp

=head1 CONFIGURATION

You need to have the Openfire "Load Statistic" Plugin-installed!

This plugin is configurable environment variables.


user root 		must be root user
env.statsfile 	path to stats.txt file

Example:

[openfire_*]
user root
env.statsfile /var/log/openfire/stats.txt


=head1 AUTHOR

Philipp Haussleiter <philipp@haussleiter.de> (email)

=head1 LICENSE

GPLv2

=cut

# MAIN
use warnings;
use strict;
use File::Basename;

my $statsfile = ($ENV{'statsfile'} || '/usr/share/openfire/logs/stats.txt');

my $tailBin = "/usr/bin/tail";

# db,threads,tasks,sessions,nio,timestamp
my $type = basename($0);
$type =~ s/openfire_//;


if ( exists $ARGV[0] and $ARGV[0] eq "config" ) {
	if( $type eq "db" ) {
		print "graph_args --base 1000 -l 0\n";
		print "graph_title Openfire DB Usage\n";
		print "graph_vlabel Number of Connections\n";
		print "graph_category Openfire\n";
		print "graph_info The Graph shows Number of DB Connections\n";
		print "db_used.draw AREA\ndb_used.label used\ndb_used.type GAUGE\n";
		print "db_current.draw STACK\ndb_current.label current\ndb_current.type GAUGE\n";
		print "db_min.draw STACK\ndb_min.label min\ndb_min.type GAUGE\n";
		print "db_max.draw STACK\ndb_max.label max\ndb_max.type GAUGE\n";
	}
	if( $type eq "threads" ) {
		print "graph_args --base 1000 -l 0\n";
		print "graph_scale no\n";
		print "graph_title Openfire Threads\n";
		print "graph_vlabel Number of Threads\n";
		print "graph_category Openfire\n";
		print "graph_info The Graph shows Number of Threads\n";
		print "thread_core.draw AREA\nthread_core.label Core Threads\nthread_core.type GAUGE\n";
		print "thread_active.draw STACK\nthread_active.label active Threads\nthread_active.type GAUGE\n";
	}
	if( $type eq "tasks" ) {
		print "graph_args --base 1000 -l 0\n";
		print "graph_scale no\n";
		print "graph_title Openfire Tasks\n";
		print "graph_vlabel Number of Tasks\n";
		print "graph_category Openfire\n";
		print "graph_info The Graph shows Number of Tasks\n";
		print "task_queue.draw AREA\ntask_queue.label queued Tasks\ntask_queue.type GAUGE\n";
		print "task_completed.draw STACK\ntask_completed.label completed Tasks\ntask_completed.type DERIVE\n";
	}
	if( $type eq "sessions" ) {
		print "graph_args --base 1000 -l 0\n";
		print "graph_scale no\n";
		print "graph_title Openfire Sessions\n";
		print "graph_vlabel Number of Sessions\n";
		print "graph_category Openfire\n";
		print "graph_info The Graph shows Number of Jabber Sessions\n";
		print "sessions.label Jabber Sessions\nsessions.type GAUGE\n";
	}
	if( $type eq "nio" ) {
		print "graph_args --base 1000 -l 0\n";
		print "graph_title Openfire I/O (NIO)\n";
		print "graph_vlabel Reads/Writes per \${graph_period} in (-) / out (+)\n";
		print "graph_category Openfire\n";
		print "graph_info The Graph shows Number of Stanzas\n";
       		print "nio_written.label nio\nnio_written.draw LINE1\nnio_written.max 10000\nnio_written.min 0\nnio_written.graph no\n";
        	print "nio_read.label nio\nnio_read.draw LINE1\nnio_read.max 10000\nnio_read.min 0\nnio_read.negative nio_written\nnio_read.type DERIVE\n";
	}
    	if( $type eq "queue" ) {
		print "graph_args --base 1000 -l 0\n";
		print "graph_title Openfire Queue\n";
		print "graph_vlabel Number of queued Stanzas\n";
		print "graph_category Openfire\n";
		print "graph_info The Graph shows Number of queued Stanzas\n";
		print "nio_queued_events.draw AREA\nnio_queued_events.label queued Events\nnio_queued_events.type GAUGE\n";
		print "nio_queued_writes.draw STACK\nnio_queued_writes.label queued Writes\nnio_queued_writes.type GAUGE\n";
	}
	if( $type eq "timestamp" ) {
		print "graph_args --base 1000 -l 0\n";
		print "graph_title Openfire Log Timestamp\n";
		print "graph_vlabel Timestamp\n";
		print "graph_category Openfire\n";
		print "graph_info The Graph shows the Timestamp of the Log\n";
		print "timestamp.draw AREA\ntimestamp.label Log Timestamp\ntimestamp.type GAUGE\n";
	}
} else {
	my $cmd = "$tailBin -1 $statsfile";
	my $result = `$cmd`;
	my @array=split(/,/,$result);
	my $count = scalar @array;
	my $diff = 0;
	if($count < 14) {
		$diff = 4;
	}

	if( $type eq "timestamp" ) {
		print "timestamp.value $array[0]\n";
	}
	if( $count == 14 && $type eq "db" ) {
		print "db_min.value $array[1]\n";
		print "db_max.value $array[2]\n";
		print "db_current.value $array[3]\n";
		print "db_used.value $array[4]\n";
	}
	if( $type eq "threads" ) {
		print "thread_core.value $array[5-$diff]\n";
		print "thread_active.value $array[6-$diff]\n";
	}
	if( $type eq "tasks" ) {
		print "task_queue.value $array[7-$diff]\n";
		print "task_completed.value $array[8-$diff]\n";
	}
	if( $type eq "sessions" ) {
		print "sessions.value $array[9-$diff]\n";
	}
	if( $type eq "nio" ) {
		print "nio_read.value $array[10-$diff]\n";
		print "nio_written.value $array[11-$diff]\n";
	}
	if( $type eq "queue" ) {
		print "nio_queued_events.value $array[12-$diff]\n";
		print "nio_queued_writes.value $array[13-$diff]";
	}
}