From 8e35db0e385c404fd21eed25ae0cbbffa7b1db2d Mon Sep 17 00:00:00 2001 From: Mstislav Bobakov Date: Thu, 21 Jan 2016 13:08:25 +0300 Subject: [PATCH 1/3] Add custom parameter for a sensu_check --- monitoring/sensu_check.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/monitoring/sensu_check.py b/monitoring/sensu_check.py index 09edae63813..6a4ef90b8e4 100644 --- a/monitoring/sensu_check.py +++ b/monitoring/sensu_check.py @@ -149,6 +149,11 @@ options: - The low threshhold for flap detection required: false default: null + custom: + description: + - JSON mixin to add to the configuration + required: false + default: "{}" requirements: [ ] author: "Anders Ingemann (@andsens)" ''' @@ -183,6 +188,7 @@ except ImportError: # Let snippet from module_utils/basic.py return a proper error in this case pass +import ast def sensu_check(module, path, name, state='present', backup=False): changed = False @@ -257,6 +263,36 @@ def sensu_check(module, path, name, state='present', backup=False): changed = True reasons.append('`{opt}\' was removed'.format(opt=opt)) + if module.params['custom']: + # Convert to json + try: + custom_params = ast.literal_eval(module.params['custom']) + except: + msg = 'Module parameter "custom" contains invalid JSON. Example: custom=\'{"JSON": "here"}\'' + module.fail_json(msg=msg) + + overwrited_fields = set(custom_params.keys()) & set(simple_opts + ['type','subdue']) + if overwrited_fields: + msg = 'You can\'t overwriting standard module parameters via "custom". You are trying overwrite: {of}'.format(of=list(overwrited_fields)) + module.fail_json(msg=msg) + + for k,v in custom_params.items(): + if k in config['checks'][name].keys(): + if not config['checks'][name][k] == v: + changed = True + reasons.append('`custom param {k}\' was changed'.format(k=k)) + else: + changed = True + reasons.append('`custom param {k}\' was added'.format(k=k)) + check[k] = v + simple_opts += custom_params.keys() + + # Remove obsolete custom params + for opt in set(config['checks'][name].keys()) - set(simple_opts + ['type','subdue']): + changed = True + reasons.append('`custom param {opt}\' was deleted'.format(opt=opt)) + del check[opt] + if module.params['metric']: if 'type' not in check or check['type'] != 'metric': check['type'] = 'metric' @@ -320,6 +356,7 @@ def main(): 'aggregate': {'type': 'bool'}, 'low_flap_threshold': {'type': 'int'}, 'high_flap_threshold': {'type': 'int'}, + 'custom': {'type': 'str'}, } required_together = [['subdue_begin', 'subdue_end']] From 6ef3697c52f51b76bbcd6753f9f568e7fde90fe8 Mon Sep 17 00:00:00 2001 From: Mstislav Bobakov Date: Thu, 28 Jan 2016 15:42:09 +0300 Subject: [PATCH 2/3] Add custom parameter for a sensu_check. Fixes. JSON replaced within dict. Added more docs. --- monitoring/sensu_check.py | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/monitoring/sensu_check.py b/monitoring/sensu_check.py index 6a4ef90b8e4..f2e336dc114 100644 --- a/monitoring/sensu_check.py +++ b/monitoring/sensu_check.py @@ -151,9 +151,10 @@ options: default: null custom: description: - - JSON mixin to add to the configuration + - A hash/dictionary of custom parameters for mixing to the configuration. + - You can't rewrite others module parameters using this required: false - default: "{}" + default: {} requirements: [ ] author: "Anders Ingemann (@andsens)" ''' @@ -188,8 +189,6 @@ except ImportError: # Let snippet from module_utils/basic.py return a proper error in this case pass -import ast - def sensu_check(module, path, name, state='present', backup=False): changed = False reasons = [] @@ -265,30 +264,25 @@ def sensu_check(module, path, name, state='present', backup=False): if module.params['custom']: # Convert to json - try: - custom_params = ast.literal_eval(module.params['custom']) - except: - msg = 'Module parameter "custom" contains invalid JSON. Example: custom=\'{"JSON": "here"}\'' - module.fail_json(msg=msg) - - overwrited_fields = set(custom_params.keys()) & set(simple_opts + ['type','subdue']) + custom_params = module.params['custom'] + overwrited_fields = set(custom_params.keys()) & set(simple_opts + ['type','subdue','subdue_begin','subdue_end']) if overwrited_fields: - msg = 'You can\'t overwriting standard module parameters via "custom". You are trying overwrite: {of}'.format(of=list(overwrited_fields)) + msg = 'You can\'t overwriting standard module parameters via "custom". You are trying overwrite: {opt}'.format(opt=list(overwrited_fields)) module.fail_json(msg=msg) for k,v in custom_params.items(): - if k in config['checks'][name].keys(): + if k in config['checks'][name]: if not config['checks'][name][k] == v: changed = True - reasons.append('`custom param {k}\' was changed'.format(k=k)) + reasons.append('`custom param {opt}\' was changed'.format(opt=k)) else: changed = True - reasons.append('`custom param {k}\' was added'.format(k=k)) + reasons.append('`custom param {opt}\' was added'.format(opt=k)) check[k] = v simple_opts += custom_params.keys() # Remove obsolete custom params - for opt in set(config['checks'][name].keys()) - set(simple_opts + ['type','subdue']): + for opt in set(config['checks'][name].keys()) - set(simple_opts + ['type','subdue','subdue_begin','subdue_end']): changed = True reasons.append('`custom param {opt}\' was deleted'.format(opt=opt)) del check[opt] @@ -356,7 +350,7 @@ def main(): 'aggregate': {'type': 'bool'}, 'low_flap_threshold': {'type': 'int'}, 'high_flap_threshold': {'type': 'int'}, - 'custom': {'type': 'str'}, + 'custom': {'type': 'dict'}, } required_together = [['subdue_begin', 'subdue_end']] From c4aa5ee024f9eac010a195955dca80a972c977b2 Mon Sep 17 00:00:00 2001 From: Mstislav Bobakov Date: Thu, 28 Jan 2016 15:44:57 +0300 Subject: [PATCH 3/3] Add custom parameter for a sensu_check. revert newline --- monitoring/sensu_check.py | 1 + 1 file changed, 1 insertion(+) diff --git a/monitoring/sensu_check.py b/monitoring/sensu_check.py index f2e336dc114..3fd13ff0aa6 100644 --- a/monitoring/sensu_check.py +++ b/monitoring/sensu_check.py @@ -189,6 +189,7 @@ except ImportError: # Let snippet from module_utils/basic.py return a proper error in this case pass + def sensu_check(module, path, name, state='present', backup=False): changed = False reasons = []