#!/usr/bin/perl -w
#    ---------------------------------------------------------------------------
#    F5 probe for Wide IPs Copyright 2010 Lionel Cottin (cottin@free.fr)
#    ---------------------------------------------------------------------------
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#    ---------------------------------------------------------------------------
#
my $version = "0.1";
my $release = "2010/06/18";

#
# Interesting MIB entries
# -----------------------
my $gtmWideipName = ".1.3.6.1.4.1.3375.2.3.12.1.2.1.1";
my $gtmWideipStatusAvailState = ".1.3.6.1.4.1.3375.2.3.12.3.2.1.2";
my $gtmWideipStatusEnabledState = ".1.3.6.1.4.1.3375.2.3.12.3.2.1.3";
my $gtmWideipStatusDetailReason = ".1.3.6.1.4.1.3375.2.3.12.3.2.1.5";
my $gtmWideipStatRequests = ".1.3.6.1.4.1.3375.2.3.12.2.3.1.2";

use Getopt::Std;
use Net::SNMP qw(:snmp);

#-------------------------------------------------------------------------------
#    Global variable declarations
#-------------------------------------------------------------------------------

my @str = ("OK", "WARNING", "CRITICAL", "UNKNOWN"); # Nagios status strings
my (
   $usage,              # Help message
   $hostname,           # Target router
   $community,          # SNMP community (v2c only)
   $base_oid,           # Base OID
   $state,              # Nagios status: 3=unknown, 2=critical, 1=warning, 0=ok, -1=error
   $short,              # Message for $SERVICEOUTPUT$
   $result,             # Temp variable to store SNMP results
   $message,            # Final message to print out
   $ip_idx,             # Wide IP Index
   $ip_name,            # Wide IP name in ASCII form
   $ip_oid,             # Wide IP name in DECIMAL form
   $ip_state,           # Wide IP state
   $ip_status,          # Wide IP status
   $ip_enabled,         # Wide IP enabled ?
   %ip                  # Wide IP hash
                        #  $ip->{index}->{"name"}       # String
                        #  $ip->{index}->{"oid"}        # String
                        #  $ip->{index}->{"enabled"}    # Boolean
                        #  $ip->{index}->{"state"}      # Integer
                        #  $ip->{index}->{"conns"}      # Integer
);


#-------------------------------------------------------------------------------
#    Global variable initializations
#-------------------------------------------------------------------------------
$usage = <<"EOF";
usage:  $0 [-h] -H <hostname> -C <community> [-W <Wide IP Name>]

Version: $version
Released on: $release

Nagios check for F5 GTM Wide IPs using SNMP version 2c

[-h]                  : Print this message
[-H] <hostname>       : IP Address or Hostname
[-C] <community>      : SNMP Community String  (default = "public")
[-W] <wide ip>        : Wide IP name or "ANY" to check them all
[-d]                  : enable debug output
 
EOF

$state = 3; # status UNKNOWN by default

#-------------------------------------------------------------------------------
#                              Input Phase
#-------------------------------------------------------------------------------
die $usage if (!getopts('hH:C:W:d') || $opt_h);
die $usage if (!$opt_H || !$opt_C || !$opt_W || $opt_h);
$hostname = $opt_H;
$community = $opt_C || "public"; undef $opt_C; #use twice to remove Perl warning
$ip_name = $opt_W;
if($opt_d) {
  print "Target hostname  : $hostname\n";
  print "SNMPv2 community : $community\n";
  print "Wide IP          : $ip_name\n";
}

#-------------------------------------------------------------------------------
# Open an SNMPv2 session with the remote agent
#-------------------------------------------------------------------------------
my ($session, $error) = Net::SNMP->session(
        -version     => 'snmpv2c',
        -nonblocking => 1,
        -timeout     => 2,
        -hostname    => $hostname,
        -community   => $community
);

if (!defined($session)) {
  printf("ERROR: %s.\n", $error);
  exit (-1);
}

