[checkmk-commits] agent_aws: factor out determination of timestamp

Moritz Kiemer mo at mathias-kettner.de
Fri Apr 26 08:50:31 CEST 2019


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

Author: Moritz Kiemer <mo at mathias-kettner.de>
Date:   Thu Apr 25 10:09:01 2019 +0200

agent_aws: factor out determination of timestamp

Fix 'now' vs. 'mtime' bug along the way

Change-Id: I922611d113c28ddde72bec1bcbce3fb994fb27da

---

 cmk/special_agents/agent_aws.py | 32 +++++++++++++++++++-------------
 1 file changed, 19 insertions(+), 13 deletions(-)

diff --git a/cmk/special_agents/agent_aws.py b/cmk/special_agents/agent_aws.py
index af11a32..590853e 100644
--- a/cmk/special_agents/agent_aws.py
+++ b/cmk/special_agents/agent_aws.py
@@ -638,23 +638,29 @@ class AWSSection(object):
             cache_timestamp = time.time()
         return AWSRawContent(raw_content, cache_timestamp)
 
-    def _cache_is_recent_enough(self, colleague_contents):
+    @property
+    def cache_timestamp(self):
         if not self._cache_file.exists():
-            logging.info("New cache file %s", self._cache_file)
-            return False
+            return None
 
-        now = time.time()
         try:
-            mtime = self._cache_file.stat().st_mtime
-        except OSError as e:
-            if e.errno == 2:
-                logging.info("No such file or directory %s (calculate age)", self._cache_file)
-                return False
-            else:
-                logging.info("Cannot calculate cache file age: %s", e)
-                raise
+            return self._cache_file.stat().st_mtime
+        except OSError as exc:
+            if exc.errno == 2:
+                logging.info("No such file or directory %s (cache_timestamp)", self._cache_file)
+                return None
+            logging.info("Cannot calculate cache file age: %s", exc)
+            raise
 
-        age = now - mtime
+    def _cache_is_recent_enough(self, colleague_contents):
+        mtime = self.cache_timestamp
+        if mtime is None:
+            return False
+
+        age = time.time() - mtime
+        if age < 0:
+            logging.info("Cache file from future considered invalid: %s", self._cache_file)
+            return False
         if age >= self.interval:
             logging.info("Cache file %s is outdated", self._cache_file)
             return False



More information about the checkmk-commits mailing list