Check netapp snapmirror lag.pl

From Federal Burro of Information
Revision as of 02:36, 17 April 2012 by David (talk | contribs) (Created page with "<pre> #!/usr/bin/perl -w use strict; #use lib qw( /usr/local/nagios/libexec ); #use utils qw( %ERRORS $TIMEOUT &print_revision &support &usage ); use Net::SNMP; use Getopt::Lo...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search
#!/usr/bin/perl -w

use strict;
#use lib   qw( /usr/local/nagios/libexec );
#use utils qw( %ERRORS $TIMEOUT &print_revision &support &usage );
use Net::SNMP;
use Getopt::Long;
use Data::Dumper;
use Pod::Usage;
use POSIX;
use Switch;


my $timeout = 1;
my $debug   = 1;
my $verbose = 0;
my $man = 0;
my $help = 0;
my $comm = '';
my $host = '';
my $Src = ''; # Mirror Source - Not used at this time
my $Dst = ''; # Mirror Destination
my $warn = ''; # amount of time afterwhich to warn 1m 2h 3d 2w
my $crit = ''; # amount of time afterwhich to critical 1m 2h 3d 2w
my $warnt ;    # warn in seconds
my $critt ;    # critical in seconds

my $data ; # contails a two dimaentinoal hashref for mirrors and variables.
my $foundindex = '' ; # the index of the Destination we supplied in the command line.

my $snapmirrorStatusTable = ".1.3.6.1.4.1.789.1.9.20.1";
my $snapmirrorIndex       = ".1.3.6.1.4.1.789.1.9.20.1.1";
my $snapmirrorSrc         = ".1.3.6.1.4.1.789.1.9.20.1.2";
my $snapmirrorDst         = ".1.3.6.1.4.1.789.1.9.20.1.3";
my $snapmirrorStatus      = ".1.3.6.1.4.1.789.1.9.20.1.4";
my $snapmirrorState       = ".1.3.6.1.4.1.789.1.9.20.1.5";
my $snapmirrorLag         = ".1.3.6.1.4.1.789.1.9.20.1.6";

sub h2tick ($){
    my $string = shift;
    if ( $string =~ /(\d+)(\D+)/ ) {
        my $value = $1;
        my $units = $2;
        if ( $units eq "s" ) { return $value ; }
        if ( $units eq "m" ) { return $value * 60 ; }
        if ( $units eq "h" ) { return $value * 60 * 60 ; }
        if ( $units eq "d" ) { return $value * 60 * 60 * 24 ; }
        if ( $units eq "w" ) { return $value * 60 * 60 * 24 * 7 ; }
        die "Bad unit value arg $string : $value $units";
    } elsif ( $string =~ /(\d+)/ ) {
        return $string;
    }
}

print "2 hours "  .h2tick("2h")."\n";
print "2 days "   .h2tick("2d")."\n";
print "2 minutes ".h2tick("2m")."\n";
print "2 week "   .h2tick("2w")."\n";

my $result = GetOptions ( "comm=s"          => \$comm,
                          "H=s"             => \$host,
                          "Dst=s" => \$Dst,
                          "w=s"             => \$warn,
                          "c=s"             => \$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 ( $Src eq '' ) { die "Src not set\n"; }
if ( $Dst eq '' ) { die "Dst not set\n"; }
if ( $warn eq '' ) { die "warn not set\n"; }
if ( $crit eq '' ) { die "critical not set\n"; }

$critt = h2tick($crit);
print "Critt $critt\n";
$warnt = h2tick($warn);
print "Warn $warnt\n";

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

my ($session, $errstr) = Net::SNMP -> session(
    -hostname  => $host ,
    -community => $comm,
    -timeout   => $timeout,
    -version   => 2,
    -translate => [ -timeticks => 0x0 ]
);

die("Could not create SNMP session: $errstr\n") unless($session);

$session->translate(Net::SNMP->TRANSLATE_NONE);
$verbose && print "Session created ok, Fetching Destiantion list\n";

$result = $session->get_entries( -columns => [ $snapmirrorIndex, $snapmirrorSrc, $snapmirrorDst, $snapmirrorStatus, $snapmirrorState, $snapmirrorLag ],);

if ( not defined($result) ) {
    print "UNKNOWN: Failed to get snapmirrorStatusTable $snapmirrorStatusTable ".$session->error()."\n";
    exit(3); # UNKNOWN
}

foreach my $key (keys (%$result)) {
    if ( $key =~ /$snapmirrorStatusTable\.(\d+)\.(\d+)/ ) {
        my $var = $1;
        my $index  = $2;
        if ( ($var ne "" ) && ( $index ne "" )  ) {
            $data->{$index}->{$var} = $result->{$key};
            if ( $key =~ /$snapmirrorDst\.(\d+)/ ) {
                if ( $Dst eq $result->{$key} ) {
                    $foundindex = $1;
                }
            }
        } else {
            print "OH NO!!!!\n";
        }
    }
}

print Dumper($data);

print "The one we want\n";
print Dumper ( $data->{$foundindex});

#print "Found :".($data->{$foundindex}->{'6'}/100)."\n";
#print "Crit:  ".$crit. " ( ". h2tick($crit).")\n";
#print "Warn:  ".$warn. " ( ". h2tick($warn).")\n";

if ( $foundindex =~ /\d+/ ) {
    if ( $data->{$foundindex}->{'6'} > h2tick($crit) ) {
        # print "CRIT: ".$Dst." Lag too high ".($data->{$foundindex}->{'6'}/100)." Seconds|'lag'=".$data->{$foundindex}->{'6'}/100.":".h2tick($warn).":".h2tick($crit)."\n";
        print "CRIT: ".$Dst." Lag too high ".($data->{$foundindex}->{'6'}/100)."\n";
        exit(2); # CRITICAL
    }
    if ( $data->{$foundindex}->{'6'} > h2tick($warn) ) {
        # print "WARN: ".$Dst." Lag too high ".($data->{$foundindex}->{'6'}/100)." Seconds|'lag'=".$data->{$foundindex}->{'6'}/100.":".h2tick($warn).":".h2tick($crit)."\n";
        print "WARN: ".$Dst." Lag too high ".($data->{$foundindex}->{'6'}/100)." Seconds|'lag'=";
        exit(1); # WARN
    }
    #print "OK: ".$Dst." ". Lag is Ok ".($data->{$foundindex}->{'6'}/100)." Seconds|'lag'=".$data->{$foundindex}->{'6'}/100.":".h2tick($warn).":".h2tick($crit)."\n";
    print "OK: ".$Dst." Lag is Ok ".($data->{$foundindex}->{'6'}/100)." Seconds\n";
    exit(0); #OK
} else {
    print "CRIT: Could not find Dst $Dst\n";
    exit(2); # CRITICAL
}

__END__
=head1 NAME

check_snmp_ratio.pl

=head1 SYNOPSIS

check_snmp_ratio.pl -comm <string> -H <host> -Dst <.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<-Dst>

The Destination volume

=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