-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmegaraid-checkup
executable file
·449 lines (389 loc) · 14.2 KB
/
megaraid-checkup
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
#!/usr/bin/perl
# vim:sw=4:sts=4:ts=8:et
#
# Script for checking for MegaRAID controller status
#
# Written by Niklas Edmundsson in May 2021, based on older scripts.
# Copyright (C) 2023 Niklas Edmundsson <nikke@accum.se>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
use warnings;
use strict;
use JSON;
use Data::Dumper;
$Data::Dumper::Sortkeys = 1;
$Data::Dumper::Indent = 1;
my %thresholds = (
'Media Errors' => 10, # Avoid noise from spurious errors
);
# FIXME: This is currently hard-coded to work with storcli (IBM/Lenovo etc)
# and perccli (Dell) on amd64/x86_64.
$ENV{PATH}="/opt/MegaRAID/storcli:/opt/MegaRAID/perccli:$ENV{PATH}";
my @cli = ("storcli64", "perccli64");
my $ctrltotal=0;
my $vdtotal=0;
my $pdtotal=0;
my $enctotal=0;
# Acronyms output by various storcli commands, incomplete list.
my %lsiacro = (
# From show
Ctl => 'Controller Index',
DGs => 'Drive groups',
VDs => 'Virtual drives',
Fld => 'Failed',
PDs => 'Physical drives',
DNOpt => 'DG NotOptimal',
VNOpt => 'VD NotOptimal',
Opt => 'Optimal',
Msng => 'Missing',
Dgd => 'Degraded',
NdAtn => 'Need Attention',
Unkwn => 'Unknown',
sPR => 'Scheduled Patrol Read',
DS => 'DimmerSwitch',
EHS => 'Emergency Hot Spare',
Y => 'Yes',
N => 'No',
ASOs => 'Advanced Software Options',
BBU => 'Battery backup unit',
Hlth => 'Health',
Safe => 'Safe-mode boot',
# From /c0/eall/sall show all
DHS => 'Dedicated Hot Spare',
UGood => 'Unconfigured Good',
GHS => 'Global Hotspare',
UBad => 'Unconfigured Bad',
Onln => 'Online',
Offln => 'Offline',
UGUnsp => 'Unsupported',
UGShld => 'UnConfigured shielded',
HSPShld => 'Hotspare shielded',
CFShld => 'Configured shielded',
Cpybck => 'CopyBack',
CBShld => 'Copyback Shielded',
# From /c0/vall show
Cac => 'CacheCade',
Rec => 'Recovery',
OfLn => 'OffLine',
Pdgd => 'Partially Degraded',
Dgrd => 'Degraded',
Optl => 'Optimal',
RO => 'Read Only',
RW => 'Read Write',
HD => 'Hidden',
B => 'Blocked',
Consist => 'Consistent',
# From /c0 show all (Topology)
Rbld => 'Rebuild',
Offln => 'Offline',
Frgn => 'Foreign',
dflt => 'Default',
Msng => 'Missing',
FSpace => 'Free Space Present'
);
sub showacro
{
my ($acro) = @_;
if($lsiacro{$acro}) {
return $lsiacro{$acro};
}
return $acro;
}
sub runcmd
{
my ($cmd) = @_;
my $json = `$cmd J 2>/dev/null`;
if(!$json) {
# $cmd not found
return undef;
}
my $data;
# decode_json croaks on error, so need to catch it if we don't want to
# abort script...
eval {
$data = decode_json($json);
};
if($@) {
warn "runcmd(): $cmd failed: $@";
return undef;
}
if($ENV{DEBUG}) {
print STDERR "runcmd(): '$cmd' data:\n";
print STDERR Dumper $data;
}
if($data && $data->{Controllers}) {
foreach my $ctrl( @{$data->{Controllers}} ) {
if(!$ctrl->{'Response Data'}) {
warn "runcmd(): $cmd: No Response Data";
$data = undef;
last;
}
}
}
else {
warn "runcmd(): No data or data->{Controllers}";
$data = undef;
}
return $data;
}
# -------------------------------------------------------
# main()
my $ret = 0;
foreach my $storcli (@cli) {
my $data = runcmd("$storcli show");
next unless($data);
# 'Number of Controllers' is sum of megaraid and IT mode controllers, but
# a good basic check to catch a drastic JSON format change.
if(!defined($data->{Controllers}[0]{'Response Data'}{'Number of Controllers'})) {
die "Unable to determine Number of Controllers from $storcli, check if JSON output has changed";
next;
}
# The actual number of megaraid controllers is actually the size of the
# 'System Overview' array, if it exists!
my $ctrlsubtotal = 0;
if(exists($data->{Controllers}[0]{'Response Data'}{'System Overview'})) {
$ctrlsubtotal = scalar @{$data->{Controllers}[0]{'Response Data'}{'System Overview'}};
}
if($ctrlsubtotal < 1) {
next;
}
$ctrltotal += $ctrlsubtotal;
for my $c (@{$data->{Controllers}[0]{'Response Data'}{'System Overview'}}) {
my @summary;
my @faults;
my @warns;
my $ctrl = $c->{Ctl};
# Double-check that the keys we use from the summary exists
foreach my $key (qw(DNOpt DGs VNOpt VDs BBU Hlth)) {
if(!defined($c->{$key})) {
die "Unable to determine find $key from $storcli, check if JSON output has changed";
}
}
# The overview is good enough to detect brokenness and emit a
# short summary message.
if($c->{DNOpt}) {
push @summary, "$c->{DNOpt} of $c->{DGs} Drive groups NOT OK";
}
if($c->{VNOpt}) {
push @summary, "$c->{VNOpt} of $c->{VDs} Virtual drives NOT OK";
}
if($c->{BBU} !~ /^(Opt|Msng|N\/A)$/) {
push @summary, "BBU " . showacro($c->{BBU});
}
my $cshowall = runcmd("$storcli /c$ctrl show all");
if(!$cshowall) {
die "Unable to get controller $ctrl show all from $storcli";
}
my $rebuilding = 0;
# We need to collect the drive info from the various arrays into a hash
# so we can emit useful message/status based on the overall state.
my %drv;
foreach my $t (@{$cshowall->{Controllers}[0]{'Response Data'}{'TOPOLOGY'}})
{
# Deliberately ignore per-array status, it shows up as
# degraded until rebuild is done which screws things up
# for us...
next if($t->{DG}=~/^\d+$/ && $t->{Arr}=~/^\d+$/ && $t->{Row} eq '-');
if($t->{State} eq "Rbld") {
$rebuilding++;
}
elsif($t->{State} eq "Msng") {
# Completely missing (ie. logically/physically unplugged
# status) don't show up in the drive list, only in the topology,
# so we need to emit something useful/helpful here.
push @faults, "Physical Drive $t->{DG}/$t->{Arr}/$t->{Row} Missing";
}
}
my %vall;
if($cshowall->{Controllers}[0]{'Response Data'}{'VD LIST'}) {
my $vshowall = runcmd("$storcli /c$ctrl/vall show all");
if(!$vshowall) {
die "Unable to get controller $ctrl show vall from $storcli";
}
my $bgi = runcmd("$storcli /c$ctrl/vall show bgi");
# Let's just have a mental meltdown for a moment and cry, then
# rearrange the data structure we get from storcli into hierarchy
# instead of a flat-ish mix of arrays and hashes...
while (my ($k, $v) = each %{$vshowall->{Controllers}[0]{'Response Data'}})
{
next unless($k =~ m#^/c.*/v(\d+)#);
my $vd = $1;
$vall{$k}{info} = $v->[0];
$vall{$k}{properties} = $vshowall->{Controllers}[0]{'Response Data'}{"VD$vd Properties"};
foreach my $d (@{$vshowall->{Controllers}[0]{'Response Data'}{"PDs for VD $vd"}})
{
# FIXME: Or point at relevant %drvs entry?
$vall{$k}{pds}{$d->{'EID:Slt'}} = $d;
}
}
foreach my $b (@{$bgi->{Controllers}[0]{'Response Data'}{'VD Operation Status'}})
{
$vall{"/c$ctrl/v$b->{VD}"}{bgi} = $b
}
}
if($ENV{DEBUG}) {
print "vall:\n";
print Dumper \%vall;
}
my $numvds = scalar keys %vall;
if($numvds) {
$vdtotal += $numvds;
}
foreach my $vd (sort keys %vall)
{
# $vd is on the form /cX/vY
my $info = $vall{$vd}{info};
my $prop = $vall{$vd}{properties};
my $bgi = $vall{$vd}{bgi};
my $vdstr = "Virtual Drive $vd ($info->{Name}, $info->{TYPE}, $info->{Size})";
if($info->{State} ne 'Optl') {
push @faults, "$vdstr: State " . showacro($info->{State});
}
if($info->{Access} ne 'RW') {
push @faults, "$vdstr: Access $info->{Access}";
}
if($prop->{'Active Operations'} ne 'None' && $prop->{'Active Operations'} !~ /Background Initialization/i) {
push @faults, "$vdstr: $prop->{'Active Operations'}";
}
if($bgi->{Status} eq 'In progress')
{
push @faults, "$vdstr: BG Init ($bgi->{'Progress%'}%, ETA $bgi->{'Estimated Time Left'})";
}
elsif($info->{Consist} ne 'Yes') {
push @faults, "$vdstr: NOT Consistent";
}
}
my $numpds = $cshowall->{Controllers}[0]{'Response Data'}{'Physical Drives'};
$pdtotal += $numpds;
foreach my $p (@{$cshowall->{Controllers}[0]{'Response Data'}{'PD LIST'}})
{
my $d;
if($p->{'EID:Slt'} =~ /^(\S+):(\S+)$/) {
$d = "/c$ctrl/e$1/s$2";
$drv{$d}{state} = $p->{State};
}
}
if($rebuilding) {
my $rebuild = runcmd("$storcli /c$ctrl/eall/sall show rebuild");
if(!$rebuild) {
warn "Unable to get ctrl $ctrl drive rebuild status from $storcli";
last;
}
foreach my $r (@{$rebuild->{Controllers}[0]{'Response Data'}}) {
$drv{$r->{'Drive-ID'}}{progress} = $r->{'Progress%'};
}
}
my $drvinfo = runcmd("$storcli /c$ctrl/eall/sall show all");
if(!$drvinfo) {
warn "Unable to get ctrl $ctrl drive show all from $storcli";
last;
}
foreach my $s (keys %drv) {
my $ddi = $drvinfo->{Controllers}[0]{'Response Data'}{"Drive $s - Detailed Information"};
next unless($ddi);
$drv{$s}{smartalert} = $ddi->{"Drive $s State"}{'S.M.A.R.T alert flagged by drive'};
foreach my $itm ('Predictive Failure', 'Media Error') {
my $val = $ddi->{"Drive $s State"}{"$itm Count"};
if($val > 0) {
$drv{$s}{"${itm}s"} = $val;
}
}
}
if($ENV{DEBUG}) {
print "drv:\n";
print Dumper \%drv;
}
my $drvucount = 0;
my $drvrebuild = 0;
my $drvnonopt = 0;
foreach my $d (sort keys %drv) {
my $ds = $drv{$d}{state};
if($ds !~ /^(Onln|JBOD|GHS|DHS)$/) {
if($ds eq 'UGood') {
$drvucount++;
push @warns, "Physical Drive $d Unused";
}
else {
if($ds eq 'Rbld') {
$drvrebuild++;
}
else {
$drvnonopt++;
}
my $extra="";
if(defined($drv{$d}{progress})) {
$extra = " ($drv{$d}{progress}%)";
}
push @faults, "Physical Drive $d State " . showacro($ds) . $extra;
# Don't check/complain again for this drive
next;
}
}
if($drv{$d}{smartalert} && $drv{$d}{smartalert} ne 'No') {
push @faults, "Physical Drive $d: SMART alert";
}
foreach my $s (('Predictive Failures', 'Media Errors'))
{
if($drv{$d}{$s} && ($thresholds{$s} // 0) < $drv{$d}{$s}) {
push @faults, "Physical Drive $d: $s $drv{$d}{$s}";
}
}
}
my $encs = runcmd("$storcli /c$ctrl/eall show all");
if(!$encs) {
warn "Unable to get ctrl $ctrl enclosure show all from $storcli";
last;
}
foreach my $enc (sort keys %{$encs->{Controllers}[0]{'Response Data'}}) {
$enctotal++;
my $status = $encs->{Controllers}[0]{'Response Data'}{$enc}{Information}{Status};
if(!$status) {
warn "Unable to determine enclosure status from $storcli, check if JSON output has changed";
last;
}
$enc =~ s/^Enclosure //i; # Drop "Enclosure" text
$enc =~ s/ *$//; # Drop trailing space...
if($status ne 'OK') {
push @faults, "Enclosure $enc $status";
}
}
# --------------------------------------------------------------
if($drvucount) {
push @summary, "$drvucount of $c->{PDs} Physical drives unused";
}
if($drvrebuild) {
push @summary, "$drvrebuild of $c->{PDs} Physical drives Rebuilding";
}
if($drvnonopt) {
push @summary, "$drvnonopt of $c->{PDs} Physical drives NOT OK";
}
if(@summary || @faults || @warns) {
$ret = 1;
print "/c$ctrl ($c->{Model}): " . showacro($c->{Hlth}) . "\n";
if(@summary) {
print " ", join("\n ", @summary) . "\n";
}
if(@faults) {
print " ", join("\n ", @faults) . "\n";
}
if(@warns) {
print " ", join("\n ", @warns) . "\n";
}
}
}
}
if($ctrltotal < 1) {
warn "No MegaRAID controllers found";
}
exit $ret;