Skip to content

Commit

Permalink
host_parent for CMK 2.2
Browse files Browse the repository at this point in the history
  • Loading branch information
gurubert committed Dec 18, 2023
1 parent bc11866 commit a3346e8
Show file tree
Hide file tree
Showing 3 changed files with 172 additions and 0 deletions.
79 changes: 79 additions & 0 deletions host_parent/bin/host_parent.py
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 added host_parent/host_parent-0.3.0.mkp
Binary file not shown.
93 changes: 93 additions & 0 deletions host_parent/web/plugins/views/host_parent.py
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': ''}
}
}
}
)

0 comments on commit a3346e8

Please sign in to comment.