diff --git a/checks/cisco_api_status b/checks/cisco_api_status index a2dc7fe1ac62ae2f9247d3634504d84a40832dc1..aed0a8e530854e76334cd6e2c4bb85c3f0f6e4b5 100644 --- a/checks/cisco_api_status +++ b/checks/cisco_api_status @@ -13,41 +13,42 @@ def cisco_api_status_arguments(params): # example # {'psirt': {'warn-severity': 3, 'warn-days': 31, 'crit-days': 7, 'crit-severity': 1}} + # print params if params: if params.get('psirt'): - if params.get('psirt').get('crit-days'): + if params.get('psirt').get('crit-days') is not None: args.append('--psirt-crit-days %s' % params.get('psirt').get('crit-days')) - if params.get('psirt').get('warn-days'): + if params.get('psirt').get('warn-days') is not None: args.append('--psirt-warn-days %s' % params.get('psirt').get('warn-days')) - if params.get('psirt').get('warn-severity'): + if params.get('psirt').get('warn-severity') is not None: args.append('--psirt-warn-severity %s' % params.get('psirt').get('warn-severity')) - if params.get('psirt').get('crit-severity'): + if params.get('psirt').get('crit-severity') is not None: args.append('--psirt-crit-severity %s' % params.get('psirt').get('crit-severity')) - if params.get('psirt').get('not-active'): + if params.get('psirt').get('not-active') is not None: args.append('--psirt-not-active %s' % params.get('psirt').get('not-active')) if params.get('eox'): - if params.get('eox').get('crit-new'): + if params.get('eox').get('crit-new') is not None: args.append('--eox-crit-new %s' % params.get('eox').get('crit-new')) - if params.get('eox').get('warn-new'): + if params.get('eox').get('warn-new') is not None: args.append('--eox-warn-new %s' % params.get('eox').get('warn-new')) - if params.get('eox').get('crit-change'): + if params.get('eox').get('crit-change') is not None: args.append('--eox-crit-change %s' % params.get('eox').get('crit-change')) - if params.get('eox').get('warn-change'): + if params.get('eox').get('warn-change') is not None: args.append('--eox-warn-change %s' % params.get('eox').get('warn-change')) - if params.get('eox').get('not-active'): + if params.get('eox').get('not-active') is not None: args.append('--eox-not-active %s' % params.get('eox').get('not-active')) if params.get('sn2info'): - if params.get('sn2info').get('crit-change'): + if params.get('sn2info').get('crit-change') is not None: args.append('--sn2info-crit-change %s' % params.get('sn2info').get('crit-change')) - if params.get('sn2info').get('warn-change'): + if params.get('sn2info').get('warn-change') is not None : args.append('--sn2info-warn-change %s' % params.get('sn2info').get('warn-change')) - if params.get('sn2info').get('not-active'): + if params.get('sn2info').get('not-active') is not None: args.append('--sn2info-not-active %s' % params.get('sn2info').get('not-active')) if params.get('suggestion'): - if params.get('suggestion').get('not-active'): + if params.get('suggestion').get('not-active') is not None: args.append('--suggestion-not-active %s' % params.get('suggestion').get('not-active')) # in CMK v1.5.x global variable g_hostname was replaced by API call host_name() diff --git a/inv_cisco_support.mkp b/inv_cisco_support.mkp index 91f4ee02be3d3a24f475279e91bd0096c5d66807..006a84293927d2cd83448153a954b743fcc02d2e 100644 Binary files a/inv_cisco_support.mkp and b/inv_cisco_support.mkp differ diff --git a/lib/nagios/plugins/cisco_api_status b/lib/nagios/plugins/cisco_api_status index a95e1d6e7b2e748e576798489551a8d2b802bbf8..387115b6b37f447239cc668549a2421f7cad16a0 100755 --- a/lib/nagios/plugins/cisco_api_status +++ b/lib/nagios/plugins/cisco_api_status @@ -1,12 +1,15 @@ #!/usr/bin/python # encoding: utf-8 - - +# Monitor Cisco API plugins for CheckMK +# Author: thl-cmk[al]outlook[dot]com +# +# +# 15.03.2020: changed getopt to argparse import os import sys -import getopt +import argparse import ConfigParser import json from datetime import datetime @@ -14,61 +17,6 @@ import logging import sys -def bail_out(reason): - # set logg modul name <file>:<module>.<function> - logger = logging.getLogger(__file__ + ':' + __name__ + '.' + sys._getframe().f_code.co_name) - - sys.stderr.write("FATAL ERROR: %s\n" % reason) - sys.exit(3) - - -def usage(): - # set logg modul name <file>:<module>.<function> - logger = logging.getLogger(__file__ + ':' + __name__ + '.' + sys._getframe().f_code.co_name) - - print """cisco_api_atatus [options] TARGET - -Check status of Cisco APIs for TARGET. TARGET is the hostname used by Check_MK. - -Other options: - - PSIRT options - --psirt-crit-severity <severity> if new/updated advisory equal or above severity set outcome to critical - --psirt-crit-days <days> set outcome to critical is new/updated advisory not older then days - --psirt-warn-severity <severity> if new/updated advisory equal or above severity set outcome to warning - --psirt-warn-days <days> set outcome to warning is new/updated advisory not older then days - --psirt-not-active <status> set outcome to status if PSIRT API is not active - - --psirt-crit-days should be less then --psirt-warn-days - --psirt-warn-severity should be less then --psirt-crit-severity - - EoX options - --eox-crit-new <days> set outcome to critical if new announcement not older then days - --eox-warn-new <days> set outcome to warning if new announcement not older then days - --eox-crit-change <days> set outcome to critical if announcement is about to change in lees then days - --eox-warn-change <days> set outcome to warning if announcement is about to change in lees then days - --eox-not-active <status> set outcome to status if EoX API is not active - - sn2info (contract) options - --sn2info-crit-change <days> set outcome to critical if contract is about to change in lees then days - --sn2info-warn-change <days> set outcome to warning if contract is about to change in lees then days - --sn2info-not-active <status> set outcome to status if sn2info API is not active - - suggestion options - --suggestion-not-active <status> set outcome to status if suggestion API is not active - - - <severity> is one of : 1 (Critical), 2 (High), 3 (Medium), 4 (Low), 0 (All) - <days> : >= 1 - <status> is one of : 0 (Ok), 1 (Warning), 2 (Critical) - - - -h, --help show this help and exit - --debug show Python exceptions verbosely - -""" - - # check if dir exists, if not try to create it. # return True if dir exists or creation was ok. # return False if dir not exists and creation was not ok @@ -100,7 +48,7 @@ def expand_path(path): return path -def check_eox_status(apistatus, options): +def check_eox_status(apistatus, options, cmd_args): # set logg modul name <file>:<module>.<function> logger = logging.getLogger(__file__ + ':' + __name__ + '.' + sys._getframe().f_code.co_name) @@ -112,10 +60,10 @@ def check_eox_status(apistatus, options): lastrun = apistatus.get('lastrun') refresh_known = options.get('refresh_known', 31) refresh_unknown = options.get('refresh_unknown', 7) - crit_new = options.get('crit-new', 0) - warn_new = options.get('warn-new', 0) - crit_change = options.get('crit-change', 0) - warn_change = options.get('warn-change', 0) + crit_new = cmd_args.eox_crit_new + warn_new = cmd_args.eox_warn_new + crit_change = cmd_args.eox_crit_change + warn_change = cmd_args.eox_warn_change for entry in lastrun: last_checked = entry.get('Last_checked') @@ -142,7 +90,7 @@ def check_eox_status(apistatus, options): # check if date is about to change for key in entry.keys(): - if key in EoL_dates: + if key in EoL_dates and entry.get(key) != '': change_days = (datetime.strptime(entry.get(key), '%Y-%m-%d').date() - datetime.now().date()).days change_text = EoL_dates_desc.get(key, 'key not found') if ((change_days < crit_change) or (change_days < warn_change)) and (change_days >= 0): @@ -189,7 +137,7 @@ def check_eox_status(apistatus, options): return output, long_output, status -def check_psirt_status(apistatus, options): +def check_psirt_status(apistatus, options, cmd_args): # set logg modul name <file>:<module>.<function> logger = logging.getLogger(__file__ + ':' + __name__ + '.' + sys._getframe().f_code.co_name) @@ -212,10 +160,10 @@ def check_psirt_status(apistatus, options): refresh_found = options.get('refresh_found', 1) refresh_notfound = options.get('refresh_notfound', 1) - warn_days = options.get('warn-days') - crit_days = options.get('crit-days') - warn_severity = options.get('warn-severity', 0) - crit_severity = options.get('crit-severity', 0) + warn_days = cmd_args.psirt_warn_days + crit_days = cmd_args.psirt_crit_days + warn_severity = cmd_args.psirt_warn_severity + crit_severity = cmd_args.psirt_crit_severity if (psirt_status == 'found') and (last_refresh > refresh_found): max_refresh_known = last_refresh @@ -254,7 +202,7 @@ def check_psirt_status(apistatus, options): return output, long_output, status -def check_sn2info_status(apistatus, options): +def check_sn2info_status(apistatus, options, cmd_args): # set logg modul name <file>:<module>.<function> logger = logging.getLogger(__file__ + ':' + __name__ + '.' + sys._getframe().f_code.co_name) @@ -267,8 +215,8 @@ def check_sn2info_status(apistatus, options): refresh_covered = options.get('refresh_covered', 31) refresh_notcovered = options.get('refresh_notcovered', 7) - crit_change = options.get('crit-change', 0) - warn_change = options.get('warn-change', 0) + crit_change = cmd_args.sn2info_crit_change + warn_change = cmd_args.sn2info_warn_change for entry in lastrun: last_checked = entry.get('Last_checked') @@ -322,7 +270,7 @@ def check_sn2info_status(apistatus, options): return output, long_output, status -def check_suggestion_status(apistatus, options): +def check_suggestion_status(apistatus, options, cmd_args): # set logg modul name <file>:<module>.<function> logger = logging.getLogger(__file__ + ':' + __name__ + '.' + sys._getframe().f_code.co_name) @@ -377,7 +325,6 @@ def main(): output = '' long_output = '' apistatus = {} - target = '' loglevel = 'warning' @@ -421,79 +368,29 @@ def main(): os.unsetenv("LANG") - # short_options = "hw:W:c:C:nTI" - short_options = 'h' - long_options = ['help', 'debug', - 'psirt-crit-severity=', 'psirt-crit-days=', 'psirt-warn-severity=', 'psirt-warn-days=', 'psirt-not-active=', - 'eox-not-active=', 'eox-crit-new=', 'eox-warn-new=', 'eox-crit-change=', 'eox-warn-change=', - 'sn2info-not-active=', 'sn2info-crit-change=', 'sn2info-warn-change=', - 'suggestion-not-active=', - ] - - try: - opts, args = getopt.getopt(sys.argv[1:], short_options, long_options) - - # first parse modifers - # for o, a in opts: - # if o in [ '-v', '--verbose' ]: - # opt_verbose += 1 - # elif o in [ '-d', '--debug' ]: - # opt_debug = True - # elif o in [ '-w', '-W', '-c', '-C' ]: - # routes.append((o[1], a)) - # elif o == '-n': - # opt_nodns = True - # elif o in [ '-T', '-I' ]: - # opt_method = o - - # now handle action options - for o, a in opts: - if o in ['--psirt-crit-severity']: - opt_psirt.update({'crit-severity': int(a)}) - if o in ['--psirt-warn-severity']: - opt_psirt.update({'warn-severity': int(a)}) - if o in ['--psirt-crit-days']: - if int(a) > 0: - opt_psirt.update({'crit-days': int(a)}) - if o in ['--psirt-warn-days']: - if int(a) > 0: - opt_psirt.update({'warn-days': int(a)}) - if o in ['--psirt-not-active']: - opt_psirt.update({'not-active': int(a)}) - - if o in ['--eox-not-active']: - opt_eox.update({'not-active': int(a)}) - if o in ['--eox-crit-new']: - opt_eox.update({'crit-new': int(a)}) - if o in ['--eox-warn-new']: - opt_eox.update({'warn-new': int(a)}) - if o in ['--eox-crit-change']: - opt_eox.update({'crit-change': int(a)}) - if o in ['--eox-warn-change']: - opt_eox.update({'warn-change': int(a)}) - - if o in ['--sn2info-not-active']: - opt_sn2info.update({'not-active': int(a)}) - if o in ['--sn2info-crit-change']: - opt_sn2info.update({'crit-change': int(a)}) - if o in ['--sn2info-warn-change']: - opt_sn2info.update({'warn-change': int(a)}) - - if o in ['--suggestion-not-active']: - opt_suggestion.update({'not-active': int(a)}) - - if o in ['-h', '--help']: - usage() - sys.exit(0) - - if len(args) < 1: - bail_out("Please specify the target.") - - target = args[0] - - - except Exception, e: - bail_out(e) + parser = argparse.ArgumentParser() + parser.add_argument('TARGET', type=str, help='Host to check (exact name form CheckMK)') + parser.add_argument('--psirt-crit-days', type=int, default=0, help='set check status to critical if new/updated advisory not older then days, should be less then --psirt-warn-days') + parser.add_argument('--psirt-warn-days', type=int, default=0, help='set check status to warning if new/updated advisory not older then days') + parser.add_argument('--psirt-crit-severity', type=int, default=0, choices=[0, 1, 2, 3, 4], help='if new/updated advisory equal or above severity {All, Critical, High, Medium, Low} set check status to critical') + parser.add_argument('--psirt-warn-severity', type=int, default=0, choices=[0, 1, 2, 3, 4], help='if new/updated advisory equal or above severity {All, Critical, High, Medium, Low} set check status to warning, should be less then --psirt-crit-severity') + parser.add_argument('--psirt-not-active', type=int, choices=[0, 1, 2], help='set check status to {Ok, Warn, Crit} if PSIRT API is not active') + parser.add_argument('--eox-crit-new', type=int, default=0, help='set check status to critical if new announcement not older then days') + parser.add_argument('--eox-warn-new', type=int, default=0, help='set check status to warning if new announcement not older then days') + parser.add_argument('--eox-crit-change', type=int, default=0, help='set check status to critical if announcement is about to change in lees then days') + parser.add_argument('--eox-warn-change', type=int, default=0, help='set check status to warning if announcement is about to change in lees then days') + parser.add_argument('--eox-not-active', type=int, choices=[0, 1, 2], help='set check status to {Ok, Warn, Crit} if EoX API is not active') + parser.add_argument('--sn2info-crit-change', type=int, default=0, help='set check status to critical if contract is about to change in lees then days') + parser.add_argument('--sn2info-warn-change', type=int, default=0, help='set check status to warning if contract is about to change in lees then days') + parser.add_argument('--sn2info-not-active', type=int, choices=[0, 1, 2], help='set check status to {Ok, Warn, Crit} if sn2info API is not active') + parser.add_argument('--suggestion-not-active', type=int, choices=[0, 1, 2], help='set check status to {Ok, Warn, Crit} if suggestion API is not active') + + parser.parse_args() + cmd_args = parser.parse_args() + # print 'cmd_args: {}'.format(cmd_args) + + target = cmd_args.TARGET + not_active = [] eox_output = '' eox_long_output = '' @@ -515,16 +412,16 @@ def main(): output = 'Cisco API active.' with open(ciscoapistatus + target) as f: apistatus = json.load(f) - long_output = 'active APIs found: ' + ', '.join(apistatus.keys()) + long_output = 'active API(s) found: ' + ', '.join(apistatus.keys()) for key in apistatus.keys(): if key == 'eox': - eox_output, eox_long_output, eox_status = check_eox_status(apistatus.get(key), opt_eox) + eox_output, eox_long_output, eox_status = check_eox_status(apistatus.get(key), opt_eox, cmd_args) if key == 'psirt': - psirt_output, psirt_long_output, psirt_status = check_psirt_status(apistatus.get(key), opt_psirt) + psirt_output, psirt_long_output, psirt_status = check_psirt_status(apistatus.get(key), opt_psirt, cmd_args) if key == 'sn2info': - sn2info_output, sn2info_long_output, sn2info_status = check_sn2info_status(apistatus.get(key), opt_sn2info) + sn2info_output, sn2info_long_output, sn2info_status = check_sn2info_status(apistatus.get(key), opt_sn2info, cmd_args) if key == 'suggestion': - suggestion_output, suggestion_long_output, suggestion_status = check_suggestion_status(apistatus.get(key), opt_suggestion) + suggestion_output, suggestion_long_output, suggestion_status = check_suggestion_status(apistatus.get(key), opt_suggestion, cmd_args) long_output += eox_long_output + sn2info_long_output + psirt_long_output + suggestion_long_output + '\n' @@ -538,26 +435,24 @@ def main(): if status > 0: output += ' (see long output for details)' - if opt_psirt.get('not-active') and ('psirt' not in apistatus.keys()): - if status <= opt_psirt.get('not-active'): - status = opt_psirt.get('not-active') - output += ' PSIRT API not active.' - - if opt_eox.get('not-active') and ('eox' not in apistatus.keys()): - if status <= opt_eox.get('not-active'): - status = opt_eox.get('not-active') - output += ' EoX API not active.' - - if opt_sn2info.get('not-active') and ('sn2info' not in apistatus.keys()): - if status <= opt_sn2info.get('not-active'): - status = opt_sn2info.get('not-active') - output += ' SN2INFO API not active.' - - if opt_suggestion.get('not-active') and ('suggestion' not in apistatus.keys()): - if status <= opt_suggestion.get('not-active'): - status = opt_suggestion.get('not-active') - output += ' SUGGESTION API not active.' - + if cmd_args.psirt_not_active is not None and ('psirt' not in apistatus.keys()): + status = max(status, cmd_args.psirt_not_active) + not_active.append('PSIRT') + + if cmd_args.eox_not_active is not None and ('eox' not in apistatus.keys()): + status = max(status, cmd_args.eox_not_active) + not_active.append('EoX') + + if cmd_args.sn2info_not_active is not None and ('sn2info' not in apistatus.keys()): + status = max(status, cmd_args.sn2info_not_active) + not_active.append('SN2INFO') + + if cmd_args.suggestion_not_active is not None and ('suggestion' not in apistatus.keys()): + status = max(status, cmd_args.suggestion_not_active) + not_active.append('SUGGESTION') + + if len(not_active) > 0: + output += ' Not active API(s): {}'.format(', '.join(not_active)) else: output = 'Cisco API not active(!)' status = max(1, status)