diff --git a/helper/bin/container_piggyback_translation.py b/helper/bin/container_piggyback_translation.py new file mode 100755 index 00000000..c0246dfb --- /dev/null +++ b/helper/bin/container_piggyback_translation.py @@ -0,0 +1,84 @@ +#!/usr/bin/env python3 +# -*- encoding: utf-8; py-indent-offset: 4 -*- + +# +# (C) 2017 Heinlein Support GmbH +# Robert Sander +# + +ruleset_name = 'piggyback_translation' +tag_string = 'generated by container_piggyback_translation' + +import argparse +import checkmkapi +from pprint import pprint + +parser = argparse.ArgumentParser() +parser.add_argument('-s', '--url', + required=False, + help='URL to Check_MK site') +parser.add_argument('-u', '--username', + required=False, + help='name of the Automation user') +parser.add_argument('-p', '--password', + required=False) +parser.add_argument('-i', '--site', + required=False) +args = parser.parse_args() + +mapi = checkmkapi.MultisiteAPI(args.url, args.username, args.password) +wato = checkmkapi.CMKRESTAPI(args.url, args.username, args.password) + +if args.site: + resp = mapi.view(view_name='servicedesc', service='Docker node info', filled_in='filter', site=args.site) +else: + resp = mapi.view(view_name='servicedesc', service='Docker node info') +hosts = list(map(lambda x: x['host'], resp)) + +rules, etag = wato.get_rules(ruleset_name) + +host_rules = {} +del_rules = [] + +for rule in rules['value']: + rule_id = rule['id'] + rule_ex = rule['extensions'] + rule_prop = rule_ex.get('properties', {}) + conditions = rule_ex.get('conditions', {}) + host_match = conditions.get('host_name', {}) + if len(host_match.get('match_on', [])) == 1 and host_match.get('operator', '') == 'one_of' and host_match['match_on'][0] in hosts: + host_rules[host_match['match_on'][0]] = rule_id + hosts.remove(host_match['match_on'][0]) + elif rule_prop.get('description', '') == tag_string: + del_rules.append(rule_id) + +changes = False + +for rule_id in del_rules: + print(f"Removing {rule_id}.") + wato.delete_rule(rule_id) + changes = True + +for host in hosts: + value_raw = "{'regex': [('(.+?)', '%s_\\\\1')]}" % host.replace(".", "_") + print(value_raw) + wato.create_rule( + ruleset_name, + "/", + value_raw, + conditions = { + "host_name": { + "match_on": [host], + "operator": "one_of", + }, + }, + properties = { + "disabled": False, + "description": tag_string, + }, + ) + changes = True + +if changes: + print("Activating changes.") + wato.activate() diff --git a/helper/helper-0.14.0.mkp b/helper/helper-0.14.0.mkp deleted file mode 100644 index 6d937d8a..00000000 Binary files a/helper/helper-0.14.0.mkp and /dev/null differ diff --git a/helper/helper-0.15.0.mkp b/helper/helper-0.15.0.mkp new file mode 100644 index 00000000..9444f06c Binary files /dev/null and b/helper/helper-0.15.0.mkp differ