JWK - Jackall's IT Wiki

Sécurité, Système, Réseau

Outils pour utilisateurs

Outils du site


check_snmp_boundaries

Table des matières

Check SNMP Boundaries

Nagios Plugin that checks that a snmp-obtained value stands between two specified boundaries.

Currently working with SNMP V1 and v2c.

I use this plugin to check a lot a different things like :

  • Monitor Number of up tunnel on firewall
  • Compare the current value of an OID to a fixed value
  • Verify that a ressource does not fall above or below boundaries

<note tip>This plugins is published on NagiosExchange </note>

Example

#perl check_value_between.pl -v 2c -H 192.168.100.251 -C Int  -o .1.3.6.1.4.1.9.9.109.1.1.1.1.6.1 --state=critical --min 1 --max 14
 Test OK - 1 <=  12 <= 14 
# echo $?
0

# perl check_value_between.pl -v 2c -H 192.168.100.251 -C Int  -o .1.3.6.1.4.1.9.9.109.1.1.1.1.6.1 --state=critical --min 1 --max 1
 Test NOK - 11 out of boundaries
# echo $?
2

Code

#!/usr/bin/perl -w
 
 
#
#@name check_snmp__boundaries.pl
#@Author : Damien BOLUS - jackall
#@Date 26/07/2011
#@Depends: snmpget, Nagios utils, getopt::Long
#@Description : Nagios plugin that checks that a snmp-obtained value stands between two specified boundaries
#
 
 
 
use strict;
use Getopt::Long;
 
 
#NAGIOS specific
 
use lib "/usr/local/nagios/libexec";
use utils qw(%ERRORS $TIMEOUT);
 
#PATH and binaries
my $snmpget=`which snmpget`;
chomp($snmpget);
 
#Variables
my $hostaddress=undef;
my $oid=undef;
my $community=undef;
my $snmp_version=undef;
my $low_boundary=undef;
my $high_boundary=undef;
my $help=undef;
my $out_of_bound_return_state=$ERRORS{'CRITICAL'};
 
my $snmp_check_raw_value=undef;
my $output_value=undef;
my $output_text=undef;
 
## options handling
 
sub check_options {
	my $exit=undef;
    Getopt::Long::Configure ("bundling");
    GetOptions(
		'H|hostname=s'	=> \$hostaddress,
		'o=s'			=> \$oid,
		'C|community=s'			=> \$community,
		'v|version=s'		=> \$snmp_version,
		'min=f'			=> \$low_boundary,
		'max=f'			=> \$high_boundary,
		'h|help'		=> \$help,
		'state=s'		=> \$out_of_bound_return_state,
	);
 
	##Options check
 
	if (defined ($help) ) {
		print_help();
		exit $ERRORS{'OK'};
	}
 
	#Check required argument presence
	unless(defined($low_boundary) and defined($high_boundary) and defined ($community) and defined($hostaddress) and defined($oid) and defined($snmp_version)   ){
		$exit="Check required argument";
		do_exit($exit,$ERRORS{'CRITICAL'});
 
	}
	## boundaries checks
	if(defined($low_boundary) and defined ($high_boundary) and $low_boundary=~/[+-]*[[:digit:]]+(\.[[:digit:]]*)?/ and $high_boundary=~/[+-]*[[:digit:]]+(\.[[:digit:]]*)?/) {
			if ($low_boundary > $high_boundary) { 
 
				print_usage();
				$exit="Error: min > max!!!!\n";
				do_exit($exit,$ERRORS{'UNKNOWN'});
			}
 
	}
	else {
 
		$exit=" Check boundary (min , max)\n";
		do_exit($exit,$ERRORS{'UNKNOWN'});
 
	}
 
	## return status
	if ( defined ($out_of_bound_return_state) ){
 
		if ($out_of_bound_return_state=~/^warning$/i){ 
			$out_of_bound_return_state=$ERRORS{'WARNING'};
		}
		elsif($out_of_bound_return_state=~/^unknown$/i){ 
			$out_of_bound_return_state=$ERRORS{'UNKNOWN'};
		}
		else { 
			$out_of_bound_return_state=$ERRORS{'CRITICAL'};
		}
 
 
	}
}
 
