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, ], )
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.livestatus_utils.testing import simple_expect >>> from cmk.gui.utils.script_helpers import application_and_request_context >>> from cmk.gui.logged_in import SuperUserContext >>> from cmk.gui.config import load_config >>> cmd = "COMMAND [...] DEL_HOST_COMMENT;1234" >>> expect = simple_expect(cmd, match_type="ellipsis") >>> with expect as live, application_and_request_context(), SuperUserContext(): ... load_config() ... del_host_comment(live, 1234) """ _user.need_permission("action.addcomment") # also for removing comments return send_command( connection, "DEL_HOST_COMMENT", [comment_id], )
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.livestatus_utils.testing import simple_expect >>> cmd = "COMMAND [...] SCHEDULE_FORCED_SVC_CHECK;example.com;CPU Load;0" >>> with simple_expect(cmd, match_type="ellipsis") 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)], )
def force_schedule_host_check(connection, host_name: str, check_time: dt.datetime): """Schedule a forced active check of a particular host Args: connection: A livestatus connection object host_name: The name of the host where 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.livestatus_utils.testing import simple_expect >>> from cmk.gui.config import load_config >>> from cmk.gui.utils.script_helpers import application_and_request_context >>> from cmk.gui.logged_in import SuperUserContext >>> cmd = "COMMAND [...] SCHEDULE_FORCED_HOST_CHECK;example.com;0" >>> expect = simple_expect(cmd, match_type="ellipsis") >>> with expect as live, application_and_request_context(), SuperUserContext(): ... load_config() ... force_schedule_host_check(live, 'example.com', _check_time) """ _user.need_permission("action.reschedule") return send_command(connection, "SCHEDULE_FORCED_HOST_CHECK", [host_name, to_timestamp(check_time)])
def del_service_downtime( connection, downtime_id: int, site_id: SiteId, ): """Delete a service downtime. Args: connection: A livestatus connection object. downtime_id: The downtime-id. site_id: Id of site where command should be executed. Examples: >>> from cmk.gui.livestatus_utils.testing import simple_expect >>> from cmk.gui.config import load_config >>> from cmk.gui.utils.script_helpers import application_and_request_context >>> from cmk.gui.logged_in import SuperUserContext >>> expect = simple_expect("COMMAND [...] DEL_SVC_DOWNTIME;1", match_type="ellipsis") >>> with expect as live, application_and_request_context(), SuperUserContext(): ... load_config() ... del_service_downtime(live, 1, "") """ _user.need_permission("action.downtimes") return send_command(connection, "DEL_SVC_DOWNTIME", [downtime_id], site_id)
def del_service_downtime( connection, downtime_id: int, site_id: SiteId, ): """Delete a service downtime. Args: connection: A livestatus connection object. downtime_id: The downtime-id. site_id: Id of site where command should be executed. Examples: >>> from cmk.gui.livestatus_utils.testing import simple_expect >>> with simple_expect("COMMAND [...] DEL_SVC_DOWNTIME;1", match_type="ellipsis") as live: ... del_service_downtime(live, 1, "") """ return send_command(connection, "DEL_SVC_DOWNTIME", [downtime_id], site_id)
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.livestatus_utils.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, ], )
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 deleted manually. If not set, the comment will be deleted the next time the Core is restarted. user: Examples: >>> from cmk.gui.livestatus_utils.testing import simple_expect >>> from cmk.gui.utils.script_helpers import application_and_request_context >>> from cmk.gui.logged_in import SuperUserContext >>> from cmk.gui.config import load_config >>> cmd = "COMMAND [...] ADD_SVC_COMMENT;example.com;CPU Load;0;;test" >>> expect = simple_expect(cmd, match_type="ellipsis") >>> with expect as live, application_and_request_context(), SuperUserContext(): ... load_config() ... add_service_comment(live, 'example.com', 'CPU Load', 'test') """ _user.need_permission("action.addcomment") return send_command( connection, "ADD_SVC_COMMENT", [host_name, service_description, int(persistent), user, comment], )
def _schedule_downtime( sites, command: LivestatusCommand, site_id, 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 _user.need_permission("action.downtimes") recur_mode = _recur_mode(recur, duration) 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", ""), ], site_id, )
def del_host_downtime(connection, downtime_id: int): """Delete a host downtime. Args: connection: A livestatus connection object. downtime_id: The downtime-id. Examples: >>> from cmk.gui.livestatus_utils.testing import simple_expect >>> with simple_expect("COMMAND [...] DEL_HOST_DOWNTIME;1", match_type="ellipsis") as live: ... del_host_downtime(live, 1) """ return send_command(connection, "DEL_HOST_DOWNTIME", [downtime_id])
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.livestatus_utils.testing import simple_expect >>> cmd = "COMMAND [...] ADD_SVC_COMMENT;example.com;CPU Load;0;;test" >>> with simple_expect(cmd, match_type="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], )
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.livestatus_utils.testing import simple_expect >>> cmd = "COMMAND [...] DEL_HOST_COMMENT;1234" >>> with simple_expect(cmd, match_type="ellipsis") as live: ... del_host_comment(live, 1234) """ return send_command( connection, "DEL_HOST_COMMENT", [comment_id], )
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.livestatus_utils.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], )
def acknowledge_servicegroup_problem( connection, servicegroup_name: str, sticky: bool = False, notify: bool = False, persistent: bool = False, user: str = "", comment: str = "", ): """Acknowledge the problems of the current services of the servicegroup 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. servicegroup_name: The host-name for which this acknowledgement is for. 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 servicegroup could not be found. """ with detailed_connection(connection) as conn: group_entries = Query( [tables.Servicegroups.members], tables.Servicegroups.name.equals(servicegroup_name), ).fetchall(conn) acknowledgement = 2 if sticky else 1 # 1: normal, 2: sticky for entry in group_entries: site_id = entry["site"] for host_name, service_description in entry["members"]: send_command( connection, "ACKNOWLEDGE_SVC_PROBLEM", [ host_name, service_description, acknowledgement, int(notify), int(persistent), user, comment, ], site_id=site_id, )
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: If set, this comment will be stored alongside the acknowledgement. Examples: >>> from cmk.gui.livestatus_utils.testing import simple_expect >>> cmd = "COMMAND [...] ACKNOWLEDGE_SVC_PROBLEM;example.com;drain;1;0;0;;" >>> with simple_expect() as live: ... _ = live.expect_query("GET hosts\\nColumns: name\\nFilter: name = example.com") ... _ = live.expect_query(cmd, match_type="ellipsis") ... acknowledge_service_problem(live, 'example.com', 'drain') """ site_id = _query_site(connection, host_name) 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, ], site_id=site_id, )
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.livestatus_utils.testing import simple_expect >>> from cmk.gui.utils.script_helpers import application_and_request_context >>> from cmk.gui.logged_in import SuperUserContext >>> from cmk.gui.config import load_config >>> cmd = "COMMAND [...] ACKNOWLEDGE_HOST_PROBLEM;example.com;1;0;0;;" >>> with simple_expect() as live, application_and_request_context(), SuperUserContext(): ... load_config() ... _ = live.expect_query("GET hosts\\nColumns: name\\nFilter: name = example.com") ... _ = live.expect_query(cmd, match_type="ellipsis") ... acknowledge_host_problem(live, 'example.com') Not authenticated users can't call this function: >>> with application_and_request_context(): ... acknowledge_host_problem(live, 'example.com') # doctest: +ELLIPSIS Traceback (most recent call last): ... cmk.gui.exceptions.MKAuthException: ... """ _user.need_permission("action.acknowledge") acknowledgement = 2 if sticky else 1 # 1: normal, 2: sticky with detailed_connection(connection) as conn: site_id = Query([Hosts.name], Hosts.name.equals(host_name)).first_value(conn) return send_command( connection, "ACKNOWLEDGE_HOST_PROBLEM", [ host_name, acknowledgement, int(notify), int(persistent), user, comment, ], site_id=site_id, )