#!@@PERL@@
# -*- perl -*-

=head1 NAME

snmp__netapp_diskusage_ - Munin plugin to retrieve file systems usage on
NetApp storage appliances.

=head1 APPLICABLE SYSTEMS

File systems usage stats should be reported by any NetApp storage
appliance with SNMP agent daemon activated. See na_snmp(8) for details.

=head1 CONFIGURATION

Unfortunately, SNMPv3 is not fully supported on all NetApp equipments.
For this reason, this plugin will use SNMPv2 by default, which is
insecure because it doesn't encrypt the community string.

The following parameters will help you get this plugin working :

  [snmp_*]
     env.community MyCommunity

If your community name is 'public', you should really worry about
security and immediately reconfigure your appliance.

Please see 'perldoc Munin::Plugin::SNMP' for further configuration.

=head1 INTERPRETATION

The plugin reports file systems usage. This can help you monitoring file
systems usage in a given period of time.

=head1 MIB INFORMATION

This plugin requires support for the NETWORK-APPLIANCE-MIB issued by
Network Appliance. It reports the content of the DfEntry OID.

=head1 MAGIC MARKERS

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

=head1 VERSION

v1.0 - 06/22/2009 14:05:03 CEST Initial revision

=head1 AUTHOR

This plugin is copyright (c) 2009 by Guillaume Blairon.

NetApp is a registered trademark and Network Appliance is a trademark
of Network Appliance, Inc. in the U.S. and other countries.

=head1 BUGS

This plugin hasn't be tested on many hardware. If you encounter
bugs, please report any to Guillaume Blairon E<lt>L<g@yom.be>E<gt>.

=head1 LICENSE

GPLv2 or (at your option) any later version.

=cut

use strict;
use warnings;
use Munin::Plugin::SNMP;

my %oids = (

    # - dfHigh.* : 32 most significant bits counters
    # - dfLow.*  : 32 least significant bits counters

    dfHighTotalKBytes => '1.3.6.1.4.1.789.1.5.4.1.14.',
    dfLowTotalKBytes  => '1.3.6.1.4.1.789.1.5.4.1.15.',
    dfHighUsedKBytes  => '1.3.6.1.4.1.789.1.5.4.1.16.',
    dfLowUsedKBytes   => '1.3.6.1.4.1.789.1.5.4.1.17.',
    dfHighAvailKBytes => '1.3.6.1.4.1.789.1.5.4.1.18.',
    dfLowAvailKBytes  => '1.3.6.1.4.1.789.1.5.4.1.19.',

);

sub to_32bit_int {
    my ($l, $h) = @_;
    return "U" if ((!defined $l) || (!defined $h));
    my $bin = unpack( 'B32', pack('N', $l) . pack('N', $h) );
    return unpack( 'N', pack('B32', $bin) );
}

if (defined $ARGV[0] and $ARGV[0] eq 'snmpconf') {
    print "index 1.3.6.1.4.1.789.1.5.4.1.1.\n";
    foreach (keys %oids) {
        print "require $oids{$_} [0-9]\n";
    }
    exit 0;
}

my $session = Munin::Plugin::SNMP->session();
my ($host, undef, undef, $tail) = Munin::Plugin::SNMP->config_session();
my ($df_id, $name_oid);

if ($tail =~ /^netapp_diskusage_(\d)*$/) {
    $df_id    = $1;
    $name_oid = '1.3.6.1.4.1.789.1.5.4.1.2.' . $df_id;
} else {
    die "Couldn't understand what I'm supposed to monitor";
}

if (defined $ARGV[0] and $ARGV[0] eq "config") {
    my $df_name = $session->get_single($name_oid);

    print "host_name $host\n" unless $host eq 'localhost';
    print "graph_title $host disk usage on $df_name\n";
    print "graph_args --base 1024 --lower-limit 0\n";
    print "graph_vlabel bytes\n";
    print "graph_category disk\n";
    print "graph_info This graph shows the disk usage for $df_name on NetApp host $host\n";
    print "graph_order used avail total\n";
    print "used.info The total disk space in KBytes that is in use on the $df_name file system.\n";
    print "used.type GAUGE\n";
    print "used.draw AREA\n";
    print "used.label Used\n";
    print "used.cdef used,1024,*\n";
    print "used.min 0\n";
    print "avail.info The total disk space in KBytes that is free for use on the $df_name file system.\n";
    print "avail.type GAUGE\n";
    print "avail.draw STACK\n";
    print "avail.label Available\n";
    print "avail.cdef avail,1024,*\n";
    print "avail.min 0\n";
    print "total.info The total capacity in KBytes for the $df_name file system.\n";
    print "total.type GAUGE\n";
    print "total.draw LINE2\n";
    print "total.label Total\n";
    print "total.cdef total,1024,*\n";
    print "total.min 0\n";

    exit 0;
}

my $used_l  = $session->get_single($oids{dfLowUsedKBytes}.$df_id);
my $used_h  = $session->get_single($oids{dfHighUsedKBytes}.$df_id);
my $avail_l = $session->get_single($oids{dfLowAvailKBytes}.$df_id);
my $avail_h = $session->get_single($oids{dfHighAvailKBytes}.$df_id);
my $total_l = $session->get_single($oids{dfLowTotalKBytes}.$df_id);
my $total_h = $session->get_single($oids{dfHighTotalKBytes}.$df_id);

my $used  = to_32bit_int($used_l, $used_h);
my $avail = to_32bit_int($avail_l, $avail_h);
my $total = to_32bit_int($total_l, $total_h);

print "used.value $used\n";
print "avail.value $avail\n";
print "total.value $total\n";

exit 0;

__END__