## SNMP check
 
sub do_check() {
	#DBG print "$snmpget  -On -v $snmp_version -c $community $hostaddress $oid \n" ;
	$snmp_check_raw_value=`$snmpget  -On -v $snmp_version -c $community $hostaddress $oid` ;
	if ($snmp_check_raw_value=~/^$oid[[:space:]]+=[[:space:]]+[[:alnum:]]+:[[:space:]]+([[:digit:]]+)$/) {
		#DBG print "$1\n";
		do_compare($1);
	}
	else {
		my $exit="Could not find value\n";
	 	do_exit($exit,$ERRORS{'UNKNOWN'});
	}
 
 
 
 
}#do check
 
 
sub do_compare(){
	my $value = shift( @_);
	my $exit=undef;
	#DBG print "[do_compare] $value\n low $low_boundary \n high $high_boundary\n ";
 
	if ( ($low_boundary <= $value) and ($value <= $high_boundary) ) {
 
		$exit=" Test OK - $low_boundary <=  $1 <= $high_boundary \n";
		do_exit($exit,$ERRORS{'OK'});
	}
	else { 
 
		$exit=" Test NOK - $1 out of boundaries";
		do_exit($exit,$out_of_bound_return_state);
	}
}#do_compare
 
sub do_exit(){
	## ARGS : output text , output value
	$output_text = shift(@_);
	$output_value = shift (@_);
 
	print "$output_text\n"; 
	exit $output_value;
}#do_exit
 
 
#USAGE
sub print_usage() { 
	print "Usage : $0  [-h] -v <snmp version> -H <hostaddress> -C <snmpcommunity>\n\t\\-o <oid> --state <error return state> --min <low boundary> --max <high boundary> \n ";  
 
}#USAGE
 
 
sub print_help() {
 
   print_usage();
   print <<EOT;
This plugin checks an SNMP-obtained value  against two boundaries 
Return OK if :
	min <= SNMP-obtained value <= max
 
-h, --help
   print this help message
-H, --hostname=HOST
   name or IP address of host to check
-C, --community=COMMUNITY NAME
   community name for the host's SNMP agent
-v, --version=1|2c 
   SNMP community strings. Only version 1 and 2c are currently supported
-o 
   SNMP OID
--min
   low boundary 
--max
   high boundary
--state=warning|unknown|critical - case insensitive  (default critical)
   Prefered return state if the checked value is out of boundaries 
   If state parameter is not entered correctly, it returns to the default value
 
 
   Note that some cases are Hard coded and will not use state value.
   Returns OK if min <= value <=max
   Returns OK if help was called , because help should never be called in actual check
   Returns CRITICAL if required aguments are not present
   Returns UNKNOWN if min > max
   Returns UNKNOWN if min or max does not match the pattern, ergo is not recognized as numbers 
   Returns UNKNOWN if anything goes wrong with snmp polling 
   Return "state" if the value is out of boundaries
 
EOT
 
 
}#print_help
 
## main
 
check_options();
 
 
#print "host $hostaddress\n";
#print "OID $oid\n";
#print "community $community\n";
#print "snmp version  $snmp_version\n";
#print "min $low_boundary\n";
#print "max $high_boundary\n";
#print "help $help\n";
#print "return state $out_of_bound_return_state\n";
 
do_check();
 
# vim: ts=4

search?q=%20Nagios%20Supervision%20Monitoring%20Plugin%20Dev%20Scripting%20EN&amp;btnI=lucky search?q=%20Nagios%20Plugins%20%28check_snmp_boundaries.pl%29%20which%20compare%20a%20value%20to%20two%20boundaries&amp;btnI=lucky

check_snmp_boundaries.txt · Dernière modification: 2018/02/18 16:01 (modification externe)