Exemplo n.º 1
0
def acknowledge_hostgroup_problem(
    connection,
    hostgroup_name: str,
    sticky: bool = False,
    notify: bool = False,
    persistent: bool = False,
    user: str = "",
    comment: str = "",
):
    """Acknowledge the problems of the current hosts of the hostgroup

    When acknowledging a problem, further notifications for the respective services are disabled, as
    long as a specific service doesn't change state. At state change, notifications are re-enabled.

    Args:
        connection:
            A livestatus connection object.

        hostgroup_name:
            The name of the host group.

        sticky:
            If set, only a state-change of the service to an OK state will discard the
            acknowledgement. Otherwise it will be discarded on any state-change. Defaults to False.

        notify:
            If set, notifications will be sent out to the configured contacts. Defaults to False.

        persistent:
            If set, the comment will persist a restart. Defaults to False.

        user:
        comment:
            If set, this comment will be stored alongside the acknowledgement.

    Raises:
        ValueError:
            when the Hostgroup in question doesn't exist.

    """
    members: List[str] = Query(
        [tables.Hostgroups.members],
        tables.Hostgroups.name.equals(hostgroup_name)).value(connection)

    acknowledgement = 2 if sticky else 1  # 1: normal, 2: sticky

    for host_name in members:
        send_command(
            connection,
            "ACKNOWLEDGE_HOST_PROBLEM",
            [
                host_name,
                acknowledgement,
                int(notify),
                int(persistent),
                user,
                comment,
            ],
        )
Exemplo 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)])
Exemplo n.º 3
0
def acknowledge_host_problem(
    connection,
    host_name,
    sticky: bool = False,
    notify: bool = False,
    persistent: bool = False,
    user: str = "",
    comment: str = "",
):
    """Acknowledge the current problem for the given host.

    When acknowledging a problem, notifications for the host are disabled, as long as the
    host doesn't change state. At state change, notifications are re-enabled.

    Args:
        connection:
            A livestatus connection object.

        host_name:
            The host-name for which this acknowledgement is for.

        sticky:
            If set, only a state-change of the host to an UP state will discard the acknowledgement.
            Otherwise it will be discarded on any state-change. Defaults to False.

        notify:
            If set, notifications will be sent out to the configured contacts. Defaults to False.

        persistent:
            If set, the comment will persist a restart. Defaults to False.

        user:
        comment:
            If set, this comment will be stored alongside the acknowledgement.

    Examples:

        >>> from cmk.gui.plugins.openapi.livestatus_helpers.testing import simple_expect
        >>> cmd = "COMMAND [...] ACKNOWLEDGE_HOST_PROBLEM;example.com;1;0;0;;"
        >>> with simple_expect(cmd, match_type="ellipsis") as live:
        ...     acknowledge_host_problem(live, 'example.com')

    """
    acknowledgement = 2 if sticky else 1  # 1: normal, 2: sticky

    return send_command(
        connection,
        "ACKNOWLEDGE_HOST_PROBLEM",
        [
            host_name,
            acknowledgement,
            int(notify),
            int(persistent),
            user,
            comment,
        ],
    )
Exemplo n.º 4
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", ""),
        ],
    )
Exemplo n.º 5
0
def del_service_downtime(connection, downtime_id: int):
    """Delete a service downtime.

    Args:
        connection:
            A livestatus connection object.

        downtime_id:
            The downtime-id.

    Examples:

        >>> from cmk.gui.plugins.openapi.livestatus_helpers.testing import simple_expect
        >>> with simple_expect("COMMAND [...] DEL_SVC_DOWNTIME;1") as live:
        ...     del_service_downtime(live, 1)

    """
    return send_command(connection, "DEL_SVC_DOWNTIME", [downtime_id])