#-------------------------------------------------------------------------------
# Get Wide IP names
#-------------------------------------------------------------------------------
$base_oid = $gtmWideipName;
$result = $session->get_bulk_request(
        -callback       => [\&table_cb, {}],
        -maxrepetitions => 20,
        -varbindlist => [$base_oid]
);
if (!defined($result)) {
  printf("ERROR: %s.\n", $session->error);
  $session->close;
  exit (-1);
}
snmp_dispatcher();
undef $result;

#-------------------------------------------------------------------------------
# Get administrative state
#-------------------------------------------------------------------------------
$base_oid = $gtmWideipStatusEnabledState;
$result = $session->get_bulk_request(
        -callback       => [\&table_cb, {}],
        -maxrepetitions => 20,
        -varbindlist => [$base_oid]
);
if (!defined($result)) {
  printf("ERROR: %s.\n", $session->error);
  $session->close;
  exit (-1);
}
snmp_dispatcher();
undef $result;

#-------------------------------------------------------------------------------
# Get availability status
#-------------------------------------------------------------------------------
$base_oid = $gtmWideipStatusAvailState;
$result = $session->get_bulk_request(
        -callback       => [\&table_cb, {}],
        -maxrepetitions => 20,
        -varbindlist => [$base_oid]
);
if (!defined($result)) {
  printf("ERROR: %s.\n", $session->error);
  $session->close;
  exit (-1);
}
snmp_dispatcher();
undef $result;


#-------------------------------------------------------------------------------
# Get some stats on Wide IPs
#-------------------------------------------------------------------------------
$base_oid = $gtmWideipStatRequests;
$result = $session->get_bulk_request(
        -callback       => [\&table_cb, {}],
        -maxrepetitions => 20,
        -varbindlist => [$base_oid]
);
if (!defined($result)) {
  printf("ERROR: %s.\n", $session->error);
  $session->close;
  exit (-1);
}
snmp_dispatcher();
undef $result;

#-------------------------------------------------------------------------------
#                              Output Phase
#-------------------------------------------------------------------------------
my $conns = 0;
my ($w,$c,$o) = ("","","");
for my $i ( keys %ip ) {
  # Don't validate server name if ANY is given in cmd line
  if ( $ip_name eq "ANY" ) {
    if ( $ip{$i}{"enabled"} == 1 ) {
      $conns = $conns + $ip{$i}{"conns"};
      if ($ip{$i}{"state"} =~ m/(1)/ ) {
        $o = $o . " " . $ip{$i}{"name"};
      }
      if ($ip{$i}{"state"} =~ m/(0|2|3)/ ) {
        $c = $c . " " . $ip{$i}{"name"};
      }
      if ($ip{$i}{"state"} =~ m/(4|5)/ ) {
        $w = $w . " " . $ip{$i}{"name"};
      }
    }
  # Validate server name before assessing the status
  } elsif ( $ip{$i}{"name"} eq $ip_name ) {
    if ( $ip{$i}{"enabled"} == 1 ) {
      $conns = $conns + $ip{$i}{"conns"};
      if ($ip{$i}{"state"} =~ m/(1)/ ) {
        $o = $o . " " . $ip{$i}{"name"};
      }
      if ($ip{$i}{"state"} =~ m/(0|2|3)/ ) {
        $c = $c . " " . $ip{$i}{"name"};
      }
      if ($ip{$i}{"state"} =~ m/(4|5)/ ) {
        $w = $w . " " . $ip{$i}{"name"};
      }
    }
  }
}
$state = 0 if $o =~ m/.+/;
$state = 1 if $w =~ m/.+/;
$state = 2 if $c =~ m/.+/;
if ($state == 3) {
  $short = "$str[$state]: Wide IP status ($ip_name) could not be determined";
} elsif ($state == 2) {
  $short = "$str[$state]: Wide IPs ($c) are DOWN";  
} elsif ($state == 1) {
  $short = "$str[$state]: Wide IPs ($w) may be DOWN";
} else {
  $short = "$str[$state]: Wide IPs ($ip_name) are UP";
}
print "$short | requests=$conns";
print "c\n";
exit $state;

