[checkmk-commits] Check_MK Git: check_mk: New configuration check_periods

git version control git at mathias-kettner.de
Wed Nov 21 16:34:11 CET 2012


Module: check_mk
Branch: master
Commit: dee64aaa6326b24584eac391fd857f53b91b255a
URL:    http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=dee64aaa6326b24584eac391fd857f53b91b255a

Author: Mathias Kettner <mk at mathias-kettner.de>
Date:   Wed Nov 21 16:34:04 2012 +0100

New configuration check_periods

---

 ChangeLog                                  |    2 +
 modules/check_mk.py                        |   22 +++++++++++++++++++-
 modules/check_mk_base.py                   |   31 ++++++++++++++++++++++++++++
 modules/notify.py                          |   18 ----------------
 web/plugins/wato/check_mk_configuration.py |   10 ++++++++-
 5 files changed, 63 insertions(+), 20 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index b21b0a1..a506f70 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,6 +1,8 @@
 1.2.1i3:
     Core:
     * added HOST/SERVICEPROBLEMID to notification macros
+    * New configuration check_periods for limiting execution of
+      Check_MK checks to a certain time period.
 
     Checks & Agents:
     * Windows agent: persist offsets for logfile monitoring
diff --git a/modules/check_mk.py b/modules/check_mk.py
index ea3131a..122389d 100755
--- a/modules/check_mk.py
+++ b/modules/check_mk.py
@@ -316,6 +316,7 @@ donation_command                     = 'mail -r checkmk at yoursite.de  -s "Host do
 scanparent_hosts                     = [ ( ALL_HOSTS ) ]
 host_attributes                      = {} # needed by WATO, ignored by Check_MK
 ping_levels                          = [] # special parameters for host/PING check_command
+check_periods                        = []
 
 # global variables used to cache temporary values (not needed in check_mk_base)
 ip_to_hostname_cache = None
@@ -676,6 +677,17 @@ def is_snmpv2c_host(hostname):
 def is_usewalk_host(hostname):
     return in_binary_hostlist(hostname, usewalk_hosts)
 
+def check_period_of(hostname, service):
+    periods = service_extra_conf(hostname, service, check_periods)
+    if periods:
+        period = periods[0]
+        if period == "24X7":
+            return None
+        else:
+            return period
+    else:
+        return None
+
 def get_single_oid(hostname, ipaddress, oid):
     # New in Check_MK 1.1.11: oid can end with ".*". In that case
     # we do a snmpgetnext and try to find an OID with the prefix
@@ -2627,7 +2639,7 @@ no_inventory_possible = None
                  'snmpwalks_dir', 'check_mk_basedir', 'nagios_user',
                  'www_group', 'cluster_max_cachefile_age', 'check_max_cachefile_age',
                  'simulation_mode', 'agent_simulator', 'aggregate_check_mk', 'debug_log',
-                 'check_mk_perfdata_with_times'
+                 'check_mk_perfdata_with_times', 'livestatus_unix_socket',
                  ]:
         output.write("%s = %r\n" % (var, globals()[var]))
 
@@ -2640,15 +2652,23 @@ no_inventory_possible = None
     need_snmp_module = False
     needed_check_types = set([])
     needed_sections = set([])
+    service_timeperiods = {}
     for check_type, item, param, descr, aggr in check_table:
         if check_type not in check_info:
             sys.stderr.write('Warning: Ignoring missing check %s.\n' % check_type)
             continue
+        period = check_period_of(hostname, descr)
+        if period:
+            service_timeperiods[descr] = period
+
         needed_sections.add(check_type.split(".")[0])
         needed_check_types.add(check_type)
         if check_uses_snmp(check_type):
             need_snmp_module = True
 
+    output.write("precompiled_service_timeperiods = %r\n" % service_timeperiods)
+    output.write("def check_period_of(hostname, service):\n    return precompiled_service_timeperiods.get(service)\n\n")
+
     if need_snmp_module:
         output.write(stripped_python_file(modules_dir + "/snmp.py"))
 
diff --git a/modules/check_mk_base.py b/modules/check_mk_base.py
index e46b2e5..43520ca 100755
--- a/modules/check_mk_base.py
+++ b/modules/check_mk_base.py
@@ -831,6 +831,17 @@ def do_all_checks_on_host(hostname, ipaddress, only_check_types = None):
         if only_check_types != None and checkname not in only_check_types:
             continue
 
