#!/usr/bin/perl -w

# nagios: +epn

=pod

=head1 NAME

check_tp_fs.pl - Check Tipping Point filesystems

=head1 DESCRIPTION

This script will check filesystems on a Tipping Point device.  

=cut

sub check_tp_fs{

    use strict;

    use FindBin;
    use lib "$FindBin::Bin/lib";

    use Nagios::Plugin::SNMP;
    my $total=0;
    my $used=0;
    my $warn;
	my $crit;
    my $LABEL = 'TPT FS';

    my $plugin = Nagios::Plugin::SNMP->new(
        'shortname' => $LABEL,
        'usage' => 'USAGE: %s'
    );

    $plugin->getopts;

    my $DEBUG = $plugin->opts->get('snmp-debug');

    #  Return from current state will be one of these:
    my @states = qw(ok low-warning warning high-warning
                    low-critical critical high-critical);

    my %oids = qw(
	resourceFSInUseMB.1	.1.3.6.1.4.1.10734.3.3.2.5.2.1.1.1
	resourceFSInUseMB.2     .1.3.6.1.4.1.10734.3.3.2.5.2.1.1.2
	resourceFSInUseMB.3     .1.3.6.1.4.1.10734.3.3.2.5.2.1.1.3
	resourceFSInUseMB.4     .1.3.6.1.4.1.10734.3.3.2.5.2.1.1.4
	resourcewarning		.1.3.6.1.4.1.10734.3.3.2.5.2.1.2.1
	resourcecrit		.1.3.6.1.4.1.10734.3.3.2.5.2.1.3.1
	resourceFSRangeMax.1    .1.3.6.1.4.1.10734.3.3.2.5.2.1.5.1
	resourceFSRangeMax.2    .1.3.6.1.4.1.10734.3.3.2.5.2.1.5.2
	resourceFSRangeMax.3    .1.3.6.1.4.1.10734.3.3.2.5.2.1.5.3
	resourceFSRangeMax.4    .1.3.6.1.4.1.10734.3.3.2.5.2.1.5.4	
    );

    #  Get the mem values
    my $result = $plugin->get(values %oids);

    #  Close and destroy session
    $plugin->close();

    my @perf_data;

    my %fs;

    $fs{'boot'} = $result->{$oids{'resourceFSInUseMB.1'}};
    $fs{'opt'} = $result->{$oids{'resourceFSInUseMB.2'}};
    $fs{'usr'} = $result->{$oids{'resourceFSInUseMB.3'}};
    $fs{'log'} = $result->{$oids{'resourceFSInUseMB.4'}};

    $fs{'warn'} = $result->{$oids{'resourcewarning'}};
    $fs{'crit'} = $result->{$oids{'resourcecrit'}};

    $fs{'bootsize'}=$result->{$oids{'resourceFSRangeMax.1'}};
    $fs{'optsize'}=$result->{$oids{'resourceFSRangeMax.2'}};
    $fs{'usrsize'}=$result->{$oids{'resourceFSRangeMax.3'}};
    $fs{'logsize'}=$result->{$oids{'resourceFSRangeMax.4'}};
    my $level =OK;
    my $output = "$LABEL ";
    

    my $boot=$fs{'boot'}/$fs{'bootsize'}*100;
	my $boot2=sprintf("%.2f", $boot);
    my $opt=$fs{'opt'}/$fs{'optsize'}*100;
	my $opt2=sprintf("%.2f", $opt);
    my $usr=$fs{'usr'}/$fs{'usrsize'}*100;
	my $usr2=sprintf("%.2f", $usr);
    my $log=$fs{'log'}/$fs{'logsize'}*100;
	my $log2=sprintf("%.2f", $log);


    if(($boot<$fs{'warn'}) || ($opt<$fs{'warn'}) || ($usr<$fs{'warn'}) || ($boot<$fs{'warn'})) { 
	$output .= "OK - Filesystems in OK state";
	} elsif( ($boot>=$fs{'crit'}) || ($opt>=$fs{'crit'}) || ($usr>=$fs{'crit'}) || ($boot>=$fs{'crit'})) {
	$level=CRITICAL;
	$output.="CRITICAL  - FilesystemS in Critical State";
	} else {
	$level=WARNING;
        $output.="WARNING  - FilesystemS in WARNING State";
       }

	my $total2=sprintf("%.2f", $total);

    print "$output. Filesystems \/boot $boot2%, \/opt $opt2%, \/usr $usr2%, \/log $log2%\n",
          "|'Percentage_used_boot'=$boot2%;$fs{'warn'};$fs{'crit'};0;100",
	   "'Percentage_used_opt'=$opt2%;$fs{'warn'};$fs{'crit'};0;100",	  
           "'Percentage_used_usr'=$usr2%;$fs{'warn'};$fs{'crit'};0;100",
	   "'Percentage_used_log'=$log2%;$fs{'warn'};$fs{'crit'};0;100\n";

    return $level;

    sub debug {

        #return unless $DEBUG == 1;

        my $msg = shift;

        print STDERR scalar(localtime()) . ": $msg\n";

    }

}

exit check_tp_fs();

