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 _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 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 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 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)