#!/usr/bin/perl
#############################################################################
#                                                                           #
# This script was initially developed by Anstat Pty Ltd for internal use    #
# and has kindly been made available to the Open Source community for       #
# redistribution and further development under the terms of the             #
# GNU General Public License: http://www.gnu.org/licenses/gpl.html          #
#                                                                           #
#############################################################################
#                                                                           #
# Readapted to munin by Nicolai Langfeldt for Oslo Airport                  #
#                                                                           #
#############################################################################
#
# #%# family=auto
# #%# capabilities=autoconf

use strict;
use Getopt::Std;

my $printer;
my @printers;
my $status;
my @jobs;
my $n_jobs;
my @exclude;  # Should take this from environment.

# This is a dumb-down.  Should take hostname(s) from environment or
# as wildcard plugin.
my $host = '127.0.0.1';

$ENV{PATH}="/bin:/usr/bin";

if ($ARGV[0] eq 'autoconf') {
    if( ! open(LPSTAT_R, "lpstat $host -r|") ) {
        print "no (could not execute lpstat)\n";
        exit 1;
    }
    $_ = <LPSTAT_R>;
    if (! m/is running/mi) {
        print "no (scheduler is not running)\n";
        exit 1;
    }
    print "yes\n";
    exit 0;
}

####################################################
# Check printers are accepting jobs
####################################################
# Get list of printers, showing which are accepting jobs...
if( ! open(LPSTAT_A, "lpstat $host -a|") ) {
    print "graph_title Could not execute lpstat command\n";
    exit -1;
}

while(<LPSTAT_A>) {
    chomp;
    /(\S+) (.*) since/mi ;
    $printer = $1;
    $status = $2;
    if( grep /^$printer$/, @exclude ) {
	next;
    }
    if( /accepting/ ) {
	@printers = ( @printers, $printer );
    }
}
close(LPSTAT_A);

####################################################
# Check printers are enabled
####################################################
# Get list of printers, showing which are enabled/disabled...
if( ! open(LPSTAT_P, "lpstat $host -p|") ) {
    print "graph_title Could not execute lpstat command\n";
    exit -1;
}

my %jobs = ();

while(<LPSTAT_P>) {
    if ( /^printer\s+(\S+)\s.*disabled/mi ) {
        $printer=$1;
        if( grep /^$printer$/, @exclude ) {
            next;
        }
    }
}
close(LPSTAT_P);

# Get list of jobs for each printer...
foreach $printer ( @printers ) {
    if( grep /^$printer$/, @exclude ) {
        next;
    }

    if( ! open(LPSTAT, "lpstat $host -o $printer|") ) {
        print STDERR "Could not execute command: 'lpstat -o $printer' \n";
        exit 2;
    }
    @jobs = ( <LPSTAT> );
    $jobs{$printer}=$n_jobs || 0;
}

if ($ARGV[0] eq 'config') {
    print "graph_title Print queues
graph_args --base 1000
graph_vlabel Queued jobs
graph_category printing
";
    foreach my $printer (sort(keys %jobs)) {
        print "$printer.label $printer\n";
        print "$printer.type COUNTER\n";
    }
    exit 0;
}

foreach my $printer (sort(keys %jobs)) {
    print "$printer.value ",$jobs{$printer},"\n";
}
