magikman 0 Light Poster

I have the script below that hits devices with SNMP to pull back values on interface errors, load, etc. I would like to modify this script to pull free disk space. I am stumped on how to pull the data back and then calculate free space on each partition. If you could point in the right direction, I would appreciate it. Thanks!

#!/usr/bin/perl -w




#use strict;
use Getopt::Long qw(:config no_ignore_case_always);
use Net::SNMP;
use Data::Dumper;
use Switch;
use strict;


#define vars for strict
my $host_address;
my $crit;
my $warn;
my $port;
my $type;
my %oids;
my %errors;
my $errors;
my $opt_h;
my %up_interfaces;

#snmp stuff
my $snmpuser = 'snmpuser';
my $snmpauthkey = 'authkey';
my $snmpprivkey = 'privkey';
my $snmpprivproto = 'DES';
my $snmpauthproto = 'MD5';

my %STATUS_CODE = ( 'OK' => '0', 'WARNING' => '1', 'CRITICAL' => '2', 'UNKNOWN' => '3' );

my $options = GetOptions ("h|help" => \$opt_h, "H|hostname=s"  => \$host_address, "w=i" => \$warn, "c=i" => \$crit, "p=i" => \$port, "t=s" => \$type);

if ( $options == 0 || $opt_h || !$host_address || !$type) {
   print_usage();
   exit $STATUS_CODE{'UNKNOWN'};
}

if (!$port) {
        $port = 161;
}

#sanitize
$host_address =~ s/[^A-Za-z0-9\.]//;

#print $host_address;

#my @oids = ( '1.3.6.1.4.1.8962.2.1.3.1.1.1.1.2','1.3.6.1.4.1.8962.2.1.3.1.1.1.1.1.2','1.3.6.1.4.1.8962.2.1.3.1.1.1.1.3.2','1.3.6.1.4.1.8962.2.1.2.1.10.7.1.1.2');
#my @oids = ( '1.3.6.1.4.1.8962.2.1.2.1.7.1.0', '1.3.6.1.4.1.8962.2.1.2.1.7.2.0' , '1.3.6.1.4.1.8962.2.1.2.1.7.3.0', '1.3.6.1.4.1.8962.2.1.2.1.7.4.0', '1.3.6.1.4.1.8962.2.1.2.1.1.13.0');

#MaxTCBs 1.3.6.1.4.1.8962.2.1.3.1.1.1.1.1.2

my %tcpflows = qw(
        TotalactiveTCPflows 1.3.6.1.2.1.6.9
        TotalactiveUDPflows 1.3.6.1.4.1.8962.2.1.3.1.1.1.1.3.2
        TotalTCPflowsintimewait 1.3.6.1.4.1.8962.2.1.3.1.1.1.1.6.2
        TotalflowsinSYNstate 1.3.6.1.4.1.8962.2.1.3.1.1.1.1.7.2
);
# TotalfreeTCBs 1.3.6.1.4.1.8962.2.1.3.1.1.1.1.9.2
# TotalTCPflowscreated 1.3.6.1.4.1.8962.2.1.3.1.1.1.1.4.2
# Totaltimedoutflows 1.3.6.1.4.1.8962.2.1.3.1.1.1.1.5.2
# TotalinactiveTCPflows 1.3.6.1.4.1.8962.2.1.3.1.1.1.1.8.2

my %tcpflows_errors = qw(
        TotalactiveTCPflows =0
        TotalactiveUDPflows =0
);

my %sensorhealth = qw(
      SensorTemperature 1.3.6.1.4.1.8962.2.1.2.1.7.1.0
      SensorFan 1.3.6.1.4.1.8962.2.1.2.1.7.2.0
      SensorPrimaryPowerSupply 1.3.6.1.4.1.8962.2.1.2.1.7.3.0
      SensorSecondaryPowerSupply 1.3.6.1.4.1.8962.2.1.2.1.7.4.0
      SensorHealth 1.3.6.1.4.1.8962.2.1.2.1.1.13.0
);
my %sensorhealth_errors = qw(
        SensorHealth =0
);

my %sensorload = qw(
        OneMinLdAvg 1.3.6.1.4.1.2021.10.1.5.1
        FiveMinLdAvg 1.3.6.1.4.1.2021.10.1.5.2
        FfTnMinLdAvg 1.3.6.1.4.1.2021.10.1.5.3
);
#Probably will want to tweak these...
my %sensorload_errors = qw(
        OneMinLdAvg >0.10
        FiveMinLdAvg >0.10
        FfTnMinLdAvg >0.10
);

my %diskusage = qw(
        RootSize 1.3.6.1.2.1.25.2.3.1.5.31
        RootUsed 1.3.6.1.2.1.25.2.3.1.6.31
        BootSize 1.3.6.1.2.1.25.2.3.1.5.32
        BootUsed 1.3.6.1.2.1.25.2.3.1.6.32
        VolSize 1.3.6.1.2.1.25.2.3.1.5.33
        VolUsed 1.3.6.1.2.1.25.2.3.1.6.33
        VarSize 1.3.6.1.2.1.25.2.3.1.5.34
        VarUsed 1.3.6.1.2.1.25.2.3.1.6.34
        SFSize 1.3.6.1.2.1.25.2.3.1.5.35
        SFUsed 1.3.6.1.2.1.25.2.3.1.6.35
        SQLSize 1.3.6.1.2.1.25.2.3.1.5.36
        SQLUsed 1.3.6.1.2.1.25.2.3.1.6.36
);

my %disk_errors = qw(
        SpaceFree >90

);

