#!/usr/bin/perl
#
# AUTHORS:
#	Copyright (C) 2008 Altinity Limited
#	Written by Neil Ferguson, kindly sponsored by GotVMail
#
#    This file is part of Opsview
#
#    Opsview 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 2 of the License, or
#    (at your option) any later version.
#
#    Opsview 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 Opsview; if not, write to the Free Software
#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#

use strict;
use lib qw ( /usr/local/nagios/perl/lib );
use Net::SNMP;
use Getopt::Std;

# About us
my $script         = "check_snmp_vmware_vmkernel";
my $script_version = "0.1";

my $hostname = '';

# SNMP variables
my $oid_sysDescr =
  ".1.3.6.1.2.1.1.1.0";    # Used to check whether SNMP is actually responding
my $oid_vmkernelstatus = ".1.3.6.1.4.1.6876.4.1.1.1.0";
my $community          = "public";                        # Default community
my $timeout            = 10;                              # SNMP timeout
my $retval  = 3;      # Innocent until proven guilty
my $version = "2c";

our ( $s, $e );

# Command line arguments
our ( $opt_h, $opt_H, $opt_c, $opt_t );
getopts("hH:c:t:");
if ($opt_h) {
  usage();
  exit 0;
}
if ($opt_H) {
  $hostname = $opt_H;
}
else {
  print "No hostname specified\n";
  usage();
  exit 3;
}
if ($opt_c) {
  $community = $opt_c;
}
if ($opt_t) {

  # Validity test - must be numeric
  unless ( $opt_t =~ /^[0-9]+$/ ) {
    print "Specify time in seconds - $opt_t is not a valid integer\n";
    exit 3;
  }
  $timeout = $opt_t;
}

sub usage {
  print <<EOF
--------------------------------------------------------------------
$script $script_version

Checks whether or not the ESX VM Kernel is loaded

Usage: $script -H <hostname> -c <community> [-t <timeout>]

Options: -H     Hostname or IP address
         -c     SNMP community string
         -t     SNMP timeout (in seconds)

--------------------------------------------------------------------     
Copyright 2008 Altinity Limited  
         
Plugin development was sponsored by GotVMail (http://www.gotvmail.com)

This program is free software; you can redistribute it or modify
it under the terms of the GNU General Public License
------------------------------------------------------------------
EOF

}

# Call this when you know you'll get a single value back
sub get_oid_value {
  our ( $oid, $result, $status, $returnstring );

  $oid = shift(@_);

  if ( !defined( $s->get_request($oid) ) ) {
    if ( !defined( $s->get_request($oid_sysDescr) ) ) {
      print "SNMP agent not responding\n";
      exit 3;
    }
    else {
      print "SNMP OID does not exist\n";
      exit 3;
    }
  }

  foreach ( $s->var_bind_names() ) {
    $result = $s->var_bind_list()->{$_};
  }

  return $result;
}

# Create the SNMP session
$version = "2c";
( $s, $e ) = Net::SNMP->session(
  -community => $community,
  -hostname  => $hostname,
  -version   => $version,
  -timeout   => $timeout,
);

if ( !defined( $s->get_request($oid_sysDescr) ) ) {

  # If we can't connect using SNMPv1 lets try as SNMPv2
  $s->close();
  sleep 0.5;
  $version = "1";
  ( $s, $e ) = Net::SNMP->session(
    -community => $community,
    -hostname  => $hostname,
    -version   => $version,
    -timeout   => $timeout,
  );
  if ( !defined( $s->get_request($oid_sysDescr) ) ) {
    print "Agent not responding, tried SNMP v1 and v2\n";
    exit 3;
  }
}

# Check for an SNMP error first...
if ( $s->error ) {
  print "UNKNOWN - " . $s->error . "|\n";
  exit 3;
}

# Check what the value is
if ( get_oid_value($oid_vmkernelstatus) eq "yes" ) {
  $retval = 0;
}
else {
  $retval = 2;
}

# Show appropriate message (we don't have a warning state)
if ( $retval == 0 ) {
  print "OK - VM Kernel is loaded | kernel_loaded=1\n";
}
elsif ( $retval == 2 ) {
  print "CRITICAL - VM Kernel is not loaded! | kernel_loaded=0\n";
}
else {
  print "UNKNOWN - plugin error\n";
}

exit $retval;
