#!/usr/bin/python ########################################################################## # Copyright (C) 2018 Igor Castro # # 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 ########################################################################### ########################################################################### # Version = 1.0 # Organisation = IATA # Author = I.CASTRO # Description = # Nagios check in python to fetch by snmpwalk he Gauge32 value of # OID 1.3.6.1.4.1.9.9.618.1.8.4.0 and trigger alarm based on it. # # # 1.3.6.1.4.1.9.9.618.1.8.4.0 = Number of AP on Cisco Wlc # # # # ########################################################################## import os, sys, time, argparse from datetime import date # Hardcoded parameters to debug my_command="snmpwalk" target_OID="1.3.6.1.4.1.9.9.618.1.8.4.0" #warning_arg=int("60") #critical_arg=int("30") # exit statuses recognized by Nagios OK = 0 WARNING = 1 CRITICAL = 2 UNKNOWN = 3 # Create a Parser to get value from command arguments parser = argparse.ArgumentParser(description='Nagios check to collect by snmp Cisco license expiration date and evaluate the status') parser.add_argument('-c', default=2, dest='critical', type=int, help='critical threshold for nagios (default: 2) is number of AP') parser.add_argument('-w', default=1, dest='warning', type=int, help='warning threshold for nagios (default: 1) is number of AP') parser.add_argument('-H', dest='host', help='target host') parser.add_argument('--snmp', dest='snmp', help='snmp argument in form \"-v 3 -a MD5 -A authpass -l authPriv -u username -x des -X password -r 5 -t 30\"') args = parser.parse_args() warning_arg=args.warning critical_arg=args.critical my_host=args.host snmp_parameters=args.snmp # Create the actual snmpwalk command with all parameters #snmp_command="%s -a %s -A %s -l %s -u %s -x %s -X %s %s %s" % (my_command,snmp3_auth_hash,snmp3_auth_pass,snmp3_mode,snmp3_user,snmp3_enc_cypher,snmp3_enc_pass,my_host,target_OID) snmp_command="%s %s %s %s" % (my_command,snmp_parameters,my_host,target_OID) # Print snmpwalk command #print "snmp_command is %s" % snmp_command result_snmp=os.popen(snmp_command).readline().strip() # Detect when data is starting just after Gauge32: value value_str=result_snmp.find("Gauge32:")+9 # If snmpwalk failed for any reason date_str=-1 +9 if value_str == 8 : print "UNKNOWN" sys.exit(UNKNOWN) else : # SNMPWALK Result has a valid value # Detect end of string to display only valuable data end_str=len(result_snmp) # value separated by space final_val=result_snmp[value_str:end_str] # Cast back to Integer final_val=int(final_val) # Check if the remaining number of AP is less than crtitical threshold if final_val < critical_arg : print "CRITICAL - Number of APs is %d which is below critical threshold %d" % (final_val,critical_arg) sys.exit(CRITICAL) # Check if the remaining number of days is less than warning threshold if final_val < warning_arg : print "WARNING - Number of APs is %d which is below warning threshold %d" % (final_val,warning_arg) sys.exit(WARNING) else : print "OK - Number of APs is %d" % (final_val) sys.exit(OK)