Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions check_log_ng.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import base64
import fcntl
import warnings
from datetime import datetime

FALLBACK_PATH = "/usr/local/hb-agent/bin"

Expand Down Expand Up @@ -745,9 +746,19 @@ def _get_cache(self, cachefile):
line = fileobj.readline()
fileobj.close()
state, message = line.split("\t", 1)
cache_created_at = self._get_cache_created_at(cachefile)
message = "{0} (use cache created at {1})".format(message, cache_created_at)
_debug("cache: state={0}, message='{1}'".format(state, message))
return int(state), message

def _get_cache_created_at(self, cachefile):
"""Get the cache's created time

Returns:
string representing the in ISO 8601 format (YYYY-MM-DDThh:mm:ss.ffffff).
"""
return datetime.fromtimestamp(os.stat(cachefile).st_mtime).isoformat()

def _update_cache(self, cachefile):
"""Update the cache."""
if self.config['dry_run']:
Expand Down
7 changes: 5 additions & 2 deletions test_check_log_ng.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class LogCheckerTestCase(unittest.TestCase):
# Class constant
MESSAGE_OK = "OK - No matches found."
MESSAGE_WARNING_ONE = "WARNING: Found 1 lines (limit=1/0): {0} at {1}"
MESSAGE_WARNING_ONE_WITH_CACHE = "WARNING: Found 1 lines (limit=1/0): {0} at {1} (use cache created at {2})"
MESSAGE_WARNING_ONE_WITH_QUIET = "WARNING: Found 1 lines (limit=1/0, QUIET): at {0}"
MESSAGE_WARNING_ONE_WITH_HEADER = "WARNING: Found 1 lines (limit=1/0, HEADER): {0} at {1}"
MESSAGE_WARNING_TWO = "WARNING: Found 2 lines (limit=1/0): {0},{1} at {2}"
Expand Down Expand Up @@ -1037,9 +1038,10 @@ def test_cachetime(self):
log.check(self.logfile)

self.assertEqual(log.get_state(), LogChecker.STATE_WARNING)
cache_created_at = log._get_cache_created_at(cachefile)
self.assertEqual(
log.get_message(),
self.MESSAGE_WARNING_ONE.format(line, self.logfile))
self.MESSAGE_WARNING_ONE_WITH_CACHE.format(line, self.logfile, cache_created_at))

log._remove_cache(cachefile)

Expand Down Expand Up @@ -1112,9 +1114,10 @@ def test_cachetime_with_dry_run(self):
log.check(self.logfile)

self.assertEqual(log.get_state(), LogChecker.STATE_WARNING)
cache_created_at = log._get_cache_created_at(cachefile)
self.assertEqual(
log.get_message(),
self.MESSAGE_WARNING_ONE.format(line, self.logfile))
self.MESSAGE_WARNING_ONE_WITH_CACHE.format(line, self.logfile, cache_created_at))

log._remove_cache(cachefile)

Expand Down