def test_servicegroup_service_downtime(mock_livestatus, with_request_context, dates): start_time, end_time = dates with mock_livestatus( expect_status_query=True) as live, application_and_request_context( ), SuperUserContext(): load_config() live.expect_query([ "GET servicegroups", "Columns: members", "Filter: name = example", ]) live.expect_query( "COMMAND [...] SCHEDULE_SVC_DOWNTIME;example.com;Memory;0;86400;16;0;120;;Boom", match_type="ellipsis", ) live.expect_query( "COMMAND [...] SCHEDULE_SVC_DOWNTIME;example.com;CPU load;0;86400;16;0;120;;Boom", match_type="ellipsis", ) live.expect_query( "COMMAND [...] SCHEDULE_SVC_DOWNTIME;heute;CPU load;0;86400;16;0;120;;Boom", match_type="ellipsis", ) downtimes.schedule_servicegroup_service_downtime( sites.live(), "example", start_time, end_time, recur="day_of_month", duration=120, comment="Boom", )
def create_service_related_downtime(params): """Create a service related scheduled downtime""" body = params['body'] live = sites.live() downtime_type: DowntimeType = body['downtime_type'] if downtime_type == 'service': downtime_commands.schedule_service_downtime( live, host_name=body['host_name'], service_description=body['service_descriptions'], start_time=body['start_time'], end_time=body['end_time'], recur=body['recur'], duration=body['duration'], user_id=config.user.ident, comment=body.get( 'comment', f"Downtime for services {', '.join(body['service_descriptions'])!r}@{body['host_name']!r}" ), ) elif downtime_type == 'servicegroup': downtime_commands.schedule_servicegroup_service_downtime( live, servicegroup_name=body['servicegroup_name'], start_time=body['start_time'], end_time=body['end_time'], recur=body['recur'], duration=body['duration'], user_id=config.user.ident, comment=body.get('comment', f"Downtime for servicegroup {body['servicegroup_name']!r}"), ) elif downtime_type == 'service_by_query': try: downtime_commands.schedule_services_downtimes_with_query( live, query=body['query'], start_time=body['start_time'], end_time=body['end_time'], recur=body['recur'], duration=body['duration'], user_id=config.user.ident, comment=body.get('comment', ''), ) except QueryException: return problem( status=422, title="Query did not match any service", detail="The provided query returned an empty list so no downtime was set", ) else: return problem(status=400, title="Unhandled downtime-type.", detail=f"The downtime-type {downtime_type!r} is not supported.") return Response(status=204)
def test_servicegroup_service_downtime_and_hosts(mock_livestatus, with_request_context, dates): start_time, end_time = dates with mock_livestatus(expect_status_query=True) as live: live.expect_query( [ "GET servicegroups", "Columns: members", "Filter: name = example", ] ) live.expect_query( "COMMAND [...] SCHEDULE_SVC_DOWNTIME;example.com;Memory;0;86400;16;0;120;;Boom", match_type="ellipsis", ) live.expect_query( "COMMAND [...] SCHEDULE_SVC_DOWNTIME;example.com;CPU load;0;86400;16;0;120;;Boom", match_type="ellipsis", ) live.expect_query( "COMMAND [...] SCHEDULE_SVC_DOWNTIME;heute;CPU load;0;86400;16;0;120;;Boom", match_type="ellipsis", ) live.expect_query( "GET hosts\nColumns: name\nFilter: name = example.com\nFilter: name = heute\nOr: 2" ) live.expect_query( "COMMAND [...] SCHEDULE_HOST_DOWNTIME;heute;0;86400;16;0;120;;Boom", match_type="ellipsis", ) live.expect_query( "COMMAND [...] SCHEDULE_HOST_DOWNTIME;example.com;0;86400;16;0;120;;Boom", match_type="ellipsis", ) downtimes.schedule_servicegroup_service_downtime( sites.live(), "example", start_time, end_time, include_hosts=True, recur="day_of_month", duration=120, comment="Boom", )
def create_service_related_downtime(params): """Create a service related scheduled downtime""" body = params["body"] live = sites.live() downtime_type: DowntimeType = body["downtime_type"] if downtime_type == "service": host_name = body["host_name"] with detailed_connection(live) as conn: site_id = Query(columns=[Hosts.name], filter_expr=Hosts.name.op("=", host_name)).value( conn ) downtime_commands.schedule_service_downtime( live, site_id, host_name=body["host_name"], service_description=body["service_descriptions"], start_time=body["start_time"], end_time=body["end_time"], recur=body["recur"], duration=body["duration"], user_id=user.ident, comment=body.get( "comment", f"Downtime for services {', '.join(body['service_descriptions'])!r}@{body['host_name']!r}", ), ) elif downtime_type == "servicegroup": downtime_commands.schedule_servicegroup_service_downtime( live, servicegroup_name=body["servicegroup_name"], start_time=body["start_time"], end_time=body["end_time"], recur=body["recur"], duration=body["duration"], user_id=user.ident, comment=body.get("comment", f"Downtime for servicegroup {body['servicegroup_name']!r}"), ) elif downtime_type == "service_by_query": try: downtime_commands.schedule_services_downtimes_with_query( live, query=body["query"], start_time=body["start_time"], end_time=body["end_time"], recur=body["recur"], duration=body["duration"], user_id=user.ident, comment=body.get("comment", ""), ) except QueryException: return problem( status=422, title="Query did not match any service", detail="The provided query returned an empty list so no downtime was set", ) else: return problem( status=400, title="Unhandled downtime-type.", detail=f"The downtime-type {downtime_type!r} is not supported.", ) return Response(status=204)