Check snmp percent.pl: Difference between revisions

From Federal Burro of Information
Jump to navigationJump to search
(Created page with "<pre> #!/usr/bin/perl -w use strict; use Data::Dumper; use Net::SNMP; use Getopt::Long; use Pod::Usage; use POSIX; my $timeout = 1; my $debug = 1; my $verbose = 0; my $man =...")
 
No edit summary
 
Line 178: Line 178:
=cut
=cut
</pre>
</pre>
[[Category:SNMP]]
[[Category:Perl]]

Latest revision as of 16:15, 16 April 2012


#!/usr/bin/perl -w

use strict;
use Data::Dumper;
use Net::SNMP;
use Getopt::Long;
use Pod::Usage;
use POSIX;

my $timeout = 1;
my $debug   = 1;
my $verbose = 0;
my $man = 0;
my $help = 0;
my $comm = '';
my $host = '';
my $oid1 = ''; # numerator
my $oid2 = ''; # denominator
my $warn = '';
my $crit = '';
my $percent = '';

my $result = GetOptions ( "comm=s" => \$comm,
                          "H=s"    => \$host,
                          "oid1=s" => \$oid1,
                          "oid2=s" => \$oid2,
                          "w=i"    => \$warn,
                          "c=i"    => \$crit,
                          'help|?' => \$help,
                          man      => \$man,
                          'v'      => \$verbose) or pod2usage(2);
pod2usage(1) if $help;
pod2usage(-exitstatus => 0, -verbose => 2) if $man;

if ( $comm eq '' ) { die "community string not set\n"; }
if ( $host eq '' ) { die "host not set\n"; }
if ( $oid1 eq '' ) { die "oid1 not set\n"; }
if ( $oid2 eq '' ) { die "oid2 not set\n"; }
if ( $warn eq '' ) { die "warn not set\n"; }
if ( $crit eq '' ) { die "critical not set\n"; }

if ( $verbose ) {
    print "Community string is $comm\n";
    print "Host is $host\n";
    print "oid1 is $oid1\n";
    print "oid2 is $oid2\n";
    print "warn is $warn\n";
    print "Critical is $crit\n";
    print "Verbose is $verbose\n";
}

sub isip ( $ ) {
    my $string = shift;
    if ( $string =~ /(\d+)(\.\d+){3}/) {
        print "$string", ' matches  /(\d+)(\.\d+){3}/', "\n";
    } else {
        print "$string", ' does not matche  /(\d+)(\.\d+){3}/', "\n";
    }
    # if ($string !~ /([^.]+)\.([^.]+)\.([^.]+)\.([^.]+)/) {
    # a.b.c.d will be considered as legal ip address
    # without ^ and $ below -123.235.1.248 is a legal ip address
    if ($string !~ /^([\d]+)\.([\d]+)\.([\d]+)\.([\d]+)$/) {
        print "$string not an IP address\n";
        next;
    }
    my $notIP = 0;

    foreach my $s (($1, $2, $3, $4)) {
        # print "s=$s;";
        if (0 > $s || $s > 255) {
            $notIP = 1;
            last;
        }
   }
   if ($notIP) { return 0; }
   return 1;
}

my ($session, $error) = Net::SNMP -> session(
    -hostname  => $host ,
    -community => $comm,
    -timeout   => $timeout,
    -version   => 2
);
if ( $session ) {

    $verbose && print "Session created ok\n";

    my $result = $session->get_request( -varbindlist => [ $oid1, $oid2 ],);
    if ( $result ) {
        if ( ! defined $result->{$oid1} ) {
            print "UNKNOWN: Failed to get oid1 $oid1\n";
            exit(3); # UNKNOWN
        }
        if ( ! defined $result->{$oid2} ) {
            print "UNKNOWN: Failed to get oid2 $oid2\n";
            exit(3); # UNKNOWN
        }
        die "oid1 not found\n" unless ( $result->{$oid1} ) ;
        die "oid2 not found\n" unless ( $result->{$oid2} ) ;

        $percent = floor( $result->{$oid1} / $result->{$oid2} * 100 ) ;

        if ( $percent > $crit ) {
            print "CRIT: ".$percent."%|'percent'=".$percent."%:$warn:$crit\n";
            exit(2); # CRITICAL
        }
        if ( $percent > $warn ) {
            print "WARN: ".$percent."%|'percent'=".$percent."%:$warn:$crit\n";
            exit(1); # WARN
        }
        print "OK: ".$percent."%|'percent'=$percent%:$warn:$crit\n";

    } else {
        print "Failed to get oid1\n";
        print $session->error()."\n";
    }

} else {
    $verbose && print "failed to make snmp session for ".$host."\n";
    return 0;
}

__END__
=head1 NAME

check_snmp_ratio.pl

=head1 SYNOPSIS

check_snmp_ratio.pl -comm <string> -H <host> -oid1 <.1.1.1> -oid2 <.1.1.2> -w 80 -c 90

=head1 OPTIONS

=over 8

=item B<-comm>

The community string

=item B<-H>

The host to connect to.

=item B<-oid1>

The numerator ( the top bit )

=item B<-oid2>

The denominator ( the bottom bit )

=item B<-w>

The warn threshold, a percent

=item B<-c>

The critical threshold, a precent

=item B<-man>

Prints the manual page and exits.

=item B<-help>

I need somebody, not just anybody, help me , please.

=back

=head1 DESCRIPTION

B<This program> will read the given options , collect that data
and do something useful, I swear.

=cut