-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
172 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,79 @@ | ||
#!/usr/bin/env python3 | ||
# -*- encoding: utf-8; py-indent-offset: 4 -*- | ||
|
||
# | ||
# (C) 2023 Heinlein Support GmbH | ||
# Robert Sander <[email protected]> | ||
# | ||
|
||
# Needs a view "hostparents": | ||
|
||
|
||
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('-a', '--append', | ||
required=False, | ||
help='append this to the hypervisor name if not already there (usually the domainname)') | ||
parser.add_argument('-r', '--remove', | ||
action='store_true', | ||
required=False, | ||
help="remove the domainname from the hypervisor's name") | ||
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='hostparents', filled_in='filter', site=args.site) | ||
else: | ||
resp = mapi.view(view_name='hostparents') | ||
hosts = {} | ||
for item in resp: | ||
node = False | ||
if item['svc_plugin_output.vmware'].startswith('Running on '): | ||
node = item['svc_plugin_output.vmware'][11:] | ||
elif item['svc_plugin_output.proxmox']: | ||
for key, value in map(lambda x: x.split(': ', 1), | ||
item['svc_plugin_output.proxmox'].split(', ')): | ||
if key == 'Host': | ||
node = value | ||
break | ||
if node: | ||
if args.remove: | ||
node = node.split('.')[0] | ||
if args.append and not node.endswith(args.append): | ||
node += args.append | ||
if node != item['host_parents']: | ||
hosts[item['host']] = node | ||
|
||
changes = False | ||
for host, parent in hosts.items(): | ||
try: | ||
watohost, etag = wato.get_host(host, effective_attr=True) | ||
except: | ||
continue | ||
if watohost['extensions']['effective_attributes'].get('parents', []) != [ parent ]: | ||
if parent: | ||
print("%s gets %s as parent" % (host, parent)) | ||
wato.edit_host(host, etag=etag, update_attr={'parents': [ parent ]}) | ||
changes = True | ||
elif len(watohost['extensions']['attributes'].get('parents', [])) == 1: | ||
print("%s gets no specific parent" % host) | ||
wato.edit_host(host, etag=etag, unset_attr=['parents']) | ||
changes = True | ||
if changes: | ||
wato.activate() | ||
|
Binary file not shown.
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,93 @@ | ||
#!/usr/bin/env python3 | ||
# -*- encoding: utf-8; py-indent-offset: 4 -*- | ||
|
||
from cmk.utils.type_defs import UserId | ||
from cmk.gui.i18n import _ | ||
from cmk.gui.type_defs import ( | ||
ColumnSpec, | ||
PainterParameters, | ||
SorterSpec, | ||
VisualLinkSpec, | ||
) | ||
from cmk.gui.views.store import multisite_builtin_views | ||
|
||
|
||
multisite_builtin_views.update( | ||
{ | ||
'hostparents': { | ||
'link_from': {}, | ||
'packaged': False, | ||
'single_infos': [], | ||
'name': 'host_parents', | ||
'title': _('Host and Parents'), | ||
'owner': UserId.builtin(), | ||
'topic': 'heinlein', | ||
'sort_index': 99, | ||
'is_show_more': False, | ||
'description': '', | ||
'icon': None, | ||
'add_context_to_title': True, | ||
'hidden': False, | ||
'hidebutton': True, | ||
'public': True, | ||
'datasource': 'hosts', | ||
'browser_reload': 0, | ||
'layout': 'table', | ||
'num_columns': 1, | ||
'column_headers': 'pergroup', | ||
'painters': [ | ||
ColumnSpec( | ||
name='host', | ||
parameters=PainterParameters(color_choices=[]), | ||
link_spec=VisualLinkSpec(type_name='views', name='host'), | ||
tooltip=None, | ||
), | ||
ColumnSpec(name='host_parents',), | ||
ColumnSpec( | ||
name='svc_plugin_output', | ||
join_value='ESX Hostsystem', | ||
column_title='VMware', | ||
# column_type='join_column', | ||
), | ||
ColumnSpec( | ||
name='svc_plugin_output', | ||
join_value='Proxmox VE VM Info', | ||
column_title='Proxmox', | ||
#'column_type': 'join_column'}], | ||
), | ||
], | ||
'group_painters': [], | ||
'sorters': [ | ||
SorterSpec(sorter='host_name', negate=False), | ||
], | ||
'context': { | ||
'host_labels': { | ||
'host_labels_indexof_@!@': '', | ||
'host_labels_orig_indexof_@!@': '', | ||
'host_labels_@!@_bool': 'and', | ||
'host_labels_@!@_vs_indexof_@:@': '', | ||
'host_labels_@!@_vs_orig_indexof_@:@': '', | ||
'host_labels_@!@_vs_@:@_bool': 'and', | ||
'host_labels_@!@_vs_count': '1', | ||
'host_labels_@!@_vs_indexof_1': '1', | ||
'host_labels_@!@_vs_orig_indexof_1': '1', | ||
'host_labels_@!@_vs_1_bool': 'and', | ||
'host_labels_count': '1', | ||
'host_labels_indexof_1': '1', | ||
'host_labels_orig_indexof_1': '1', | ||
'host_labels_1_bool': 'and', | ||
'host_labels_1_vs_indexof_@:@': '', | ||
'host_labels_1_vs_orig_indexof_@:@': '', | ||
'host_labels_1_vs_@:@_bool': 'and', | ||
'host_labels_1_vs_count': '1', | ||
'host_labels_1_vs_indexof_1': '1', | ||
'host_labels_1_vs_orig_indexof_1': '1', | ||
'host_labels_1_vs_1_bool': 'and' | ||
}, | ||
'hostregex': {'host_regex': '', 'neg_host_regex': ''}, | ||
'siteopt': {'site': ''}, | ||
'wato_folder': {'wato_folder': ''} | ||
} | ||
} | ||
} | ||
) |