#!/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 Cisco # OID related to license expiration and trigger alarm based on it. # OID is 1.3.6.1.4.1.9.9.543.1.2.3.1.16 which is clmgmtLicenseEndDate # # snmp Output Format is like "Hex-STRING: 07 E1 0C 12 17 00 00 00" # 07 E1 = 2017 # 0C = 12 # 12 = 18 # ########################################################################## 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.543.1.2.3.1.16" #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=30, dest='critical', type=int, help='critical threshold for nagios (default: 30)') parser.add_argument('-w', default=90, dest='warning', type=int, help='warning threshold for nagios (default: 90)') 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() #test = parser.parse_args(['var_critical']) 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 Hex data is starting just after Hex-STRING: value date_str=result_snmp.find("Hex-STRING:")+12 # If snmpwalk failed for any reason date_str=-1 +12 if date_str == 11 : print "UNKNOWN" sys.exit(UNKNOWN) else : # SNMPWALK Result has a valid hex value # Detect end of string to display only valuable data (eg License expiration date) end_str=len(result_snmp) #print "OK - command output %s " % result_snmp[date_str:end_str] # Split Hex value separated by space date_hex=result_snmp[date_str:end_str].split(" ") # Detect Year of expiration and convert it back to decimal value date_year="%s%s" % (date_hex[0],date_hex[1]) date_year_int= int (date_year, 16) date_year=str(date_year_int) # Detect Month of expiration and convert it back to decimal value date_month="%s" % (date_hex[2]) date_month_int= int (date_month, 16) date_month=str(date_month_int) # Detect Day of expiration and convert it back to decimal value date_day="%s" % (date_hex[3]) date_day_int= int (date_day, 16) date_day=str(date_day_int) # Calculate current today value in days today = date.today() expiration_date=date(date_year_int,date_month_int,date_day_int) remaining_day = expiration_date - today # Check if the remaining number of days is less than crtitical threshold if remaining_day.days < critical_arg : print "CRITICAL - license will expire on %s/%s/%s as dd/mm/yyyy which is less than critical threshold %d" % (date_day,date_month,date_year,critical_arg) sys.exit(CRITICAL) # Check if the remaining number of days is less than warning threshold if remaining_day.days < warning_arg : print "WARNING - license will expire on %s/%s/%s as dd/mm/yyyy which is less than warning threshold %d" % (date_day,date_month,date_year,warning_arg) sys.exit(WARNING) else : print "OK - license will expire on %s/%s/%s as dd/mm/yyyy which is more than warning threshold %d" % (date_day,date_month,date_year,warning_arg) sys.exit(OK)