#!/usr/bin/perl

#
## Copyright (C) 2012 Andrey Pankov <a.pankov@gmail.com>
##
## 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.
##
##
## $Log$
##
## Based on resque monitoring code from https://github.com/caius/redis-munin
##
## Installation process:
##
## 1. Download the plugin to your plugins directory (e.g. /usr/share/munin/plugins)
## 2. Create 4 symlinks at the directory that is used by munin for plugins detection (e.g. /etc/munin/plugins): resque_failed, resque_queues, resque_workers_count, resque_workers_working
## 3. Edit plugin-conf.d/munin-node if it is needed (env.host and env.port variables are accepted)
## 4. Restart munin-node service

use strict;
use Redis;

my $HOST = exists $ENV{'host'} ? $ENV{'host'} : "127.0.0.1";
my $PORT = exists $ENV{'port'} ? $ENV{'port'} : 6379;

my $server = "$HOST:$PORT";
my $r = Redis->new( server => sprintf("%s:%d", $HOST, $PORT) );

my $config = ( defined $ARGV[0] and $ARGV[0] eq "config" );


$0 =~ s/(.+)resque_//g;
my $opt = $0 ? $0 : 'default';

if ($opt eq 'failed') {
    if ($config) {
        print "graph_title Resque Failure rate\n";
        print "graph_category Resque\n";
        print "graph_info This graph shows resque jobs that failed\n";
        print "graph_args --lower-limit 0\n";
        print 'graph_vlabel fails/s\n';

        print "failed.label Failed per/s\n";
        print "failed.type COUNTER\n";
    }
    else {
        my $value = $r->get('resque:stat:failed') || 0;
        print "failed.value $value\n";
    }
}
elsif ($opt eq 'queues') {
    if ($config) {
        print "graph_title Resque queue rates\n";
        print "graph_category Resque\n";
        print "graph_vlabel queue rates/s\n";
        print "graph_info This graph monitors the in and out rate of the queues\n";
        print "graph_args --lower-limit 0\n";

        my @queues = $r->smembers( 'resque:queues' );
        for my $name (@queues) {
            $name =~ s/:/_/;

            print "${name}_pushed.label ${name}_pushed\n";
            print "${name}_pushed.type COUNTER\n";

            print "${name}_finished.label ${name}_finished\n";
            print "${name}_finished.type COUNTER\n";
        }
    }
    else {
        my @queues = $r->smembers( 'resque:queues' );
        for my $queue (@queues) {
            my $name = $queue;
            $name =~ s/:/_/;

            my $pushed = $r->get("resque:stat:${queue}:pushed") || 0;
            print "${name}_pushed.value ${pushed}\n";

            my $finished = $r->get("resque:stat:${queue}:finished") || 0;
            print "${name}_finished.value ${finished}\n";
        }
    }
}
elsif ($opt eq 'workers_count') {
    if ($config) {
        print "graph_title Resque Workers Count\n";
        print "graph_category Resque\n";
        print "graph_info This graph shows number of resque workers\n";
        print "graph_args --lower-limit 0\n";
        print "graph_vlabel workers\n";

        print "workers_count.label No. of workers\n";
        print "workers_count.type COUNTER\n";
    }
    else {
        my @workers = $r->smembers('resque:workers');
        print "workers_count.value " . (scalar @workers) . "\n";
    }
}
elsif ($opt eq 'workers_working') {
    if ($config) {
        print "graph_title Resque Workers in use\n";
        print "graph_category Resque\n";
        print "graph_info This graph shows the \%age of resque workers busy\n";
        print "graph_args --lower-limit 0 --upper-limit 100\n";
        print "graph_vlabel %\n";

        print "workers_working.label Workers Busy\n";
        print "workers_working.type GAUGE\n";
    }
    else {
        my @workers = $r->smembers('resque:workers');
        my $working = 0;
        for my $worker (@workers) {
            my $value = $r->get("worker:$worker") || 0;
            $working++ if $value;
        }
        my $value = scalar @workers;
        if ($value) {
            $value = $working * 100 / $value;
        }
        print "workers_working.value $value\n";
    }
}
