#!/usr/bin/perl -w
# -*- perl -*-
#
# ==========================================================================
#
# YaCy Monitoring Plugin for Munin
# Copyright (C) 2010  Christophe Nowicki
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
# ==========================================================================
#

=head1 NAME

yacy - Munin plugin to monitor YaCy distributed search engine network.

=head1 APPLICABLE SYSTEMS

YaCy 

=head1 CONFIGURATION

=head1 INTERPRETATION

The plugin shows various YaCy Network stats.

=head1 MAGIC MARKERS

#%# family=auto
#%# capabilities=autoconf suggest

=head1 BUGS

=head1 VERSION

  $Id$

=head1 AUTHOR

Christophe 'CSCMEU' Nowicki <cscm dot csquad dot org>

=head1 LICENSE

GPLv2

=cut

BEGIN {
	if(!eval "require XML::Smart;") {	
		die("XML::Smart not found");
	}
	if(!eval "require LWP;") {	
		die("LWP not found");
	}
}
	
use XML::Smart;
use strict;

my $host   = $ENV{'host'}   || 'localhost';

$0 =~ /yacy_(.+)*$/;
my $action = $1;

# Autoconf
if ($ARGV[0] and $ARGV[0] eq "autoconf") {
	print "yes\n";
	exit;
}

# Suggest
if ($ARGV[0] and $ARGV[0] eq "suggest")
{
	print <<EOF;
network_peers
network_documents
network_ppm
network_qpm
local_documents
local_ppm
local_qpm
EOF
	exit 0;
}

die("PUDDI PUDDI PUDDI PUDDI PUDDI") unless ($action);

# Config
if ($ARGV[0] and $ARGV[0] eq "config") 
{
	my $titles = {
		network_peers => 'YaCy Network Online Peers',
		network_documents => 'YaCy Network Number of Documents',
		network_ppm => 'YaCy Network Indexing Speed Pages Per Minutes',
		network_qph => 'YaCy Network Query Frequency Queries Per Hour',
		local_documents => 'YaCy Local Number of Documents',
		local_ppm => 'YaCy Local Indexing Speed Pages Per Minutes',
		local_qph => 'YaCy Local Query Frequency Queries Per Hour',
	};
	print <<EOF;
graph_title $titles->{$action}
graph_info $titles->{$action}
graph_category search
EOF
	if ($action =~ /network_(peers|documents)/) {
		print <<EOF;
active.label Active
passive.label Passive
potential.label Potential
EOF
	} elsif($action =~ /local_documents$/)  {
		print "documents.label documents\n";
	} elsif($action =~ /_ppm$/)  {
		print "ppm.label Pages Per Minute\n";
	} elsif($action =~ /_qph$/)  {
		print "qph.label Pages Per Hour\n";
	} else {
		die("PUDDI PUDDI PUDDI PUDDI PUDDI");
	}
	exit 0;
}

my $xml = XML::Smart->new('http://' . $host . '/Network.xml') or die("PUDDI");

if ($action =~ /network_peers/) {
	print <<EOF;
active.value $xml->{peers}{active}{count}
passive.value $xml->{peers}{passive}{count}
potential.value $xml->{peers}{potential}{count}
EOF
} elsif ($action =~ /network_documents/) {
	print <<EOF;
active.value $xml->{peers}{active}{links}
passive.value $xml->{peers}{passive}{links}
potential.value $xml->{peers}{potential}{links}
EOF
} elsif ($action =~ /network_ppm/) {
	print "ppm.value $xml->{peers}{cluster}{ppm}\n"
} elsif ($action =~ /local_ppm/) {
	print "ppm.value $xml->{peers}{your}{ppm}\n"
} elsif ($action =~ /network_qph/) {
	print "qph.value $xml->{peers}{cluster}{qph}\n"
} elsif ($action =~ /local_qph/) {
	print "qph.value $xml->{peers}{your}{qph}\n"
} elsif ($action =~ /local_documents/) {
	print "documents.value $xml->{peers}{your}{links}\n"
} else {
}

# vim:syntax=perl
