コード例 #1
0
def _schedule_downtime(
    sites,
    command: LivestatusCommand,
    host_or_group: str,
    service_description: Optional[str],
    start_time: dt.datetime,
    end_time: dt.datetime,
    recur: RecurMode = 'fixed',
    trigger_id: int = 0,
    duration: int = 0,
    user_id: str = "",
    comment: str = "",
):
    """Unified low level function

    See:
     * schedule_host_downtime
     * schedule_service_downtime
    """
    # TODO: provide reference documents for recurring magic numbers

    recur_mode = _recur_to_even_mode(recur)

    if duration:
        # When a duration is set then the mode shifts to the next one. Even numbers (incl 0) signal
        # fixed recurring modes, odd numbers ones with a duration.
        recur_mode += 1

    if command == 'SCHEDULE_HOST_DOWNTIME':
        params = [host_or_group]
    elif command == 'SCHEDULE_SVC_DOWNTIME':
        if not service_description:
            raise ValueError("Service description necessary.")
        params = [host_or_group, service_description]
    else:
        raise ValueError(f"Unsupported command: {command}")

    return send_command(
        sites,
        command,
        [
            *params,
            to_timestamp(start_time),
            to_timestamp(end_time),
            recur_mode,
            trigger_id,
            duration,
            user_id,
            comment.replace("\n", ""),
        ],
    )
コード例 #2
0
def force_schedule_service_check(connection, host_name: str,
                                 service_description: str,
                                 check_time: dt.datetime):
    """Schedule a forced active check of a particular service

    Args:
        connection:
            A livestatus connection object

        host_name:
            The name of the host where the service is

        service_description:
            The service description for which the forced check should be performed on

        check_time:
            The time at which this forced check should be performed

    Examples:
        >>> import pytz
        >>> _check_time = dt.datetime(1970, 1, 1, tzinfo=pytz.timezone("UTC"))

        >>> from cmk.gui.plugins.openapi.livestatus_helpers.testing import simple_expect
        >>> cmd = "COMMAND [...] SCHEDULE_FORCED_SVC_CHECK;example.com;CPU Load;0"
        >>> with simple_expect(cmd) as live:
        ...     force_schedule_service_check(live,'example.com', 'CPU Load', _check_time)
    """

    return send_command(
        connection, "SCHEDULE_FORCED_SVC_CHECK",
        [host_name, service_description,
         to_timestamp(check_time)])