Skip to content

Commit 4e35469

Browse files
committed
fix: use the threshold when storing nagios info rather than when reading it
1 parent 246e364 commit 4e35469

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

lib/vsc/utils/nagios.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -269,14 +269,16 @@ def report_and_exit(self):
269269
logging.critical("Error opening file %s for reading", self.filename)
270270
unknown_exit("%s nagios gzipped JSON file unavailable (%s)" % (self.header, self.filename))
271271

272-
(timestamp, ((nagios_exit_code, nagios_exit_string), nagios_message)) = nagios_cache.load('nagios')
272+
(timestamp, nagios_exit_info) = nagios_cache.load('nagios')
273+
274+
if nagios_exit_info is None:
275+
unknown_exit("%s nagios exit info expired" % self.header)
276+
277+
((nagios_exit_code, nagios_exit_string), nagios_message) = nagios_exit_info
278+
279+
print("%s %s" % (nagios_exit_string, nagios_message))
280+
sys.exit(nagios_exit_code)
273281

274-
if self.threshold <= 0 or time.time() - timestamp < self.threshold:
275-
logging.info("Nagios check cache file %s contents delivered: %s", self.filename, nagios_message)
276-
print("%s %s" % (nagios_exit_string, nagios_message))
277-
sys.exit(nagios_exit_code)
278-
else:
279-
unknown_exit("%s gzipped JSON file too old (timestamp = %s)" % (self.header, time.ctime(timestamp)))
280282

281283
def cache(self, nagios_exit, nagios_message):
282284
"""Store the result in the cache file with a timestamp.
@@ -289,7 +291,7 @@ def cache(self, nagios_exit, nagios_message):
289291
"""
290292
try:
291293
nagios_cache = FileCache(self.filename)
292-
nagios_cache.update('nagios', (nagios_exit, nagios_message), 0) # always update
294+
nagios_cache.update('nagios', (nagios_exit, nagios_message), threshold=self.threshold)
293295
nagios_cache.close()
294296
logging.info("Wrote nagios check cache file %s at about %s", self.filename, time.ctime(time.time()))
295297
except (IOError, OSError):

test/nagios.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def test_cache(self):
7070
"""Test the caching mechanism in the reporter."""
7171
length = random.randint(1, 30)
7272
exit_code = random.randint(0, 3)
73-
threshold = random.randint(0, 10)
73+
threshold = None
7474

7575
message = ''.join(random.choice(string.printable) for x in range(length))
7676
message = message.rstrip()

0 commit comments

Comments
 (0)