Module: check_mk
Branch: master
Commit: 07ed3867166284a6632207292683416cab8922d2
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=07ed3867166284…
Author: Andreas Boesl <ab(a)mathias-kettner.de>
Date: Fri Jun 30 13:55:31 2017 +0200
some timeperiod code refactorings related to the latest modules->cmk_base changes
Change-Id: I5198a2ab4178b857bf734e07b886b46ee628b515
---
cmk_base/core.py | 78 +++++++++++++++++++++++++++++-------------------------
cmk_base/events.py | 5 ++--
cmk_base/utils.py | 2 +-
3 files changed, 45 insertions(+), 40 deletions(-)
diff --git a/cmk_base/core.py b/cmk_base/core.py
index 7f2c494..52d158d 100644
--- a/cmk_base/core.py
+++ b/cmk_base/core.py
@@ -35,6 +35,7 @@ import sys
import cmk.paths
import cmk.debug
import cmk.tty as tty
+import livestatus
from cmk.exceptions import MKGeneralException
import cmk_base.console as console
@@ -42,6 +43,7 @@ import cmk_base.config as config
import cmk_base.core_config as core_config
import cmk_base.core_nagios as core_nagios
from cmk_base.exceptions import MKTimeout
+from cmk_base import config_cache
try:
import cmk_base.cee.core_cmc as core_cmc
@@ -182,49 +184,53 @@ def do_core_action(action, quiet=False):
# | Fetching timeperiods from the core |
# '----------------------------------------------------------------------'
-g_inactive_timerperiods = None # Cache for current state of timeperiods
-
-# Return plain response from local Livestatus - without any parsing
-# TODO: Use livestatus module
-def simple_livestatus_query(lql):
- s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
- s.connect(cmk.paths.livestatus_unix_socket)
- # We just get the currently inactive timeperiods. All others
- # (also non-existing) are considered to be active
- s.send(lql)
- s.shutdown(socket.SHUT_WR)
- response = ""
- while True:
- chunk = s.recv(4096)
- if not chunk:
- break
- response += chunk
- return response
-
-
# 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...
def check_timeperiod(timeperiod):
- global g_inactive_timerperiods
# Let exceptions happen, they will be handled upstream.
- if g_inactive_timerperiods == None:
- try:
- response = simple_livestatus_query("GET timeperiods\nColumns:
name\nFilter: in = 0\n")
- g_inactive_timerperiods = response.splitlines()
- except MKTimeout:
+ try:
+ update_timeperiods_cache()
+ except MKTimeout:
+ raise
+
+ except:
+ if cmk.debug.enabled():
raise
- except:
- if cmk.debug.enabled():
- raise
- else:
- # If the query is not successful better skip this check then fail
- return True
+ # If the query is not successful better skip this check then fail
+ return True
+
+ # Note: This also returns True when the timeperiod is unknown
+ # The following function timeperiod_active handles this differently
+ return config_cache.get_dict("timeperiods_cache").get(timeperiod, True) ==
True
+
+
+# Returns
+# True : match
+# False: no match
+# None : unable to connect to livestatus or timeperiod is unknown
+def timeperiod_active(timeperiod):
+ try:
+ update_inactive_timeperiods()
+ return config_cache.get_dict("timeperiods_cache").get(timeperiod)
+ except: # socket.error / MKTimeout / ?
+ if cmk.debug.enabled():
+ raise
+ return None
+
+
+def update_timeperiods_cache():
+ # { "last_update": 1498820128, "timeperiods": [{"24x7":
True}] }
+ # The value is store within the config cache since we need a fresh start on reload
+ tp_cache = config_cache.get_dict("timeperiods_cache")
+
+ if not tp_cache:
+ response = livestatus.LocalConnection().query("GET timeperiods\nColumns:
name in")
+ for tp_name, tp_active in response:
+ tp_cache[tp_name] = bool(tp_active)
- return timeperiod not in g_inactive_timerperiods
+def cleanup_timeperiod_caches():
+ config_cache.get_dict("timeperiods_cache").clear()
-def cleanup_inactive_timeperiods():
- global g_inactive_timerperiods
- g_inactive_timerperiods = None
diff --git a/cmk_base/events.py b/cmk_base/events.py
index 503f8ca..c55e788 100644
--- a/cmk_base/events.py
+++ b/cmk_base/events.py
@@ -59,9 +59,8 @@ def event_keepalive(event_function, log_function, call_every_loop=None,
loop_int
while True:
try:
- # Invalidate timeperiod cache
- global g_inactive_timerperiods
- g_inactive_timerperiods = None
+ # Invalidate timeperiod caches
+ core.cleanup_timeperiod_caches()
# If the configuration has changed, we do a restart. But we do
# this check just before the next event arrives. We must
diff --git a/cmk_base/utils.py b/cmk_base/utils.py
index 63d6428..720fbd8 100644
--- a/cmk_base/utils.py
+++ b/cmk_base/utils.py
@@ -177,7 +177,7 @@ def cleanup_globals():
import cmk_base.item_state
cmk_base.item_state.cleanup_item_states()
import cmk_base.core
- cmk_base.core.cleanup_inactive_timeperiods()
+ cmk_base.core.cleanup_timeperiod_caches()
import cmk_base.snmp
cmk_base.snmp.cleanup_host_caches()
import cmk_base.agent_data