make drive audit regexes detect 4-letter drives
addresses bug 827913 Change-Id: I691eee191f5951186158c553281f88aae9e5d25f
This commit is contained in:
parent
296de6ed76
commit
b3c2800497
@ -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,15 +68,17 @@ 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:
|
||||
@ -85,6 +90,7 @@ def comment_fstab(mount_point):
|
||||
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)
|
||||
|
Loading…
Reference in New Issue
Block a user