make drive audit regexes detect 4-letter drives

addresses bug 827913

Change-Id: I691eee191f5951186158c553281f88aae9e5d25f
This commit is contained in:
Mike Barton 2011-09-30 00:17:35 +00:00
parent 296de6ed76
commit b3c2800497

View File

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