#!/usr/bin/perl -wT
# check_netapp
# 
# Copyright (C) 2000  Leland E. Vandervort <leland@mmania.com>
#
# 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 2
# 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 (or with Nagios);  if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330, 
# Boston, MA 02111-1307, USA
####################################
# checks for overtemperature, fans, psu, and nfs operations/second on
# Network Appliance Filers.  
# Returns:  
#  OK if temp, fans, psu OK and Ops/Sec below warning and critical
#   Thresholds (default is warning=3500, critical=5000)  
#    ** Note:  See the specifications for your Filer model for 
#       the thresholds !
#  Returns Warning if NFS Ops/Sec is above warning threshold
#    (default 3500, or specified by -o command line option)
#  Returns Critical if NFS Ops/Sec is above critical threshold
#    ( -m option, or default 5000), or if overtem, psufault, or
#    fanfault detected.
#
####################################
#  Notes on operational limits for NetApp Filers:
#   Platform                     Maximum Ops/Second (recommended)
#   -------------------------------------------------------------
#   F230                         1000
#   F740                         5500
#   F760                         9000
####################################

require 5.004;
use strict;
use lib utils.pm ;
use Plugin;
use Plugin::Parameter qw(:DEFAULT :snmp);

use Net::SNMP;

use vars qw($opt_m $opt_o $opt_H $opt_C $opt_p $opt_t $PROGNAME);

use utils qw(%ERRORS);

$p_opt->default("snmp");
my $o_opt = new Plugin::Parameter(-name => "warningops", -flags => [ 'o', 'warnops' ],
				  -optional => "yes", -valueoptional => "no", -type => "INTEGER",
				  -default => 3500,
				  -description => "Operations per second warning threshold");
my $m_opt = new Plugin::Parameter(-name => "criticalops", -flags => [ 'm', 'critops' ],
				  -optional => "yes", -valueoptional => "no", -type =>"INTEGER",
				  -default => 5000,
				  -description => "Operations per second critical threshold");
my $plugin = new Plugin(-revision => '$Revision: 1.1.1.1 $',
			-copyright => "2000 Leland E. Vandervort, Howard Wilkinson <howard\@cohtech.com>",
			-shortcomment => "Perl NetApp filer plugin for Nagios",
			-longcomment => "Monitor Network Appliance Filers, for overtemperature, fans operation, PSU function and NFS operations/second",
			-parameterlists => [ [ $H_opt, $C_opt, $o_opt, $m_opt, $p_opt, $t_opt ], $h_opts, $V_opts ]);

my %OIDLIST = (overtemp 	=> 	'1.3.6.1.4.1.789.1.2.4.1.0',
	       failedfan	=>	'1.3.6.1.4.1.789.1.2.4.2.0',
	       failedpsu	=>	'1.3.6.1.4.1.789.1.2.4.4.0',
	       nfsops		=>	'1.3.6.1.4.1.789.1.2.2.1.0'
	      );



my $status_string = "";
my $state = "UNKNOWN";

$plugin->init();

my $tempcheck = &SNMPGET($OIDLIST{overtemp});
if($tempcheck == 1) {
  escalate_exitval('OK');
  $status_string .= "Temp OK ";
} else {
  esclate_exitval('CRITICAL');
  $status_string .= "Temp CRIT";
}

foreach my $element ('failedfan','failedpsu') {
  my $my_return = &SNMPGET($OIDLIST{$element});
  if(($my_return =~ /no/) || ($my_return == 0)) {
    $status_string .= "$element = $my_return ";
    escalate_exitval('OK');
  } else {
    $status_string .= "$element = $my_return ";
    escalate_exitval('CRITICAL');
  }
}

my $tmp_opssec = &get_nfsops();

if ($tmp_opssec >= $opt_m) {
  escalate_exitval('CRITICAL');
} elsif ($tmp_opssec >= $opt_o) {
  escalate_exitval('WARNING');
} else {
  escalate_exitval('OK');
}

$status_string .= "Ops\/Sec = $tmp_opssec ";

print "$PROGNAME $state $status_string\n";
exit($ERRORS{$state});

sub escalate_exitval {
  my ($newstatus) = @_;
  $state = $newstatus if ($ERRORS{$newstatus} > $ERRORS{$state});
}

sub get_nfsops {
  my $nfsops_start = &SNMPGET($OIDLIST{nfsops});
  sleep(1);
  my $nfsops_end = &SNMPGET($OIDLIST{nfsops});
  my $nfsopspersec = $nfsops_end - $nfsops_start;
  return($nfsopspersec);
}

sub SNMPGET {
  my $OID = shift;
  my ($session,$error) = Net::SNMP->session(-Hostname        =>      $opt_H,
					    -Community       =>      $opt_C,
					    -Port            =>      $opt_p,
					    -Timeout         =>      $opt_t
					   );
  if(!defined($session)) {
    printf("$PROGNAME $state: %s\n", $error);
    exit($ERRORS{$state});
  }
  my $response = $session->get_request($OID);
  if(!defined $response) {
    printf("$PROGNAME $state: %s\n", $session->error());
    $session->close();
    exit($ERRORS{$state});
  }
  $session->close();
  return($response->{$OID});
}

