#!/usr/bin/perl -w
# -*- perl -*-
# vim: ft=perl

=head1 NAME

snmp__netapp_sis -  Munin plugin to retrieve sis operation status from NetApp storage appliances.

=head1 APPLICABLE SYSTEMS

sis 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 sis status (initialising, running, pending) per filer and per volume.
This could be helpful for sis schedule optimization.

=head1 MIB INFORMATION

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

=head1 MAGIC MARKERS

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

=head1 BUGS

This plugin wasn't tested on many hardware and only on Ontap 7.3.x.

=head1 AUTHOR

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

=head1 LICENSE

GPLv2.

=cut

use strict;
use warnings;
use Munin::Plugin;
use Munin::Plugin::SNMP;
need_multigraph();

if (defined $ARGV[0] and $ARGV[0] eq "snmpconf")
{
    print "require  1.3.6.1.4.1.789.1.23.2.1.1. [0-9]\n";
    exit 0;
}

my $volOIDBase = "1.3.6.1.4.1.789.1.23.2.1";

# Needed as globals
my $snmpinfo;

sub do_collect
{
    # Collect information from SNMP agent
    my $session = Munin::Plugin::SNMP->session();

    $snmpinfo = $session->get_hash
    (
        -baseoid => $volOIDBase,
        -cols =>
        {
            2 => 'sisPath',
            4 => 'sisStatus',
        }
    );
}

sub do_config_vol
{
    my ($host,$vol) = @_;

    if ( ! $vol )
    {
        print "multigraph sis_status\n";
        print "graph_title $host SIS status\n";
        print "graph_info This graph shows the sis status for $host\n";
    }
    else
    {
        $snmpinfo->{$vol}->{sisPath} =~ s/(\/vol\/)(.*?)/$2/;
        print "multigraph sis_status.$snmpinfo->{$vol}->{sisPath}\n";
        print "graph_title sis status for volume $snmpinfo->{$vol}->{sisPath}\n";
        print "graph_info This graph shows the sis status for the $snmpinfo->{$vol}->{sisPath} volume\n";
    }
    print "graph_args --base 1000 --lower-limit 0 --rigid\n";
    print "graph_vlabel sis status\n";
    print "graph_category netapp\n";
    print "graph_order sisInitialising sisRunning sisPending sisDebug\n";
    foreach my $state ("debug", "initialising", "running", "pending")
    {
        if ($vol)
        {
            print "$state.info This is the $state status of $snmpinfo->{$vol}->{sisPath}.\n";
        }
        print "$state.label $state\n";
        print "$state.min 0\n";
        print "$state.draw AREASTACK\n";
        print "$state.type GAUGE\n";
    }
}

sub do_fetch_root
{
    # Construct root graphs for the sis_status spaces
    my $sisSumIni = 0;
    my $sisSumRun = 0;
    my $sisSumPen = 0;
    my $sisDebug = 0;

    foreach my $vol (keys %{$snmpinfo})
    {
        my $status;
        $status = $snmpinfo->{$vol}->{sisStatus};

        if ($status == 1)
        {
        # sis idle
        }
        elsif ($status == 2 )
        {
            $sisSumIni++; #sis initialising
        }
        elsif ( $status == 3)
        {
            $sisSumRun++; #sis running
        }
        elsif ( $status == 5)
        {
            $sisSumPen++; #sis pending
        }
        else
        {
            $sisDebug++;
        }
    }
    print "multigraph sis_status\n";
    print "initialising.value $sisSumIni\n";
    print "running.value $sisSumRun\n";
    print "pending.value $sisSumPen\n";
    print "debug.value $sisDebug\n";
}

sub do_fetch_vol
{
    my($vol) = @_;
    my $status = $snmpinfo->{$vol}->{sisStatus};
    my $sisIni = 0;
    my $sisRun = 0;
    my $sisPen = 0;
    my $sisDebug = 0;

    if ($status == 1)
    {
        # sis idle
    }
    elsif ($status == 2)
    {
        $sisIni = 1;
    }
    elsif ($status == 3)
    {
        $sisRun = 1;
    }
    elsif ($status == 5)
    {
        $sisPen = 1;
    }
    else
    {
        $sisDebug = $status;
    }
    $snmpinfo->{$vol}->{sisPath} =~ s/(\/vol\/)(.*?)/$2/;
    print "multigraph sis_status.$snmpinfo->{$vol}->{sisPath}\n";
    print "initialising.value $sisIni\n";
    print "running.value $sisRun\n";
    print "pending.value $sisPen\n";
    print "debug.value $sisDebug\n";
}

sub do_config
{
    my ($host, undef, undef, undef) = Munin::Plugin::SNMP->config_session();

    print "host_name $host\n" unless $host eq 'localhost';
    foreach my $vol (sort {$a <=> $b} keys %{$snmpinfo})
    {
        do_config_vol($host,$vol);
    }
    do_config_vol($host);
}

sub do_fetch
{
    foreach my $vol (sort {$a <=> $b} keys %{$snmpinfo})
    {
        do_fetch_vol($vol);
    }
    do_fetch_root();
}

do_collect;

if ($ARGV[0] and $ARGV[0] eq "config")
{
    do_config();
    exit 0;
}

do_fetch();

exit 0;

__END__
