@@ -23,12 +23,14 @@ from ConfigParser import ConfigParser
23
23
24
24
from swift .common .utils import get_logger
25
25
26
+
26
27
# To search for more types of errors, add the regex to the list below
27
28
error_re = [
28
- 'error.* (sd[a-z])' ,
29
- ' (sd[a-z]).*error' ,
29
+ re . compile ( r'\berror\b.*\b (sd[a-z]{1,2}\d?)\b' ) ,
30
+ re . compile ( r'\b (sd[a-z]{1,2}\d?)\b.*\berror\b' ) ,
30
31
]
31
32
33
+
32
34
def get_devices (device_dir , logger ):
33
35
devices = []
34
36
for line in open ('/proc/mounts' ).readlines ():
@@ -50,13 +52,14 @@ def get_devices(device_dir, logger):
50
52
device ['minor' ] = str (os .minor (device_num ))
51
53
devices .append (device )
52
54
for line in open ('/proc/partitions' ).readlines ()[2 :]:
53
- major ,minor ,blocks ,kernel_device = line .strip ().split ()
55
+ major , minor , blocks , kernel_device = line .strip ().split ()
54
56
device = [d for d in devices
55
57
if d ['major' ] == major and d ['minor' ] == minor ]
56
58
if device :
57
59
device [0 ]['kernel_device' ] = kernel_device
58
60
return devices
59
61
62
+
60
63
def get_errors (minutes ):
61
64
errors = {}
62
65
start_time = datetime .datetime .now () - datetime .timedelta (minutes = minutes )
@@ -65,26 +68,29 @@ def get_errors(minutes):
65
68
# Ignore anything before the last boot
66
69
errors = {}
67
70
continue
68
- log_time_string = '%s %s' % (start_time .year ,' ' .join (line .split ()[:3 ]))
71
+ log_time_string = '%s %s' % (start_time .year ,
72
+ ' ' .join (line .split ()[:3 ]))
69
73
log_time = datetime .datetime .strptime (
70
- log_time_string ,'%Y %b %d %H:%M:%S' )
74
+ log_time_string , '%Y %b %d %H:%M:%S' )
71
75
if log_time > start_time :
72
76
for err in error_re :
73
- for device in re .findall (err , line ):
74
- errors [device ] = errors .get (device ,0 ) + 1
77
+ for device in err .findall (line ):
78
+ errors [device ] = errors .get (device , 0 ) + 1
75
79
return errors
76
80
81
+
77
82
def comment_fstab (mount_point ):
78
83
with open ('/etc/fstab' , 'r' ) as fstab :
79
84
with open ('/etc/fstab.new' , 'w' ) as new_fstab :
80
85
for line in fstab :
81
86
parts = line .split ()
82
87
if len (parts ) > 2 and line .split ()[1 ] == mount_point :
83
- new_fstab .write ('#' + line )
88
+ new_fstab .write ('#' + line )
84
89
else :
85
90
new_fstab .write (line )
86
91
os .rename ('/etc/fstab.new' , '/etc/fstab' )
87
92
93
+
88
94
if __name__ == '__main__' :
89
95
c = ConfigParser ()
90
96
try :
@@ -108,16 +114,15 @@ if __name__ == '__main__':
108
114
errors = get_errors (minutes )
109
115
logger .debug ("Errors found: %s" % str (errors ))
110
116
unmounts = 0
111
- for kernel_device ,count in errors .items ():
117
+ for kernel_device , count in errors .items ():
112
118
if count >= error_limit :
113
- device = [d for d in devices
114
- if d ['kernel_device' ].startswith (kernel_device )]
119
+ device = [d for d in devices if d ['kernel_device' ] == kernel_device ]
115
120
if device :
116
121
mount_point = device [0 ]['mount_point' ]
117
- if mount_point .startswith ('/srv/node' ):
122
+ if mount_point .startswith (device_dir ):
118
123
logger .info ("Unmounting %s with %d errors" %
119
124
(mount_point , count ))
120
- subprocess .call (['umount' ,'-fl' ,mount_point ])
125
+ subprocess .call (['umount' , '-fl' , mount_point ])
121
126
logger .info ("Commenting out %s from /etc/fstab" %
122
127
(mount_point ))
123
128
comment_fstab (mount_point )
0 commit comments