summaryrefslogtreecommitdiffstats
path: root/LogActio/__init__.py
diff options
context:
space:
mode:
authorDavid Sommerseth <davids@redhat.com>2012-11-15 02:27:24 +0100
committerDavid Sommerseth <davids@redhat.com>2012-11-15 02:27:24 +0100
commit369024f04e8582a2aee503416a670418e9d57e50 (patch)
treee7907bb91b74b13fdfd247e8d264ec30072eb845 /LogActio/__init__.py
parent7bee9e7c48cb6de919906c89ac9144e8b9bacf88 (diff)
downloadlogactio-369024f04e8582a2aee503416a670418e9d57e50.tar.gz
logactio-369024f04e8582a2aee503416a670418e9d57e50.tar.xz
logactio-369024f04e8582a2aee503416a670418e9d57e50.zip
Added a simple logfile reopen mechanism
If logrotate has been run inbetween since last time the log file was checked, the opened fd will not point at the new file. In this case reopen the log file and process all new events in this new file. Signed-off-by: David Sommerseth <davids@redhat.com>
Diffstat (limited to 'LogActio/__init__.py')
-rw-r--r--LogActio/__init__.py16
1 files changed, 15 insertions, 1 deletions
diff --git a/LogActio/__init__.py b/LogActio/__init__.py
index 6456b91..5a7c167 100644
--- a/LogActio/__init__.py
+++ b/LogActio/__init__.py
@@ -74,6 +74,7 @@ class WatcherThread(threading.Thread):
self.start()
def run(self):
+ fp = None
# This is started by threading.Thread
try:
fp = fp = open(self.__logfile, "r")
@@ -87,8 +88,21 @@ class WatcherThread(threading.Thread):
where = fp.tell()
line = fp.readline()
if len(line) == 0:
+ # Before sleeping, grab a copy of the file size.
+ # If it has become truncated, we need to reopen the file
+ filesize_before = os.stat(self.__logfile).st_size
time.sleep(self.__polltime)
- fp.seek(where)
+ filesize_after = os.stat(self.__logfile).st_size
+ if filesize_after < filesize_before:
+ # Reopen is needed.
+ fp.close()
+ time.sleep(1) # Just in case
+ fp = open(self.__logfile)
+ # We will not go to end of file, so
+ # we catch all changes in the log file
+ # since the truncation point
+ else:
+ fp.seek(where)
continue
now = int(time.time())