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 hosts: raise QueryException if not comment: comment = f"Downtime for hosts {', '.join([entry[1] for entry in hosts])}" schedule_host_downtime( connection, host_entry=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 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 delete_downtime_with_query(connection, query): """Delete scheduled downtimes based upon a query""" q = Query([Downtimes.id, Downtimes.is_service]).filter(query) with detailed_connection(connection) as conn: downtimes = [(row["site"], row["id"], row["is_service"]) for row in q.iterate(conn)] for site_id, downtime_id, is_service in downtimes: if is_service: del_service_downtime(connection, downtime_id, site_id) else: del_host_downtime(connection, downtime_id, site_id)
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 = 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 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, ) with detailed_connection(connection) as conn: result = [(row["site"], row["host_name"], row["description"]) for row in q.iterate(conn)] if not result: raise QueryException for site_id, host_name, service_description in result: if not comment: downtime_comment = f"Downtime for service {service_description}@{host_name}" else: downtime_comment = comment schedule_service_downtime( connection, site_id=site_id, 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 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, ) result = [(row['host_name'], row['description']) for row in q.iterate(connection)] if not result: raise QueryException for host_name, service_description in result: 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 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, ]) query_expr = param.get('query') if query_expr is not None: q = q.filter(query_expr) host_name = param.get('host_name') if host_name is not None: q = q.filter( And(Downtimes.host_name.op("=", host_name), Downtimes.is_service.equals(0))) 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)