def _list_services(param): live = sites.live() q = Query(param['columns']) host_name = param.get('host_name') if host_name is not None: q = q.filter(Services.host_name == host_name) query_expr = param.get('query') if query_expr: q = q.filter(query_expr) result = q.iterate(live) return constructors.serve_json( constructors.collection_object( domain_type='service', value=[ constructors.domain_object( domain_type='service', title=f"{entry['description']} on {entry['host_name']}", identifier=f"{entry['host_name']}:{entry['description']}", editable=False, deletable=False, extensions=entry, self_link=constructors.link_rel( rel='cmk/show', href=constructors.object_action_href( 'host', entry['host_name'], 'show_service', query_params=[('service_description', entry['description'])], ), method='get', title=f"Show the service {entry['description']}", ), ) for entry in result ], ))
def _list_services(param): live = sites.live() q = Query(param["columns"]) host_name: typing.Optional[str] = param.get("host_name") if host_name is not None: q = q.filter(Services.host_name == host_name) query_expr = param.get("query") if query_expr: q = q.filter(query_expr) result = q.iterate(live) return constructors.serve_json( constructors.collection_object( domain_type="service", value=[ constructors.domain_object( domain_type="service", title=f"{entry['description']} on {entry['host_name']}", identifier=f"{entry['host_name']}:{entry['description']}", editable=False, deletable=False, extensions=entry, self_link=constructors.link_rel( rel="cmk/show", href=constructors.object_action_href( "host", entry["host_name"], "show_service", query_params=[("service_description", entry["description"])], ), method="get", title=f"Show the service {entry['description']}", ), ) for entry in result ], ))
def _serialize_folder(folder: CREFolder, show_hosts): links = [] if not folder.is_root(): links.append( constructors.link_rel( rel="cmk/move", href=constructors.object_action_href( "folder_config", folder_slug(folder), action_name="move", ), method="post", title="Move the folder", ) ) rv = constructors.domain_object( domain_type="folder_config", identifier=folder_slug(folder), title=folder.title(), extensions={ "path": "/" + folder.path(), "attributes": folder.attributes().copy(), }, links=links, ) if show_hosts: rv["members"]["hosts"] = constructors.collection_property( name="hosts", base=constructors.object_href("folder_config", folder_slug(folder)), value=[ constructors.collection_item( domain_type="host_config", identifier=host.id(), title=host.name(), ) for host in folder.hosts().values() ], ) return rv
def _serialize_folder(folder: CREFolder, show_hosts): links = [] if not folder.is_root(): links.append( constructors.link_rel( rel='cmk/move', href=constructors.object_action_href( "folder_config", folder_slug(folder), action_name='move', ), method='post', title='Move the folder', )) rv = constructors.domain_object( domain_type='folder_config', identifier=folder_slug(folder), title=folder.title(), extensions={ 'attributes': folder.attributes().copy(), }, links=links, ) if show_hosts: rv['members']['hosts'] = constructors.collection_property( name='hosts', base=constructors.object_href('folder_config', folder_slug(folder)), value=[ constructors.collection_item( domain_type='host_config', obj={ 'id': host.id(), 'title': host.name(), }, ) for host in folder.hosts().values() ], ) return rv
def list_passwords(params): """Show all passwords""" user.need_permission("wato.passwords") password_collection = { "id": "password", "domainType": "password", "value": [ constructors.collection_item( domain_type="password", title=details["title"], identifier=password_id, ) for password_id, details in load_passwords().items() ], "links": [ constructors.link_rel("self", constructors.collection_href("password")) ], } return constructors.serve_json(password_collection)
def list_passwords(params): """Show all passwords""" password_collection = { 'id': 'password', 'domainType': 'password', 'value': [ constructors.collection_item(domain_type='password', obj={ 'title': details['title'], 'id': password_id, }) for password_id, details in load_passwords().items() ], 'links': [ constructors.link_rel('self', constructors.collection_href('password')) ], } return constructors.serve_json(password_collection)
def host_collection(hosts: Iterable[watolib.CREHost]) -> Response: _hosts = { 'id': 'host', 'domainType': 'host_config', 'value': [ constructors.collection_item( domain_type='host_config', obj={ 'title': host.name(), 'id': host.id() }, ) for host in hosts ], 'links': [ constructors.link_rel('self', constructors.collection_href('host_config')) ], } return constructors.serve_json(_hosts)
def host_collection(hosts: Iterable[watolib.CREHost]) -> Response: _hosts = { "id": "host", "domainType": "host_config", "value": [ constructors.collection_item( domain_type="host_config", obj={ "title": host.name(), "id": host.id() }, ) for host in hosts ], "links": [ constructors.link_rel("self", constructors.collection_href("host_config")) ], } return constructors.serve_json(_hosts)
def get_bi_packs(params): """Show all BI Packs""" bi_packs = get_cached_bi_packs() bi_packs.load_config() packs = [ constructors.collection_item( domain_type='bi_pack', obj={ 'id': pack.id, 'title': pack.title, }, ) for pack in bi_packs.packs.values() ] collection_object = constructors.collection_object( domain_type='bi_pack', value=packs, links=[constructors.link_rel('self', constructors.collection_href('bi_pack'))], ) return constructors.serve_json(collection_object)
def _serialize_folder(folder: CREFolder) -> DomainObject: uri = constructors.object_href('folder_config', folder.id()) return constructors.domain_object( domain_type='folder_config', identifier=folder.id(), title=folder.title(), members={ 'hosts': constructors.object_collection( name='hosts', domain_type='host_config', entries=[ constructors.link_rel( rel='.../value', parameters={'collection': "items"}, href=constructors.object_href('host_config', host), ) for host in folder.hosts().values() ], base=uri, ), 'move': constructors.object_action( name='move', base=uri, parameters=dict([ constructors.action_parameter( action='move', parameter='destination', friendly_name= 'The destination folder of this move action', optional=False, pattern="[0-9a-fA-F]{32}|root", ), ]), ), }, extensions={ 'attributes': folder.attributes(), }, )
def list_users(params): """Show all users""" user_collection = { 'id': 'user', 'domainType': 'user_config', 'value': [ constructors.collection_item( domain_type='user_config', obj={ 'title': attrs['alias'], 'id': user_id }, ) for user_id, attrs in userdb.load_users(False).items() ], 'links': [ constructors.link_rel('self', constructors.collection_href('user_config')) ], } return constructors.serve_json(user_collection)
def list_time_periods(params): """Show all time periods""" time_periods = [] for time_period_id, time_period_details in load_timeperiods().items(): alias = time_period_details['alias'] if not isinstance(alias, str): # check for mypy continue time_periods.append( constructors.collection_item( domain_type='time_period', obj={ 'title': alias, 'id': time_period_id }, )) time_period_collection = { 'id': "timeperiod", 'domainType': 'time_period', 'value': time_periods, 'links': [constructors.link_rel('self', constructors.collection_href('time_period'))], } return constructors.serve_json(time_period_collection)
def list_host_tag_groups(params): """Show all host tag groups""" tag_config = load_tag_config() tag_config += BuiltinTagConfig() tag_groups_collection = { "id": "host_tag", "domainType": "host_tag_group", "value": [ constructors.collection_item( domain_type="host_tag_group", title=tag_group_obj.title, identifier=tag_group_obj.id, ) for tag_group_obj in tag_config.get_tag_groups() ], "links": [ constructors.link_rel( "self", constructors.collection_href("host_tag_group")) ], } return constructors.serve_json(tag_groups_collection)
def get_bi_packs(params): """Show all BI packs""" bi_packs = get_cached_bi_packs() bi_packs.load_config() packs = [ constructors.collection_item( domain_type="bi_pack", identifier=pack.id, title=pack.title, ) for pack in bi_packs.packs.values() ] collection_object = constructors.collection_object( domain_type="bi_pack", value=packs, links=[ constructors.link_rel("self", constructors.collection_href("bi_pack")) ], ) return constructors.serve_json(collection_object)
def list_passwords(params): """Show all passwords""" password_collection = { "id": "password", "domainType": "password", "value": [ constructors.collection_item( domain_type="password", obj={ "title": details["title"], "id": password_id, }, ) for password_id, details in load_passwords().items() ], "links": [ constructors.link_rel("self", constructors.collection_href("password")) ], } return constructors.serve_json(password_collection)
def list_folders(_params): """List folders""" folders = [ constructors.collection_item( domain_type='folder_config', obj={ 'title': folder.title(), 'id': folder.id() }, ) for folder in watolib.Folder.root_folder().subfolders() ] collection_object = constructors.collection_object( domain_type='folder_config', value=folders, links=[ constructors.link_rel( 'self', constructors.collection_href('folder_config')) ], ) return constructors.serve_json(collection_object)
def list_users(params): """Show all users""" user_collection = { "id": "user", "domainType": "user_config", "value": [ constructors.collection_item( domain_type="user_config", obj={ "title": attrs["alias"], "id": user_id }, ) for user_id, attrs in userdb.load_users(False).items() ], "links": [ constructors.link_rel("self", constructors.collection_href("user_config")) ], } return constructors.serve_json(user_collection)
def list_host_tag_groups(params): """Show all host tags""" tag_config = load_tag_config() tag_groups_collection = { 'id': 'host_tag', 'domainType': 'host_tag_group', 'value': [ constructors.collection_item( domain_type='host_tag_group', obj={ 'title': tag_group_obj.title, 'id': tag_group_obj.id }, ) for tag_group_obj in tag_config.get_tag_groups() ], 'links': [ constructors.link_rel( 'self', constructors.collection_href('host_tag_group')) ] } return constructors.serve_json(tag_groups_collection)
def get_bi_pack(params): """Get BI Pack""" bi_packs = get_cached_bi_packs() bi_packs.load_config() bi_pack = bi_packs.get_pack(params["pack_id"]) if bi_pack is None: _bailout_with_message("Unknown bi_pack: %s" % params["pack_id"]) assert bi_pack is not None uri = constructors.object_href('bi_pack', bi_pack.id) domain_members = {} for (name, entities) in [("aggregation", bi_pack.get_aggregations()), ("rule", bi_pack.get_rules())]: elements = entities.values() # type: ignore[attr-defined] domain_members["%ss" % name] = constructors.object_collection( name=name, domain_type="bi_" + name, # type: ignore[arg-type] entries=[ constructors.link_rel( rel='.../value', parameters={'collection': "items"}, href=constructors.object_href( "bi_" + name # type: ignore[arg-type] , element.id), ) for element in elements ], base=uri, ) domain_object = constructors.domain_object(domain_type='bi_pack', identifier=bi_pack.id, title=bi_pack.title, members=domain_members) return constructors.serve_json(domain_object)
def serialize_service_discovery( host: watolib.CREHost, discovered_services: CheckTable, discovery_phases: List[str], ): members = {} host_name = host.name() for entry in discovered_services: (table_source, check_type, _checkgroup, item, _discovered_params, _check_params, descr, _service_phase, _output, _perfdata, _service_labels, _found_on_nodes) = entry if _in_phase(table_source, discovery_phases): service_phase = _lookup_phase_name(table_source) members[f"{check_type}-{item}"] = object_property( name=descr, title=f"The service is currently {service_phase!r}", value=service_phase, prop_format='string', linkable=False, extensions={ "host_name": host_name, "check_plugin_name": check_type, "service_name": descr, "service_item": item, "service_phase": service_phase, }, base='', links=[ link_rel( rel="cmk/service.move-monitored", href=update_service_phase.path.format( host_name=host_name), body_params={ 'target_phase': 'monitored', 'check_type': check_type, 'service_item': item, }, method='put', title='Move the service to monitored', ), link_rel( rel="cmk/service.move-undecided", href=update_service_phase.path.format( host_name=host_name), body_params={ 'target_phase': 'undecided', 'check_type': check_type, 'service_item': item, }, method='put', title='Move the service to undecided', ), link_rel( rel="cmk/service.move-ignored", href=update_service_phase.path.format( host_name=host_name), body_params={ 'target_phase': 'ignored', 'check_type': check_type, 'service_item': item, }, method='put', title='Move the service to ignored', ), ], ) return domain_object( domain_type='service_discovery', identifier=f'{host_name}-services-wato', title='Services discovery', members=members, editable=False, deletable=False, extensions={}, )
def serialize_service_discovery( host: CREHost, discovered_services: Sequence[CheckPreviewEntry], discovery_phases: List[str], ): members = {} host_name = host.name() for entry in discovered_services: if _in_phase(entry.check_source, discovery_phases): service_phase = _lookup_phase_name(entry.check_source) members[ f"{entry.check_plugin_name}-{entry.item}"] = object_property( name=entry.description, title=f"The service is currently {service_phase!r}", value=service_phase, prop_format="string", linkable=False, extensions={ "host_name": host_name, "check_plugin_name": entry.check_plugin_name, "service_name": entry.description, "service_item": entry.item, "service_phase": service_phase, }, base="", links=[ link_rel( rel="cmk/service.move-monitored", href=update_service_phase.path.format( host_name=host_name), body_params={ "target_phase": "monitored", "check_type": entry.check_plugin_name, "service_item": entry.item, }, method="put", title="Move the service to monitored", ), link_rel( rel="cmk/service.move-undecided", href=update_service_phase.path.format( host_name=host_name), body_params={ "target_phase": "undecided", "check_type": entry.check_plugin_name, "service_item": entry.item, }, method="put", title="Move the service to undecided", ), link_rel( rel="cmk/service.move-ignored", href=update_service_phase.path.format( host_name=host_name), body_params={ "target_phase": "ignored", "check_type": entry.check_plugin_name, "service_item": entry.item, }, method="put", title="Move the service to ignored", ), ], ) return domain_object( domain_type="service_discovery", identifier=f"{host_name}-services-wato", title="Services discovery", members=members, editable=False, deletable=False, extensions={}, )