Exemplo n.º 6
0
def add_service_comment(
    connection,
    host_name: str,
    service_description: str,
    comment: str,
    persistent: bool = False,
    user: str = '',
):
    """ Add service comment

    Args:
        connection:
            A livestatus connection object

        host_name:
            The host-name where the service is located

        service_description:
            The service description for which the comment is for

        comment:
            The comment which will be stored for the service

        persistent:
            If set, the comment will persist across program restarts until it is delete manually.
            If not set, the comment will be deleted the next time the Core is restarted.

        user:

    Examples:
        >>> from cmk.gui.plugins.openapi.livestatus_helpers.testing import simple_expect
        >>> cmd = "COMMAND [...] ADD_SVC_COMMENT;example.com;CPU Load;0;;test"
        >>> with simple_expect(cmd, "ellipsis") as live:
        ...     add_service_comment(live, 'example.com', 'CPU Load', 'test')


    """
    return send_command(
        connection,
        "ADD_SVC_COMMENT",
        [host_name, service_description,
         int(persistent), user, comment],
    )
Exemplo n.º 7
0
def del_host_comment(connection, comment_id: int):
    """Delete a host comment

    Args:
        connection:
            A livestatus connection object

        comment_id:
            The id of the host comment

    Examples:
        >>> from cmk.gui.plugins.openapi.livestatus_helpers.testing import simple_expect
        >>> cmd = "COMMAND [...] DEL_HOST_COMMENT;1234"
        >>> with simple_expect(cmd, "ellipsis") as live:
        ...     del_host_comment(live, 1234)

    """
    return send_command(
        connection,
        "DEL_HOST_COMMENT",
        [comment_id],
    )
Exemplo n.º 8
0
def add_host_comment(
    connection,
    host_name: str,
    comment: str,
    persistent: bool = False,
    user: str = '',
):
    """Add a comment for a particular host.

    Args:
        connection:
            A livestatus connection object

        host_name:
            The host-name for which the comment is for

        comment:
            The comment which will be stored for the host

        persistent:
            If set, the comment will persist across program restarts until it is delete manually.
            If not set, the comment will be deleted the next time the Core is restarted.

        user:

    Examples:
        >>> from cmk.gui.plugins.openapi.livestatus_helpers.testing import simple_expect
        >>> cmd = "COMMAND [...] ADD_HOST_COMMENT;example.com;0;;test"
        >>> with simple_expect(cmd, match_type="ellipsis") as live:
        ...     add_host_comment(live, 'example.com', 'test')


    """
    return send_command(
        connection,
        "ADD_HOST_COMMENT",
        [host_name, int(persistent), user, comment],
    )
Exemplo n.º 9
0
def acknowledge_service_problem(
    connection,
    host_name: str,
    service_description: str,
    sticky: bool = False,
    notify: bool = False,
    persistent: bool = False,
    user: str = '',
    comment: str = '',
):
    """Acknowledge the current problem for the given service.

    When acknowledging a problem, furhter notifications for the service are disabled, as
    long as the service doesn't change state. At state change, notifications are re-enabled.

    Args:
        connection:
            A livestatus connection object.

        host_name:
            The host-name for which this acknowledgement is for.

        service_description:
            The service description of the service, whose problems shall be acknowledged.

        sticky:
            If set, only a state-change of the service to an OK state will discard the
            acknowledgement. Otherwise it will be discarded on any state-change. Defaults to False.

        notify:
            If set, notifications will be sent out to the configured contacts. Defaults to False.

        persistent:
            If set, the comment will persist a restart. Defaults to False.

        user:
        comment:

    Examples:

        >>> from cmk.gui.plugins.openapi.livestatus_helpers.testing import simple_expect
        >>> cmd = "COMMAND [...] ACKNOWLEDGE_SVC_PROBLEM;example.com;drain;1;0;0;;"
        >>> with simple_expect(cmd) as live:
        ...     acknowledge_service_problem(live, 'example.com', 'drain')

    """
    acknowledgement = 2 if sticky else 1  # 1: normal, 2: sticky

    return send_command(
        connection,
        "ACKNOWLEDGE_SVC_PROBLEM",
        [
            host_name,
            service_description,
            acknowledgement,
            int(notify),
            int(persistent),
            user,
            comment,
        ],
    )