def _set_acknowledgement_on_queried_services( connection, services: List[Tuple[str, str]], sticky: bool, notify: bool, persistent: bool, comment: str, ): if not len(services): return problem( status=400, title='No services with problems found.', detail='All queried services are OK.', ) for host_name, service_description in services: acknowledge_service_problem( connection, host_name, service_description, sticky=sticky, notify=notify, persistent=persistent, user=_user_id(), comment=comment, ) 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_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_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 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 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)