Protect against unintended log deletions in logmgmt

The logmgmt daemon is using a "logrotate -d" query to determine
the list of monitored files. If that query fails, logmgmt should
not purge unmonitored files, as it may be deleting files that
should be protected.

Change-Id: I8a0b092fdbe8fccb8cf493dc631c22e1d7156269
Signed-off-by: Don Penney <don.penney@windriver.com>
Signed-off-by: Scott Little <scott.little@windriver.com>
This commit is contained in:
Don Penney 2018-04-11 14:23:07 -04:00 committed by Scott Little
parent 696e01e065
commit 636898cd68
1 changed files with 32 additions and 23 deletions

View File

@ -129,8 +129,11 @@ class LogMgmtDaemon():
self.monitored_files.extend(glob.glob(fields[2]))
self.monitored_files.extend(glob.glob(fields[2] + '.[0-9].gz'))
self.monitored_files.extend(glob.glob(fields[2] + '.[0-9][0-9].gz'))
self.monitored_files.extend(glob.glob(fields[2] + '.[0-9]'))
self.monitored_files.extend(glob.glob(fields[2] + '.[0-9][0-9]'))
except:
logging.error('Failed to determine monitored files')
raise
def get_unmonitored_files(self):
self.unmonitored_files = []
@ -214,6 +217,7 @@ class LogMgmtDaemon():
logging.warning("Reached critical disk usage for /var/log: %d%% free" % pf)
# We're running out of disk space, so we need to start deleting files
try:
for index in range(20, 11, -1):
logging.info("/var/log is %d%% free. Purging rotated .%d.gz files to free space" % (pf, index))
self.get_monitored_files()
@ -225,6 +229,8 @@ class LogMgmtDaemon():
logging.info("/var/log is %d%% free. Running logrotate" % pf)
self.run_logrotate()
return
except Exception as e:
logging.exception('Failed purging rotated files', e)
# We still haven't freed up enough space, so try a logrotate
logging.info("/var/log is %d%% free. Running logrotate" % pf)
@ -243,6 +249,7 @@ class LogMgmtDaemon():
return
# Start deleting unmonitored files
try:
self.get_monitored_files()
self.get_unmonitored_files()
logging.info("/var/log is %d%% free. Deleting unmonitored files to free space" % pf)
@ -256,6 +263,8 @@ class LogMgmtDaemon():
if pf >= PERCENT_FREE_MAJOR:
logging.info("/var/log is %d%% free." % pf)
return
except Exception as e:
logging.exception('Failed checking unmonitored files', e)
# Nothing else to be done
logging.info("/var/log is %d%% free." % pf)