+        # Skip checks that are not in their check period
+        period = check_period_of(hostname, description)
+        if period and not check_timeperiod(period):
+            if opt_debug:
+                sys.stderr.write("Skipping service %s: currently not in timeperiod %s.\n" % 
+                        (description, period))
+            continue
+        elif period and opt_debug:
+            sys.stderr.write("Service %s: timeperiod %s is currently active.\n" % 
+                    (description, period))
+
         # In case of a precompiled check table info is the aggrated
         # service name. In the non-precompiled version there are the dependencies
         if type(info) == str:
@@ -1210,3 +1221,23 @@ def get_age_human_readable(secs):
 # in command definitions as $ARG1$)
 def quote_shell_string(s):
     return "'" + s.replace("'", "'\"'\"'") + "'"
+
+
+# Check if a timeperiod is currently active. We have no other way than
+# doing a Livestatus query. This is not really nice, but if you have a better
+# idea, please tell me...
+g_inactive_timerperiods = None
+def check_timeperiod(timeperiod):
+    global g_inactive_timerperiods
+    # Let exceptions happen, they will be handled upstream.
+    if g_inactive_timerperiods == None:
+        import socket
+        s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
+        s.connect(livestatus_unix_socket)
+        # We just get the currently inactive timeperiods. All others
+        # (also non-existing) are considered to be active
+        s.send("GET timeperiods\nColumns:name\nFilter: in = 0\n")
+        s.shutdown(socket.SHUT_WR)
+        g_inactive_timerperiods = s.recv(10000000).splitlines()
+    return timeperiod not in g_inactive_timerperiods
+
diff --git a/modules/notify.py b/modules/notify.py
index e1ce824..a258034 100644
--- a/modules/notify.py
+++ b/modules/notify.py
@@ -275,24 +275,6 @@ def call_notification_script(plugin, parameters):
 
 
 
-# Check if a timeperiod is currently active. We have no other way than
-# doing a Livestatus query. This is not really nice, but if you have a better
-# idea, please tell me...
-g_inactive_timerperiods = None
-def check_timeperiod(timeperiod):
-    global g_inactive_timerperiods
-    # Let exceptions happen, they will be handled upstream.
-    if g_inactive_timerperiods == None:
-        import socket
-        s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
-        s.connect(livestatus_unix_socket)
-        # We just get the currently inactive timeperiods. All others
-        # (also non-existing) are considered to be active
-        s.send("GET timeperiods\nColumns:name\nFilter: in = 0\n")
-        s.shutdown(socket.SHUT_WR)
-        g_inactive_timerperiods = s.recv(10000000).splitlines()
-    return timeperiod not in g_inactive_timerperiods
-
 
 def check_notification_type(context, host_events, service_events):
     notification_type = context["NOTIFICATIONTYPE"]
diff --git a/web/plugins/wato/check_mk_configuration.py b/web/plugins/wato/check_mk_configuration.py
index 98a0326..a7ca00c 100644
--- a/web/plugins/wato/check_mk_configuration.py
+++ b/web/plugins/wato/check_mk_configuration.py
@@ -716,7 +716,7 @@ register_rule(group,
 register_rule(group,
     "extra_service_conf:check_period",
     TimeperiodSelection(
-        title = _("Check period for services"),
+        title = _("Check period for active services"),
         help = _("If you specify a notification period for a service then active checks "
                  "of that service will only be done in that period. Please note, that the "
                  "checks driven by Check_MK are passive checks and are not affected by this "
@@ -724,6 +724,14 @@ register_rule(group,
     itemtype = "service")
 
 register_rule(group,
+    "check_periods",
+    TimeperiodSelection(
+        title = _("Check period for passive Check_MK services"),
+        help = _("If you specify a notification period for a Check_MK service then "
+                 "results will be processed only within this period.")),
+    itemtype = "service")
+
+register_rule(group,
     "extra_service_conf:process_perf_data",
     DropdownChoice(
         title = _("Enable/disable processing of perfdata for services"),



More information about the checkmk-commits mailing list