def test_hostgroup_host_downtime(mock_livestatus, register_builtin_html,
                                 dates):
    start_time, end_time = dates

    with mock_livestatus(expect_status_query=True) as live:
        live.expect_query([
            "GET hostgroups",
            "Columns: members",
            "Filter: name = example",
        ])
        live.expect_query(
            'COMMAND [...] SCHEDULE_HOST_DOWNTIME;example.com;0;86400;17;0;120;;Boom',
            match_type='ellipsis',
        )
        live.expect_query(
            'COMMAND [...] SCHEDULE_HOST_DOWNTIME;heute;0;86400;17;0;120;;Boom',
            match_type='ellipsis',
        )

        downtimes.schedule_hostgroup_host_downtime(
            sites.live(),
            'example',
            start_time,
            end_time,
            recur="day_of_month",
            duration=120,
            comment="Boom",
        )
Example #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_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':
        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', ''),
        )
    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_hostgroup_host_downtime_with_services(mock_livestatus,
                                               register_builtin_html, dates):
    start_time, end_time = dates

    with mock_livestatus(expect_status_query=True) as live:
        live.expect_query([
            "GET hostgroups",
            "Columns: members",
            "Filter: name = example",
        ])
        live.expect_query(
            'COMMAND [...] SCHEDULE_HOST_DOWNTIME;example.com;0;86400;17;0;120;;Boom',
            match_type='ellipsis',
        )
        live.expect_query(
            'COMMAND [...] SCHEDULE_HOST_DOWNTIME;heute;0;86400;17;0;120;;Boom',
            match_type='ellipsis',
        )
        live.expect_query([
            'GET services',
            'Columns: host_name description',
            'Filter: host_name = example.com',
            'Filter: host_name = heute',
            'Or: 2',
        ])
        live.expect_query(
            'COMMAND [...] SCHEDULE_SVC_DOWNTIME;example.com;Memory;0;86400;17;0;120;;Boom',
            match_type='ellipsis',
        )
        live.expect_query(
            'COMMAND [...] SCHEDULE_SVC_DOWNTIME;example.com;CPU load;0;86400;17;0;120;;Boom',
            match_type='ellipsis',
        )
        live.expect_query(
            'COMMAND [...] SCHEDULE_SVC_DOWNTIME;heute;CPU load;0;86400;17;0;120;;Boom',
            match_type='ellipsis',
        )
        downtimes.schedule_hostgroup_host_downtime(
            sites.live(),
            'example',
            start_time,
            end_time,
            include_all_services=True,
            recur="day_of_month",
            duration=120,
            comment="Boom",
        )
Example #4
0
def create_downtime(params):
    """Create a scheduled downtime"""
    body = params['body']
    downtime_type: DowntimeType = body['downtime_type']
    if downtime_type == 'host':
        downtime_commands.schedule_host_downtime(
            sites.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(
            sites.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 == 'service':
        downtime_commands.schedule_service_downtime(
            sites.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(
            sites.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}"),
        )
    else:
        return problem(
            status=400,
            title="Unhandled downtime-type.",
            detail=f"The downtime-type {downtime_type!r} is not supported.")

    return Response(status=204)