Ejemplo n.º 1
0
def test_host_downtime_with_services(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 hosts\nColumns: name\nFilter: name = example.com")
        live.expect_query(
            "COMMAND [...] SCHEDULE_HOST_DOWNTIME;example.com;0;86400;12;0;120;;Going down",
            match_type="ellipsis",
        )
        live.expect_query(
            "GET services\nColumns: host_name description\nFilter: host_name = example.com",
        )
        live.expect_query(
            "COMMAND [...] SCHEDULE_SVC_DOWNTIME;example.com;Memory;0;86400;12;0;120;;Going down",
            match_type="ellipsis",
        )
        live.expect_query(
            "COMMAND [...] SCHEDULE_SVC_DOWNTIME;example.com;CPU load;0;86400;12;0;120;;Going down",
            match_type="ellipsis",
        )
        downtimes.schedule_host_downtime(
            sites.live(),
            "example.com",
            start_time,
            end_time,
            include_all_services=True,
            recur="weekday_start",
            duration=120,
            comment="Going down",
        )
Ejemplo n.º 2
0
def create_host_related_downtime(params):
    """Create a host related scheduled downtime"""
    body = params["body"]
    live = sites.live()

    downtime_type: DowntimeType = body["downtime_type"]

    if downtime_type == "host":
        downtime_commands.schedule_host_downtime(
            live,
            host_entry=body["host_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 host {body['host_name']!r}"),
        )
    elif downtime_type == "hostgroup":
        downtime_commands.schedule_hostgroup_host_downtime(
            live,
            hostgroup_name=body["hostgroup_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 hostgroup {body['hostgroup_name']!r}"),
        )

    elif downtime_type == "host_by_query":
        try:
            downtime_commands.schedule_hosts_downtimes_with_query(
                live,
                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 host",
                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)
Ejemplo n.º 3
0
def test_host_downtime_with_services(mock_livestatus, with_request_context,
                                     dates):
    start_time, end_time = dates

    with mock_livestatus(expect_status_query=True) as live:
        live.expect_query(
            'COMMAND [...] SCHEDULE_HOST_DOWNTIME;example.com;0;86400;12;0;120;;Going down',
            match_type='ellipsis',
        )
        live.expect_query(
            'GET services\nColumns: host_name description\nFilter: host_name = example.com',
        )
        live.expect_query(
            'COMMAND [...] SCHEDULE_SVC_DOWNTIME;example.com;Memory;0;86400;12;0;120;;Going down',
            match_type='ellipsis',
        )
        live.expect_query(
            'COMMAND [...] SCHEDULE_SVC_DOWNTIME;example.com;CPU load;0;86400;12;0;120;;Going down',
            match_type='ellipsis',
        )
        downtimes.schedule_host_downtime(
            sites.live(),
            'example.com',
            start_time,
            end_time,
            include_all_services=True,
            recur='weekday_start',
            duration=120,
            comment='Going down',
        )
Ejemplo n.º 4
0
def create_host_related_downtime(params):
    """Create a host related scheduled downtime"""
    body = params['body']
    live = sites.live()

    downtime_type: DowntimeType = body['downtime_type']

    if downtime_type == 'host':
        downtime_commands.schedule_host_downtime(
            live,
            host_name=body['host_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 host {body['host_name']!r}"),
        )
    elif downtime_type == 'hostgroup':
        downtime_commands.schedule_hostgroup_host_downtime(
            live,
            hostgroup_name=body['hostgroup_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 hostgroup {body['hostgroup_name']!r}"),
        )

    elif downtime_type == 'host_by_query':
        try:
            downtime_commands.schedule_hosts_downtimes_with_query(
                live,
                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 host",
                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)
Ejemplo n.º 5
0
def test_host_downtime(mock_livestatus, with_request_context, dates):
    start_time, end_time = dates

    with mock_livestatus(expect_status_query=True) as live:
        live.expect_query(
            'COMMAND [...] SCHEDULE_HOST_DOWNTIME;example.com;0;86400;12;0;120;;Going down',
            match_type='ellipsis',
        )
        downtimes.schedule_host_downtime(
            sites.live(),
            'example.com',
            start_time,
            end_time,
            recur='weekday_start',
            duration=120,
            comment='Going down',
        )
Ejemplo n.º 6
0
def test_host_downtime(mock_livestatus, with_request_context, dates):
    start_time, end_time = dates

    with mock_livestatus(expect_status_query=True) as live:
        live.expect_query("GET hosts\nColumns: name\nFilter: name = example.com")
        live.expect_query(
            "COMMAND [...] SCHEDULE_HOST_DOWNTIME;example.com;0;86400;12;0;120;;Going down",
            match_type="ellipsis",
        )
        downtimes.schedule_host_downtime(
            sites.live(),
            "example.com",
            start_time,
            end_time,
            recur="weekday_start",
            duration=120,
            comment="Going down",
        )