def schedule_services_downtimes_with_query( connection, query: QueryExpression, start_time: dt.datetime, end_time: dt.datetime, recur: RecurMode = 'fixed', duration: int = 0, user_id: str = '', comment: str = '', ): """Schedule downtimes for services based upon a query""" q = Query( [Services.description, Services.host_name], query, ) for host_name, service_description in [ (row['host_name'], row['description']) for row in q.iterate(connection) ]: if not comment: downtime_comment = f"Downtime for service {service_description}@{host_name}" else: downtime_comment = comment schedule_service_downtime( connection, host_name=host_name, service_description=service_description, start_time=start_time, end_time=end_time, recur=recur, duration=duration, user_id=user_id, comment=downtime_comment, )
def list_service_downtimes(param): """Show all scheduled downtimes""" live = sites.live() q = Query([ Downtimes.id, Downtimes.host_name, Downtimes.service_description, Downtimes.is_service, Downtimes.author, Downtimes.start_time, Downtimes.end_time, Downtimes.recurring, Downtimes.comment, ]) host_name = param.get('host_name') if host_name is not None: q = q.filter(Downtimes.host_name.contains(host_name)) service_description = param.get('service_description') if service_description is not None: q = q.filter( Downtimes.service_description.contains(service_description)) gen_downtimes = q.iterate(live) return _serve_downtimes(gen_downtimes)
def _list_services(param): live = sites.live() columns = verify_columns(Services, param['columns']) q = Query(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=entry['description'], editable=False, deletable=False, extensions=entry, ) for entry in result ], ))
def list_hosts(param): """Show hosts of specific condition""" live = sites.live() sites_to_query = param['sites'] if sites_to_query: live.only_sites = sites_to_query columns = verify_columns(Hosts, param['columns']) q = Query(columns) # TODO: add sites parameter 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='host', value=[ constructors.domain_object( domain_type='host', title=f"{entry['name']}", identifier=entry['name'], editable=False, deletable=False, extensions=entry, ) for entry in result ], ))
def schedule_hosts_downtimes_with_query( connection, query: QueryExpression, start_time: dt.datetime, end_time: dt.datetime, include_all_services=False, recur: RecurMode = 'fixed', duration: int = 0, user_id: str = '', comment: str = '', ): """Schedule a downtimes for hosts based upon a query""" q = Query([Hosts.name]).filter(query) hosts = [row['name'] for row in q.iterate(connection)] if not comment: comment = f"Downtime for hosts {', '.join(hosts)}" schedule_host_downtime( connection, host_name=hosts, start_time=start_time, end_time=end_time, include_all_services=include_all_services, recur=recur, duration=duration, user_id=user_id, comment=comment, )
def schedule_hosts_downtimes_with_query( connection, query: str, start_time: dt.datetime, end_time: dt.datetime, recur: RecurMode = 'fixed', duration: int = 0, user_id: str = '', comment: str = '', ): """Schedule a downtimes for hosts based upon a query""" q = Query([Hosts.name]).filter(tree_to_expr(query, Hosts.__tablename__)) hosts = [row['name'] for row in q.iterate(connection)] if not comment: comment = f"Downtime for hosts {', '.join(hosts)}" schedule_host_downtime( connection, host_name=hosts, start_time=start_time, end_time=end_time, recur=recur, duration=duration, user_id=user_id, comment=comment, )
def _set_acknowlegement_on_queried_hosts( connection, query: str, sticky: bool, notify: bool, persistent: bool, comment: str, ): q = Query([Hosts.name, Hosts.state]).filter(tree_to_expr(query, Hosts.__tablename__)) hosts = list(q.iterate(connection)) if not hosts: return problem(status=404, title="The provided query returned no monitored hosts") for host in hosts: if host.state == 0: continue acknowledge_host_problem( connection, host.name, sticky=sticky, notify=notify, persistent=persistent, user=_user_id(), comment=comment, ) return http.Response(status=204)
def delete_downtime_with_query(connection, query): """Delete scheduled downtimes based upon a query""" q = Query([Downtimes.id, Downtimes.is_service]).filter(query) for downtime_id, is_service in [(row['id'], row['is_service']) for row in q.iterate(connection)]: if is_service: del_service_downtime(connection, downtime_id) else: del_host_downtime(connection, downtime_id)
def bulk_set_acknowledgement_on_host_service(params): """Bulk Acknowledge specific services on specific host""" live = sites.live() body = params['body'] host_name = body['host_name'] entries = body.get('entries', []) query = Query([Services.description, Services.state], And( Services.host_name.equals(host_name), Or(*[ Services.description.equals(service_description) for service_description in entries ]))) services = query.to_dict(live) not_found = [] for service_description in entries: if service_description not in services: not_found.append(service_description) if not_found: return problem( status=400, title= f"Services {', '.join(not_found)} not found on host {host_name}", detail='Currently not monitored') up_services = [] for service_description in entries: if services[service_description] == 0: up_services.append(service_description) if up_services: return problem( status=400, title=f"Services {', '.join(up_services)} do not have a problem", detail="The states of these services are OK") for service_description in entries: acknowledge_service_problem( sites.live(), host_name, service_description, sticky=body.get('sticky', False), notify=body.get('notify', False), persistent=body.get('persistent', False), user=str(config.user.id), comment=body.get('comment', 'Acknowledged'), ) return http.Response(status=204)
def set_acknowledgement_on_service_related(params): """Set acknowledgement on related services""" body = params['body'] live = sites.live() sticky = body['sticky'] notify = body['notify'] persistent = body['persistent'] comment = body['comment'] acknowledge_type = body['acknowledge_type'] if acknowledge_type == 'service': return _set_acknowledgement_for_service( live, unquote(body['service_description']), sticky, notify, persistent, comment, ) if acknowledge_type == 'servicegroup': acknowledge_servicegroup_problem( live, body['servicegroup_name'], sticky=sticky, notify=notify, persistent=persistent, user=_user_id(), comment=comment, ) return http.Response(status=204) if acknowledge_type == 'service_by_query': q = Query([Services.host_name, Services.description, Services.state]).filter( tree_to_expr(body['query'], Services.__tablename__)) return _set_acknowledgement_on_queried_services( live, [(row['host_name'], row['description']) for row in q.iterate(live) if row['state'] > 0], sticky=sticky, notify=notify, persistent=persistent, comment=comment, ) return problem( status=400, title="Unhandled acknowledge-type.", detail=f"The acknowledge-type {acknowledge_type!r} is not supported.")
def delete_downtime(params): live = sites.live() q = Query([Downtimes.id, Downtimes.is_service]) q = q.filter(Downtimes.id.contains(params['downtime_id'])) gen_downtime = q.iterate(live) downtime_info = next(gen_downtime) downtime_type = "SVC" if downtime_info['is_service'] else "HOST" command_delete = remove_downtime_command(downtime_type, params['downtime_id']) execute_livestatus_command(command_delete, params['host_name']) return Response(status=204)
def set_acknowledgement_on_host(params): """Acknowledge for a specific host""" host_name = params['host_name'] host = Query([Hosts.name, Hosts.state], Hosts.name.equals(host_name)).first(sites.live()) if host is None: return problem( status=404, title=f'Host {host_name} does not exist.', detail='It is not currently monitored.', ) if host.state == 0: return problem(status=400, title=f"Host {host_name} does not have a problem.", detail="The state is UP.") acknowledge_host_problem( sites.live(), host_name, sticky=bool(params.get('sticky')), notify=bool(params.get('notify')), persistent=bool(params.get('persistent')), user=_user_id(), comment=params.get('comment', 'Acknowledged'), ) return http.Response(status=204)
def set_acknowledgement_on_host_service(params): """Acknowledge for services on a host""" host_name = params['host_name'] service_description = unquote(params['service_description']) body = params['body'] service = Query([Services.description, Services.state], And(Services.description.equals(service_description), Services.host_name.equals(host_name))).first(sites.live()) if service is None: return problem( status=404, title=f'Service {service_description!r} on host {host_name!r} does not exist.', detail='It is not currently monitored.', ) if service.state == 0: return problem(status=400, title=f"Service {service_description!r} does not have a problem.", detail="The state is OK.") acknowledge_service_problem( sites.live(), host_name, service_description, sticky=body.get('sticky', False), notify=body.get('notify', False), persistent=body.get('persistent', False), user=_user_id(), comment=body.get('comment', 'Acknowledged'), ) return http.Response(status=204)
def bulk_delete_downtimes(params): """Bulk delete downtimes""" live = sites.live() entries = params['entries'] not_found = [] downtimes: Dict[int, int] = Query( [Downtimes.id, Downtimes.is_service], And(*[Downtimes.id.equals(downtime_id) for downtime_id in entries]), ).to_dict(live) for downtime_id in entries: if downtime_id not in downtimes: not_found.append(downtime_id) if not_found: raise ProblemException(404, http.client.responses[400], f"Downtimes {', '.join(not_found)} not found") for downtime_id, is_service in downtimes.items(): if is_service: downtime_commands.del_service_downtime(live, downtime_id) else: downtime_commands.del_host_downtime(live, downtime_id) return Response(status=204)
def _set_acknowledgement_on_host( connection, host_name: str, sticky: bool, notify: bool, persistent: bool, comment: str, ): """Acknowledge for a specific host""" host = Query([Hosts.name, Hosts.state], Hosts.name.equals(host_name)).first(connection) if host is None: return problem( status=404, title=f'Host {host_name} does not exist.', detail='It is not currently monitored.', ) if host.state == 0: return problem(status=400, title=f"Host {host_name} does not have a problem.", detail="The state is UP.") acknowledge_host_problem( sites.live(), host_name, sticky=sticky, notify=notify, persistent=persistent, user=_user_id(), comment=comment, ) return http.Response(status=204)
def acknowledge_hostgroup_problem( connection, hostgroup_name: str, sticky: bool = False, notify: bool = False, persistent: bool = False, user: str = "", comment: str = "", ): """Acknowledge the problems of the current hosts of the hostgroup When acknowledging a problem, further notifications for the respective services are disabled, as long as a specific service doesn't change state. At state change, notifications are re-enabled. Args: connection: A livestatus connection object. hostgroup_name: The name of the host group. sticky: If set, only a state-change of the service to an OK state will discard the acknowledgement. Otherwise it will be discarded on any state-change. Defaults to False. notify: If set, notifications will be sent out to the configured contacts. Defaults to False. persistent: If set, the comment will persist a restart. Defaults to False. user: comment: If set, this comment will be stored alongside the acknowledgement. Raises: ValueError: when the Hostgroup in question doesn't exist. """ members: List[str] = Query( [tables.Hostgroups.members], tables.Hostgroups.name.equals(hostgroup_name)).value(connection) acknowledgement = 2 if sticky else 1 # 1: normal, 2: sticky for host_name in members: send_command( connection, "ACKNOWLEDGE_HOST_PROBLEM", [ host_name, acknowledgement, int(notify), int(persistent), user, comment, ], )
def delete_downtime(params): """Delete downtime""" is_service = Query( [Downtimes.is_service], Downtimes.id.contains(params['downtime_id']), ).value(sites.live()) downtime_type = "SVC" if is_service else "HOST" command_delete = remove_downtime_command(downtime_type, params['downtime_id']) execute_livestatus_command(command_delete, params['host_name']) return Response(status=204)
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 delete_downtime(connection, downtime_id): """Delete a scheduled downtime based upon the downtime id""" is_service = Query( [Downtimes.is_service], Downtimes.id.contains(downtime_id), ).value(connection) if is_service: del_service_downtime(connection, downtime_id) else: del_host_downtime(connection, downtime_id)
def show_downtimes(param): """Show all scheduled downtimes""" live = sites.live() sites_to_query = param.get('sites') if sites_to_query: live.only_sites = sites_to_query q = Query([ Downtimes.id, Downtimes.host_name, Downtimes.service_description, Downtimes.is_service, Downtimes.author, Downtimes.start_time, Downtimes.end_time, Downtimes.recurring, Downtimes.comment, ]) filter_tree = param.get('query') host_name = param.get('host_name') service_description = param.get('service_description') if filter_tree is not None: expr = tree_to_expr(filter_tree) q = q.filter(expr) if host_name is not None: q = q.filter(Downtimes.host_name.contains(host_name)) if service_description is not None: q = q.filter( Downtimes.service_description.contains(service_description)) gen_downtimes = q.iterate(live) return _serve_downtimes(gen_downtimes)
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 _set_acknowledgement_for_service( connection, service_description: str, sticky: bool, notify: bool, persistent: bool, comment: str, ): q = Query([Services.host_name, Services.description, Services.state]).filter( tree_to_expr({ 'op': '=', 'left': 'services.description', 'right': service_description })) services = list(q.iterate(connection)) if not len(services): return problem( status=404, title=f'No services with {service_description!r} were found.', ) for service in services: if service.state == 0: continue acknowledge_service_problem( connection, service.host_name, service.description, sticky=sticky, notify=notify, persistent=persistent, user=_user_id(), comment=comment, ) return http.Response(status=204)
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 bulk_set_acknowledgement_on_hosts(params): """Bulk acknowledge for hosts""" live = sites.live() entries = params['entries'] hosts: Dict[str, int] = { host_name: host_state for host_name, host_state in Query( # pylint: disable=unnecessary-comprehension [Hosts.name, Hosts.state], And(*[Hosts.name.equals(host_name) for host_name in entries]), ).fetch_values(live) } not_found = [] for host_name in entries: if host_name not in hosts: not_found.append(host_name) if not_found: return problem(status=400, title=f"Hosts {', '.join(not_found)} not found", detail='Current not monitored') up_hosts = [] for host_name in entries: if hosts[host_name] == 0: up_hosts.append(host_name) if up_hosts: return problem( status=400, title=f"Hosts {', '.join(up_hosts)} do not have a problem", detail="The states of these hosts are UP") for host_name in entries: acknowledge_host_problem( sites.live(), host_name, sticky=params.get('sticky'), notify=params.get('notify'), persistent=params.get('persistent'), user=_user_id(), comment=params.get('comment', 'Acknowledged'), ) return http.Response(status=204)
def create_url(site: SiteId, query: Query) -> str: """Create a REST-API query URL. Examples: >>> create_url('heute', ... Query.from_string("GET hosts\\nColumns: name\\nFilter: name = heute")) '/heute/check_mk/api/v0/domain-types/host/collections/all?query=%7B%22op%22%3A+%22%3D%22%2C+%22left%22%3A+%22hosts.name%22%2C+%22right%22%3A+%22heute%22%7D' Args: site: A valid site-name. query: The Query() instance which the endpoint shall create again. Returns: The URL. Raises: A ValueError when no URL could be created. """ table = cast(str, query.table.__tablename__) try: domain_type = { 'hosts': 'host', 'services': 'service', }[table] except KeyError: raise ValueError(f"Could not find a domain-type for table {table}.") url = f"/{site}/check_mk/api/v0/domain-types/{domain_type}/collections/all" query_dict = query.dict_repr() if query_dict: query_string_value = quote_plus(json.dumps(query_dict)) url += f"?query={query_string_value}" return url
def set_acknowledgement_for_service(params): """Acknowledge for a service globally""" service_description = unquote(params['service_description']) body = params['body'] live = sites.live() services = Query( [Services.host_name, Services.description], And( Services.description.equals(service_description), Or( Services.state == 1, Services.state == 2, ), ), ).fetch_values(live) if not len(services): return problem( status=400, title=f'No services {service_description!r} with problems found.', detail='All services are OK.', ) for _host_name, _service_description in services: acknowledge_service_problem( live, _host_name, _service_description, sticky=body.get('sticky', False), notify=body.get('notify', False), persistent=body.get('persistent', False), user=_user_id(), comment=body.get('comment', 'Acknowledged'), ) return http.Response(status=204)
def schedule_host_downtime( connection, host_name: Union[List[str], str], start_time: dt.datetime, end_time: dt.datetime, include_all_services: bool = False, recur: RecurMode = 'fixed', trigger_id: int = 0, duration: int = 0, user_id: str = '', comment: str = '', ): """Schedule the downtime of a host. Notes: If `include_all_services` is set to True, the services table is only queried once, instead of len(host_name) times. If a lot of hosts are to be scheduled, this will save N queries. Issuing the command is still done sequentially. Args: connection: A livestatus connection object. host_name: The host-name for which this downtime is for. start_time: When the downtime shall begin. end_time: When the downtime shall end. include_all_services: If set, downtimes for all services associated with the given host will be scheduled. Defaults to False. recur: The recurring mode of the new downtime. Available modes are: * fixed * hour * day * week * second_week * fourth_week * weekday_start * weekday_end * day_of_month This only works when using CMC. Defaults to 'fixed'. trigger_id: The id of another downtime-entry. If given (other than 0) then this downtime will be triggered by the other downtime. duration: Duration in seconds. When set, the downtime does not begin automatically at a nominated time, but when a real problem status appears for the host. Consequencely, the start_time/end_time is only the time window in which the scheduled downtime can begin. user_id: comment: A comment which will be added to the downtime. See Also: * https://assets.nagios.com/downloads/nagioscore/docs/externalcmds/cmdinfo.php?command_id=118 * https://assets.nagios.com/downloads/nagioscore/docs/externalcmds/cmdinfo.php?command_id=122 Examples: >>> import pytz >>> _start_time = dt.datetime(1970, 1, 1, tzinfo=pytz.timezone("UTC")) >>> _end_time = dt.datetime(1970, 1, 2, tzinfo=pytz.timezone("UTC")) >>> from cmk.gui.plugins.openapi.livestatus_helpers.testing import simple_expect >>> cmd = "COMMAND [...] SCHEDULE_HOST_DOWNTIME;example.com;0;86400;17;0;120;;Boom" >>> with simple_expect(cmd) as live: ... schedule_host_downtime(live, ... 'example.com', ... _start_time, ... _end_time, ... recur="day_of_month", ... duration=120, ... comment="Boom") """ if isinstance(host_name, str): host_names = [host_name] else: host_names = host_name for _host_name in host_names: _schedule_downtime( connection, "SCHEDULE_HOST_DOWNTIME", _host_name, None, start_time, end_time, recur, trigger_id, duration, user_id, comment, ) if include_all_services: services = Query( [tables.Services.host_name, tables.Services.description], Or(*[ tables.Services.host_name.equals(_host_name) for _host_name in host_names ])).fetch_values(connection) for _host_name, service_description in services: schedule_service_downtime( connection, host_name=_host_name, service_description=service_description, start_time=start_time, end_time=end_time, recur=recur, trigger_id=trigger_id, duration=duration, user_id=user_id, comment=comment, )
def set_acknowledgement_on_services(params): """Set acknowledgement on related services""" body = params['body'] live = sites.live() sticky = body['sticky'] notify = body['notify'] persistent = body['persistent'] comment = body['comment'] acknowledge_type = body['acknowledge_type'] if acknowledge_type == 'service': description = unquote(body['service_description']) host_name = body['host_name'] service = Query( [Services.host_name, Services.description, Services.state], And(Services.host_name == host_name, Services.description == description)).first(live) if not service: raise ProblemException( status=404, title= f'Service {description!r}@{host_name!r} could not be found.', ) if not service.state: raise ProblemException( status=422, title=f'Service {description!r}@{host_name!r} has no problem.', ) acknowledge_service_problem( live, service.host_name, service.description, sticky=sticky, notify=notify, persistent=persistent, user=config.user.ident, comment=comment, ) elif acknowledge_type == 'servicegroup': service_group = body['servicegroup_name'] try: acknowledge_servicegroup_problem( live, service_group, sticky=sticky, notify=notify, persistent=persistent, user=config.user.ident, comment=comment, ) except ValueError: raise ProblemException( status=404, title="Servicegroup could not be found.", detail=f"Unknown servicegroup: {service_group}", ) elif acknowledge_type == 'service_by_query': services = Query( [Services.host_name, Services.description, Services.state], body['query'], ).fetchall(live) if not services: raise ProblemException( status=422, title='No services with problems found.', detail='All queried services are OK.', ) for service in services: if not service.state: continue acknowledge_service_problem( live, service.host_name, service.description, sticky=sticky, notify=notify, persistent=persistent, user=config.user.ident, comment=comment, ) else: raise ProblemException( status=400, title="Unhandled acknowledge-type.", detail= f"The acknowledge-type {acknowledge_type!r} is not supported.", ) return http.Response(status=204)
def set_acknowledgement_on_hosts(params): """Set acknowledgement on related hosts""" body = params['body'] live = sites.live() sticky = body['sticky'] notify = body['notify'] persistent = body['persistent'] comment = body['comment'] acknowledge_type = body['acknowledge_type'] if acknowledge_type == 'host': name = body['host_name'] host_state = Query([Hosts.state], Hosts.name == name).value(live) if not host_state: raise ProblemException( status=422, title=f'Host {name!r} has no problem.', ) acknowledge_host_problem( live, name, sticky=sticky, notify=notify, persistent=persistent, user=config.user.ident, comment=comment, ) elif acknowledge_type == 'hostgroup': host_group = body['hostgroup_name'] try: acknowledge_hostgroup_problem( live, host_group, sticky=sticky, notify=notify, persistent=persistent, user=config.user.ident, comment=comment, ) except ValueError: raise ProblemException( 404, title="Hostgroup could not be found.", detail=f"Unknown hostgroup: {host_group}", ) elif acknowledge_type == 'host_by_query': query = body['query'] hosts = Query([Hosts.name], query).fetchall(live) if not hosts: raise ProblemException( status=422, title="The provided query returned no monitored hosts", ) for host in hosts: acknowledge_host_problem( live, host.name, sticky=sticky, notify=notify, persistent=persistent, user=config.user.ident, comment=comment, ) else: raise ProblemException( status=400, title="Unhandled acknowledge-type.", detail= f"The acknowledge-type {acknowledge_type!r} is not supported.", ) return http.Response(status=204)