#-------------------------------------------------------------------------------
# Subroutine to handle the SNMP responses.
#-------------------------------------------------------------------------------
sub table_cb 
{
  my ($session, $table) = @_;
  if (!defined($session->var_bind_list)) {
    printf("ERROR: %s\n", $session->error);
  } else {
    #---------------------------------------------------------------
    # Loop through each of the OIDs in the response and assign
    # the key/value pairs to the anonymous hash that is passed
    # to the callback.  Make sure that we are still in the table
    # before assigning the key/values.
    #---------------------------------------------------------------
    my $next;
    foreach my $oid (oid_lex_sort(keys(%{$session->var_bind_list}))) {
      if (!oid_base_match($base_oid, $oid)) {
        $next = undef;
        last;   
      }       
      $next = $oid;
      $table->{$oid} = $session->var_bind_list->{$oid};
      }
    #---------------------------------------------------------------
    # If $next is defined we need to send another request
    # to get more of the table.
    #---------------------------------------------------------------
    if (defined($next)) {
      $result = $session->get_bulk_request(
              -callback       => [\&table_cb, $table],
              -maxrepetitions => 10,
              -varbindlist    => [$next]
              );
      if (!defined($result)) {
        printf("ERROR: %s\n", $session->error);
      }
    } else {
      #---------------------------------------------------------------
      # Processing results now
      #---------------------------------------------------------------
      foreach my $oid (oid_lex_sort(keys(%{$table}))) {
        # Process: gtmWideipName
        #-------------------------------------------------------------
        if ($oid =~ /^$gtmWideipName.(\d+).(.*)$/) {
          $ip_idx = $1;
          $ip_oid = $2;
          $ip{"$ip_idx"}{"name"} = $table->{$oid};
          $ip{"$ip_idx"}{"oid"}  = $ip_oid;
          print "Base OID: gtmWideipName\n\tIP IDX : $ip_idx\n" if $opt_d;
          print "\tIP OID : $ip{\"$ip_idx\"}{\"oid\"}\n" if $opt_d;
          print "\tIP NAME: $ip{\"$ip_idx\"}{\"name\"}\n" if $opt_d;
        }
        # Process: gtmWideipStatusEnabledState
        #-------------------------------------------------------------
        if ($oid =~ /^$gtmWideipStatusEnabledState.(\d+).(.*)$/) {
          #$ip_oid = join '.', unpack 'C*', $table->{$oid};
          $ip_idx = $1;
          $ip_oid = $2;
          $ip{"$ip_idx"}{"enabled"} = $table->{$oid};
          print "Base OID: gtmWideipStatusEnabledState\n\tIP IDX : $ip_idx\n" if $opt_d;
          print "\tIP OID : $ip{\"$ip_idx\"}{\"oid\"}\n" if $opt_d;
          print "\tENABLED: $ip{\"$ip_idx\"}{\"enabled\"}\n" if $opt_d;
        }
        # Process: gtmWideipStatusAvailState
        #-------------------------------------------------------------
        if ($oid =~ /^$gtmWideipStatusAvailState.(\d+).(.*)$/) {
          #$ip_oid = join '.', unpack 'C*', $table->{$oid};
          $ip_idx = $1;
          $ip_oid = $2;
          $ip{"$ip_idx"}{"state"} = $table->{$oid};
          print "Base OID: gtmWideipStatusAvailState\n\tIP IDX : $ip_idx\n" if $opt_d;
          print "\tIP OID : $ip{\"$ip_idx\"}{\"oid\"}\n" if $opt_d;
          print "\tSTATE  : $ip{\"$ip_idx\"}{\"state\"}\n" if $opt_d;
        }
        # Process: gtmWideipStatRequests
        #-------------------------------------------------------------
        if ($oid =~ /^$gtmWideipStatRequests.(\d+).(.*)$/) {
          #$ip_oid = join '.', unpack 'C*', $table->{$oid};
          $ip_idx = $1;
          $ip_oid = $2;
          $ip{"$ip_idx"}{"conns"} = $table->{$oid};
          print "Base OID: gtmWideipStatRequests\n\tIP IDX : $ip_idx\n" if $opt_d;
          print "\tIP OID : $ip{\"$ip_idx\"}{\"oid\"}\n" if $opt_d;
          print "\tCONNS  : $ip{\"$ip_idx\"}{\"conns\"}\n" if $opt_d;
        }
      }
    }
  }
} 