#!/usr/bin/perl -w
use Getopt::Std;
use Net::SNMP qw(:snmp);
#    ---------------------------------------------------------------------------
#    Riverbed Optimized vs. Pass-through connections
#    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 $optimized   = ".1.3.6.1.4.1.17163.1.1.5.2.1.0";
my $passthrough = ".1.3.6.1.4.1.17163.1.1.5.2.2.0";

#-------------------------------------------------------------------------------
#    Global variable declarations
#-------------------------------------------------------------------------------
my @str = ("OK", "WARNING", "CRITICAL", "UNKNOWN"); # Nagios status strings
my (
   $usage,              # Help message
   $hostname,           # Target host
   $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
   @oids,
   $pass,
   $opti
);


#-------------------------------------------------------------------------------
#    Global variable initializations
#-------------------------------------------------------------------------------
$usage = <<"EOF";
usage:  $0 [-h] -H <hostname> -C <community>

Version: $version
Released on: $release

Nagios plugin to monitor optimized and pass through connections using SNMP version 2c

!!! Useful for graphing purposes only !!!

[-h]                  : Print this message
[-H] <hostname>       : IP Address or Hostname
[-C] <community>      : SNMP Community String  (default = "public")
 
EOF

$state = 0; # Assume OK nagios status by default

#-------------------------------------------------------------------------------
#                              Input Phase
#-------------------------------------------------------------------------------
die $usage if (!getopts('hH:C:') || $opt_h);
die $usage if (!$opt_H || !$opt_C || $opt_h);
$hostname = $opt_H;
$community = $opt_C || "public"; undef $opt_C; #use twice to remove Perl warning

#-------------------------------------------------------------------------------
# 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 optimized and passthrough counters
#-------------------------------------------------------------------------------
@oids = ( $optimized,$passthrough );
$result = $session->get_request(
          -varbindlist  => \@oids,
          -callback     => [\&cb_get, {}],
        );
if (!defined($result)) {
  printf("ERROR: %s.\n", $session->error);
  $session->close;
  exit (-1);
}
snmp_dispatcher();
undef $result;

#-------------------------------------------------------------------------------
# Process results
#-------------------------------------------------------------------------------
$message = "Riverbed connections ($opti optimized; $pass passthrough): OK ";
$perf = "optimized=$opti passthrough=$pass";
print "$message | $perf\n";
exit $state;

#-------------------------------------------------------------------------------
# Sub routines
#-------------------------------------------------------------------------------
sub cb_get
{
  my ($session) = @_;
  my $result = $session->var_bind_list();
  if (!defined $result) {
     printf "ERROR: Get request failed for host '%s': %s.\n",
            $session->hostname(), $session->error();
     exit -1;
  }
  $opti = $result->{$oids[0]};
  $pass = $result->{$oids[1]};
}