#!/usr/bin/perl -w
#
# Copyright (C) 2007 Guillaume Blairon
#
# 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; version 2 dated June,
# 1991.
#
# 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.
#
#
#
# Script to monitor MogileFS tracker queries.
# Mostly an adaptation of Jimmy Olsen's squid_cache script.
#
# Usage:
#    ln -s /usr/share/munin/plugins/mogilefsd_activity \
#       /etc/munin/plugins/
# 
# Configuration variables:
#
#    host (default: '127.0.0.1')
#    port (default: '6001')
#
# Parameters:
#
#    config    (required)
#    autoconf  (optional - only used by munin-config)
#
#%# family=auto
#%# capabilities=autoconf

my $ret = undef;

my $DEBUG = 0;
my @known_states=qw(queries);

if (! eval "require IO::Socket;")
{
        $ret = "IO::Socket not found";
}

$mogilefsd_host = $ENV{host} || "127.0.0.1";
$mogilefsd_port = $ENV{port} || 6001;

if($ARGV[0] and $ARGV[0] eq "autoconf") {
    &autoconf($mogilefsd_host, $mogilefsd_port);
}

sub autoconf {
    my ($host, $port) = @_;

    if ($ret)
    {
        print "no ($ret)\n";
        exit 1;
    }

    my $conn = IO::Socket::INET->new(PeerAddr => $host,
                                        PeerPort => $port,
                                        Proto    => 'tcp',
                                        Timeout  => 5) or die($!);

    if (!$conn)
    {
        print "no (could not connect: $!)\n";
        exit 1;
    }

    my $request = "!stats\n";

    $conn->syswrite($request, length($request));

    my @lines = qw();
    while (my $line = $conn->getline) {
        if ($line =~ /^\./) {
            last;
        }
        push(@lines, $line);
    }
    close($conn);

    print "yes\n";
    exit 0;
}


sub query_mogilefsd {
    my ($host, $port) = @_;

    my $conn = IO::Socket::INET->new(PeerAddr => $host,
                                        PeerPort => $port,
                                        Proto    => 'tcp') or die($!);

    my $request = "!stats\n";

    $conn->syswrite("$request", length("$request"));

    my @lines = qw();
    while (my $line = $conn->getline) {
        if ($line =~ /^\./) {
            last;
        }
        push(@lines, $line);
    }
    close($conn);

    for(my $i = 0; $i <= $#lines; $i++) {
        if($lines[$i] =~ /^([^\s]*)\s(\d+)/) {
            $states{"$1"} = "$2";
        }
    }
    keys %states;
}

if($ARGV[0] and $ARGV[0] eq "config") {

    print "graph_title MogileFS tracker queries\n";
    print "graph_vlabel queries/sec\n";
    print "graph_args -l 0\n";
    print "graph_category fs\n";
    print "queries.label queries\n";
    print "queries.type DERIVE\n";
    print "queries.draw AREA\n";
    print "queries.min 0\n";

    exit 0;
}   
    
print %states;
        
&query_mogilefsd($mogilefsd_host, $mogilefsd_port);

foreach $key (@known_states) {
        print "$key.value ";
        if (exists $states{$key}) {
            print $states{$key}, "\n";
        } else {
            print "0\n";
        }
}
