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

#%# family=snmpauto
#%# capabilities=snmpconf

use strict;

use Munin::Plugin;
use Munin::Plugin::SNMP;

my $debug = $Munin::Plugin::SNMP::DEBUG;

sub print_debug{
    print("# Debug: @_\n");
}

sub snmp_get_request{
    if ($debug){ for my $oid (@_){ print_debug("request: <$oid>"); }}

    my $session = Munin::Plugin::SNMP->session();
    my $response = $session->get_request(-varbindlist => \@_);

    if (!defined $response){
        print("# Error: " . $session->error() . "\n");
        return {};
    }

    while($debug && (my ($oid, $value) = each %$response)){
        print_debug("response: <$oid> = <$value>");
    }

    return $response;
}

my $base_oid = "1.3.6.1.4.1.29690.2.3.1.3";

my $request = {
    "Bytes" => ".$base_oid.102.0"
};

if (defined $ARGV[0] and $ARGV[0] eq "snmpconf")
{
    for my $oid (values %$request){
        print "require $oid [0-9]+\n";
    }
    exit 0;
}

my ($host, $port, $version, $tail) = Munin::Plugin::SNMP->config_session();

print_debug("host=<$host:$port>, version=<$version>") if $debug;

if (!defined($host))
{
    die "# Error: Cannot understand what I'm supposed to monitor.";
}

if ($ARGV[0] and $ARGV[0] eq "config")
{
    print "host_name $host\n" unless $host eq 'localhost';

    print "graph_title drweb-filecheck: bytes scanned\n";
    print "graph_info This graph shows the speed of the files scanning\n";
    print "graph_args --base 1024 -l 0\n";
    print "graph_vlabel bytes scanned per / \${graph_period}\n";
    print "graph_category antivirus\n";

    print "Bytes.info Bytes in the scanned files\n";
    print "Bytes.label bytes scanned\n";
    print "Bytes.type DERIVE\n";
    print "Bytes.min 0\n";
    print "Bytes.draw AREA\n";
    print "Bytes.colour 00FF00\n";

    exit 0;
}

my $response = snmp_get_request(values %$request);

sub print_values{
    for my $name (@_){
        my $value = $response->{$request->{$name}};
        if (!(defined $value && $value =~ /[0-9]+/)){
            $value = 'U';
        }
        print "$name.value $value\n";
    }
}

print_values("Bytes");