#2 is down, 1 is up for interfaces
my %interfaces = qw(
        eth0 1.3.6.1.2.1.2.2.1.8.8
        eth1 1.3.6.1.2.1.2.2.1.8.9
        eth2 1.3.6.1.2.1.2.2.1.8.6
        eth3 1.3.6.1.2.1.2.2.1.8.7
        eth4 1.3.6.1.2.1.2.2.1.8.4
        eth5 1.3.6.1.2.1.2.2.1.8.5
);

my %interfaces_oids = qw(
        OERR 1.3.6.1.2.1.2.2.1.20.
        IERR 1.3.6.1.2.1.2.2.1.14.
        BytesR 1.3.6.1.2.1.2.2.1.10.
        BytesS 1.3.6.1.2.1.2.2.1.16.
);

#This is here for reference, I'm going to shortcut this when i loop the active Ints further below
my %interfaces_errors = qw(
        OERR >0
        IERR >0
);


#Nothing really to edit/tweak past this point.

switch($type) {
        case('interfaces') { %oids = %interfaces; %errors = %interfaces_errors; }
        case('flows') { %oids = %tcpflows; ; %errors = %tcpflows_errors;}
        case('health') { %oids = %sensorhealth; ; %errors = %sensorhealth_errors;}
        case('space') { %oids = %diskusage; ; %errors = %disk_errors;}
        case('load') { %oids = %sensorload; ; %errors = %sensorload_errors;}
        else    { print_usage(); exit $STATUS_CODE{'UNKNOWN'}; }

}


my ($snmpsession, $snmperror) = Net::SNMP->session(
        -hostname => $host_address,
        -version => 'snmpv3',
        -port => $port,
        -username => $snmpuser,
        -timeout => 20,
        -retries => 3,
        -authprotocol => $snmpauthproto,
        -authpassword => $snmpauthkey,
        -privprotocol => $snmpprivproto,
        -privpassword => $snmpprivkey
);

if (!defined $snmpsession) {
      printf "ERROR: %s.\n", $snmperror;
      exit $STATUS_CODE{'CRITICAL'};;
}

my $result = $snmpsession->get_request(values %oids);

my @partitions = Dumper($result);




#print Dumper($result);
#print Dumper(%oids);

if (!defined $result) {
      printf "ERROR: %s.\n", $snmpsession->error();
      $snmpsession->close();
      exit $STATUS_CODE{'CRITICAL'};
}

#snmp_dispatcher();

switch($type) {
        case('interfaces') {

                #Grab all the up interfaces
                while (my ($key,$value) = each(%oids)) {
                        #print "--- $value --- ";
                        #print "$result->{$value}\n";
                        if ($result->{$value} == 1) {
                                my @temp = $interfaces{$key} =~ /\.(\d+)$/;
                                #print "$temp[0]\n";
                                my $oid_tail = $temp[0];
                                #sort(%interfaces_oids);
                                while (my ($name,$partialoid) = each(%interfaces_oids)) {
                                #       print "\t$name\n";
                                        my $index = $key . '_' . $name;
                                        #print "$index\n";
                                        my $complete_oid = $partialoid . $oid_tail;
                                        #Now jam into hash
                                        $up_interfaces{$index} = $complete_oid;
                                        #sort(%up_interfaces);
                                }
                        }
                }
        %oids = %up_interfaces;
        #print Dumper(%up_interfaces);
        undef %up_interfaces;
        undef %errors;
        #regen the error hash
        foreach (keys %oids) {
                if (/CRC/) { $errors{$_} = '>40'; }
        }
        $result = $snmpsession->get_request(values %oids);
#print Dumper($result);
        }
}



$snmpsession->close();

#Loop and compare to the error hash
my $critical = 0;
while (my ($name,$test) = each(%errors)) {
        #print $result->{$oids{$name}};
        my $value = $result->{$oids{$name}};
        my $operator = substr $test, 0, 1 ;
        my $compare = substr $test, 1;
        $operator = '==' if $operator eq '=';
        my $toeval = "$value $operator $compare";
        if (eval($toeval)) {
        #       print "$name in error!";
                $critical = 1;
        }
}

#Nagios Output
if ($critical) {
        if ($type eq 'interfaces') {
                print "CRITICAL - Please check CRCs (show intfport int-num) and run clrstat if errors are not increasing";
        } elsif ($type eq 'flows') {
                print "CRITICAL - Check traffic is flowing through sensor";
        } else {
                print "CRITICAL";
        }
} else {
        print "OK\n";
}

#Start Extended Service Info here
print "|";
while (my ($key,$value) = each(%oids)) {
        if($type eq 'load') { print "$key=" . ($result->{$value}/100) . ";\n"; }
        else { print "$key=" . $result->{$value} . ";\n"; }
}
print "\n";
if ($critical) {
         exit $STATUS_CODE{'CRITICAL'};
} else {
         exit $STATUS_CODE{'OK'};
}

sub print_usage {
   print "Usage: check_snmp.pl -H host -t check_type\n\n";
   print "Options:\n";
   print " -H --host STRING or IPADDRESS\n";
   print " -w INTEGER\n";
   print "   warning threshold \n";
   print " -c INTEGER\n";
   print "   critical threshold \n";
   print " -l STRING\n";
   print "   Login name for SSH \n";
   print " -p NUMBER\n";
   print "   port number \n";
   print " -t STRING\n";
   print "   Check type: Valid values - interfaces, flows, space, load\n";

   exit( $STATUS_CODE{"UNKNOWN"} );
}