Check snmp follow.pl/Check snmp follow.pl

From Federal Burro of Information
Revision as of 14:18, 22 September 2016 by David (talk | contribs) (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 $ma...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

#!/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 $string = ;  # the string to grep from oid1
my $oid1 = ;  # the list of value from which to grep $string
my $oid2 = ;  # we find an ID greep oid1, now grep oid2 for the id, looking for $r
my $r = ; # the value to find in the end to go green, active , up , good, that sort of thing
my $foundoid = ; # has the full oid where thestring was fond , need to get the id out of that.
my $id = ; # the index in the table of the thing we want to find.

my $result = GetOptions ( "comm=s"   => \$comm,
                          "H=s"      => \$host,
                          "string=s" => \$string,
                          "oid1=s"   => \$oid1,
                          "oid2=s"   => \$oid2,
                          "r=s"      => \$r,
                          '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 ( $string eq  ) { die "string not set\n"; }
if ( $oid1 eq  )   { die "oid1 not set\n"; }
if ( $oid2 eq  )   { die "oid2 not set\n"; }
if ( $result eq  ) { die "result not set\n"; }

if ( $verbose ) {
    print "Community string is $comm\n";
    print "Host is $host\n";
    print "string is $string\n";
    print "oid1 is $oid1\n";
    print "oid2 is $oid2\n";
    print "r is $result\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_entries( -columns => [ $oid1 ],);
    if ( $result ) {
        my $found =0;
        # print Dumper ( $result );
        #if ( ! defined $result->{$oid1} ) {
        #    print "UNKNOWN: Failed to get oid1 $oid1\n";
        #    exit(3); # UNKNOWN
        #}
        for (keys %$result) {
                if ( $result->{$_} eq $string ) {
                        $found++;
                        $foundoid = $_;
                }
        }

        if ( $found == 1 ) {
                $verbose && print "Found exacly one match $foundoid\n";
                if ( $foundoid =~ /$oid1\.(\d+)/ ) {
                        $id = $1;
                        $verbose && print "Id is $1\n";
                }
        } elsif ( $found > 1 ) {
                print "Found more than one match\n";
                exit 3;
        } else {
                print "Did not find string in $oid1 grep\n";
                exit 3;
        }

        my $result = $session->get_request( -varbindlist => [ $oid2.".".$id ],);
        if ( $result ) {
                if ( ! defined $result->{$oid2.".".$id} ) {
                        print "UNKNOWN: Failed to get oid1 $oid1\n";
                        exit(3); # UNKNOWN
                }
                $verbose && print "Found value ".$result->{$oid2.".".$id}."\n";
                if ( $result->{$oid2.".".$id} eq $r ) {
                        print "OK: $r\n";
                        exit 0;
                }
        }


        # print "OK: ".$percent."%|'percent'=$percent%:$warn:$crit\n";

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

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

__END__
=head1 NAME

check_snmp_ratio.pl

=head1 SYNOPSIS

check_snmp_follow.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<-grep>

The string to grep for first oid for.

=item B<-oid1>

The OID to grep for the string.

=item B<-oid2>

Once the id was found via the string above, go look for the value for that id in this OID.

=item B<-r>

The value that needs to be found to be green

=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 search for
=cut