Skip to content

Commit

Permalink
Merge branch 'cmk2.2' into cmk2.3
Browse files Browse the repository at this point in the history
  • Loading branch information
gurubert committed Sep 9, 2024
2 parents bdb40f8 + 1b235d0 commit 087269b
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
84 changes: 84 additions & 0 deletions helper/bin/container_piggyback_translation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#!/usr/bin/env python3
# -*- encoding: utf-8; py-indent-offset: 4 -*-

#
# (C) 2017 Heinlein Support GmbH
# Robert Sander <[email protected]>
#

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()
Binary file removed helper/helper-0.14.0.mkp
Binary file not shown.
Binary file added helper/helper-0.15.0.mkp
Binary file not shown.

0 comments on commit 087269b

Please sign in to comment.