diff --git a/bin/swift-drive-audit b/bin/swift-drive-audit index d269b25414..514db51a30 100755 --- a/bin/swift-drive-audit +++ b/bin/swift-drive-audit @@ -23,12 +23,14 @@ from ConfigParser import ConfigParser from swift.common.utils import get_logger + # To search for more types of errors, add the regex to the list below error_re = [ - 'error.*(sd[a-z])', - '(sd[a-z]).*error', + re.compile(r'\berror\b.*\b(sd[a-z]{1,2}\d?)\b'), + re.compile(r'\b(sd[a-z]{1,2}\d?)\b.*\berror\b'), ] + def get_devices(device_dir, logger): devices = [] for line in open('/proc/mounts').readlines(): @@ -50,13 +52,14 @@ def get_devices(device_dir, logger): device['minor'] = str(os.minor(device_num)) devices.append(device) for line in open('/proc/partitions').readlines()[2:]: - major,minor,blocks,kernel_device = line.strip().split() + major, minor, blocks, kernel_device = line.strip().split() device = [d for d in devices if d['major'] == major and d['minor'] == minor] if device: device[0]['kernel_device'] = kernel_device return devices + def get_errors(minutes): errors = {} start_time = datetime.datetime.now() - datetime.timedelta(minutes=minutes) @@ -65,26 +68,29 @@ def get_errors(minutes): # Ignore anything before the last boot errors = {} continue - log_time_string = '%s %s' % (start_time.year,' '.join(line.split()[:3])) + 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') + log_time_string, '%Y %b %d %H:%M:%S') if log_time > start_time: for err in error_re: - for device in re.findall(err,line): - errors[device] = errors.get(device,0) + 1 + for device in err.findall(line): + errors[device] = errors.get(device, 0) + 1 return errors + def comment_fstab(mount_point): with open('/etc/fstab', 'r') as fstab: with open('/etc/fstab.new', 'w') as new_fstab: for line in fstab: parts = line.split() if len(parts) > 2 and line.split()[1] == mount_point: - new_fstab.write('#' + line) + new_fstab.write('#' + line) else: new_fstab.write(line) os.rename('/etc/fstab.new', '/etc/fstab') + if __name__ == '__main__': c = ConfigParser() try: @@ -108,16 +114,15 @@ if __name__ == '__main__': errors = get_errors(minutes) logger.debug("Errors found: %s" % str(errors)) unmounts = 0 - for kernel_device,count in errors.items(): + for kernel_device, count in errors.items(): if count >= error_limit: - device = [d for d in devices - if d['kernel_device'].startswith(kernel_device)] + device = [d for d in devices if d['kernel_device'] == kernel_device] if device: mount_point = device[0]['mount_point'] - if mount_point.startswith('/srv/node'): + if mount_point.startswith(device_dir): logger.info("Unmounting %s with %d errors" % (mount_point, count)) - subprocess.call(['umount','-fl',mount_point]) + subprocess.call(['umount', '-fl', mount_point]) logger.info("Commenting out %s from /etc/fstab" % (mount_point)) comment_fstab(mount_point)