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

=head1 NAME


=head1 APPLICABLE SYSTEMS


=head1 CONFIGURATION

You have to setup ssh with public key authentication for this plugin
SNMP is only used for getting the hostname

   [snmp_$host_netapp_diskbusy]
    env.ssh /usr/bin/ssh (default)
    env.sshuser munin (default)
    env.sshopts -i /home/munin/.ssh/id_rsa -o UserKnownHostsFile=/home/munin/.ssh/known_hosts (no default)
    env.spares 2 (no default)

Number of spares is only used for total diskusage.

=head1 INTERPRETATION

This plugin only prints the disk busy status at check time. There is no
average calculated, but it still gives a goood overview if all disk are
used equally or you have got a single hot disk.

=head1 AUTHOR

2013, Claudius Herder

=head1 LICENSE

GPLv2.

=cut

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

my %disks;

sub do_collect
{
    my $input;
    my @tmp;
    my $ssh = $ENV{'ssh'} || '/usr/bin/ssh';
    my $sshuser = $ENV{'sshuser'} || $ENV{'USER'};
    my $sshopts = $ENV{'sshopts'} || "";
    my ($host, undef, undef, undef) = Munin::Plugin::SNMP->config_session();

    $input=`$ssh $sshopts $sshuser\@$host stats show disk:*:disk_busy`;
    foreach my $line (split(/\n/, $input))
    {
        @tmp = split(/:/, $line);
        ($disks{$tmp[2]} = $tmp[12]) =~ s/%//;
    }
}

sub do_config_root
{
    # graph_category netapp # To show plugin in Gallery also in this category
 
    my ($host) = @_;

    print "multigraph diskbusy\n";
    print "graph_title $host Aggregate busy status\n";
    print "graph_args --base 1000 --lower-limit 0 --rigid\n";
    print "graph_vlabel aggr busy status\n";
    print "graph_category disk\n";
    print "graph_info This graph shows the aggr busy status in percent for $host without spares\n";
    print "diskbusy.label DiskBusy\n";
    print "diskbusy.min 0\n";
    print "diskbusy.draw AREASTACK\n";
    print "diskbusy.type GAUGE\n";
}

sub do_config_disk
{
    my ($host,$disk) = @_;
    my $extrainfo = '';

    print "multigraph diskbusy.$disk\n";
    print "graph_title disk busy status for disk $disk\n";
    print "graph_args --base 1000 --lower-limit 0 --rigid\n";
    print "graph_vlabel disk busy status\n";
    print "graph_category disk\n";
    print "graph_info This graph shows disk busy status in percent for the $disk disk.$extrainfo\n";
    print "diskbusy.info This is the disk busy status in percent of $disk\n";
    print "diskbusy.type GAUGE\n";
    print "diskbusy.label DiskBusy\n";
    print "diskbusy.min 0\n";
    print "diskbusy.draw AREASTACK\n";
}

sub do_fetch_root
{
    my $spares=$ENV{'spares'} || 0;
    my $busy =0;
    my $numberofdisk=0;
    my $diskbusy=0;
    $numberofdisk =(keys %disks);
    foreach my $disk (keys %disks)
    {
        $busy += $disks{$disk};
    }
    $diskbusy=$busy/($numberofdisk-$spares);
    print "multigraph diskbusy\n";
    printf("diskbusy.value %.3f \n",$diskbusy);
}

sub do_fetch_disk
{
    my($disk) = @_;
    my $busy;
    $busy = $disks{$disk};
    print "multigraph diskbusy.$disk\n";
    print "diskbusy.value $busy\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 $disk (sort keys %disks)
    {
        do_config_disk($host,$disk);
    }
    do_config_root($host);
}

sub do_fetch
{
    foreach my $disk (sort  keys %disks)
    {
        do_fetch_disk($disk);
    }
    do_fetch_root();
}

do_collect();

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

do_fetch();

exit 0;

__END__
