From 463da7e170bb185fd80862c948ab73d24ed7f9d5 Mon Sep 17 00:00:00 2001 From: Andy McCrae Date: Tue, 11 Sep 2012 14:22:11 +0000 Subject: [PATCH] Adds Error Handling to swift-drive-audit for missing or unreadable /var/log/kern.log Fixes Bug 1049081 Change-Id: If977080350cc5cdb6bc633b6af7d3c490ed23d46 --- AUTHORS | 1 + bin/swift-drive-audit | 33 +++++++++++++++++++-------------- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/AUTHORS b/AUTHORS index 25e751e366..b803239e48 100644 --- a/AUTHORS +++ b/AUTHORS @@ -52,6 +52,7 @@ Dragos Manolescu (dragosm@hp.com) Juan J. Martinez (juan@memset.com) Marcelo Martins (btorch@gmail.com) Donagh McCabe (donagh.mccabe@hp.com) +Andy McCrae (andy.mccrae@gmail.com) Paul McMillan (paul.mcmillan@nebula.com) Ewan Mellor (ewan.mellor@citrix.com) Samuel Merritt (sam@swiftstack.com) diff --git a/bin/swift-drive-audit b/bin/swift-drive-audit index 06a21bd391..e7926aeab2 100755 --- a/bin/swift-drive-audit +++ b/bin/swift-drive-audit @@ -63,20 +63,25 @@ def get_devices(device_dir, logger): def get_errors(minutes): errors = {} start_time = datetime.datetime.now() - datetime.timedelta(minutes=minutes) - for line in open('/var/log/kern.log'): - if '[ 0.000000]' in line: - # Ignore anything before the last boot - errors = {} - continue - log_time_string = '%s %s' % (start_time.year, - ' '.join(line.split()[:3])) - log_time = datetime.datetime.strptime( - log_time_string, '%Y %b %d %H:%M:%S') - if log_time > start_time: - for err in error_re: - for device in err.findall(line): - errors[device] = errors.get(device, 0) + 1 - return errors + try: + for line in open('/var/log/kern.log'): + if '[ 0.000000]' in line: + # Ignore anything before the last boot + errors = {} + continue + log_time_string = '%s %s' % (start_time.year, + ' '.join(line.split()[:3])) + log_time = datetime.datetime.strptime( + log_time_string, '%Y %b %d %H:%M:%S') + if log_time > start_time: + for err in error_re: + for device in err.findall(line): + errors[device] = errors.get(device, 0) + 1 + return errors + except IOError: + logger.error("Error: Unable to open /var/log/kern.log") + print("Unable to open /var/log/kern.log") + sys.exit(1) def comment_fstab(mount_point):