forked from omame/megaclisas-status
-
Notifications
You must be signed in to change notification settings - Fork 0
/
megaclisas-status
executable file
·251 lines (221 loc) · 8.9 KB
/
megaclisas-status
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#!/usr/bin/python
import os
import re
from sys import argv, exit
def exithelp(error_code=0):
'''
Print help message and exit
:error_code: Exit code
'''
print 'Usage: %s [--nagios]' % argv[0]
exit(error_code)
def exiterror(msg, error_code=2):
'''
Exit with a specific error code (default 2)
:msg: Message to be returned
:error_code: Exit code
'''
print 'ERROR - ' + msg
exit(error_code)
def get_output(flags):
'''
Execute megacli with the specified flags and return the output
Assumes that none of the keys in the output has a colon (:) in the name
:flags: flags for megacli
:return: a dictionary with the output of the command
'''
cmd = binarypath + ' ' + flags
output = os.popen(cmd).read()
lines = []
for line in output.split('\n'):
if ':' in line:
k, v = line.split(':', 1)
lines += [(k.strip(), v.strip())]
return lines
def get_info(key, input, output_format='string'):
'''
Extract information from the output of get_output
:key: the regexp for the needed key
:input: output from get_output
:output_format: preferred output format (list or string)
:return: the requested information as a list or string
'''
res = []
try:
for elem in input:
if re.match(key, elem[0]):
if output_format == 'string':
return elem[1]
else:
res.append(elem[1])
return res
except KeyError:
print 'ERROR - Key %s not found' % key
exit(2)
nagiosmode = False
binarypath = '/usr/sbin/megacli'
something_is_wrong = False
# We are going to put everything into a dictionary
# Sample structure:
# {'c0': {'arrays': {'c0u0': ['drives_number', 'RAID1', '465G', 'Optimal', 'None'],
# 'c0u1': ['drives_number', 'RAID1', '465G', 'Optimal', 'None']},
# 'disks': [[controller_number,
# virtual_disk,
# disk_id,
# serial_number,
# status],
# [controller_number,
# virtual_disk,
# disk_id,
# serial_number,
# status],
# [controller_number,
# virtual_disk,
# disk_id,
# serial_number,
# status],
# [controller_number,
# virtual_disk,
# disk_id,
# serial_number,
# status]],
# 'model': model}}
resulting_info = {}
# Some nagios counters
nagiosgoodarray = 0
nagiosbadarray = 0
nagiosgooddisk = 0
nagiosbaddisk = 0
if '--nagios' in argv:
nagiosmode = True
if len(argv) > 2:
exithelp(1)
# Check binary exists and can be executed.
# If not, return UNKNOWN nagios error code or a console message.
if not os.path.exists(binarypath) and os.access(binarypath, os.X_OK):
if nagiosmode:
print 'UNKNOWN - Cannot find %s' % binarypath
else:
print 'Cannot find %s. Please install it.' % binarypath
exit(3)
# Get the number of controllers
output = get_output('-adpCount -NoLog')
controllers_number = int(get_info('Controller Count', output).rstrip('.'))
if not controllers_number:
if nagiosmode:
print 'UNKNOWN - No controllers found'
else:
print 'No controllers found'
exit(3)
# Start collecting info about the controllers
for controller_number in range(controllers_number):
controller_id = 'c%d' % controller_number
output = get_output('-AdpAllInfo -a%d -NoLog' % controller_number)
controllermodel = get_info('Product Name', output)
resulting_info[controller_id] = {}
resulting_info[controller_id]['model'] = controllermodel
# Now for the array info
output = get_output('-LdGetNum -a%d -NoLog' % controller_number)
arrays_number = int(get_info(r'^Number of Virtual (Disk|Drive).*$', output))
resulting_info[controller_id]['arrays'] = {}
for array_number in range(arrays_number):
# Temporary list to store information for each array
# Format: [drives_number, raid_type, size, state, operation]
this_array = []
array_id = 'c%du%d' % (controller_number, array_number)
output = get_output('-LDInfo -l%d -a%d -NoLog' % \
(array_number, controller_number))
drives_number = get_info(r'Number Of Drives\s*((per span))?', output)
if not drives_number:
exiterror('Cannot any drives in array ' + array_id)
span_depth = get_info('Span Depth', output)
if not span_depth:
exiterror('Invalid span depth for array' + array_id)
raid_level_string = get_info('RAID Level', output)
if not raid_level_string:
exiterror('Cannot fetch raid level for array ' + array_id)
# Extract the raid level
raid_level = raid_level_string.split(',')[0].split('-')[1]
raid_type = 'RAID' + raid_level
# Not too sure what this does
if drives_number and (int(span_depth) > 1):
drives_number = int(drives_number) * int(span_depth)
if int(raid_level) < 10:
raid_type = raid_type + "0"
size = get_info('Size', output)
if not size:
exiterror('Cannot fetch size for drive ', )
if 'MB' in size:
size = str(int(round((float(size.rstrip(' MB')) / 1000)))) + 'G'
elif 'TB' in size:
size = str(int(round((float(size.rstrip(' TB')) * 1000)))) + 'G'
else:
size = str(int(round((float(size.rstrip(' GB')))))) + 'G'
state = get_info('State', output)
if state != 'Optimal':
something_is_wrong = True
nagiosbadarray += 1
else:
nagiosgoodarray += 1
operation = get_info('Ongoing Progresses', output)
if operation:
# We need the following line for the description of what's going on
operation = [output[output.index(tup) + 1] for tup in output
if tup[0] == 'Ongoing Progresses']
if not operation:
operation = 'None'
this_array = [str(drives_number), raid_type, size, state, operation]
resulting_info[controller_id]['arrays'][array_id] = this_array
# Finally moving to physical disks
resulting_info[controller_id]['disks'] = []
output = get_output('-LdPdInfo -a%d -NoLog' % \
controller_number)
# There can be different enclosures attached to the same controller
enclosure_numbers = get_info('Enclosure Device ID', output, 'list')
slot_numbers = get_info('Slot Number', output, 'list')
disk_identifiers = [a.split()[0] for a in get_info(r'PD$', output, 'list')]
slots_list = zip(enclosure_numbers, slot_numbers, disk_identifiers)
for enclosure_number, slot_id, disk_id in slots_list:
if not enclosure_number or enclosure_number == 'N/A':
exiterror('Cannot fetch enclosure ID for drive %d on ' +
'controller %d' % controller_number)
output = get_output('-PDInfo -PhysDrv \[%s:%s\] -a%d' % \
(enclosure_number, slot_id, controller_number))
state = get_info('Firmware state', output)
if state not in ['Online', 'Online, Spun Up']:
something_is_wrong = True
nagiosbaddisk += 1
else:
nagiosgooddisk += 1
model = re.sub(r'\s+', ' ', get_info('Inquiry Data', output))
virtual_disk_string = get_info('Drive\'s postion', output)
virtual_disk = virtual_disk_string.split(',')[0].split()[1]
resulting_info[controller_id]['disks'].append([str(controller_number),
virtual_disk, disk_id, model, state])
# Now let's print something
if nagiosmode:
status_line = 'Arrays: OK:%d Bad:%d - Disks: OK:%d Bad:%d' % \
(nagiosgoodarray, nagiosbadarray, nagiosgooddisk, nagiosbaddisk)
if something_is_wrong:
exiterror(status_line)
else:
print 'RAID OK - ' + status_line
else:
print '-- Controller info --'
print '-- ID | Model'
for controller in sorted(resulting_info.keys()):
print '%s | %s' % (controller, resulting_info[controller]['model'])
print ''
print '-- Arrays info --'
print '-- ID | Drives | Type | Size | Status | InProgress'
for controller in sorted(resulting_info.keys()):
for array in sorted(resulting_info[controller]['arrays'].keys()):
print '%s | %s' % (array,
' | '.join(resulting_info[controller]['arrays'][array]))
print ''
print '-- Disks info --'
print '-- ID | Model | Status'
for controller in sorted(resulting_info.keys()):
for disk in sorted(resulting_info[controller]['disks']):
print 'c%su%sp%s | %s' % (disk[0], disk[1], disk[2],
' | '.join(disk[3:]))