-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adds script container_piggyback_translation
adds rules to prefix container names with Docker node name (hostname)
- Loading branch information
Showing
3 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
Binary file not shown.