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

View File

@ -129,8 +129,11 @@ class LogMgmtDaemon():
self.monitored_files.extend(glob.glob(fields[2])) 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].gz'))
self.monitored_files.extend(glob.glob(fields[2] + '.[0-9][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: except:
logging.error('Failed to determine monitored files') logging.error('Failed to determine monitored files')
raise
def get_unmonitored_files(self): def get_unmonitored_files(self):
self.unmonitored_files = [] self.unmonitored_files = []
@ -214,17 +217,20 @@ class LogMgmtDaemon():
logging.warning("Reached critical disk usage for /var/log: %d%% free" % pf) 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 # We're running out of disk space, so we need to start deleting files
for index in range(20, 11, -1): try:
logging.info("/var/log is %d%% free. Purging rotated .%d.gz files to free space" % (pf, index)) for index in range(20, 11, -1):
self.get_monitored_files() logging.info("/var/log is %d%% free. Purging rotated .%d.gz files to free space" % (pf, index))
self.purge_files(index) self.get_monitored_files()
pf = self.get_percent_free() self.purge_files(index)
pf = self.get_percent_free()
if pf >= PERCENT_FREE_MAJOR: if pf >= PERCENT_FREE_MAJOR:
# We've freed up enough space. Do a logrotate and leave # We've freed up enough space. Do a logrotate and leave
logging.info("/var/log is %d%% free. Running logrotate" % pf) logging.info("/var/log is %d%% free. Running logrotate" % pf)
self.run_logrotate() self.run_logrotate()
return return
except Exception as e:
logging.exception('Failed purging rotated files', e)
# We still haven't freed up enough space, so try a logrotate # We still haven't freed up enough space, so try a logrotate
logging.info("/var/log is %d%% free. Running logrotate" % pf) logging.info("/var/log is %d%% free. Running logrotate" % pf)
@ -243,19 +249,22 @@ class LogMgmtDaemon():
return return
# Start deleting unmonitored files # Start deleting unmonitored files
self.get_monitored_files() try:
self.get_unmonitored_files() self.get_monitored_files()
logging.info("/var/log is %d%% free. Deleting unmonitored files to free space" % pf) self.get_unmonitored_files()
for fname in sorted(self.unmonitored_files, key=os.path.getsize, reverse=True): logging.info("/var/log is %d%% free. Deleting unmonitored files to free space" % pf)
logging.info("Deleting unmonitored file: %s" % fname) for fname in sorted(self.unmonitored_files, key=os.path.getsize, reverse=True):
try: logging.info("Deleting unmonitored file: %s" % fname)
os.remove(fname) try:
except OSError as e: os.remove(fname)
logging.error('Failed to remove file: %s', e) except OSError as e:
pf = self.get_percent_free() logging.error('Failed to remove file: %s', e)
if pf >= PERCENT_FREE_MAJOR: pf = self.get_percent_free()
logging.info("/var/log is %d%% free." % pf) if pf >= PERCENT_FREE_MAJOR:
return 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 # Nothing else to be done
logging.info("/var/log is %d%% free." % pf) logging.info("/var/log is %d%% free." % pf)