def _folders_collection( folders: List[CREFolder], show_hosts: bool, ): folders_ = [] for folder in folders: members = {} if show_hosts: members["hosts"] = constructors.object_collection( name="hosts", domain_type="folder_config", entries=[ constructors.collection_item("host_config", { "title": host, "id": host }) for host in folder.hosts() ], base="", ) folders_.append( constructors.domain_object( domain_type="folder_config", identifier=folder_slug(folder), title=folder.title(), extensions={ "path": "/" + folder.path(), "attributes": folder.attributes().copy(), }, members=members, )) return constructors.collection_object( domain_type="folder_config", value=folders_, )
def get_bi_pack(params): """Get BI Pack""" 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_downtimes(downtimes): entries = [] for downtime_info in downtimes: service_description = downtime_info.get("service_description") if service_description: downtime_detail = "service: %s" % service_description else: downtime_detail = "host: %s" % downtime_info["host_name"] downtime_id = downtime_info['id'] entries.append( constructors.domain_object( domain_type='downtime', identifier=downtime_id, title='Downtime for %s' % downtime_detail, extensions=_downtime_properties(downtime_info), links=[ constructors.link_rel( rel='.../delete', href='/objects/host/%s/objects/downtime/%s' % (downtime_info['host_name'], downtime_id), method='delete', title='Delete the downtime'), ])) return constructors.object_collection( name='all', domain_type='downtime', entries=entries, base='', )
def _serialize_downtimes(downtimes): entries = [] for downtime in downtimes: entries.append(_serialize_single_downtime(downtime)) return constructors.object_collection( name='all', domain_type='downtime', entries=entries, base='', )
def _list_services(param): live = sites.live() default_columns = [ 'host_name', 'description', 'last_check', 'state', 'state_type', 'acknowledged', ] column_names = add_if_missing(param.get('columns', default_columns), ['host_name', 'description']) columns = verify_columns(Services, column_names) q = Query(columns) host_name = param.get('host_name') if host_name is not None: q = q.filter(Services.host_name.contains(host_name)) alias = param.get('host_alias') if alias is not None: q = q.filter(Services.host_alias.contains(alias)) in_downtime = param.get('in_downtime') if in_downtime is not None: q = q.filter(Services.scheduled_downtime_depth == int(in_downtime)) acknowledged = param.get('acknowledged') if acknowledged is not None: q = q.filter(Services.acknowledged.equals(acknowledged)) status = param.get('status') if status is not None: q = q.filter(Services.state.equals(status)) result = q.iterate(live) return constructors.object_collection( name='all', domain_type='service', entries=[ constructors.domain_object( domain_type='service', title=f"{entry['description']} on {entry['host_name']}", identifier=entry['description'], editable=False, deletable=False, extensions=entry, ) for entry in result ], base='', )
def list_hosts(param): """List currently monitored hosts.""" live = sites.live() default_columns = [ 'name', 'address', 'alias', 'downtimes_with_info', 'scheduled_downtime_depth', ] column_names = add_if_missing(param.get('columns', default_columns), ['name', 'address']) columns = verify_columns(Hosts, column_names) q = Query(columns) host_name = param.get('host_name') if host_name is not None: q = q.filter(Hosts.name.contains(host_name)) host_alias = param.get('host_alias') if host_alias is not None: q = q.filter(Hosts.alias.contains(host_alias)) in_downtime = param.get('in_downtime') if in_downtime is not None: q = q.filter(Hosts.scheduled_downtime_depth == int(in_downtime)) acknowledged = param.get('acknowledged') if acknowledged is not None: q = q.filter(Hosts.acknowledged.equals(acknowledged)) status = param.get('status') if status is not None: q = q.filter(Hosts.state.equals(status)) result = q.iterate(live) return constructors.object_collection( name='all', domain_type='host', entries=[ constructors.domain_object( domain_type='host', title=f"{entry['name']} ({entry['address']})", identifier=entry['name'], editable=False, deletable=False, extensions=entry, ) for entry in result ], base='', )
def _list_services(param): live = sites.live() q = Query([ Services.host_name, Services.description, Services.last_check, Services.state, Services.state_type, Services.acknowledged, ]) host_name = param.get('host_name') if host_name is not None: q = q.filter(Services.host_name.contains(host_name)) alias = param.get('host_alias') if alias is not None: q = q.filter(Services.host_alias.contains(alias)) in_downtime = param.get('in_downtime') if in_downtime is not None: q = q.filter(Services.scheduled_downtime_depth == int(in_downtime)) acknowledged = param.get('acknowledged') if acknowledged is not None: q = q.filter(Services.acknowledged.equals(acknowledged)) status = param.get('status') if status is not None: q = q.filter(Services.state.equals(status)) result = q.iterate(live) return constructors.object_collection( name='all', domain_type='service', entries=[ constructors.domain_object( domain_type='service', title=f"{entry['description']} on {entry['host_name']}", identifier=entry['description'], editable=False, deletable=False, extensions=dict(entry), ) for entry in result ], base='', )
def get_bi_pack(params): """Get a BI pack and its rules and aggregations""" user.need_permission("wato.bi_rules") 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("This pack_id does not exist: %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() 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, element.id # type: ignore[arg-type] ), ) for element in elements ], base=uri, ) extensions = { "title": bi_pack.title, "contact_groups": bi_pack.contact_groups, "public": bi_pack.public, } domain_object = constructors.domain_object( domain_type="bi_pack", identifier=bi_pack.id, title=bi_pack.title, extensions=extensions, members=domain_members, ) return constructors.serve_json(domain_object)
def list_hosts(param): live = sites.live() q = Query([ Hosts.name, Hosts.address, Hosts.alias, Hosts.downtimes_with_info, Hosts.scheduled_downtime_depth, ]) hostname = param.get('host_name') if hostname is not None: q = q.filter(Hosts.name.contains(hostname)) alias = param.get('host_alias') if alias is not None: q = q.filter(Hosts.alias.contains(alias)) in_downtime = param.get('in_downtime') if in_downtime is not None: q = q.filter(Hosts.scheduled_downtime_depth == int(in_downtime)) acknowledged = param.get('acknowledged') if acknowledged is not None: q = q.filter(Hosts.acknowledged.equals(acknowledged)) status = param.get('status') if status is not None: q = q.filter(Hosts.state.equals(status)) result = q.iterate(live) return constructors.object_collection( name='all', domain_type='host', entries=[ constructors.domain_object( domain_type='host', title=f"{entry['name']} ({entry['address']})", identifier=entry['name'], editable=False, deletable=False, ) for entry in result ], base='', )
def _serialize_folder(folder): # type: (CREFolder) -> DomainObject uri = '/objects/folder/%s' % (folder.id(), ) return constructors.domain_object( domain_type='folder', identifier=folder.id(), title=folder.title(), members={ 'hosts': constructors.object_collection( name='hosts', entries=[ constructors.link_rel( rel='.../value;collection="items"', href=constructors.object_href('host', 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", ), ]), ), 'title': constructors.object_property( name='title', value=folder.title(), prop_format='string', base=uri, ), }, extensions={ 'attributes': folder.attributes(), }, )
def _folders_collection( folders: List[CREFolder], show_hosts: bool, ): folders_ = [] for folder in folders: if show_hosts: folders_.append( constructors.domain_object( domain_type='folder_config', identifier=folder.id(), title=folder.title(), extensions={ 'attributes': folder.attributes().copy(), }, members={ 'hosts': constructors.object_collection( name='hosts', domain_type='folder_config', entries=[ constructors.collection_item( "host_config", { "title": host, "id": host }) for host in folder.hosts() ], base="", ) }, )) folders_.append( constructors.collection_item( domain_type='folder_config', obj={ 'title': folder.title(), 'id': folder.id() }, )) return constructors.collection_object( domain_type='folder_config', value=folders_, )
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(), }, )