]> git.uio.no Git - check_openmanage.git/blob - check_openmanage
* version 3.5.7-beta11
[check_openmanage.git] / check_openmanage
1 #!/usr/bin/perl
2 #
3 # Nagios plugin
4 #
5 # Monitor Dell server hardware status using Dell OpenManage Server
6 # Administrator, either locally via NRPE, or remotely via SNMP.
7 #
8 # $Id$
9 #
10 # Copyright (C) 2010 Trond H. Amundsen
11 #
12 # This program is free software: you can redistribute it and/or modify
13 # it under the terms of the GNU General Public License as published by
14 # the Free Software Foundation, either version 3 of the License, or
15 # (at your option) any later version.
16 #
17 # This program is distributed in the hope that it will be useful, but
18 # WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20 # General Public License for more details.
21 #
22 # You should have received a copy of the GNU General Public License
23 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
24 #
25
26 require 5.006;  # Perl v5.6.0 or newer is required
27 use strict;
28 use warnings;
29 use POSIX qw(isatty ceil);
30 use Getopt::Long qw(:config no_ignore_case);
31
32 # Global (package) variables used throughout the code
33 use vars qw( $NAME $VERSION $AUTHOR $CONTACT $E_OK $E_WARNING $E_CRITICAL
34              $E_UNKNOWN $FW_LOCK $USAGE $HELP $LICENSE
35              $snmp_session $snmp_error $omreport $globalstatus $global
36              $linebreak $omopt_chassis $omopt_system $blade
37              $exit_code $snmp $original_sigwarn
38              %check %opt %perfdata %reverse_exitcode %status2nagios
39              %snmp_status %snmp_probestatus %probestatus2nagios %sysinfo
40              %blacklist %nagios_alert_count %count
41              @perl_warnings @controllers @enclosures
42              @report_storage @report_chassis @report_other
43           );
44
45 #---------------------------------------------------------------------
46 # Initialization and global variables
47 #---------------------------------------------------------------------
48
49 # Small subroutine to collect any perl warnings during execution
50 sub collect_perl_warning {
51   push @perl_warnings, [@_];
52 }
53
54 # Set the WARN signal to use our collect subroutine above
55 $original_sigwarn = $SIG{__WARN__};
56 $SIG{__WARN__} = \&collect_perl_warning;
57
58 # Version and similar info
59 $NAME    = 'check_openmanage';
60 $VERSION = '3.5.7-beta11';
61 $AUTHOR  = 'Trond H. Amundsen';
62 $CONTACT = 't.h.amundsen@usit.uio.no';
63
64 # Exit codes
65 $E_OK       = 0;
66 $E_WARNING  = 1;
67 $E_CRITICAL = 2;
68 $E_UNKNOWN  = 3;
69
70 # Firmware update lock file [FIXME: location on Windows?]
71 $FW_LOCK = '/var/lock/.spsetup';  # default on Linux
72
73 # Usage text
74 $USAGE = <<"END_USAGE";
75 Usage: $NAME [OPTION]...
76 END_USAGE
77
78 # Help text
79 $HELP = <<'END_HELP';
80
81 GENERAL OPTIONS:
82
83    -p, --perfdata      Output performance data
84    -t, --timeout       Plugin timeout in seconds
85    -c, --critical      Customise temperature critical limits
86    -w, --warning       Customise temperature warning limits
87    -d, --debug         Debug output, reports everything
88    -h, --help          Display this help text
89    -V, --version       Display version info
90
91 SNMP OPTIONS:
92
93    -H, --hostname      Hostname or IP of the server (needed for SNMP)
94    -C, --community     SNMP community string
95    -P, --protocol      SNMP protocol version
96    --port              SNMP port number
97
98 OUTPUT OPTIONS:
99
100    -i, --info          Prefix any alerts with the service tag
101    -e, --extinfo       Append system info to alerts
102    -s, --state         Prefix alerts with alert state
103    -S, --short-state   Prefix alerts with alert state (abbreviated)
104    -o, --okinfo        Verbosity when check result is OK
105    -I, --htmlinfo      HTML output with clickable links
106
107 CHECK CONTROL AND BLACKLISTING:
108
109    -a, --all           Check everything, even log content
110    -b, --blacklist     Blacklist missing and/or failed components
111    --only              Only check a certain component or alert type
112    --check             Fine-tune which components are checked
113
114 For more information and advanced options, see the manual page or URL:
115   http://folk.uio.no/trondham/software/check_openmanage.html
116 END_HELP
117
118 # Version and license text
119 $LICENSE = <<"END_LICENSE";
120 $NAME $VERSION
121 Copyright (C) 2010 $AUTHOR
122 License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
123 This is free software: you are free to change and redistribute it.
124 There is NO WARRANTY, to the extent permitted by law.
125
126 Written by $AUTHOR <$CONTACT>
127 END_LICENSE
128
129 # Options with default values
130 %opt = ( 'blacklist'         => [],       # blacklisting
131          'check'             => [],       # check control
132          'critical'          => [],       # temperature critical limits
133          'warning'           => [],       # temperature warning limits
134          'timeout'           => 30,       # default timeout is 30 seconds
135          'debug'             => 0,        # debugging / verbose output
136          'help'              => 0,        # display help output
137          'perfdata'          => undef,    # output performance data
138          'info'              => 0,        # display servicetag
139          'extinfo'           => 0,        # display extra info
140          'htmlinfo'          => undef,    # html tags in output
141          'postmsg'           => undef,    # post message
142          'state'             => 0,        # display alert type
143          'short-state'       => 0,        # display alert type (short)
144          'okinfo'            => 0,        # default "ok" output level
145          'linebreak'         => undef,    # specify linebreak
146          'version'           => 0,        # plugin version info
147          'all'               => 0,        # check everything
148          'only'              => undef,    # only one component
149          'omreport'          => undef,    # omreport path
150          'port'              => 161,      # default SNMP port
151          'hostname'          => undef,    # hostname or IP
152          'community'         => 'public', # SMNP v1 or v2c
153          'protocol'          => 2,        # default SNMP protocol 2c
154          'username'          => undef,    # SMNP v3
155          'authpassword'      => undef,    # SMNP v3
156          'authkey'           => undef,    # SMNP v3
157          'authprotocol'      => undef,    # SMNP v3
158          'privpassword'      => undef,    # SMNP v3
159          'privkey'           => undef,    # SMNP v3
160          'privprotocol'      => undef,    # SMNP v3
161          'use_get_table'     => 0,        # hack for SNMPv3 on Windows with net-snmp
162        );
163
164 # Get options
165 GetOptions('b|blacklist=s'      => \@{ $opt{blacklist} },
166            'check=s'            => \@{ $opt{check} },
167            'c|critical=s'       => \@{ $opt{critical} },
168            'w|warning=s'        => \@{ $opt{warning} },
169            't|timeout=i'        => \$opt{timeout},
170            'd|debug'            => \$opt{debug},
171            'h|help'             => \$opt{help},
172            'V|version'          => \$opt{version},
173            'p|perfdata:s'       => \$opt{perfdata},
174            'i|info'             => \$opt{info},
175            'e|extinfo'          => \$opt{extinfo},
176            'I|htmlinfo:s'       => \$opt{htmlinfo},
177            'postmsg=s'          => \$opt{postmsg},
178            's|state'            => \$opt{state},
179            'S|short-state'      => \$opt{shortstate},
180            'o|ok-info=i'        => \$opt{okinfo},
181            'linebreak=s'        => \$opt{linebreak},
182            'a|all'              => \$opt{all},
183            'only=s'             => \$opt{only},
184            'omreport=s'         => \$opt{omreport},
185            'port=i'             => \$opt{port},
186            'H|hostname=s'       => \$opt{hostname},
187            'C|community=s'      => \$opt{community},
188            'P|protocol=i'       => \$opt{protocol},
189            'U|username=s'       => \$opt{username},
190            'authpassword=s'     => \$opt{authpassword},
191            'authkey=s'          => \$opt{authkey},
192            'authprotocol=s'     => \$opt{authprotocol},
193            'privpassword=s'     => \$opt{privpassword},
194            'privkey=s'          => \$opt{privkey},
195            'privprotocol=s'     => \$opt{privprotocol},
196            'use-get_table'      => \$opt{use_get_table},
197           ) or do { print $USAGE; exit $E_UNKNOWN };
198
199 # If user requested help
200 if ($opt{help}) {
201     print $USAGE, $HELP;
202     exit $E_OK;
203 }
204
205 # If user requested version info
206 if ($opt{version}) {
207     print $LICENSE;
208     exit $E_OK;
209 }
210
211 # Setting timeout
212 $SIG{ALRM} = sub {
213     print "PLUGIN TIMEOUT: $NAME timed out after $opt{timeout} seconds";
214     if (defined $opt{htmlinfo}) {
215         # check_openmanage.html#plugin-timeout-check-openmanage-timed-out-after-30-seconds
216         print ' [<a href="http://bit.ly/bPOygB">more info</a>]';
217     }
218     print "\n";
219     exit $E_UNKNOWN;
220 };
221 alarm $opt{timeout};
222
223 # If we're using SNMP
224 $snmp = defined $opt{hostname} ? 1 : 0;
225
226 # SNMP session variables
227 $snmp_session = undef;
228 $snmp_error   = undef;
229
230 # The omreport command
231 $omreport = undef;
232
233 # Check flags, override available with the --check option
234 %check = ( 'storage'     => 1,   # check storage subsystem
235            'memory'      => 1,   # check memory (dimms)
236            'fans'        => 1,   # check fan status
237            'power'       => 1,   # check power supplies
238            'temp'        => 1,   # check temperature
239            'cpu'         => 1,   # check processors
240            'voltage'     => 1,   # check voltage
241            'batteries'   => 1,   # check battery probes
242            'amperage'    => 1,   # check power consumption
243            'intrusion'   => 1,   # check intrusion detection
244            'alertlog'    => 0,   # check the alert log
245            'esmlog'      => 0,   # check the ESM log (hardware log)
246            'esmhealth'   => 1,   # check the ESM log overall health
247          );
248
249 # Default line break
250 $linebreak = isatty(*STDOUT) ? "\n" : '<br/>';
251
252 # Line break from option
253 if (defined $opt{linebreak}) {
254     if ($opt{linebreak} eq 'REG') {
255         $linebreak = "\n";
256     }
257     elsif ($opt{linebreak} eq 'HTML') {
258         $linebreak = '<br/>';
259     }
260     else {
261         $linebreak = $opt{linebreak};
262     }
263 }
264
265 # Exit with status=UNKNOWN if there is firmware upgrade in progress
266 if (!$snmp && -f $FW_LOCK) {
267     print "MONITORING DISABLED - Firmware update in progress ($FW_LOCK exists)\n";
268     exit $E_UNKNOWN;
269 }
270
271 # List of controllers and enclosures
272 @controllers = ();  # controllers
273 @enclosures  = ();  # enclosures
274
275 # Messages
276 @report_storage = ();  # messages with associated nagios level (storage)
277 @report_chassis = ();  # messages with associated nagios level (chassis)
278 @report_other   = ();  # messages with associated nagios level (other)
279
280 # Counters for everything
281 %count
282   = (
283      'pdisk'  => 0, # number of physical disks
284      'vdisk'  => 0, # number of logical drives (virtual disks)
285      'temp'   => 0, # number of temperature probes
286      'volt'   => 0, # number of voltage probes
287      'amp'    => 0, # number of amperage probes
288      'intr'   => 0, # number of intrusion probes
289      'dimm'   => 0, # number of memory modules
290      'fan'    => 0, # number of fan probes
291      'cpu'    => 0, # number of CPUs
292      'bat'    => 0, # number of batteries
293      'power'  => 0, # number of power supplies
294      'esm'    => {
295                   'Critical'     => 0, # critical entries in ESM log
296                   'Non-Critical' => 0, # warning entries in ESM log
297                   'Ok'           => 0, # ok entries in ESM log
298                  },
299      'alert'  => {
300                   'Critical'     => 0, # critical entries in alert log
301                   'Non-Critical' => 0, # warning entries in alert log
302                   'Ok'           => 0, # ok entries in alert log
303                  },
304     );
305
306 # Performance data
307 %perfdata = ();
308
309 # Global health status
310 $global         = 1;      # default is to check global status
311 $globalstatus   = $E_OK;  # default global health status is "OK"
312
313 # Nagios error levels reversed
314 %reverse_exitcode
315   = (
316      $E_OK       => 'OK',
317      $E_WARNING  => 'WARNING',
318      $E_CRITICAL => 'CRITICAL',
319      $E_UNKNOWN  => 'UNKNOWN',
320     );
321
322 # OpenManage (omreport) and SNMP error levels
323 %status2nagios
324   = (
325      'Unknown'         => $E_CRITICAL,
326      'Critical'        => $E_CRITICAL,
327      'Non-Critical'    => $E_WARNING,
328      'Ok'              => $E_OK,
329      'Non-Recoverable' => $E_CRITICAL,
330      'Other'           => $E_CRITICAL,
331     );
332
333 # Status via SNMP
334 %snmp_status
335   = (
336      1 => 'Other',
337      2 => 'Unknown',
338      3 => 'Ok',
339      4 => 'Non-Critical',
340      5 => 'Critical',
341      6 => 'Non-Recoverable',
342     );
343
344 # Probe Status via SNMP
345 %snmp_probestatus
346   = (
347      1  => 'Other',               # probe status is not one of the following:
348      2  => 'Unknown',             # probe status is unknown (not known or monitored)
349      3  => 'Ok',                  # probe is reporting a value within the thresholds
350      4  => 'nonCriticalUpper',    # probe has crossed upper noncritical threshold
351      5  => 'criticalUpper',       # probe has crossed upper critical threshold
352      6  => 'nonRecoverableUpper', # probe has crossed upper non-recoverable threshold
353      7  => 'nonCriticalLower',    # probe has crossed lower noncritical threshold
354      8  => 'criticalLower',       # probe has crossed lower critical threshold
355      9  => 'nonRecoverableLower', # probe has crossed lower non-recoverable threshold
356      10 => 'failed',              # probe is not functional
357     );
358
359 # Probe status translated to Nagios alarm levels
360 %probestatus2nagios
361   = (
362      'Other'               => $E_CRITICAL,
363      'Unknown'             => $E_CRITICAL,
364      'Ok'                  => $E_OK,
365      'nonCriticalUpper'    => $E_WARNING,
366      'criticalUpper'       => $E_CRITICAL,
367      'nonRecoverableUpper' => $E_CRITICAL,
368      'nonCriticalLower'    => $E_WARNING,
369      'criticalLower'       => $E_CRITICAL,
370      'nonRecoverableLower' => $E_CRITICAL,
371      'failed'              => $E_CRITICAL,
372     );
373
374 # System information gathered
375 %sysinfo
376   = (
377      'bios'     => 'N/A',  # BIOS version
378      'biosdate' => 'N/A',  # BIOS release date
379      'serial'   => 'N/A',  # serial number (service tag)
380      'model'    => 'N/A',  # system model
381      'osname'   => 'N/A',  # OS name
382      'osver'    => 'N/A',  # OS version
383      'om'       => 'N/A',  # OMSA version
384      'bmc'      => 0,      # HAS baseboard management controller (BMC)
385      'rac'      => 0,      # HAS remote access controller (RAC)
386      'rac_name' => 'N/A',  # remote access controller (RAC)
387      'bmc_fw'   => 'N/A',  # BMC firmware
388      'rac_fw'   => 'N/A',  # RAC firmware
389     );
390
391 # Adjust which checks to perform
392 adjust_checks() if defined $opt{check};
393
394 # Blacklisted components
395 %blacklist = defined $opt{blacklist} ? %{ get_blacklist() } : ();
396
397 # If blacklisting is in effect, don't check global health status
398 if (scalar keys %blacklist > 0) {
399     $global = 0;
400 }
401
402 # Take into account new hardware and blades
403 $omopt_chassis = 'chassis';  # default "chassis" option to omreport
404 $omopt_system  = 'system';   # default "system" option to omreport
405 $blade         = 0;          # if this is a blade system
406
407 # Some initializations and checking before we begin
408 if ($snmp) {
409     snmp_initialize();    # initialize SNMP
410     snmp_check();         # check that SNMP works
411     snmp_detect_blade();  # detect blade via SNMP
412 }
413 else {
414     # Find the omreport binary
415     find_omreport();
416     # Check help output from omreport, see which options are available.
417     # Also detecting blade via omreport.
418     check_omreport_options();
419 }
420
421
422 #---------------------------------------------------------------------
423 # Helper functions
424 #---------------------------------------------------------------------
425
426 #
427 # Store a message in one of the message arrays
428 #
429 sub report {
430     my ($type, $msg, $exval, $id) = @_;
431     defined $id or $id = q{};
432
433     my %type2array
434       = (
435          'storage' => \@report_storage,
436          'chassis' => \@report_chassis,
437          'other'   => \@report_other,
438         );
439
440     return push @{ $type2array{$type} }, [ $msg, $exval, $id ];
441 }
442
443
444 #
445 # Run command, put resulting output lines in an array and return a
446 # pointer to that array
447 #
448 sub run_command {
449     my $command = shift;
450
451     open my $CMD, '-|', $command
452       or do { report('other', "Couldn't run command '$command': $!", $E_UNKNOWN)
453                 and return [] };
454     my @lines = <$CMD>;
455     close $CMD
456       or do { report('other', "Couldn't close filehandle for command '$command': $!", $E_UNKNOWN)
457                 and return \@lines };
458     return \@lines;
459 }
460
461 #
462 # Run command, put resulting output in a string variable and return it
463 #
464 sub slurp_command {
465     my $command = shift;
466
467     open my $CMD, '-|', $command
468       or do { report('other', "Couldn't run command '$command': $!", $E_UNKNOWN) and return };
469     my $rawtext = do { local $/ = undef; <$CMD> }; # slurping
470     close $CMD;
471
472     # NOTE: We don't check the return value of close() since omreport
473     # does something weird sometimes.
474
475     return $rawtext;
476 }
477
478 #
479 # Initialize SNMP
480 #
481 sub snmp_initialize {
482     # Legal SNMP v3 protocols
483     my $snmp_v3_privprotocol = qr{\A des|aes|aes128|3des|3desde \z}xms;
484     my $snmp_v3_authprotocol = qr{\A md5|sha \z}xms;
485
486     # Parameters to Net::SNMP->session()
487     my %param
488       = (
489          '-port'     => $opt{port},
490          '-hostname' => $opt{hostname},
491          '-version'  => $opt{protocol},
492         );
493
494     # Parameters for SNMP v3
495     if ($opt{protocol} == 3) {
496
497         # Username is mandatory
498         if (defined $opt{username}) {
499             $param{'-username'} = $opt{username};
500         }
501         else {
502             print "SNMP ERROR: With SNMPv3 the username must be specified\n";
503             exit $E_UNKNOWN;
504         }
505
506         # Authpassword is optional
507         if (defined $opt{authpassword}) {
508             $param{'-authpassword'} = $opt{authpassword};
509         }
510
511         # Authkey is optional
512         if (defined $opt{authkey}) {
513             $param{'-authkey'} = $opt{authkey};
514         }
515
516         # Privpassword is optional
517         if (defined $opt{privpassword}) {
518             $param{'-privpassword'} = $opt{privpassword};
519         }
520
521         # Privkey is optional
522         if (defined $opt{privkey}) {
523             $param{'-privkey'} = $opt{privkey};
524         }
525
526         # Privprotocol is optional
527         if (defined $opt{privprotocol}) {
528             if ($opt{privprotocol} =~ m/$snmp_v3_privprotocol/xms) {
529                 $param{'-privprotocol'} = $opt{privprotocol};
530             }
531             else {
532                 print "SNMP ERROR: Unknown privprotocol '$opt{privprotocol}', "
533                   . "must be one of [des|aes|aes128|3des|3desde]\n";
534                 exit $E_UNKNOWN;
535             }
536         }
537
538         # Authprotocol is optional
539         if (defined $opt{authprotocol}) {
540             if ($opt{authprotocol} =~ m/$snmp_v3_authprotocol/xms) {
541                 $param{'-authprotocol'} = $opt{authprotocol};
542             }
543             else {
544                 print "SNMP ERROR: Unknown authprotocol '$opt{authprotocol}', "
545                   . "must be one of [md5|sha]\n";
546                 exit $E_UNKNOWN;
547             }
548         }
549     }
550     # Parameters for SNMP v2c or v1
551     elsif ($opt{protocol} == 2 or $opt{protocol} == 1) {
552         $param{'-community'} = $opt{community};
553     }
554     else {
555         print "SNMP ERROR: Unknown SNMP version '$opt{protocol}'\n";
556         exit $E_UNKNOWN;
557     }
558
559     # Try to initialize the SNMP session
560     if ( eval { require Net::SNMP; 1 } ) {
561         ($snmp_session, $snmp_error) = Net::SNMP->session( %param );
562         if (!defined $snmp_session) {
563             printf "SNMP: %s\n", $snmp_error;
564             exit $E_UNKNOWN;
565         }
566     }
567     else {
568         print "ERROR: You need perl module Net::SNMP to run $NAME in SNMP mode";
569         if (defined $opt{htmlinfo}) {
570             # check_openmanage.html#error-you-need-perl-module-net-snmp-to-run-check-openmanage-in-snmp-mode
571             print ' [<a href="http://bit.ly/b4F0q1">more info</a>]';
572         }
573         print "\n";
574         exit $E_UNKNOWN;
575     }
576     return;
577 }
578
579 #
580 # Checking if SNMP works by probing for "chassisModelName", which all
581 # servers should have
582 #
583 sub snmp_check {
584     my $chassisModelName = '1.3.6.1.4.1.674.10892.1.300.10.1.9.1';
585     my $result = $snmp_session->get_request(-varbindlist => [$chassisModelName]);
586
587     # Typically if remote host isn't responding
588     if (!defined $result) {
589         printf "SNMP CRITICAL: %s", $snmp_session->error;
590         if (defined $opt{htmlinfo}) {
591             # check_openmanage.html#snmp-critical-no-response-from-remote-host-10-1-2-3
592             print ' [<a href="http://bit.ly/bRyk4B">more info</a>]';
593         }
594         print "\n";
595         exit $E_CRITICAL;
596     }
597
598     # If OpenManage isn't installed or is not working
599     if ($result->{$chassisModelName} =~ m{\A noSuch (Instance|Object) \z}xms) {
600         print "ERROR: (SNMP) OpenManage is not installed or is not working correctly";
601         if (defined $opt{htmlinfo}) {
602             # check_openmanage.html#error-snmp-openmanage-is-not-installed-or-is-not-working-correctly
603             print ' [<a href="http://bit.ly/c7oKcK">more info</a>]';
604         }
605         print "\n";
606         exit $E_UNKNOWN;
607     }
608     return;
609 }
610
611 #
612 # Detecting blade via SNMP
613 #
614 sub snmp_detect_blade {
615     my $DellBaseBoardType = '1.3.6.1.4.1.674.10892.1.300.80.1.7.1.1';
616     my $result = $snmp_session->get_request(-varbindlist => [$DellBaseBoardType]);
617
618     # Identify blade. Older models (4th and 5th gen models) and/or old
619     # OMSA (4.x) don't have this OID. If we get "noSuchInstance" or
620     # similar, we assume that this isn't a blade
621     if (exists $result->{$DellBaseBoardType} && $result->{$DellBaseBoardType} eq '3') {
622         $blade = 1;
623     }
624     return;
625 }
626
627 #
628 # Locate the omreport binary
629 #
630 sub find_omreport {
631     # If user has specified path to omreport
632     if (defined $opt{omreport} and -x $opt{omreport}) {
633         $omreport = qq{"$opt{omreport}"};
634         return;
635     }
636
637     # Possible full paths for omreport
638     my @omreport_paths
639       = (
640          '/usr/bin/omreport',                            # default on Linux
641          '/opt/dell/srvadmin/bin/omreport',              # default on Linux with OMSA 6.2.0
642          '/opt/dell/srvadmin/oma/bin/omreport.sh',       # alternate on Linux
643          '/opt/dell/srvadmin/oma/bin/omreport',          # alternate on Linux
644          'C:\Program Files (x86)\Dell\SysMgt\oma\bin\omreport.exe', # default on Windows x64
645          'C:\Program Files\Dell\SysMgt\oma\bin\omreport.exe',       # default on Windows x32
646          'c:\progra~1\dell\sysmgt\oma\bin\omreport.exe', # 8bit legacy default on Windows x32
647          'c:\progra~2\dell\sysmgt\oma\bin\omreport.exe', # 8bit legacy default on Windows x64
648         );
649
650     # Find the one to use
651   OMREPORT_PATH:
652     foreach my $bin (@omreport_paths) {
653         if (-x $bin) {
654             $omreport = qq{"$bin"};
655             last OMREPORT_PATH;
656         }
657     }
658
659     # Exit with status=UNKNOWN if OM is not installed, or we don't
660     # have permission to execute the binary
661     if (!defined $omreport) {
662         print "ERROR: Dell OpenManage Server Administrator (OMSA) is not installed";
663         if (defined $opt{htmlinfo}) {
664             # check_openmanage.html#error-dell-openmanage-server-administrator-omsa-is-not-installed
665             print ' [<a href="http://bit.ly/a4M8Z6">more info</a>]';
666         }
667         print "\n";
668         exit $E_UNKNOWN;
669     }
670     return;
671 }
672
673 #
674 # Checks output from 'omreport -?' and searches for arguments to
675 # omreport, to accommodate deprecated options "chassis" and "system"
676 # (on newer hardware), as well as blade servers.
677 #
678 sub check_omreport_options {
679     foreach (@{ run_command("$omreport -? 2>&1") }) {
680        if (m/\A servermodule /xms) {
681            # If "servermodule" argument to omreport exists, use it
682            # instead of argument "system"
683            $omopt_system = 'servermodule';
684        }
685        elsif (m/\A mainsystem /xms) {
686            # If "mainsystem" argument to omreport exists, use it
687            # instead of argument "chassis"
688            $omopt_chassis = 'mainsystem';
689        }
690        elsif (m/\A modularenclosure /xms) {
691            # If "modularenclusure" argument to omreport exists, assume
692            # that this is a blade
693            $blade = 1;
694        }
695     }
696     return;
697 }
698
699 #
700 # Read the blacklist option and return a hash containing the
701 # blacklisted components
702 #
703 sub get_blacklist {
704     my @bl = ();
705     my %blacklist = ();
706
707     if (scalar @{ $opt{blacklist} } >= 0) {
708         foreach my $black (@{ $opt{blacklist} }) {
709             my $tmp = q{};
710             if (-f $black) {
711                 open my $BL, '<', $black
712                   or do { report('other', "Couldn't open blacklist file $black: $!", $E_UNKNOWN)
713                             and return {} };
714                 $tmp = <$BL>;
715                 close $BL;
716                 chomp $tmp;
717             }
718             else {
719                 $tmp = $black;
720             }
721             push @bl, $tmp;
722         }
723     }
724
725     return {} if $#bl < 0;
726
727     # Parse blacklist string, put in hash
728     foreach my $black (@bl) {
729         my @comps = split m{/}xms, $black;
730         foreach my $c (@comps) {
731             next if $c !~ m/=/xms;
732             my ($key, $val) = split /=/xms, $c;
733             my @vals = split /,/xms, $val;
734             $blacklist{$key} = \@vals;
735         }
736     }
737
738     return \%blacklist;
739 }
740
741 #
742 # Read the check option and adjust the hash %check, which is a rough
743 # list of components to be checked
744 #
745 sub adjust_checks {
746     my @cl = ();
747
748     # Adjust checking based on the '--all' option
749     if ($opt{all}) {
750         # Check option usage
751         if (defined $opt{only} and $opt{only} !~ m{\A critical|warning \z}xms) {
752             print qq{ERROR: Wrong simultaneous usage of the "--all" and "--only" options\n};
753             exit $E_UNKNOWN;
754         }
755         if (scalar @{ $opt{check} } > 0) {
756             print qq{ERROR: Wrong simultaneous usage of the "--all" and "--check" options\n};
757             exit $E_UNKNOWN;
758         }
759
760         # set the check hash to check everything
761         map { $_ = 1 } values %check;
762
763         return;
764     }
765
766     # Adjust checking based on the '--only' option
767     if (defined $opt{only} and $opt{only} !~ m{\A critical|warning \z}xms) {
768         # Check option usage
769         if (scalar @{ $opt{check} } > 0) {
770             print qq{ERROR: Wrong simultaneous usage of the "--only" and "--check" options\n};
771             exit $E_UNKNOWN;
772         }
773         if (! exists $check{$opt{only}} && $opt{only} ne 'chassis') {
774             print qq{ERROR: "$opt{only}" is not a known keyword for the "--only" option\n};
775             exit $E_UNKNOWN;
776         }
777
778         # reset the check hash
779         map { $_ = 0 } values %check;
780
781         # adjust the check hash
782         if ($opt{only} eq 'chassis') {
783             map { $check{$_} = 1 } qw(memory fans power temp cpu voltage
784                                       batteries amperage intrusion esmhealth);
785         }
786         else {
787             $check{$opt{only}} = 1;
788         }
789
790         return;
791     }
792
793     # Adjust checking based on the '--check' option
794     if (scalar @{ $opt{check} } >= 0) {
795         foreach my $check (@{ $opt{check} }) {
796             my $tmp = q{};
797             if (-f $check) {
798                 open my $CL, '<', $check
799                   or do { report('other', "Couldn't open check file $check: $!", $E_UNKNOWN) and return };
800                 $tmp = <$CL>;
801                 close $CL;
802             }
803             else {
804                 $tmp = $check;
805             }
806             push @cl, $tmp;
807         }
808     }
809
810     return if $#cl < 0;
811
812     # Parse checklist string, put in hash
813     foreach my $check (@cl) {
814         my @checks = split /,/xms, $check;
815         foreach my $c (@checks) {
816             next if $c !~ m/=/xms;
817             my ($key, $val) = split /=/xms, $c;
818             $check{$key} = $val;
819         }
820     }
821
822     # Check if we should check global health status
823   CHECK_KEY:
824     foreach (keys %check) {
825         next CHECK_KEY if $_ eq 'esmlog';   # not part of global status
826         next CHECK_KEY if $_ eq 'alertlog'; # not part of global status
827
828         if ($check{$_} == 0) { # found something with checking turned off
829             $global = 0;
830             last CHECK_KEY;
831         }
832     }
833
834     return;
835 }
836
837 #
838 # Runs omreport and returns an array of anonymous hashes containing
839 # the output.
840 # Takes one argument: string containing parameters to omreport
841 #
842 sub run_omreport {
843     my $command = shift;
844     my @output  = ();
845     my @keys    = ();
846
847     # Errors that are OK. Some low-end poweredge (and blades) models
848     # don't have RAID controllers, intrusion detection sensor, or
849     # redundant/instrumented power supplies etc.
850     my $ok_errors
851       = qr{
852             Intrusion\sinformation\sis\snot\sfound\sfor\sthis\ssystem  # No intrusion probe
853           | No\sinstrumented\spower\ssupplies\sfound\son\sthis\ssystem # No instrumented PS (blades/low-end)
854           | No\scontrollers\sfound                                     # No RAID controller
855           | No\sbattery\sprobes\sfound\son\sthis\ssystem               # No battery probes
856           | Invalid\scommand:\spwrmonitoring                           # Older OMSAs lack this command(?)
857 #          | Current\sprobes\snot\sfound                                # OMSA + RHEL5.4 bug
858         }xms;
859
860     # Errors that are OK on blade servers
861     my $ok_blade_errors
862       = qr{
863               No\sfan\sprobes\sfound\son\sthis\ssystem   # No fan probes
864       }xms;
865
866     # Run omreport and fetch output
867     my $rawtext = slurp_command("$omreport $command -fmt ssv 2>&1");
868     return [] if !defined $rawtext;
869
870     # Workaround for Openmanage BUG introduced in OMSA 5.5.0
871     $rawtext =~ s{\n;}{;}gxms if $command eq 'storage controller';
872
873     # Openmanage sometimes puts a linebreak between "Error" and the
874     # actual error text
875     $rawtext =~ s{^Error\s*\n}{Error: }xms;
876
877     # Parse output, store in array
878     for ((split m{\n}xms, $rawtext)) {
879         if (m{\AError}xms) {
880             next if m{$ok_errors}xms;
881             next if ($blade and m{$ok_blade_errors}xms);
882             report('other', "Problem running 'omreport $command': $_", $E_UNKNOWN);
883         }
884
885         next if !m/(.*?;){2}/xms;  # ignore lines with less than 3 fields
886         my @vals = split /;/xms;
887         if ($vals[0] =~ m/\A (Index|ID|Severity|Processor|Current\sSpeed) \z/xms) {
888             @keys = @vals;
889         }
890         else {
891             my $i = 0;
892             push @output, { map { $_ => $vals[$i++] } @keys };
893         }
894
895     }
896
897     # Finally, return the collected information
898     return \@output;
899 }
900
901
902 #
903 # Checks if a component is blacklisted. Returns 1 if the component is
904 # blacklisted, 0 otherwise. Takes two arguments:
905 #   arg1: component name
906 #   arg2: component id or index
907 #
908 sub blacklisted {
909     my $name = shift;  # component name
910     my $id   = shift;  # component id
911     my $ret  = 0;      # return value
912
913     if (defined $blacklist{$name}) {
914         foreach my $comp (@{ $blacklist{$name} }) {
915             if (defined $id and ($comp eq $id or uc($comp) eq 'ALL')) {
916                 $ret = 1;
917             }
918         }
919     }
920
921     return $ret;
922 }
923
924 # Converts the NexusID from SNMP to our version
925 sub convert_nexus {
926     my $nexus = shift;
927     $nexus =~ s{\A \\}{}xms;
928     $nexus =~ s{\\}{:}gxms;
929     return $nexus;
930 }
931
932 # Sets custom temperature thresholds based on user supplied options
933 sub custom_temperature_thresholds {
934     my $type   = shift; # type of threshold, either w (warning) or c (critical)
935     my %thres  = ();    # will contain the thresholds
936     my @limits = ();    # holds the input
937
938     my @opt =  $type eq 'w' ? @{ $opt{warning} } : @{ $opt{critical} };
939
940     if (scalar @opt >= 0) {
941         foreach my $t (@opt) {
942             my $tmp = q{};
943             if (-f $t) {
944                 open my $F, '<', $t
945                   or do { report('other', "Couldn't open temperature threshold file $t: $!",
946                                  $E_UNKNOWN) and return {} };
947                 $tmp = <$F>;
948                 close $F;
949             }
950             else {
951                 $tmp = $t;
952             }
953             push @limits, $tmp;
954         }
955     }
956
957     # Parse checklist string, put in hash
958     foreach my $th (@limits) {
959         my @tmp = split m{,}xms, $th;
960         foreach my $t (@tmp) {
961             next if $t !~ m{=}xms;
962             my ($key, $val) = split m{=}xms, $t;
963             if ($val =~ m{/}xms) {
964                 my ($max, $min) = split m{/}xms, $val;
965                 $thres{$key}{max} = $max;
966                 $thres{$key}{min} = $min;
967             }
968             else {
969                 $thres{$key}{max} = $val;
970             }
971         }
972     }
973
974     return \%thres;
975 }
976
977
978 # Gets the output from SNMP result according to the OIDs checked
979 sub get_snmp_output {
980     my ($result,$oidref) = @_;
981     my @temp   = ();
982     my @output = ();
983
984     foreach my $oid (keys %{ $result }) {
985         my $short = $oid;
986         $short =~ s{\s}{}gxms;                   # remove whitespace
987         $short =~ s{\A (.+) \. (\d+) \z}{$1}xms; # remove last number
988         my $id = $2;
989         if (exists $oidref->{$short}) {
990             $temp[$id]{$oidref->{$short}} = $result->{$oid};
991         }
992     }
993
994     # Remove any empty indexes
995     foreach my $out (@temp) {
996         if (defined $out) {
997             push @output, $out;
998         }
999     }
1000
1001     return \@output;
1002 }
1003
1004
1005 # Map the controller or other item in-place
1006 sub map_item {
1007     my ($key, $val, $list)  = @_;
1008
1009     foreach my $lst (@{ $list }) {
1010         if (!exists $lst->{$key}) {
1011             $lst->{$key} = $val;
1012         }
1013     }
1014     return;
1015 }
1016
1017 # Return the URL for official Dell documentation for a specific
1018 # PowerEdge server
1019 sub documentation_url {
1020     my $model = shift;
1021
1022     # create model short form, e.g. "r710"
1023     $model =~ s{\A PowerEdge \s (.+?) \z}{lc($1)}exms;
1024
1025     # special case for blades (e.g. M600, M710), they have common
1026     # documentation
1027     $model =~ s{\A m\d+ \z}{m}xms;
1028
1029     return 'http://support.dell.com/support/edocs/systems/pe' . $model . '/';
1030 }
1031
1032 # Return the URL for warranty information for a server with a given
1033 # serial number (servicetag)
1034 sub warranty_url {
1035     my $tag = shift;
1036
1037     # Dell support sites for different parts of the world
1038     my %supportsite
1039       = (
1040          'emea' => 'http://support.euro.dell.com/support/topics/topic.aspx/emea/shared/support/my_systems_info/',
1041          'ap'   => 'http://supportapj.dell.com/support/topics/topic.aspx/ap/shared/support/my_systems_info/en/details?',
1042          'glob' => 'http://support.dell.com/support/topics/global.aspx/support/my_systems_info/details?',
1043         );
1044
1045     # warranty URLs for different country codes
1046     my %url
1047       = (
1048          # EMEA
1049          'at' => $supportsite{emea} . 'de/details?c=at&l=de&ServiceTag=',  # Austria
1050          'be' => $supportsite{emea} . 'nl/details?c=be&l=nl&ServiceTag=',  # Belgium
1051          'cz' => $supportsite{emea} . 'cs/details?c=cz&l=cs&ServiceTag=',  # Czech Republic
1052          'de' => $supportsite{emea} . 'de/details?c=de&l=de&ServiceTag=',  # Germany
1053          'dk' => $supportsite{emea} . 'da/details?c=dk&l=da&ServiceTag=',  # Denmark
1054          'es' => $supportsite{emea} . 'es/details?c=es&l=es&ServiceTag=',  # Spain
1055          'fi' => $supportsite{emea} . 'fi/details?c=fi&l=fi&ServiceTag=',  # Finland
1056          'fr' => $supportsite{emea} . 'fr/details?c=fr&l=fr&ServiceTag=',  # France
1057          'gr' => $supportsite{emea} . 'en/details?c=gr&l=el&ServiceTag=',  # Greece
1058          'it' => $supportsite{emea} . 'it/details?c=it&l=it&ServiceTag=',  # Italy
1059          'il' => $supportsite{emea} . 'en/details?c=il&l=en&ServiceTag=',  # Israel
1060          'me' => $supportsite{emea} . 'en/details?c=me&l=en&ServiceTag=',  # Middle East
1061          'no' => $supportsite{emea} . 'no/details?c=no&l=no&ServiceTag=',  # Norway
1062          'nl' => $supportsite{emea} . 'nl/details?c=nl&l=nl&ServiceTag=',  # The Netherlands
1063          'pl' => $supportsite{emea} . 'pl/details?c=pl&l=pl&ServiceTag=',  # Poland
1064          'pt' => $supportsite{emea} . 'en/details?c=pt&l=pt&ServiceTag=',  # Portugal
1065          'ru' => $supportsite{emea} . 'ru/details?c=ru&l=ru&ServiceTag=',  # Russia
1066          'se' => $supportsite{emea} . 'sv/details?c=se&l=sv&ServiceTag=',  # Sweden
1067          'uk' => $supportsite{emea} . 'en/details?c=uk&l=en&ServiceTag=',  # United Kingdom
1068          'za' => $supportsite{emea} . 'en/details?c=za&l=en&ServiceTag=',  # South Africa
1069          # America
1070          'br' => $supportsite{glob} . 'c=br&l=pt&ServiceTag=',  # Brazil
1071          'ca' => $supportsite{glob} . 'c=ca&l=en&ServiceTag=',  # Canada
1072          'mx' => $supportsite{glob} . 'c=mx&l=es&ServiceTag=',  # Mexico
1073          'us' => $supportsite{glob} . 'c=us&l=en&ServiceTag=',  # USA
1074          # Asia/Pacific
1075          'au' => $supportsite{ap} . 'c=au&l=en&ServiceTag=',  # Australia
1076          'cn' => $supportsite{ap} . 'c=cn&l=zh&ServiceTag=',  # China
1077          'in' => $supportsite{ap} . 'c=in&l=en&ServiceTag=',  # India
1078          # default fallback
1079          'XX' => $supportsite{glob} . 'ServiceTag=',  # default
1080         );
1081
1082     if (exists $url{$opt{htmlinfo}}) {
1083         return $url{$opt{htmlinfo}} . $tag;
1084     }
1085     else {
1086         return $url{XX} . $tag;
1087     }
1088 }
1089
1090
1091 # This helper function returns the corresponding value of a hash key,
1092 # but takes into account that the key may not exist
1093 sub get_hashval {
1094     my $key  = shift || return undef;
1095     my $hash = shift;
1096     return exists $hash->{$key} ? $hash->{$key} : "Undefined value $key";
1097 }
1098
1099
1100
1101 #---------------------------------------------------------------------
1102 # Check functions
1103 #---------------------------------------------------------------------
1104
1105 #-----------------------------------------
1106 # Check global health status
1107 #-----------------------------------------
1108 sub check_global {
1109     my $health = $E_OK;
1110
1111     if ($snmp) {
1112         #
1113         # Checks global status, i.e. both storage and chassis
1114         #
1115         my $systemStateGlobalSystemStatus = '1.3.6.1.4.1.674.10892.1.200.10.1.2.1';
1116         my $result = $snmp_session->get_request(-varbindlist => [$systemStateGlobalSystemStatus]);
1117         if (!defined $result) {
1118             printf "SNMP ERROR [global]: %s\n", $snmp_error;
1119             exit $E_UNKNOWN;
1120         }
1121         $health = $status2nagios{$snmp_status{$result->{$systemStateGlobalSystemStatus}}};
1122     }
1123     else {
1124         #
1125         # NB! This does not check storage, only chassis...
1126         #
1127         foreach (@{ run_command("$omreport $omopt_system -fmt ssv") }) {
1128             next if !m/;/xms;
1129             next if m/\A SEVERITY;COMPONENT/xms;
1130             if (m/\A (.+?);Main\sSystem(\sChassis)? /xms) {
1131                 $health = $status2nagios{$1};
1132                 last;
1133             }
1134         }
1135     }
1136
1137     # Return the status
1138     return $health;
1139 }
1140
1141
1142 #-----------------------------------------
1143 # STORAGE: Check controllers
1144 #-----------------------------------------
1145 sub check_controllers {
1146     return if blacklisted('ctrl', 'all');
1147
1148     my $id       = undef;
1149     my $nexus    = undef;
1150     my $name     = undef;
1151     my $state    = undef;
1152     my $status   = undef;
1153     my $minfw    = undef;
1154     my $mindr    = undef;
1155     my $firmware = undef;
1156     my $driver   = undef;
1157     my $minstdr  = undef;  # Minimum required Storport driver version
1158     my $stdr     = undef;  # Storport driver version
1159     my @output   = ();
1160
1161     if ($snmp) {
1162         my %ctrl_oid
1163           = (
1164              '1.3.6.1.4.1.674.10893.1.20.130.1.1.1'  => 'controllerNumber',
1165              '1.3.6.1.4.1.674.10893.1.20.130.1.1.2'  => 'controllerName',
1166              '1.3.6.1.4.1.674.10893.1.20.130.1.1.5'  => 'controllerState',
1167              '1.3.6.1.4.1.674.10893.1.20.130.1.1.8'  => 'controllerFWVersion',
1168              '1.3.6.1.4.1.674.10893.1.20.130.1.1.38' => 'controllerComponentStatus',
1169              '1.3.6.1.4.1.674.10893.1.20.130.1.1.39' => 'controllerNexusID',
1170              '1.3.6.1.4.1.674.10893.1.20.130.1.1.41' => 'controllerDriverVersion',
1171              '1.3.6.1.4.1.674.10893.1.20.130.1.1.44' => 'controllerMinFWVersion',
1172              '1.3.6.1.4.1.674.10893.1.20.130.1.1.45' => 'controllerMinDriverVersion',
1173              '1.3.6.1.4.1.674.10893.1.20.130.1.1.55' => 'controllerStorportDriverVersion',
1174              '1.3.6.1.4.1.674.10893.1.20.130.1.1.56' => 'controllerMinRequiredStorportVer',
1175             );
1176
1177         # We use get_table() here for the odd case where a server has
1178         # two or more controllers, and where some OIDs are missing on
1179         # one of the controllers.
1180         my $controllerTable = '1.3.6.1.4.1.674.10893.1.20.130.1';
1181         my $result = $snmp_session->get_table(-baseoid => $controllerTable);
1182
1183         # No controllers is OK
1184         return if !defined $result;
1185
1186         @output = @{ get_snmp_output($result, \%ctrl_oid) };
1187     }
1188     else {
1189         @output = @{ run_omreport('storage controller') };
1190     }
1191
1192     my %ctrl_state
1193       = (
1194          0 => 'Unknown',
1195          1 => 'Ready',
1196          2 => 'Failed',
1197          3 => 'Online',
1198          4 => 'Offline',
1199          6 => 'Degraded',
1200         );
1201
1202   CTRL:
1203     foreach my $out (@output) {
1204         if ($snmp) {
1205             $id       = $out->{controllerNumber} - 1;
1206             $name     = $out->{controllerName};
1207             $state    = get_hashval($out->{controllerState}, \%ctrl_state);
1208             $status   = $snmp_status{$out->{controllerComponentStatus}};
1209             $minfw    = exists $out->{controllerMinFWVersion}
1210               ? $out->{controllerMinFWVersion} : undef;
1211             $mindr    = exists $out->{controllerMinDriverVersion}
1212               ? $out->{controllerMinDriverVersion} : undef;
1213             $firmware = exists $out->{controllerFWVersion}
1214               ? $out->{controllerFWVersion} : 'N/A';
1215             $driver   = exists $out->{controllerDriverVersion}
1216               ? $out->{controllerDriverVersion} : 'N/A';
1217             $minstdr  = exists $out->{'controllerMinRequiredStorportVer'}
1218               ? $out->{controllerMinRequiredStorportVer} : undef;
1219             $stdr     = exists $out->{controllerStorportDriverVersion}
1220               ? $out->{controllerStorportDriverVersion} : undef;
1221             $nexus    = convert_nexus($out->{controllerNexusID});
1222         }
1223         else {
1224             $id       = $out->{ID};
1225             $name     = $out->{Name};
1226             $state    = $out->{State};
1227             $status   = $out->{Status};
1228             $minfw    = $out->{'Minimum Required Firmware Version'} ne 'Not Applicable'
1229               ? $out->{'Minimum Required Firmware Version'} : undef;
1230             $mindr    = $out->{'Minimum Required Driver Version'} ne 'Not Applicable'
1231               ? $out->{'Minimum Required Driver Version'} : undef;
1232             $firmware = $out->{'Firmware Version'} ne 'Not Applicable'
1233               ? $out->{'Firmware Version'} : 'N/A';
1234             $driver   = $out->{'Driver Version'} ne 'Not Applicable'
1235               ? $out->{'Driver Version'} : 'N/A';
1236             $minstdr  = (exists $out->{'Minimum Required Storport Driver Version'}
1237                          and $out->{'Minimum Required Storport Driver Version'} ne 'Not Applicable')
1238               ? $out->{'Minimum Required Storport Driver Version'} : undef;
1239             $stdr     = (exists $out->{'Storport Driver Version'}
1240                          and $out->{'Storport Driver Version'} ne 'Not Applicable')
1241               ? $out->{'Storport Driver Version'} : undef;
1242             $nexus    = $id;
1243         }
1244
1245         $name =~ s{\s+\z}{}xms; # remove trailing whitespace
1246         push @controllers, $id;
1247
1248         # Collecting some storage info
1249         $sysinfo{'controller'}{$id}{'id'}       = $nexus;
1250         $sysinfo{'controller'}{$id}{'name'}     = $name;
1251         $sysinfo{'controller'}{$id}{'driver'}   = $driver;
1252         $sysinfo{'controller'}{$id}{'firmware'} = $firmware;
1253         $sysinfo{'controller'}{$id}{'storport'} = $stdr;
1254
1255         next CTRL if blacklisted('ctrl', $nexus);
1256
1257         # Special case: old firmware
1258         if (!blacklisted('ctrl_fw', $id) && defined $minfw) {
1259             chomp $firmware;
1260             my $msg = sprintf q{Controller %d [%s]: Firmware '%s' is out of date},
1261               $id, $name, $firmware;
1262             report('storage', $msg, $E_WARNING, $nexus);
1263         }
1264         # Special case: old driver
1265         if (!blacklisted('ctrl_driver', $id) && defined $mindr) {
1266             chomp $driver;
1267             my $msg = sprintf q{Controller %d [%s]: Driver '%s' is out of date},
1268               $id, $name, $driver;
1269             report('storage', $msg, $E_WARNING, $nexus);
1270         }
1271         # Special case: old storport driver
1272         if (!blacklisted('ctrl_stdr', $id) && defined $minstdr) {
1273             chomp $stdr;
1274             my $msg = sprintf q{Controller %d [%s]: Storport driver '%s' is out of date},
1275               $id, $name, $stdr;
1276             report('storage', $msg, $E_WARNING, $nexus);
1277         }
1278         # Ok
1279         if ($status eq 'Ok' or ($status eq 'Non-Critical'
1280                                 and (defined $minfw or defined $mindr or defined $minstdr))) {
1281             my $msg = sprintf 'Controller %d [%s] is %s',
1282               $id, $name, $state;
1283             report('storage', $msg, $E_OK, $nexus);
1284         }
1285         # Default
1286         else {
1287             my $msg = sprintf 'Controller %d [%s] needs attention: %s',
1288               $id, $name, $state;
1289             report('storage', $msg, $status2nagios{$status}, $nexus);
1290         }
1291     }
1292     return;
1293 }
1294
1295
1296 #-----------------------------------------
1297 # STORAGE: Check physical drives
1298 #-----------------------------------------
1299 sub check_physical_disks {
1300     return if $#controllers == -1;
1301     return if blacklisted('pdisk', 'all');
1302
1303     my $id       = undef;
1304     my $nexus    = undef;
1305     my $name     = undef;
1306     my $state    = undef;
1307     my $status   = undef;
1308     my $fpred    = undef;
1309     my $progr    = undef;
1310     my $ctrl     = undef;
1311     my $vendor   = undef;  # disk vendor
1312     my $product  = undef;  # product ID
1313     my $capacity = undef;  # disk length (size) in bytes
1314     my @output  = ();
1315
1316     if ($snmp) {
1317         my %pdisk_oid
1318           = (
1319              '1.3.6.1.4.1.674.10893.1.20.130.4.1.1'  => 'arrayDiskNumber',
1320              '1.3.6.1.4.1.674.10893.1.20.130.4.1.2'  => 'arrayDiskName',
1321              '1.3.6.1.4.1.674.10893.1.20.130.4.1.3'  => 'arrayDiskVendor',
1322              '1.3.6.1.4.1.674.10893.1.20.130.4.1.4'  => 'arrayDiskState',
1323              '1.3.6.1.4.1.674.10893.1.20.130.4.1.6'  => 'arrayDiskProductID',
1324              '1.3.6.1.4.1.674.10893.1.20.130.4.1.9'  => 'arrayDiskEnclosureID',
1325              '1.3.6.1.4.1.674.10893.1.20.130.4.1.10' => 'arrayDiskChannel',
1326              '1.3.6.1.4.1.674.10893.1.20.130.4.1.11' => 'arrayDiskLengthInMB',
1327              '1.3.6.1.4.1.674.10893.1.20.130.4.1.15' => 'arrayDiskTargetID',
1328              '1.3.6.1.4.1.674.10893.1.20.130.4.1.16' => 'arrayDiskLunID',
1329              '1.3.6.1.4.1.674.10893.1.20.130.4.1.24' => 'arrayDiskComponentStatus',
1330              '1.3.6.1.4.1.674.10893.1.20.130.4.1.26' => 'arrayDiskNexusID',
1331              '1.3.6.1.4.1.674.10893.1.20.130.4.1.31' => 'arrayDiskSmartAlertIndication',
1332              '1.3.6.1.4.1.674.10893.1.20.130.5.1.7'  => 'arrayDiskEnclosureConnectionControllerNumber',
1333              '1.3.6.1.4.1.674.10893.1.20.130.6.1.7'  => 'arrayDiskChannelConnectionControllerNumber',
1334             );
1335         my $result = undef;
1336         if ($opt{use_get_table}) {
1337             my $arrayDiskTable = '1.3.6.1.4.1.674.10893.1.20.130.4';
1338             my $arrayDiskEnclosureConnectionControllerNumber = '1.3.6.1.4.1.674.10893.1.20.130.5.1.7';
1339             my $arrayDiskChannelConnectionControllerNumber = '1.3.6.1.4.1.674.10893.1.20.130.6.1.7';
1340
1341             $result  = $snmp_session->get_table(-baseoid => $arrayDiskTable);
1342             my $ext1 = $snmp_session->get_table(-baseoid => $arrayDiskEnclosureConnectionControllerNumber);
1343             my $ext2 = $snmp_session->get_table(-baseoid => $arrayDiskChannelConnectionControllerNumber);
1344
1345             if (defined $result) {
1346                 defined $ext1 && map { $$result{$_} = $$ext1{$_} } keys %{ $ext1 };
1347                 defined $ext2 && map { $$result{$_} = $$ext2{$_} } keys %{ $ext2 };
1348             }
1349         }
1350         else {
1351             $result = $snmp_session->get_entries(-columns => [keys %pdisk_oid]);
1352         }
1353
1354         if (!defined $result) {
1355             printf "SNMP ERROR [storage / pdisk]: %s.\n", $snmp_session->error;
1356             $snmp_session->close;
1357             exit $E_UNKNOWN;
1358         }
1359
1360         @output = @{ get_snmp_output($result, \%pdisk_oid) };
1361     }
1362     else {
1363         foreach my $c (@controllers) {
1364             # This blacklists disks with broken firmware, which includes
1365             # illegal XML characters that makes openmanage choke on itself
1366             next if blacklisted('ctrl_pdisk', $c);
1367
1368             push @output, @{ run_omreport("storage pdisk controller=$c") };
1369             map_item('ctrl', $c, \@output);
1370         }
1371     }
1372
1373     my %pdisk_state
1374       = (
1375          0  => 'Unknown',
1376          1  => 'Ready',
1377          2  => 'Failed',
1378          3  => 'Online',
1379          4  => 'Offline',
1380          6  => 'Degraded',
1381          7  => 'Recovering',
1382          11 => 'Removed',
1383          15 => 'Resynching',
1384          22 => 'Replacing', # FIXME: this one is not defined in the OM 6.2.0.1 MIBs
1385          24 => 'Rebuilding',
1386          25 => 'No Media',
1387          26 => 'Formatting',
1388          28 => 'Diagnostics',
1389          34 => 'Predictive failure',
1390          35 => 'Initializing',
1391          39 => 'Foreign',
1392          40 => 'Clear',
1393          41 => 'Unsupported',
1394          53 => 'Incompatible',
1395         );
1396
1397     # Check physical disks on each of the controllers
1398   PDISK:
1399     foreach my $out (@output) {
1400         if ($snmp) {
1401             $name   = $out->{arrayDiskName};
1402             if (exists $out->{arrayDiskEnclosureID}) {
1403                 $id = join q{:}, ($out->{arrayDiskChannel}, $out->{arrayDiskEnclosureID},
1404                                   $out->{arrayDiskTargetID});
1405             }
1406             else {
1407                 $id = join q{:}, ($out->{arrayDiskChannel}, $out->{arrayDiskTargetID});
1408             }
1409             $state    = get_hashval($out->{arrayDiskState}, \%pdisk_state);
1410             $status   = $snmp_status{$out->{arrayDiskComponentStatus}};
1411             $fpred    = $out->{arrayDiskSmartAlertIndication} == 2 ? 1 : 0;
1412             $progr    = q{};
1413             $nexus    = convert_nexus($out->{arrayDiskNexusID});
1414             $vendor   = $out->{arrayDiskVendor};
1415             $product  = $out->{arrayDiskProductID};
1416             $capacity = $out->{arrayDiskLengthInMB} * 1024**2;
1417             if (exists $out->{arrayDiskEnclosureConnectionControllerNumber}) {
1418                 $ctrl = $out->{arrayDiskEnclosureConnectionControllerNumber} - 1;
1419             }
1420             elsif (exists $out->{arrayDiskChannelConnectionControllerNumber}) {
1421                 $ctrl = $out->{arrayDiskChannelConnectionControllerNumber} - 1;
1422             }
1423             else {
1424                 $ctrl = -1;
1425             }
1426         }
1427         else {
1428             $id       = $out->{'ID'};
1429             $name     = $out->{'Name'};
1430             $state    = $out->{'State'};
1431             $status   = $out->{'Status'};
1432             $fpred    = lc($out->{'Failure Predicted'}) eq 'yes' ? 1 : 0;
1433             $progr    = ' [' . $out->{'Progress'} . ']';
1434             $ctrl     = $out->{'ctrl'};
1435             $nexus    = join q{:}, $out->{ctrl}, $id;
1436             $vendor   = $out->{'Vendor ID'};
1437             $product  = $out->{'Product ID'};
1438             $capacity = $out->{'Capacity'};
1439             $capacity =~ s{\A .*? \((\d+) \s bytes\) \z}{$1}xms;
1440         }
1441
1442         next PDISK if blacklisted('pdisk', $nexus);
1443         $count{pdisk}++;
1444
1445         $vendor  =~ s{\s+\z}{}xms; # remove trailing whitespace
1446         $product =~ s{\s+\z}{}xms; # remove trailing whitespace
1447
1448         # Calculate human readable capacity
1449         $capacity = ceil($capacity / 1000**3) >= 1000
1450           ? sprintf '%.1fTB', ($capacity / 1000**4)
1451             : sprintf '%.0fGB', ($capacity / 1000**3);
1452         $capacity = '450GB' if $capacity eq '449GB';  # quick fix for 450GB disks
1453         $capacity = '300GB' if $capacity eq '299GB';  # quick fix for 300GB disks
1454         $capacity = '146GB' if $capacity eq '147GB';  # quick fix for 146GB disks
1455
1456         # Capitalize only the first letter of the vendor name
1457         $vendor = (substr $vendor, 0, 1) . lc (substr $vendor, 1, length $vendor);
1458
1459         # Remove unnecessary trademark rubbish from vendor name
1460         $vendor =~ s{\(tm\)\z}{}xms;
1461
1462         # Special case: Failure predicted
1463         if ($fpred) {
1464             my $msg = sprintf '%s [%s %s, %s] on ctrl %d needs attention: Failure Predicted',
1465               $name, $vendor, $product, $capacity, $ctrl;
1466             $msg .= " ($state)" if $state ne 'Predictive failure';
1467             report('storage', $msg,
1468                    ($status2nagios{$status} == $E_CRITICAL ? $E_CRITICAL : $E_WARNING), $nexus);
1469         }
1470         # Special case: Rebuilding / Replacing
1471         elsif ($state =~ m{\A Rebuilding|Replacing \z}xms) {
1472             my $msg = sprintf '%s [%s %s, %s] on ctrl %d is %s%s',
1473               $name, $vendor, $product, $capacity, $ctrl, $state, $progr;
1474             report('storage', $msg, $E_WARNING, $nexus);
1475         }
1476         # Default
1477         elsif ($status ne 'Ok') {
1478             my $msg =  sprintf '%s [%s %s, %s] on ctrl %d needs attention: %s',
1479               $name, $vendor, $product, $capacity, $ctrl, $state;
1480             report('storage', $msg, $status2nagios{$status}, $nexus);
1481         }
1482         # Ok
1483         else {
1484             my $msg = sprintf '%s [%s] on ctrl %d is %s',
1485               $name, $capacity, $ctrl, $state;
1486             report('storage', $msg, $E_OK, $nexus);
1487         }
1488     }
1489     return;
1490 }
1491
1492
1493 #-----------------------------------------
1494 # STORAGE: Check logical drives
1495 #-----------------------------------------
1496 sub check_virtual_disks {
1497     return if $#controllers == -1;
1498     return if blacklisted('vdisk', 'all');
1499
1500     my $id     = undef;
1501     my $name   = undef;
1502     my $nexus  = undef;
1503     my $dev    = undef;
1504     my $state  = undef;
1505     my $status = undef;
1506     my $layout = undef;
1507     my $size   = undef;
1508     my $progr  = undef;
1509     my $ctrl   = undef;
1510     my @output = ();
1511
1512     if ($snmp) {
1513         my %vdisk_oid
1514           = (
1515              '1.3.6.1.4.1.674.10893.1.20.140.1.1.3'  => 'virtualDiskDeviceName',
1516              '1.3.6.1.4.1.674.10893.1.20.140.1.1.4'  => 'virtualDiskState',
1517              '1.3.6.1.4.1.674.10893.1.20.140.1.1.6'  => 'virtualDiskLengthInMB',
1518              '1.3.6.1.4.1.674.10893.1.20.140.1.1.13' => 'virtualDiskLayout',
1519              '1.3.6.1.4.1.674.10893.1.20.140.1.1.17' => 'virtualDiskTargetID',
1520              '1.3.6.1.4.1.674.10893.1.20.140.1.1.20' => 'virtualDiskComponentStatus',
1521              '1.3.6.1.4.1.674.10893.1.20.140.1.1.21' => 'virtualDiskNexusID',
1522             );
1523         my $result = undef;
1524         if ($opt{use_get_table}) {
1525             my $virtualDiskTable = '1.3.6.1.4.1.674.10893.1.20.140.1';
1526             $result = $snmp_session->get_table(-baseoid => $virtualDiskTable);
1527         }
1528         else {
1529             $result = $snmp_session->get_entries(-columns => [keys %vdisk_oid]);
1530         }
1531
1532         # No logical drives is OK
1533         return if !defined $result;
1534
1535         @output = @{ get_snmp_output($result, \%vdisk_oid) };
1536     }
1537     else {
1538         foreach my $c (@controllers) {
1539             push @output, @{ run_omreport("storage vdisk controller=$c") };
1540             map_item('ctrl', $c, \@output);
1541         }
1542     }
1543
1544     my %vdisk_state
1545       = (
1546          0  => 'Unknown',
1547          1  => 'Ready',
1548          2  => 'Failed',
1549          3  => 'Online',
1550          4  => 'Offline',
1551          6  => 'Degraded',
1552          15 => 'Resynching',
1553          16 => 'Regenerating',
1554          24 => 'Rebuilding',
1555          26 => 'Formatting',
1556          32 => 'Reconstructing',
1557          35 => 'Initializing',
1558          36 => 'Background Initialization',
1559          38 => 'Resynching Paused',
1560          52 => 'Permanently Degraded',
1561          54 => 'Degraded Redundancy',
1562         );
1563
1564     my %vdisk_layout
1565       = (
1566          1  => 'Concatenated',
1567          2  => 'RAID-0',
1568          3  => 'RAID-1',
1569          7  => 'RAID-5',
1570          8  => 'RAID-6',
1571          10 => 'RAID-10',
1572          12 => 'RAID-50',
1573          19 => 'Concatenated RAID-1',
1574          24 => 'RAID-60',
1575         );
1576
1577     # Check virtual disks on each of the controllers
1578   VDISK:
1579     foreach my $out (@output) {
1580         if ($snmp) {
1581             $id     = $out->{virtualDiskTargetID};
1582             $dev    = $out->{virtualDiskDeviceName};
1583             $state  = get_hashval($out->{virtualDiskState}, \%vdisk_state);
1584             $layout = get_hashval($out->{virtualDiskLayout}, \%vdisk_layout);
1585             $status = $snmp_status{$out->{virtualDiskComponentStatus}};
1586             $size   = sprintf '%.2f GB', $out->{virtualDiskLengthInMB} / 1024;
1587             $progr  = q{};  # can't get this from SNMP(?)
1588             $nexus  = convert_nexus($out->{virtualDiskNexusID});
1589             $ctrl   = $nexus;  # We use the nexus id to get the controller id
1590             $ctrl   =~ s{\A (\d+):\d+ \z}{$1}xms;
1591         }
1592         else {
1593             $id     = $out->{ID};
1594             $dev    = $out->{'Device Name'};
1595             $state  = $out->{State};
1596             $status = $out->{Status};
1597             $layout = $out->{Layout};
1598             $size   = $out->{Size};
1599             $progr  = ' [' . $out->{Progress} . ']';
1600             $size   =~ s{\A (.*GB).* \z}{$1}xms;
1601             $nexus  = join q{:}, $out->{ctrl}, $id;
1602             $ctrl   = $out->{ctrl};
1603         }
1604
1605         next VDISK if blacklisted('vdisk', $nexus);
1606         $count{vdisk}++;
1607
1608         # The device name is undefined sometimes
1609         $dev = q{} if !defined $dev;
1610
1611         # Special case: Regenerating
1612         if ($state eq 'Regenerating') {
1613             my $msg = sprintf q{Logical drive %d '%s' [%s, %s] on ctrl %d is %s%s},
1614               $id, $dev, $layout, $size, $ctrl, $state, $progr;
1615             report('storage', $msg, $E_WARNING, $nexus);
1616         }
1617         # Default
1618         elsif ($status ne 'Ok') {
1619             my $msg = sprintf q{Logical drive %d '%s' [%s, %s] on ctrl %d needs attention: %s},
1620               $id, $dev, $layout, $size, $ctrl, $state;
1621             report('storage', $msg, $status2nagios{$status}, $nexus);
1622         }
1623         # Ok
1624         else {
1625             my $msg = sprintf q{Logical drive %d '%s' [%s, %s] on ctrl %d is %s},
1626               $id, $dev, $layout, $size, $ctrl, $state;
1627             report('storage', $msg, $E_OK, $nexus);
1628         }
1629     }
1630     return;
1631 }
1632
1633
1634 #-----------------------------------------
1635 # STORAGE: Check cache batteries
1636 #-----------------------------------------
1637 sub check_cache_battery {
1638     return if $#controllers == -1;
1639     return if blacklisted('bat', 'all');
1640
1641     my $id     = undef;
1642     my $nexus  = undef;
1643     my $state  = undef;
1644     my $status = undef;
1645     my $ctrl   = undef;
1646     my $learn  = undef; # learn state
1647     my $pred   = undef; # battery's ability to be charged
1648     my @output = ();
1649
1650     if ($snmp) {
1651         my %bat_oid
1652           = (
1653              '1.3.6.1.4.1.674.10893.1.20.130.15.1.4'  => 'batteryState',
1654              '1.3.6.1.4.1.674.10893.1.20.130.15.1.6'  => 'batteryComponentStatus',
1655              '1.3.6.1.4.1.674.10893.1.20.130.15.1.9'  => 'batteryNexusID',
1656              '1.3.6.1.4.1.674.10893.1.20.130.15.1.10' => 'batteryPredictedCapacity',
1657              '1.3.6.1.4.1.674.10893.1.20.130.15.1.12' => 'batteryLearnState',
1658              '1.3.6.1.4.1.674.10893.1.20.130.16.1.5'  => 'batteryConnectionControllerNumber',
1659             );
1660         my $result = undef;
1661         if ($opt{use_get_table}) {
1662             my $batteryTable = '1.3.6.1.4.1.674.10893.1.20.130.15';
1663             my $batteryConnectionTable = '1.3.6.1.4.1.674.10893.1.20.130.16';
1664
1665             $result = $snmp_session->get_table(-baseoid => $batteryTable);
1666             my $ext = $snmp_session->get_table(-baseoid => $batteryConnectionTable);
1667
1668             if (defined $result) {
1669                 defined $ext && map { $$result{$_} = $$ext{$_} } keys %{ $ext };
1670             }
1671         }
1672         else {
1673             $result = $snmp_session->get_entries(-columns => [keys %bat_oid]);
1674         }
1675
1676         # No cache battery is OK
1677         return if !defined $result;
1678
1679         @output = @{ get_snmp_output($result, \%bat_oid) };
1680     }
1681     else {
1682         foreach my $c (@controllers) {
1683             push @output, @{ run_omreport("storage battery controller=$c") };
1684             map_item('ctrl', $c, \@output);
1685         }
1686     }
1687
1688     my %bat_state
1689       = (
1690          0  => 'Unknown',
1691          1  => 'Ready',
1692          2  => 'Failed',
1693          6  => 'Degraded',
1694          7  => 'Reconditioning',
1695          9  => 'High',
1696          10 => 'Power Low',
1697          12 => 'Charging',
1698          21 => 'Missing',
1699          36 => 'Learning',
1700         );
1701
1702     # Specifies the learn state activity of the battery
1703     my %bat_learn_state
1704       = (
1705          1  => 'Failed',
1706          2  => 'Active',
1707          4  => 'Timed out',
1708          8  => 'Requested',
1709          16 => 'Idle',
1710         );
1711
1712     # This property displays the battery's ability to be charged
1713     my %bat_pred_cap
1714       = (
1715          1 => 'Failed',  # The battery cannot be charged and needs to be replaced
1716          2 => 'Ready',   # The battery can be charged to full capacity
1717          4 => 'Unknown', # The battery is completing a Learn cycle. The charge capacity of the
1718                          # battery cannot be determined until the Learn cycle is complete
1719         );
1720
1721     # Check battery on each of the controllers
1722   BATTERY:
1723     foreach my $out (@output) {
1724         if ($snmp) {
1725             $status = $snmp_status{$out->{batteryComponentStatus}};
1726             $state  = get_hashval($out->{batteryState}, \%bat_state);
1727             $learn  = get_hashval($out->{batteryLearnState}, \%bat_learn_state);
1728             $pred   = get_hashval($out->{batteryPredictedCapacity}, \%bat_pred_cap);
1729             $ctrl   = $out->{batteryConnectionControllerNumber} - 1;
1730             $nexus  = convert_nexus($out->{batteryNexusID});
1731             $id     = $nexus;
1732             $id     =~ s{\A \d+:(\d+) \z}{$1}xms;
1733         }
1734         else {
1735             $id     = $out->{'ID'};
1736             $state  = $out->{'State'};
1737             $status = $out->{'Status'};
1738             $learn  = $out->{'Learn State'};
1739             $pred   = $out->{'Predicted Capacity Status'};
1740             $ctrl   = $out->{'ctrl'};
1741             $nexus  = join q{:}, $out->{ctrl}, $id;
1742         }
1743
1744         next BATTERY if blacklisted('bat', $nexus);
1745
1746         # Special case: Charging
1747         if ($state eq 'Charging') {
1748             if ($pred eq 'Failed') {
1749                 my $msg = sprintf 'Cache battery %d in controller %d is %s (%s) [replace battery]',
1750                   $id, $ctrl, $state, $pred;
1751                 report('storage', $msg, $E_CRITICAL, $nexus);
1752             }
1753             else {
1754                 next BATTERY if blacklisted('bat_charge', $nexus);
1755                 my $msg = sprintf 'Cache battery %d in controller %d is %s (%s) [probably harmless]',
1756                   $id, $ctrl, $state, $pred;
1757                 report('storage', $msg, $E_WARNING, $nexus);
1758             }
1759         }
1760         # Special case: Learning (battery learns its capacity)
1761         elsif ($state eq 'Learning') {
1762             if ($learn eq 'Failed') {
1763                 my $msg = sprintf 'Cache battery %d in controller %d is %s (%s)',
1764                   $id, $ctrl, $state, $learn;
1765                 report('storage', $msg, $E_CRITICAL, $nexus);
1766             }
1767             else {
1768                 next BATTERY if blacklisted('bat_charge', $nexus);
1769                 my $msg = sprintf 'Cache battery %d in controller %d is %s (%s) [probably harmless]',
1770                   $id, $ctrl, $state, $learn;
1771                 report('storage', $msg, $E_WARNING, $nexus);
1772             }
1773         }
1774         # Special case: Power Low (first part of recharge cycle)
1775         elsif ($state eq 'Power Low') {
1776             next BATTERY if blacklisted('bat_charge', $nexus);
1777             my $msg = sprintf 'Cache battery %d in controller %d is %s [probably harmless]',
1778               $id, $ctrl, $state;
1779             report('storage', $msg, $E_WARNING, $nexus);
1780         }
1781         # Special case: Degraded and Non-Critical (usually part of recharge cycle)
1782         elsif ($state eq 'Degraded' && $status eq 'Non-Critical') {
1783             next BATTERY if blacklisted('bat_charge', $nexus);
1784             my $msg = sprintf 'Cache battery %d in controller %d is %s (%s) [probably harmless]',
1785               $id, $ctrl, $state, $status;
1786             report('storage', $msg, $E_WARNING, $nexus);
1787         }
1788         # Default
1789         elsif ($status ne 'Ok') {
1790             my $msg = sprintf 'Cache battery %d in controller %d needs attention: %s (%s)',
1791               $id, $ctrl, $state, $status;
1792             report('storage', $msg, $status2nagios{$status}, $nexus);
1793         }
1794         # Ok
1795         else {
1796             my $msg = sprintf 'Cache battery %d in controller %d is %s',
1797               $id, $ctrl, $state;
1798             report('storage', $msg, $E_OK, $nexus);
1799         }
1800     }
1801     return;
1802 }
1803
1804
1805 #-----------------------------------------
1806 # STORAGE: Check connectors (channels)
1807 #-----------------------------------------
1808 sub check_connectors {
1809     return if $#controllers == -1;
1810     return if blacklisted('conn', 'all');
1811
1812     my $id     = undef;
1813     my $nexus  = undef;
1814     my $name   = undef;
1815     my $state  = undef;
1816     my $status = undef;
1817     my $type   = undef;
1818     my $ctrl   = undef;
1819     my @output = ();
1820
1821     if ($snmp) {
1822         my %conn_oid
1823           = (
1824              '1.3.6.1.4.1.674.10893.1.20.130.2.1.1'  => 'channelNumber',
1825              '1.3.6.1.4.1.674.10893.1.20.130.2.1.2'  => 'channelName',
1826              '1.3.6.1.4.1.674.10893.1.20.130.2.1.3'  => 'channelState',
1827              '1.3.6.1.4.1.674.10893.1.20.130.2.1.8'  => 'channelComponentStatus',
1828              '1.3.6.1.4.1.674.10893.1.20.130.2.1.9'  => 'channelNexusID',
1829              '1.3.6.1.4.1.674.10893.1.20.130.2.1.11' => 'channelBusType',
1830             );
1831         my $result = undef;
1832         if ($opt{use_get_table}) {
1833             my $channelTable = '1.3.6.1.4.1.674.10893.1.20.130.2';
1834             $result = $snmp_session->get_table(-baseoid => $channelTable);
1835         }
1836         else {
1837             $result = $snmp_session->get_entries(-columns => [keys %conn_oid]);
1838         }
1839
1840         if (!defined $result) {
1841             printf "SNMP ERROR [storage / channel]: %s.\n", $snmp_session->error;
1842             $snmp_session->close;
1843             exit $E_UNKNOWN;
1844         }
1845
1846         @output = @{ get_snmp_output($result, \%conn_oid) };
1847     }
1848     else {
1849         foreach my $c (@controllers) {
1850             push @output, @{ run_omreport("storage connector controller=$c") };
1851             map_item('ctrl', $c, \@output);
1852         }
1853     }
1854
1855     my %conn_state
1856       = (
1857          0 => 'Unknown',
1858          1 => 'Ready',
1859          2 => 'Failed',
1860          3 => 'Online',
1861          4 => 'Offline',
1862          6 => 'Degraded',
1863         );
1864
1865     my %conn_bustype
1866       = (
1867          1 => 'SCSI',
1868          2 => 'IDE',
1869          3 => 'Fibre Channel',
1870          4 => 'SSA',
1871          6 => 'USB',
1872          7 => 'SATA',
1873          8 => 'SAS',
1874         );
1875
1876     # Check connectors on each of the controllers
1877   CHANNEL:
1878     foreach my $out (@output) {
1879         if ($snmp) {
1880             $id     = $out->{channelNumber} - 1;
1881             $name   = $out->{channelName};
1882             $status = $snmp_status{$out->{channelComponentStatus}};
1883             $state  = get_hashval($out->{channelState}, \%conn_state);
1884             $type   = get_hashval($out->{channelBusType}, \%conn_bustype);
1885             $nexus  = convert_nexus($out->{channelNexusID});
1886             $ctrl   = $nexus;
1887             $ctrl   =~ s{(\d+):\d+}{$1}xms;
1888         }
1889         else {
1890             $id     = $out->{'ID'};
1891             $name   = $out->{'Name'};
1892             $state  = $out->{'State'};
1893             $status = $out->{'Status'};
1894             $type   = $out->{'Connector Type'};
1895             $ctrl   = $out->{ctrl};
1896             $nexus  = join q{:}, $out->{ctrl}, $id;
1897         }
1898
1899         next CHANNEL if blacklisted('conn', $nexus);
1900
1901         my $msg = sprintf '%s [%s] on controller %d is %s',
1902           $name, $type, $ctrl, $state;
1903         report('storage', $msg, $status2nagios{$status}, $nexus);
1904     }
1905     return;
1906 }
1907
1908
1909 #-----------------------------------------
1910 # STORAGE: Check enclosures
1911 #-----------------------------------------
1912 sub check_enclosures {
1913     return if blacklisted('encl', 'all');
1914
1915     my $id       = undef;
1916     my $nexus    = undef;
1917     my $name     = undef;
1918     my $state    = undef;
1919     my $status   = undef;
1920     my $firmware = undef;
1921     my $ctrl     = undef;
1922     my @output   = ();
1923
1924     if ($snmp) {
1925         my %encl_oid
1926           = (
1927              '1.3.6.1.4.1.674.10893.1.20.130.3.1.1'  => 'enclosureNumber',
1928              '1.3.6.1.4.1.674.10893.1.20.130.3.1.2'  => 'enclosureName',
1929              '1.3.6.1.4.1.674.10893.1.20.130.3.1.4'  => 'enclosureState',
1930              '1.3.6.1.4.1.674.10893.1.20.130.3.1.19' => 'enclosureChannelNumber',
1931              '1.3.6.1.4.1.674.10893.1.20.130.3.1.24' => 'enclosureComponentStatus',
1932              '1.3.6.1.4.1.674.10893.1.20.130.3.1.25' => 'enclosureNexusID',
1933              '1.3.6.1.4.1.674.10893.1.20.130.3.1.26' => 'enclosureFirmwareVersion',
1934             );
1935         my $result = undef;
1936         if ($opt{use_get_table}) {
1937             my $enclosureTable = '1.3.6.1.4.1.674.10893.1.20.130.3';
1938             $result = $snmp_session->get_table(-baseoid => $enclosureTable);
1939         }
1940         else {
1941             $result = $snmp_session->get_entries(-columns => [keys %encl_oid]);
1942         }
1943
1944         # No enclosures is OK
1945         return if !defined $result;
1946
1947         @output = @{ get_snmp_output($result, \%encl_oid) };
1948     }
1949     else {
1950         foreach my $c (@controllers) {
1951             push @output, @{ run_omreport("storage enclosure controller=$c") };
1952             map_item('ctrl', $c, \@output);
1953         }
1954     }
1955
1956     my %encl_state
1957       = (
1958          0 => 'Unknown',
1959          1 => 'Ready',
1960          2 => 'Failed',
1961          3 => 'Online',
1962          4 => 'Offline',
1963          6 => 'Degraded',
1964         );
1965
1966   ENCLOSURE:
1967     foreach my $out (@output) {
1968         if ($snmp) {
1969             $id       = $out->{enclosureNumber} - 1;
1970             $name     = $out->{enclosureName};
1971             $state    = get_hashval($out->{enclosureState}, \%encl_state);
1972             $status   = $snmp_status{$out->{enclosureComponentStatus}};
1973             $firmware = exists $out->{enclosureFirmwareVersion}
1974               ? $out->{enclosureFirmwareVersion} : 'N/A';
1975             $nexus    = convert_nexus($out->{enclosureNexusID});
1976             $ctrl     = $nexus;
1977             $ctrl     =~ s{\A (\d+):.* \z}{$1}xms;
1978         }
1979         else {
1980             $id       = $out->{ID};
1981             $name     = $out->{Name};
1982             $state    = $out->{State};
1983             $status   = $out->{Status};
1984             $firmware = $out->{'Firmware Version'} ne 'Not Applicable'
1985               ? $out->{'Firmware Version'} : 'N/A';
1986             $nexus    = join q{:}, $out->{ctrl}, $id;
1987             $ctrl     = $out->{ctrl};
1988         }
1989
1990         $name     =~ s{\s+\z}{}xms; # remove trailing whitespace
1991         $firmware =~ s{\s+\z}{}xms; # remove trailing whitespace
1992
1993         # store enclosure data for future use
1994         push @enclosures, { 'id'    => $id,
1995                             'ctrl'  => $out->{ctrl},
1996                             'name'  => $name };
1997
1998         # Collecting some storage info
1999         $sysinfo{'enclosure'}{$nexus}{'id'}       = $nexus;
2000         $sysinfo{'enclosure'}{$nexus}{'name'}     = $name;
2001         $sysinfo{'enclosure'}{$nexus}{'firmware'} = $firmware;
2002
2003         next ENCLOSURE if blacklisted('encl', $nexus);
2004
2005         my $msg = sprintf 'Enclosure %s [%s] on controller %d is %s',
2006           $nexus, $name, $ctrl, $state;
2007         report('storage', $msg, $status2nagios{$status}, $nexus);
2008     }
2009     return;
2010 }
2011
2012
2013 #-----------------------------------------
2014 # STORAGE: Check enclosure fans
2015 #-----------------------------------------
2016 sub check_enclosure_fans {
2017     return if $#controllers == -1;
2018     return if blacklisted('encl_fan', 'all');
2019
2020     my $id        = undef;
2021     my $nexus     = undef;
2022     my $name      = undef;
2023     my $state     = undef;
2024     my $status    = undef;
2025     my $speed     = undef;
2026     my $encl_id   = undef;
2027     my $encl_name = undef;
2028     my @output    = ();
2029
2030     if ($snmp) {
2031         my %fan_oid
2032           = (
2033              '1.3.6.1.4.1.674.10893.1.20.130.7.1.1'  => 'fanNumber',
2034              '1.3.6.1.4.1.674.10893.1.20.130.7.1.2'  => 'fanName',
2035              '1.3.6.1.4.1.674.10893.1.20.130.7.1.4'  => 'fanState',
2036              '1.3.6.1.4.1.674.10893.1.20.130.7.1.11' => 'fanProbeCurrValue',
2037              '1.3.6.1.4.1.674.10893.1.20.130.7.1.15' => 'fanComponentStatus',
2038              '1.3.6.1.4.1.674.10893.1.20.130.7.1.16' => 'fanNexusID',
2039              '1.3.6.1.4.1.674.10893.1.20.130.8.1.4'  => 'fanConnectionEnclosureName',
2040              '1.3.6.1.4.1.674.10893.1.20.130.8.1.5'  => 'fanConnectionEnclosureNumber',
2041             );
2042         my $result = undef;
2043         if ($opt{use_get_table}) {
2044             my $fanTable = '1.3.6.1.4.1.674.10893.1.20.130.7';
2045             my $fanConnectionTable = '1.3.6.1.4.1.674.10893.1.20.130.8';
2046
2047             $result = $snmp_session->get_table(-baseoid => $fanTable);
2048             my $ext = $snmp_session->get_table(-baseoid => $fanConnectionTable);
2049
2050             if (defined $result) {
2051                 defined $ext && map { $$result{$_} = $$ext{$_} } keys %{ $ext };
2052             }
2053         }
2054         else {
2055             $result = $snmp_session->get_entries(-columns => [keys %fan_oid]);
2056         }
2057
2058         # No enclosure fans is OK
2059         return if !defined $result;
2060
2061         @output = @{ get_snmp_output($result, \%fan_oid) };
2062     }
2063     else {
2064         foreach my $enc (@enclosures) {
2065             push @output, @{ run_omreport("storage enclosure controller=$enc->{ctrl} enclosure=$enc->{id} info=fans") };
2066             map_item('ctrl', $enc->{ctrl}, \@output);
2067             map_item('encl_id', $enc->{id}, \@output);
2068             map_item('encl_name', $enc->{name}, \@output);
2069         }
2070     }
2071
2072     my %fan_state
2073       = (
2074          0  => 'Unknown',
2075          1  => 'Ready',
2076          2  => 'Failed',
2077          3  => 'Online',
2078          4  => 'Offline',
2079          6  => 'Degraded',
2080          21 => 'Missing',
2081         );
2082
2083     # Check fans on each of the enclosures
2084   FAN:
2085     foreach my $out (@output) {
2086         if ($snmp) {
2087             $id        = $out->{fanNumber} - 1;
2088             $name      = $out->{fanName};
2089             $state     = get_hashval($out->{fanState}, \%fan_state);
2090             $status    = $snmp_status{$out->{fanComponentStatus}};
2091             $speed     = $out->{fanProbeCurrValue};
2092             $encl_id   = $out->{fanConnectionEnclosureNumber} - 1;
2093             $encl_name = $out->{fanConnectionEnclosureName};
2094             $nexus     = convert_nexus($out->{fanNexusID});
2095         }
2096         else {
2097             $id        = $out->{'ID'};
2098             $name      = $out->{'Name'};
2099             $state     = $out->{'State'};
2100             $status    = $out->{'Status'};
2101             $speed     = $out->{'Speed'};
2102             $encl_id   = join q{:}, $out->{ctrl}, $out->{'encl_id'};
2103             $encl_name = $out->{encl_name};
2104             $nexus     = join q{:}, $out->{ctrl}, $out->{'encl_id'}, $id;
2105         }
2106
2107         next FAN if blacklisted('encl_fan', $nexus);
2108
2109         # Default
2110         if ($status ne 'Ok') {
2111             my $msg = sprintf '%s in enclosure %s [%s] needs attention: %s',
2112               $name, $encl_id, $encl_name, $state;
2113             report('storage', $msg, $status2nagios{$status}, $nexus);
2114         }
2115         # Ok
2116         else {
2117             my $msg = sprintf '%s in enclosure %s [%s] is %s (speed=%s)',
2118               $name, $encl_id, $encl_name, $state, $speed;
2119             report('storage', $msg, $E_OK, $nexus);
2120         }
2121     }
2122     return;
2123 }
2124
2125
2126 #-----------------------------------------
2127 # STORAGE: Check enclosure power supplies
2128 #-----------------------------------------
2129 sub check_enclosure_pwr {
2130     return if $#controllers == -1;
2131     return if blacklisted('encl_ps', 'all');
2132
2133     my $id        = undef;
2134     my $nexus     = undef;
2135     my $name      = undef;
2136     my $state     = undef;
2137     my $status    = undef;
2138     my $encl_id   = undef;
2139     my $encl_name = undef;
2140     my @output    = ();
2141
2142     if ($snmp) {
2143         my %ps_oid
2144           = (
2145              '1.3.6.1.4.1.674.10893.1.20.130.9.1.1'  => 'powerSupplyNumber',
2146              '1.3.6.1.4.1.674.10893.1.20.130.9.1.2'  => 'powerSupplyName',
2147              '1.3.6.1.4.1.674.10893.1.20.130.9.1.4'  => 'powerSupplyState',
2148              '1.3.6.1.4.1.674.10893.1.20.130.9.1.9'  => 'powerSupplyComponentStatus',
2149              '1.3.6.1.4.1.674.10893.1.20.130.9.1.10' => 'powerSupplyNexusID',
2150              '1.3.6.1.4.1.674.10893.1.20.130.10.1.4' => 'powerSupplyConnectionEnclosureName',
2151              '1.3.6.1.4.1.674.10893.1.20.130.10.1.5' => 'powerSupplyConnectionEnclosureNumber',
2152             );
2153         my $result = undef;
2154         if ($opt{use_get_table}) {
2155             my $powerSupplyTable = '1.3.6.1.4.1.674.10893.1.20.130.9';
2156             my $powerSupplyConnectionTable = '1.3.6.1.4.1.674.10893.1.20.130.10';
2157
2158             $result = $snmp_session->get_table(-baseoid => $powerSupplyTable);
2159             my $ext = $snmp_session->get_table(-baseoid => $powerSupplyConnectionTable);
2160
2161             if (defined $result) {
2162                 defined $ext && map { $$result{$_} = $$ext{$_} } keys %{ $ext };
2163             }
2164         }
2165         else {
2166             $result = $snmp_session->get_entries(-columns => [keys %ps_oid]);
2167         }
2168
2169         # No enclosure power supplies is OK
2170         return if !defined $result;
2171
2172         @output = @{ get_snmp_output($result, \%ps_oid) };
2173     }
2174     else {
2175         foreach my $enc (@enclosures) {
2176             push @output, @{ run_omreport("storage enclosure controller=$enc->{ctrl} enclosure=$enc->{id} info=pwrsupplies") };
2177             map_item('ctrl', $enc->{ctrl}, \@output);
2178             map_item('encl_id', $enc->{id}, \@output);
2179             map_item('encl_name', $enc->{name}, \@output);
2180         }
2181     }
2182
2183     my %ps_state
2184       = (
2185          0  => 'Unknown',
2186          1  => 'Ready',
2187          2  => 'Failed',
2188          5  => 'Not Installed',
2189          6  => 'Degraded',
2190          11 => 'Removed',
2191          21 => 'Missing',
2192         );
2193
2194     # Check power supplies on each of the enclosures
2195   PS:
2196     foreach my $out (@output) {
2197         if ($snmp) {
2198             $id        = $out->{powerSupplyNumber};
2199             $name      = $out->{powerSupplyName};
2200             $state     = get_hashval($out->{powerSupplyState}, \%ps_state);
2201             $status    = $snmp_status{$out->{powerSupplyComponentStatus}};
2202             $encl_id   = $out->{powerSupplyConnectionEnclosureNumber} - 1;
2203             $encl_name = $out->{powerSupplyConnectionEnclosureName};
2204             $nexus     = convert_nexus($out->{powerSupplyNexusID});
2205         }
2206         else {
2207             $id        = $out->{'ID'};
2208             $name      = $out->{'Name'};
2209             $state     = $out->{'State'};
2210             $status    = $out->{'Status'};
2211             $encl_id   = join q{:}, $out->{ctrl}, $out->{'encl_id'};
2212             $encl_name = $out->{encl_name};
2213             $nexus     = join q{:}, $out->{ctrl}, $out->{'encl_id'}, $id;
2214         }
2215
2216         next PS if blacklisted('encl_ps', $nexus);
2217
2218         # Default
2219         if ($status ne 'Ok') {
2220             my $msg = sprintf '%s in enclosure %s [%s] needs attention: %s',
2221               $name, $encl_id, $encl_name, $state;
2222             report('storage', $msg, $status2nagios{$status}, $nexus);
2223         }
2224         # Ok
2225         else {
2226             my $msg = sprintf '%s in enclosure %s [%s] is %s',
2227               $name, $encl_id, $encl_name, $state;
2228             report('storage', $msg, $E_OK, $nexus);
2229         }
2230     }
2231     return;
2232 }
2233
2234
2235 #-----------------------------------------
2236 # STORAGE: Check enclosure temperatures
2237 #-----------------------------------------
2238 sub check_enclosure_temp {
2239     return if $#controllers == -1;
2240     return if blacklisted('encl_temp', 'all');
2241
2242     my $id        = undef;
2243     my $nexus     = undef;
2244     my $name      = undef;
2245     my $state     = undef;
2246     my $status    = undef;
2247     my $reading   = undef;
2248     my $unit      = undef;
2249     my $max_warn  = undef;
2250     my $max_crit  = undef;
2251     my $encl_id   = undef;
2252     my $encl_name = undef;
2253     my @output    = ();
2254
2255     if ($snmp) {
2256         my %temp_oid
2257           = (
2258              '1.3.6.1.4.1.674.10893.1.20.130.11.1.1'  => 'temperatureProbeNumber',
2259              '1.3.6.1.4.1.674.10893.1.20.130.11.1.2'  => 'temperatureProbeName',
2260              '1.3.6.1.4.1.674.10893.1.20.130.11.1.4'  => 'temperatureProbeState',
2261              '1.3.6.1.4.1.674.10893.1.20.130.11.1.6'  => 'temperatureProbeUnit',
2262              '1.3.6.1.4.1.674.10893.1.20.130.11.1.9'  => 'temperatureProbeMaxWarning',
2263              '1.3.6.1.4.1.674.10893.1.20.130.11.1.10' => 'temperatureProbeMaxCritical',
2264              '1.3.6.1.4.1.674.10893.1.20.130.11.1.11' => 'temperatureProbeCurValue',
2265              '1.3.6.1.4.1.674.10893.1.20.130.11.1.13' => 'temperatureProbeComponentStatus',
2266              '1.3.6.1.4.1.674.10893.1.20.130.11.1.14' => 'temperatureProbeNexusID',
2267              '1.3.6.1.4.1.674.10893.1.20.130.12.1.4'  => 'temperatureConnectionEnclosureName',
2268              '1.3.6.1.4.1.674.10893.1.20.130.12.1.5'  => 'temperatureConnectionEnclosureNumber',
2269             );
2270         my $result = undef;
2271         if ($opt{use_get_table}) {
2272             my $temperatureProbeTable = '1.3.6.1.4.1.674.10893.1.20.130.11';
2273             my $temperatureConnectionTable = '1.3.6.1.4.1.674.10893.1.20.130.12';
2274
2275             $result = $snmp_session->get_table(-baseoid => $temperatureProbeTable);
2276             my $ext = $snmp_session->get_table(-baseoid => $temperatureConnectionTable);
2277
2278             if (defined $result) {
2279                 defined $ext && map { $$result{$_} = $$ext{$_} } keys %{ $ext };
2280             }
2281         }
2282         else {
2283             $result = $snmp_session->get_entries(-columns => [keys %temp_oid]);
2284         }
2285
2286         # No enclosure temperature probes is OK
2287         return if !defined $result;
2288
2289         @output = @{ get_snmp_output($result, \%temp_oid) };
2290     }
2291     else {
2292         foreach my $enc (@enclosures) {
2293             push @output, @{ run_omreport("storage enclosure controller=$enc->{ctrl} enclosure=$enc->{id} info=temps") };
2294             map_item('ctrl', $enc->{ctrl}, \@output);
2295             map_item('encl_id', $enc->{id}, \@output);
2296             map_item('encl_name', $enc->{name}, \@output);
2297         }
2298     }
2299
2300     my %temp_state
2301       = (
2302          0  => 'Unknown',
2303          1  => 'Ready',
2304          2  => 'Failed',
2305          4  => 'Offline',
2306          6  => 'Degraded',
2307          9  => 'Inactive',
2308          21 => 'Missing',
2309         );
2310
2311     # Check temperature probes on each of the enclosures
2312   TEMP:
2313     foreach my $out (@output) {
2314         if ($snmp) {
2315             $id        = $out->{temperatureProbeNumber} - 1;
2316             $name      = $out->{temperatureProbeName};
2317             $state     = get_hashval($out->{temperatureProbeState}, \%temp_state);
2318             $status    = $snmp_status{$out->{temperatureProbeComponentStatus}};
2319             $unit      = $out->{temperatureProbeUnit};
2320             $reading   = $out->{temperatureProbeCurValue};
2321             $max_warn  = $out->{temperatureProbeMaxWarning};
2322             $max_crit  = $out->{temperatureProbeMaxCritical};
2323             $encl_id   = $out->{temperatureConnectionEnclosureNumber} - 1;
2324             $encl_name = $out->{temperatureConnectionEnclosureName};
2325             $nexus     = convert_nexus($out->{temperatureProbeNexusID});
2326         }
2327         else {
2328             $id        = $out->{'ID'};
2329             $name      = $out->{'Name'};
2330             $state     = $out->{'State'};
2331             $status    = $out->{'Status'};
2332             $unit      = 'FIXME';
2333             $reading   = $out->{'Reading'}; $reading =~ s{\s*C}{}xms;
2334             $max_warn  = $out->{'Maximum Warning Threshold'}; $max_warn =~ s{\s*C}{}xms;
2335             $max_crit  = $out->{'Maximum Failure Threshold'}; $max_crit =~ s{\s*C}{}xms;
2336             $encl_id   = join q{:}, $out->{ctrl}, $out->{'encl_id'};
2337             $encl_name = $out->{encl_name};
2338             $nexus     = join q{:}, $out->{ctrl}, $out->{'encl_id'}, $id;
2339         }
2340
2341         next TEMP if blacklisted('encl_temp', $nexus);
2342
2343         # Default
2344         if ($status ne 'Ok') {
2345             my $msg = sprintf '%s in enclosure %s [%s] is %s C at %s (%s max)',
2346               $name, $encl_id, $encl_name, $state, $reading, $max_crit;
2347             report('storage', $msg, $status2nagios{$status}, $nexus);
2348         }
2349         # Ok
2350         else {
2351             my $msg = sprintf '%s in enclosure %s [%s]: %s C (%s max)',
2352               $name, $encl_id, $encl_name, $reading, $max_crit;
2353             report('storage', $msg, $E_OK, $nexus);
2354         }
2355
2356         # Collect performance data
2357         if (defined $opt{perfdata}) {
2358             $name =~ s{\A Temperature\sProbe\s(\d+) \z}{temp_$1}gxms;
2359             my $pkey = "enclosure_${encl_id}_${name}";
2360             my $pval = join q{;}, "${reading}C", $max_warn, $max_crit;
2361             $perfdata{$pkey} = $pval;
2362         }
2363     }
2364     return;
2365 }
2366
2367
2368 #-----------------------------------------
2369 # STORAGE: Check enclosure management modules (EMM)
2370 #-----------------------------------------
2371 sub check_enclosure_emms {
2372     return if $#controllers == -1;
2373     return if blacklisted('encl_emm', 'all');
2374
2375     my $id        = undef;
2376     my $nexus     = undef;
2377     my $name      = undef;
2378     my $state     = undef;
2379     my $status    = undef;
2380     my $encl_id   = undef;
2381     my $encl_name = undef;
2382     my @output    = ();
2383
2384     if ($snmp) {
2385         my %emms_oid
2386           = (
2387              '1.3.6.1.4.1.674.10893.1.20.130.13.1.1'  => 'enclosureManagementModuleNumber',
2388              '1.3.6.1.4.1.674.10893.1.20.130.13.1.2'  => 'enclosureManagementModuleName',
2389              '1.3.6.1.4.1.674.10893.1.20.130.13.1.4'  => 'enclosureManagementModuleState',
2390              '1.3.6.1.4.1.674.10893.1.20.130.13.1.11' => 'enclosureManagementModuleComponentStatus',
2391              '1.3.6.1.4.1.674.10893.1.20.130.13.1.12' => 'enclosureManagementModuleNexusID',
2392              '1.3.6.1.4.1.674.10893.1.20.130.14.1.4'  => 'enclosureManagementModuleConnectionEnclosureName',
2393              '1.3.6.1.4.1.674.10893.1.20.130.14.1.5'  => 'enclosureManagementModuleConnectionEnclosureNumber',
2394             );
2395         my $result = undef;
2396         if ($opt{use_get_table}) {
2397             my $enclosureManagementModuleTable = '1.3.6.1.4.1.674.10893.1.20.130.13';
2398             my $enclosureManagementModuleConnectionTable = '1.3.6.1.4.1.674.10893.1.20.130.14';
2399
2400             $result = $snmp_session->get_table(-baseoid => $enclosureManagementModuleTable);
2401             my $ext = $snmp_session->get_table(-baseoid => $enclosureManagementModuleConnectionTable);
2402
2403             if (defined $result) {
2404                 defined $ext && map { $$result{$_} = $$ext{$_} } keys %{ $ext };
2405             }
2406         }
2407         else {
2408             $result = $snmp_session->get_entries(-columns => [keys %emms_oid]);
2409         }
2410
2411         # No enclosure EMMs is OK
2412         return if !defined $result;
2413
2414         @output = @{ get_snmp_output($result, \%emms_oid) };
2415     }
2416     else {
2417         foreach my $enc (@enclosures) {
2418             push @output, @{ run_omreport("storage enclosure controller=$enc->{ctrl} enclosure=$enc->{id} info=emms") };
2419             map_item('ctrl', $enc->{ctrl}, \@output);
2420             map_item('encl_id', $enc->{id}, \@output);
2421             map_item('encl_name', $enc->{name}, \@output);
2422         }
2423     }
2424
2425     my %emms_state
2426       = (
2427          0  => 'Unknown',
2428          1  => 'Ready',
2429          2  => 'Failed',
2430          3  => 'Online',
2431          4  => 'Offline',
2432          5  => 'Not Installed',
2433          6  => 'Degraded',
2434          21 => 'Missing',
2435         );
2436
2437     # Check temperature probes on each of the enclosures
2438   EMM:
2439     foreach my $out (@output) {
2440         if ($snmp) {
2441             $id        = $out->{enclosureManagementModuleNumber} - 1;
2442             $name      = $out->{enclosureManagementModuleName};
2443             $state     = get_hashval($out->{enclosureManagementModuleState}, \%emms_state);
2444             $status    = $snmp_status{$out->{enclosureManagementModuleComponentStatus}};
2445             $encl_id   = $out->{enclosureManagementModuleConnectionEnclosureNumber} - 1;
2446             $encl_name = $out->{enclosureManagementModuleConnectionEnclosureName};
2447             $nexus     = convert_nexus($out->{enclosureManagementModuleNexusID});
2448         }
2449         else {
2450             $id        = $out->{'ID'};
2451             $name      = $out->{'Name'};
2452             $state     = $out->{'State'};
2453             $status    = $out->{'Status'};
2454             $encl_id   = join q{:}, $out->{ctrl}, $out->{'encl_id'};
2455             $encl_name = $out->{encl_name};
2456             $nexus     = join q{:}, $out->{ctrl}, $out->{'encl_id'}, $id;
2457         }
2458
2459         next EMM if blacklisted('encl_emm', $nexus);
2460
2461         # Default
2462         if ($status ne 'Ok') {
2463             my $msg = sprintf '%s in enclosure %s [%s] needs attention: %s',
2464               $name, $encl_id, $encl_name, $state;
2465             report('storage', $msg, $status2nagios{$status}, $nexus);
2466         }
2467         # Ok
2468         else {
2469             my $msg = sprintf '%s in enclosure %s [%s] is %s',
2470               $name, $encl_id, $encl_name, $state;
2471             report('storage', $msg, $E_OK, $nexus);
2472         }
2473     }
2474     return;
2475 }
2476
2477
2478 #-----------------------------------------
2479 # CHASSIS: Check memory modules
2480 #-----------------------------------------
2481 sub check_memory {
2482     return if blacklisted('dimm', 'all');
2483
2484     my $index    = undef;
2485     my $status   = undef;
2486     my $location = undef;
2487     my $size     = undef;
2488     my $modes    = undef;
2489     my @failures = ();
2490     my @output   = ();
2491
2492     if ($snmp) {
2493         my %dimm_oid
2494           = (
2495              '1.3.6.1.4.1.674.10892.1.1100.50.1.2.1'  => 'memoryDeviceIndex',
2496              '1.3.6.1.4.1.674.10892.1.1100.50.1.5.1'  => 'memoryDeviceStatus',
2497              '1.3.6.1.4.1.674.10892.1.1100.50.1.8.1'  => 'memoryDeviceLocationName',
2498              '1.3.6.1.4.1.674.10892.1.1100.50.1.14.1' => 'memoryDeviceSize',
2499              '1.3.6.1.4.1.674.10892.1.1100.50.1.20.1' => 'memoryDeviceFailureModes',
2500             );
2501         my $result = undef;
2502         if ($opt{use_get_table}) {
2503             my $memoryDeviceTable = '1.3.6.1.4.1.674.10892.1.1100.50.1';
2504             $result = $snmp_session->get_table(-baseoid => $memoryDeviceTable);
2505         }
2506         else {
2507             $result = $snmp_session->get_entries(-columns => [keys %dimm_oid]);
2508         }
2509
2510         if (!defined $result) {
2511             printf "SNMP ERROR [memory]: %s.\n", $snmp_session->error;
2512             $snmp_session->close;
2513             exit $E_UNKNOWN;
2514         }
2515
2516         @output = @{ get_snmp_output($result, \%dimm_oid) };
2517     }
2518     else {
2519         @output = @{ run_omreport("$omopt_chassis memory") };
2520     }
2521
2522     # Note: These values are bit masks, so combination values are
2523     # possible. If value is 0 (zero), memory device has no faults.
2524     my %failure_mode
2525       = (
2526          1  => 'ECC single bit correction warning rate exceeded',
2527          2  => 'ECC single bit correction failure rate exceeded',
2528          4  => 'ECC multibit fault encountered',
2529          8  => 'ECC single bit correction logging disabled',
2530          16 => 'device disabled because of spare activation',
2531         );
2532
2533   DIMM:
2534     foreach my $out (@output) {
2535         @failures = ();  # Initialize
2536         if ($snmp) {
2537             $index    = $out->{memoryDeviceIndex};
2538             $status   = $snmp_status{$out->{memoryDeviceStatus}};
2539             $location = $out->{memoryDeviceLocationName};
2540             $size     = sprintf '%d MB', $out->{memoryDeviceSize}/1024;
2541             $modes    = $out->{memoryDeviceFailureModes};
2542             if ($modes > 0) {
2543                 foreach my $mask (sort keys %failure_mode) {
2544                     if (($modes & $mask) != 0) { push @failures, $failure_mode{$mask}; }
2545                 }
2546             }
2547         }
2548         else {
2549             $index    = $out->{'Type'} eq '[Not Occupied]' ? undef : $out->{'Index'};
2550             $status   = $out->{'Status'};
2551             $location = $out->{'Connector Name'};
2552             $size     = $out->{'Size'};
2553             if (defined $size) {
2554                 $size =~ s{\s\s}{ }gxms;
2555             }
2556             # Run 'omreport chassis memory index=X' to get the failures
2557             if ($status ne 'Ok' && defined $index) {
2558                 foreach (@{ run_command("$omreport $omopt_chassis memory index=$index -fmt ssv") }) {
2559                     if (m/\A Failures; (.+?) \z/xms) {
2560                         chop(my $fail = $1);
2561                         push @failures, split m{\.}xms, $fail;
2562                     }
2563                 }
2564             }
2565         }
2566         $location =~ s{\A \s*(.*?)\s* \z}{$1}xms;
2567
2568         next DIMM if blacklisted('dimm', $index);
2569
2570         # Ignore empty memory slots
2571         next DIMM if !defined $index;
2572         $count{dimm}++;
2573
2574         if ($status ne 'Ok') {
2575             my $msg = undef;
2576             if (scalar @failures == 0) {
2577                 $msg = sprintf 'Memory module %d [%s, %s] needs attention (%s)',
2578                   $index, $location, $size, $status;
2579             }
2580             else {
2581                 $msg = sprintf 'Memory module %d [%s, %s] needs attention: %s',
2582                   $index, $location, $size, (join q{, }, @failures);
2583             }
2584
2585             report('chassis', $msg, $status2nagios{$status}, $index);
2586         }
2587         # Ok
2588         else {
2589             my $msg = sprintf 'Memory module %d [%s, %s] is %s',
2590               $index, $location, $size, $status;
2591             report('chassis', $msg, $E_OK, $index);
2592         }
2593     }
2594     return;
2595 }
2596
2597
2598 #-----------------------------------------
2599 # CHASSIS: Check fans
2600 #-----------------------------------------
2601 sub check_fans {
2602     return if blacklisted('fan', 'all');
2603
2604     my $index    = undef;
2605     my $status   = undef;
2606     my $reading  = undef;
2607     my $location = undef;
2608     my $max_crit = undef;
2609     my $max_warn = undef;
2610     my @output   = ();
2611
2612     if ($snmp) {
2613         my %cool_oid
2614           = (
2615              '1.3.6.1.4.1.674.10892.1.700.12.1.2.1'  => 'coolingDeviceIndex',
2616              '1.3.6.1.4.1.674.10892.1.700.12.1.5.1'  => 'coolingDeviceStatus',
2617              '1.3.6.1.4.1.674.10892.1.700.12.1.6.1'  => 'coolingDeviceReading',
2618              '1.3.6.1.4.1.674.10892.1.700.12.1.8.1'  => 'coolingDeviceLocationName',
2619              '1.3.6.1.4.1.674.10892.1.700.12.1.10.1' => 'coolingDeviceUpperCriticalThreshold',
2620              '1.3.6.1.4.1.674.10892.1.700.12.1.11.1' => 'coolingDeviceUpperNonCriticalThreshold',
2621             );
2622         my $result = undef;
2623         if ($opt{use_get_table}) {
2624             my $coolingDeviceTable = '1.3.6.1.4.1.674.10892.1.700.12.1';
2625             $result = $snmp_session->get_table(-baseoid => $coolingDeviceTable);
2626         }
2627         else {
2628             $result = $snmp_session->get_entries(-columns => [keys %cool_oid]);
2629         }
2630
2631         if ($blade && !defined $result) {
2632             return 0;
2633         }
2634         elsif (!$blade && !defined $result) {
2635             printf "SNMP ERROR [cooling]: %s.\n", $snmp_session->error;
2636             $snmp_session->close;
2637             exit $E_UNKNOWN;
2638         }
2639
2640         @output = @{ get_snmp_output($result, \%cool_oid) };
2641     }
2642     else {
2643         @output = @{ run_omreport("$omopt_chassis fans") };
2644     }
2645
2646   FAN:
2647     foreach my $out (@output) {
2648         if ($snmp) {
2649             $index    = $out->{coolingDeviceIndex};
2650             $status   = $snmp_probestatus{$out->{coolingDeviceStatus}};
2651             $reading  = $out->{coolingDeviceReading};
2652             $location = $out->{coolingDeviceLocationName};
2653             $max_crit = exists $out->{coolingDeviceUpperCriticalThreshold}
2654               ? $out->{coolingDeviceUpperCriticalThreshold} : 0;
2655             $max_warn = exists $out->{coolingDeviceUpperNonCriticalThreshold}
2656               ? $out->{coolingDeviceUpperNonCriticalThreshold} : 0;
2657         }
2658         else {
2659             $index    = $out->{'Index'};
2660             $status   = $out->{'Status'};
2661             $reading  = $out->{'Reading'};
2662             $location = $out->{'Probe Name'};
2663             $max_crit = $out->{'Maximum Failure Threshold'} ne '[N/A]'
2664               ? $out->{'Maximum Failure Threshold'} : 0;
2665             $max_warn = $out->{'Maximum Warning Threshold'} ne '[N/A]'
2666               ? $out->{'Maximum Warning Threshold'} : 0;
2667             $reading  =~ s{\A (\d+).* \z}{$1}xms;
2668             $max_warn =~ s{\A (\d+).* \z}{$1}xms;
2669             $max_crit =~ s{\A (\d+).* \z}{$1}xms;
2670         }
2671
2672         next FAN if blacklisted('fan', $index);
2673         $count{fan}++;
2674
2675         if ($status ne 'Ok') {
2676             my $msg = sprintf 'Chassis fan %d [%s] needs attention: %s',
2677               $index, $location, $status;
2678             my $err = $snmp ? $probestatus2nagios{$status} : $status2nagios{$status};
2679             report('chassis', $msg, $err, $index);
2680         }
2681         else {
2682             my $msg = sprintf 'Chassis fan %d [%s]: %s',
2683               $index, $location, $reading;
2684             report('chassis', $msg, $E_OK, $index);
2685         }
2686
2687         # Collect performance data
2688         if (defined $opt{perfdata}) {
2689             my $pname = lc $location;
2690             $pname =~ s{\s}{_}gxms;
2691             $pname =~ s{proc_}{cpu#}xms;
2692             my $pkey = join q{_}, 'fan', $index, $pname;
2693             my $pval = join q{;}, "${reading}RPM", $max_warn, $max_crit;
2694             $perfdata{$pkey} = $pval;
2695         }
2696     }
2697     return;
2698 }
2699
2700
2701 #-----------------------------------------
2702 # CHASSIS: Check power supplies
2703 #-----------------------------------------
2704 sub check_powersupplies {
2705     return if blacklisted('ps', 'all');
2706
2707     my $index    = undef;
2708     my $status   = undef;
2709     my $type     = undef;
2710     my $err_type = undef;
2711     my $state    = undef;
2712     my @states   = ();
2713     my @output   = ();
2714
2715     if ($snmp) {
2716         my %ps_oid
2717           = (
2718              '1.3.6.1.4.1.674.10892.1.600.12.1.2.1'  => 'powerSupplyIndex',
2719              '1.3.6.1.4.1.674.10892.1.600.12.1.5.1'  => 'powerSupplyStatus',
2720              '1.3.6.1.4.1.674.10892.1.600.12.1.7.1'  => 'powerSupplyType',
2721              '1.3.6.1.4.1.674.10892.1.600.12.1.11.1' => 'powerSupplySensorState',
2722              '1.3.6.1.4.1.674.10892.1.600.12.1.12.1' => 'powerSupplyConfigurationErrorType',
2723             );
2724         my $result = undef;
2725         if ($opt{use_get_table}) {
2726             my $powerDeviceTable = '1.3.6.1.4.1.674.10892.1.600.12.1';
2727             $result = $snmp_session->get_table(-baseoid => $powerDeviceTable);
2728         }
2729         else {
2730             $result = $snmp_session->get_entries(-columns => [keys %ps_oid]);
2731         }
2732
2733         # No instrumented PSU is OK (blades, low-end servers)
2734         return 0 if !defined $result;
2735
2736         @output = @{ get_snmp_output($result, \%ps_oid) };
2737     }
2738     else {
2739         @output = @{ run_omreport("$omopt_chassis pwrsupplies") };
2740     }
2741
2742     my %ps_type
2743       = (
2744          1  => 'Other',
2745          2  => 'Unknown',
2746          3  => 'Linear',
2747          4  => 'Switching',
2748          5  => 'Battery',
2749          6  => 'Uninterruptible Power Supply',
2750          7  => 'Converter',
2751          8  => 'Regulator',
2752          9  => 'AC',
2753          10 => 'DC',
2754          11 => 'VRM',
2755         );
2756
2757     my %ps_state
2758       = (
2759          1  => 'Presence detected',
2760          2  => 'Failure detected',
2761          4  => 'Predictive Failure',
2762          8  => 'AC lost',
2763          16 => 'AC lost or out-of-range',
2764          32 => 'AC out-of-range but present',
2765          64 => 'Configuration error',
2766         );
2767
2768     my %ps_config_error_type
2769       = (
2770          1 => 'Vendor mismatch',
2771          2 => 'Revision mismatch',
2772          3 => 'Processor missing',
2773         );
2774
2775   PS:
2776     foreach my $out (@output) {
2777         if ($snmp) {
2778             @states = ();  # contains states for the PS
2779
2780             $index    = $out->{powerSupplyIndex} - 1;
2781             $status   = $snmp_status{$out->{powerSupplyStatus}};
2782             $type     = get_hashval($out->{powerSupplyType}, \%ps_type);
2783             $err_type = defined $out->{powerSupplyConfigurationErrorType}
2784               ? $ps_config_error_type{$out->{powerSupplyConfigurationErrorType}} : undef;
2785
2786             # get the combined state from the StatusReading OID
2787             foreach my $mask (sort keys %ps_state) {
2788                 if (($out->{powerSupplySensorState} & $mask) != 0) {
2789                     push @states, $ps_state{$mask};
2790                 }
2791             }
2792
2793             # If configuration error, also include the error type
2794             if (defined $err_type) {
2795                 push @states, $err_type;
2796             }
2797
2798             # Finally, construct the state string
2799             $state = join q{, }, @states;
2800         }
2801         else {
2802             $index  = $out->{'Index'};
2803             $status = $out->{'Status'};
2804             $type   = $out->{'Type'};
2805             $state  = $out->{'Online Status'};
2806         }
2807
2808         next PS if blacklisted('ps', $index);
2809         $count{power}++;
2810
2811         if ($status ne 'Ok') {
2812             my $msg = sprintf 'Power Supply %d [%s] needs attention: %s',
2813               $index, $type, $state;
2814             report('chassis', $msg, $status2nagios{$status}, $index);
2815         }
2816         else {
2817             my $msg = sprintf 'Power Supply %d [%s]: %s',
2818               $index, $type, $state;
2819             report('chassis', $msg, $E_OK, $index);
2820         }
2821     }
2822     return;
2823 }
2824
2825
2826 #-----------------------------------------
2827 # CHASSIS: Check temperatures
2828 #-----------------------------------------
2829 sub check_temperatures {
2830     return if blacklisted('temp', 'all');
2831
2832     my $index    = undef;
2833     my $status   = undef;
2834     my $reading  = undef;
2835     my $location = undef;
2836     my $max_crit = undef;
2837     my $max_warn = undef;
2838     my $min_warn = undef;
2839     my $min_crit = undef;
2840     my $type     = undef;
2841     my $discrete = undef;
2842     my @output = ();
2843
2844     # Getting custom temperature thresholds (user option)
2845     my %warn_threshold = %{ custom_temperature_thresholds('w') };
2846     my %crit_threshold = %{ custom_temperature_thresholds('c') };
2847
2848     if ($snmp) {
2849         my %temp_oid
2850           = (
2851              '1.3.6.1.4.1.674.10892.1.700.20.1.2.1'  => 'temperatureProbeIndex',
2852              '1.3.6.1.4.1.674.10892.1.700.20.1.5.1'  => 'temperatureProbeStatus',
2853              '1.3.6.1.4.1.674.10892.1.700.20.1.6.1'  => 'temperatureProbeReading',
2854              '1.3.6.1.4.1.674.10892.1.700.20.1.7.1'  => 'temperatureProbeType',
2855              '1.3.6.1.4.1.674.10892.1.700.20.1.8.1'  => 'temperatureProbeLocationName',
2856              '1.3.6.1.4.1.674.10892.1.700.20.1.10.1' => 'temperatureProbeUpperCriticalThreshold',
2857              '1.3.6.1.4.1.674.10892.1.700.20.1.11.1' => 'temperatureProbeUpperNonCriticalThreshold',
2858              '1.3.6.1.4.1.674.10892.1.700.20.1.12.1' => 'temperatureProbeLowerNonCriticalThreshold',
2859              '1.3.6.1.4.1.674.10892.1.700.20.1.13.1' => 'temperatureProbeLowerCriticalThreshold',
2860              '1.3.6.1.4.1.674.10892.1.700.20.1.16.1' => 'temperatureProbeDiscreteReading',
2861             );
2862         # this didn't work well for some reason
2863         #my $result = $snmp_session->get_entries(-columns => [keys %temp_oid]);
2864
2865         # Getting values using the table
2866         my $temperatureProbeTable = '1.3.6.1.4.1.674.10892.1.700.20';
2867         my $result = $snmp_session->get_table(-baseoid => $temperatureProbeTable);
2868
2869         if (!defined $result) {
2870             printf "SNMP ERROR [temperatures]: %s.\n", $snmp_session->error;
2871             $snmp_session->close;
2872             exit $E_UNKNOWN;
2873         }
2874
2875         @output = @{ get_snmp_output($result, \%temp_oid) };
2876     }
2877     else {
2878         @output = @{ run_omreport("$omopt_chassis temps") };
2879     }
2880
2881     my %probe_type
2882       = (
2883          1  => 'Other',      # type is other than following values
2884          2  => 'Unknown',    # type is unknown
2885          3  => 'AmbientESM', # type is Ambient Embedded Systems Management temperature probe
2886          16 => 'Discrete',   # type is temperature probe with discrete reading
2887         );
2888
2889   TEMP:
2890     foreach my $out (@output) {
2891         if ($snmp) {
2892             $index    = $out->{temperatureProbeIndex} - 1;
2893             $status   = $snmp_probestatus{$out->{temperatureProbeStatus}};
2894             $reading  = $out->{temperatureProbeReading} / 10;
2895             $location = $out->{temperatureProbeLocationName};
2896             $max_crit = $out->{temperatureProbeUpperCriticalThreshold} / 10;
2897             $max_warn = $out->{temperatureProbeUpperNonCriticalThreshold} / 10;
2898             $min_crit = exists $out->{temperatureProbeLowerCriticalThreshold}
2899               ? $out->{temperatureProbeLowerCriticalThreshold} / 10 : '[N/A]';
2900             $min_warn = exists $out->{temperatureProbeLowerNonCriticalThreshold}
2901               ? $out->{temperatureProbeLowerNonCriticalThreshold} / 10 : '[N/A]';
2902             $type     = get_hashval($out->{temperatureProbeType}, \%probe_type);
2903             $discrete = exists $out->{temperatureProbeDiscreteReading}
2904               ? $out->{temperatureProbeDiscreteReading} : undef;
2905         }
2906         else {
2907             $index    = $out->{'Index'};
2908             $status   = $out->{'Status'};
2909             $reading  = $out->{'Reading'}; $reading =~ s{\.0\s+C}{}xms;
2910             $location = $out->{'Probe Name'};
2911             $max_crit = $out->{'Maximum Failure Threshold'}; $max_crit =~ s{\.0\s+C}{}xms;
2912             $max_warn = $out->{'Maximum Warning Threshold'}; $max_warn =~ s{\.0\s+C}{}xms;
2913             $min_crit = $out->{'Minimum Failure Threshold'}; $min_crit =~ s{\.0\s+C}{}xms;
2914             $min_warn = $out->{'Minimum Warning Threshold'}; $min_warn =~ s{\.0\s+C}{}xms;
2915             $type     = $reading =~ m{\A\d+\z}xms ? 'AmbientESM' : 'Discrete';
2916             $discrete = $reading;
2917         }
2918
2919         next TEMP if blacklisted('temp', $index);
2920         $count{temp}++;
2921
2922         if ($type eq 'Discrete') {
2923             my $msg = sprintf 'Temperature probe %d (%s): is %s',
2924               $index, $location, $discrete;
2925             my $err = $snmp ? $probestatus2nagios{$status} : $status2nagios{$status};
2926             report('chassis', $msg, $err, $index);
2927         }
2928         else {
2929             # First check according to custom thresholds
2930             if (exists $crit_threshold{$index}{max} and $reading > $crit_threshold{$index}{max}) {
2931                 # Custom critical MAX
2932                 my $msg = sprintf 'Temperature Probe %d [%s] reads %d C (custom max=%d)',
2933                   $index, $location, $reading, $crit_threshold{$index}{max};
2934                 report('chassis', $msg, $E_CRITICAL, $index);
2935             }
2936             elsif (exists $warn_threshold{$index}{max} and $reading > $warn_threshold{$index}{max}) {
2937                 # Custom warning MAX
2938                 my $msg = sprintf 'Temperature Probe %d [%s] reads %d C (custom max=%d)',
2939                   $index, $location, $reading, $warn_threshold{$index}{max};
2940                 report('chassis', $msg, $E_WARNING, $index);
2941             }
2942             elsif (exists $crit_threshold{$index}{min} and $reading < $crit_threshold{$index}{min}) {
2943                 # Custom critical MIN
2944                 my $msg = sprintf 'Temperature Probe %d [%s] reads %d C (custom min=%d)',
2945                   $index, $location, $reading, $crit_threshold{$index}{min};
2946                 report('chassis', $msg, $E_CRITICAL, $index);
2947             }
2948             elsif (exists $warn_threshold{$index}{min} and $reading < $warn_threshold{$index}{min}) {
2949                 # Custom warning MIN
2950                 my $msg = sprintf 'Temperature Probe %d [%s] reads %d C (custom min=%d)',
2951                   $index, $location, $reading, $warn_threshold{$index}{min};
2952                 report('chassis', $msg, $E_WARNING, $index);
2953             }
2954             elsif ($status ne 'Ok' and $max_crit ne '[N/A]' and $reading > $max_crit) {
2955                 my $msg = sprintf 'Temperature Probe %d [%s] is critically high at %d C',
2956                   $index, $location, $reading;
2957                 my $err = $snmp ? $probestatus2nagios{$status} : $status2nagios{$status};
2958                 report('chassis', $msg, $err, $index);
2959             }
2960             elsif ($status ne 'Ok' and $max_warn ne '[N/A]' and $reading > $max_warn) {
2961                 my $msg = sprintf 'Temperature Probe %d [%s] is too high at %d C',
2962                   $index, $location, $reading;
2963                 my $err = $snmp ? $probestatus2nagios{$status} : $status2nagios{$status};
2964                 report('chassis', $msg, $err, $index);
2965             }
2966             elsif ($status ne 'Ok' and $min_crit ne '[N/A]' and $reading < $min_crit) {
2967                 my $msg = sprintf 'Temperature Probe %d [%s] is critically low at %d C',
2968                   $index, $location, $reading;
2969                 my $err = $snmp ? $probestatus2nagios{$status} : $status2nagios{$status};
2970                 report('chassis', $msg, $err, $index);
2971             }
2972             elsif ($status ne 'Ok' and $min_warn ne '[N/A]' and $reading < $min_warn) {
2973                 my $msg = sprintf 'Temperature Probe %d [%s] is too low at %d C',
2974                   $index, $location, $reading;
2975                 my $err = $snmp ? $probestatus2nagios{$status} : $status2nagios{$status};
2976                 report('chassis', $msg, $err, $index);
2977             }
2978             # Ok
2979             else {
2980                 my $msg = sprintf 'Temperature Probe %d [%s] reads %d C',
2981                   $index, $location, $reading;
2982                 if ($min_warn eq '[N/A]' and $min_crit eq '[N/A]') {
2983                     $msg .= sprintf ' (max=%s/%s)', $max_warn, $max_crit;
2984                 }
2985                 else {
2986                     $msg .= sprintf ' (min=%s/%s, max=%s/%s)',
2987                       $min_warn, $min_crit, $max_warn, $max_crit;
2988                 }
2989                 my $err = $snmp ? $probestatus2nagios{$status} : $status2nagios{$status};
2990                 report('chassis', $msg, $err, $index);
2991             }
2992
2993             # Collect performance data
2994             if (defined $opt{perfdata}) {
2995                 my $pname = lc $location;
2996                 $pname =~ s{\s}{_}gxms;
2997                 $pname =~ s{_temp\z}{}xms;
2998                 $pname =~ s{proc_}{cpu#}xms;
2999                 my $pkey = join q{_}, 'temp', $index, $pname;
3000                 my $pval = join q{;}, "${reading}C", $max_warn, $max_crit;
3001                 $perfdata{$pkey} = $pval;
3002             }
3003         }
3004     }
3005     return;
3006 }
3007
3008
3009 #-----------------------------------------
3010 # CHASSIS: Check processors
3011 #-----------------------------------------
3012 sub check_processors {
3013     return if blacklisted('cpu', 'all');
3014
3015     my $index   = undef;
3016     my $status  = undef;
3017     my $state   = undef;
3018     my $brand   = undef;
3019     my $family  = undef;
3020     my $man     = undef;
3021     my $speed   = undef;
3022     my @output = ();
3023
3024     if ($snmp) {
3025
3026         # NOTE: For some reason, older models don't have the
3027         # "Processor Device Status" OIDs. We check both the newer
3028         # (preferred) OIDs and the old ones.
3029
3030         my %cpu_oid
3031           = (
3032              '1.3.6.1.4.1.674.10892.1.1100.30.1.2.1'  => 'processorDeviceIndex',
3033              '1.3.6.1.4.1.674.10892.1.1100.30.1.5.1'  => 'processorDeviceStatus',
3034              '1.3.6.1.4.1.674.10892.1.1100.30.1.8.1'  => 'processorDeviceManufacturerName',
3035              '1.3.6.1.4.1.674.10892.1.1100.30.1.9.1'  => 'processorDeviceStatusState',
3036              '1.3.6.1.4.1.674.10892.1.1100.30.1.10.1' => 'processorDeviceFamily',
3037              '1.3.6.1.4.1.674.10892.1.1100.30.1.12.1' => 'processorDeviceCurrentSpeed',
3038              '1.3.6.1.4.1.674.10892.1.1100.30.1.23.1' => 'processorDeviceBrandName',
3039              '1.3.6.1.4.1.674.10892.1.1100.32.1.2.1'  => 'processorDeviceStatusIndex',
3040              '1.3.6.1.4.1.674.10892.1.1100.32.1.5.1'  => 'processorDeviceStatusStatus',
3041              '1.3.6.1.4.1.674.10892.1.1100.32.1.6.1'  => 'processorDeviceStatusReading',
3042             );
3043         my $result = undef;
3044         if ($opt{use_get_table}) {
3045             my $processorDeviceTable = '1.3.6.1.4.1.674.10892.1.1100.30.1';
3046             my $processorDeviceStatusTable = '1.3.6.1.4.1.674.10892.1.1100.32.1';
3047
3048             $result = $snmp_session->get_table(-baseoid => $processorDeviceTable);
3049             my $ext = $snmp_session->get_table(-baseoid => $processorDeviceStatusTable);
3050
3051             defined $ext && map { $$result{$_} = $$ext{$_} } keys %{ $ext };
3052         }
3053         else {
3054             $result = $snmp_session->get_entries(-columns => [keys %cpu_oid]);
3055         }
3056
3057         if (!defined $result) {
3058             printf "SNMP ERROR [processors]: %s.\n", $snmp_session->error;
3059             $snmp_session->close;
3060             exit $E_UNKNOWN;
3061         }
3062
3063         @output = @{ get_snmp_output($result, \%cpu_oid) };
3064     }
3065     else {
3066         @output = @{ run_omreport("$omopt_chassis processors") };
3067     }
3068
3069     my %cpu_state
3070       = (
3071          1 => 'Other',         # other than following values
3072          2 => 'Unknown',       # unknown
3073          3 => 'Enabled',       # enabled
3074          4 => 'User Disabled', # disabled by user via BIOS setup
3075          5 => 'BIOS Disabled', # disabled by BIOS (POST error)
3076          6 => 'Idle',          # idle
3077         );
3078
3079     my %cpu_reading
3080       = (
3081          1    => 'Internal Error',      # Internal Error
3082          2    => 'Thermal Trip',        # Thermal Trip
3083          32   => 'Configuration Error', # Configuration Error
3084          128  => 'Present',             # Processor Present
3085          256  => 'Disabled',            # Processor Disabled
3086          512  => 'Terminator Present',  # Terminator Present
3087          1024 => 'Throttled',           # Processor Throttled
3088         );
3089
3090     # Mapping between family numbers from SNMP and actual CPU family
3091     my %cpu_family
3092       = (
3093          1   => 'Other',                2   => 'Unknown',              3   => '8086',
3094          4   => '80286',                5   => '386',                  6   => '486',
3095          7   => '8087',                 8   => '80287',                9   => '80387',
3096          10  => '80487',                11  => 'Pentium',              12  => 'Pentium Pro',
3097          13  => 'Pentium II',           14  => 'Pentium with MMX',     15  => 'Celeron',
3098          16  => 'Pentium II Xeon',      17  => 'Pentium III',          18  => 'Pentium III Xeon',
3099          19  => 'Pentium III',          20  => 'Itanium',              21  => 'Xeon',
3100          22  => 'Pentium 4',            23  => 'Xeon MP',              24  => 'Itanium 2',
3101          25  => 'K5',                   26  => 'K6',                   27  => 'K6-2',
3102          28  => 'K6-3',                 29  => 'Athlon',               30  => 'AMD2900',
3103          31  => 'K6-2+',                32  => 'Power PC',             33  => 'Power PC 601',
3104          34  => 'Power PC 603',         35  => 'Power PC 603+',        36  => 'Power PC 604',
3105          37  => 'Power PC 620',         38  => 'Power PC x704',        39  => 'Power PC 750',
3106          48  => 'Alpha',                49  => 'Alpha 21064',          50  => 'Alpha 21066',
3107          51  => 'Alpha 21164',          52  => 'Alpha 21164PC',        53  => 'Alpha 21164a',
3108          54  => 'Alpha 21264',          55  => 'Alpha 21364',          64  => 'MIPS',
3109          65  => 'MIPS R4000',           66  => 'MIPS R4200',           67  => 'MIPS R4400',
3110          68  => 'MIPS R4600',           69  => 'MIPS R10000',          80  => 'SPARC',
3111          81  => 'SuperSPARC',           82  => 'microSPARC II',        83  => 'microSPARC IIep',
3112          84  => 'UltraSPARC',           85  => 'UltraSPARC II',        86  => 'UltraSPARC IIi',
3113          87  => 'UltraSPARC III',       88  => 'UltraSPARC IIIi',      96  => '68040',
3114          97  => '68xxx',                98  => '68000',                99  => '68010',
3115          100 => '68020',                101 => '68030',                112 => 'Hobbit',
3116          120 => 'Crusoe TM5000',        121 => 'Crusoe TM3000',        122 => 'Efficeon TM8000',
3117          128 => 'Weitek',               131 => 'Athlon 64',            132 => 'Opteron',
3118          133 => 'Sempron',              134 => 'Turion 64 Mobile',     135 => 'Dual-Core Opteron',
3119          136 => 'Athlon 64 X2 DC',      137 => 'Turion 64 X2 M',       138 => 'Quad-Core Opteron',
3120          139 => '3rd gen Opteron',      144 => 'PA-RISC',              145 => 'PA-RISC 8500',
3121          146 => 'PA-RISC 8000',         147 => 'PA-RISC 7300LC',       148 => 'PA-RISC 7200',
3122          149 => 'PA-RISC 7100LC',       150 => 'PA-RISC 7100',         160 => 'V30',
3123          171 => 'Dual-Core Xeon 5200',  172 => 'Dual-Core Xeon 7200',  173 => 'Quad-Core Xeon 7300',
3124          174 => 'Quad-Core Xeon 7400',  175 => 'Multi-Core Xeon 7400', 176 => 'M1',
3125          177 => 'M2',                   180 => 'AS400',                182 => 'Athlon XP',
3126          183 => 'Athlon MP',            184 => 'Duron',                185 => 'Pentium M',
3127          186 => 'Celeron D',            187 => 'Pentium D',            188 => 'Pentium Extreme',
3128          189 => 'Core Solo',            190 => 'Core2',                191 => 'Core2 Duo',
3129          198 => 'Core i7',              199 => 'Dual-Core Celeron',    200 => 'IBM390',
3130          201 => 'G4',                   202 => 'G5',                   203 => 'ESA/390 G6',
3131          204 => 'z/Architectur',        210 => 'C7-M',                 211 => 'C7-D',
3132          212 => 'C7',                   213 => 'Eden',                 214 => 'Multi-Core Xeon',
3133          215 => 'Dual-Core Xeon 3xxx',  216 => 'Quad-Core Xeon 3xxx',  218 => 'Dual-Core Xeon 5xxx',
3134          219 => 'Quad-Core Xeon 5xxx',  221 => 'Dual-Core Xeon 7xxx',  222 => 'Quad-Core Xeon 7xxx',
3135          223 => 'Multi-Core Xeon 7xxx', 250 => 'i860',                 251 => 'i960',
3136         );
3137
3138   CPU:
3139     foreach my $out (@output) {
3140         if ($snmp) {
3141             $index  = exists $out->{processorDeviceStatusIndex}
3142               ? $out->{processorDeviceStatusIndex} - 1
3143                 : $out->{processorDeviceIndex} - 1;
3144             $status = exists $out->{processorDeviceStatusStatus}
3145               ? $snmp_status{$out->{processorDeviceStatusStatus}}
3146                 : $snmp_status{$out->{processorDeviceStatus}};
3147             if (exists $out->{processorDeviceStatusReading}) {
3148                 my @states  = ();  # contains states for the CPU
3149
3150                 # get the combined state from the StatusReading OID
3151                 foreach my $mask (sort keys %cpu_reading) {
3152                     if (($out->{processorDeviceStatusReading} & $mask) != 0) {
3153                         push @states, $cpu_reading{$mask};
3154                     }
3155                 }
3156
3157                 # Finally, create the state string
3158                 $state = join q{, }, @states;
3159             }
3160             else {
3161                 $state  = get_hashval($out->{processorDeviceStatusState}, \%cpu_state);
3162             }
3163             $man    = $out->{processorDeviceManufacturerName};
3164             $family = (exists $out->{processorDeviceFamily}
3165                        and exists $cpu_family{$out->{processorDeviceFamily}})
3166               ? $cpu_family{$out->{processorDeviceFamily}} : undef;
3167             $speed  = $out->{processorDeviceCurrentSpeed};
3168             $brand  = $out->{processorDeviceBrandName};
3169         }
3170         else {
3171             $index  = $out->{'Index'};
3172             $status = $out->{'Status'};
3173             $state  = $out->{'State'};
3174             $brand  = exists $out->{'Processor Brand'} ? $out->{'Processor Brand'} : undef;
3175             $family = exists $out->{'Processor Family'} ? $out->{'Processor Family'} : undef;
3176             $man    = exists $out->{'Processor Manufacturer'} ? $out->{'Processor Manufacturer'} : undef;
3177             $speed  = exists $out->{'Current Speed'} ? $out->{'Current Speed'} : undef;
3178         }
3179
3180         next CPU if blacklisted('cpu', $index);
3181
3182         # Ignore unoccupied CPU slots (omreport)
3183         next CPU if (defined $out->{'Processor Manufacturer'}
3184                      and $out->{'Processor Manufacturer'} eq '[Not Occupied]')
3185           or (defined $out->{'Processor Brand'} and $out->{'Processor Brand'} eq '[Not Occupied]');
3186
3187         # Ignore unoccupied CPU slots (snmp)
3188         if ($snmp and exists $out->{processorDeviceStatusReading}
3189             and $out->{processorDeviceStatusReading} == 0) {
3190             next CPU;
3191         }
3192
3193         $count{cpu}++;
3194
3195         if (defined $brand) {
3196             $brand =~ s{\s\s+}{ }gxms;
3197             $brand =~ s{\((R|tm)\)}{}gxms;
3198             $brand =~ s{\s(CPU|Processor)}{}xms;
3199             $brand =~ s{\s\@}{}xms;
3200         }
3201         elsif (defined $family and defined $man and defined $speed) {
3202             $speed =~ s{\A (\d+) .*}{$1}xms;
3203             $brand = sprintf '%s %s %.2fGHz', $man, $family, $speed / 1000;
3204         }
3205         else {
3206             $brand = "unknown";
3207         }
3208
3209         # Default
3210         if ($status ne 'Ok') {
3211             my $msg = sprintf 'Processor %d [%s] needs attention: %s',
3212               $index, $brand, $state;
3213             report('chassis', $msg, $status2nagios{$status}, $index);
3214         }
3215         # Ok
3216         else {
3217             my $msg = sprintf 'Processor %d [%s] is %s',
3218               $index, $brand, $state;
3219             report('chassis', $msg, $E_OK, $index);
3220         }
3221     }
3222     return;
3223 }
3224
3225
3226 #-----------------------------------------
3227 # CHASSIS: Check voltage probes
3228 #-----------------------------------------
3229 sub check_volts {
3230     return if blacklisted('volt', 'all');
3231
3232     my $index    = undef;
3233     my $status   = undef;
3234     my $reading  = undef;
3235     my $location = undef;
3236     my @output = ();
3237
3238     if ($snmp) {
3239         my %volt_oid
3240           = (
3241              '1.3.6.1.4.1.674.10892.1.600.20.1.2.1'  => 'voltageProbeIndex',
3242              '1.3.6.1.4.1.674.10892.1.600.20.1.5.1'  => 'voltageProbeStatus',
3243              '1.3.6.1.4.1.674.10892.1.600.20.1.6.1'  => 'voltageProbeReading',
3244              '1.3.6.1.4.1.674.10892.1.600.20.1.8.1'  => 'voltageProbeLocationName',
3245              '1.3.6.1.4.1.674.10892.1.600.20.1.16.1' => 'voltageProbeDiscreteReading',
3246             );
3247
3248         my $voltageProbeTable = '1.3.6.1.4.1.674.10892.1.600.20.1';
3249         my $result = $snmp_session->get_table(-baseoid => $voltageProbeTable);
3250
3251         if (!defined $result) {
3252             printf "SNMP ERROR [voltage]: %s.\n", $snmp_session->error;
3253             $snmp_session->close;
3254             exit $E_UNKNOWN;
3255         }
3256
3257         @output = @{ get_snmp_output($result, \%volt_oid) };
3258     }
3259     else {
3260         @output = @{ run_omreport("$omopt_chassis volts") };
3261     }
3262
3263     my %volt_discrete_reading
3264       = (
3265          1 => 'Good',
3266          2 => 'Bad',
3267         );
3268
3269   VOLT:
3270     foreach my $out (@output) {
3271         if ($snmp) {
3272             $index    = $out->{voltageProbeIndex} - 1;
3273             $status   = $snmp_probestatus{$out->{voltageProbeStatus}};
3274             $reading  = exists $out->{voltageProbeReading}
3275               ? sprintf('%.3f V', $out->{voltageProbeReading}/1000)
3276                 : get_hashval($out->{voltageProbeDiscreteReading}, \%volt_discrete_reading);
3277             $location = $out->{voltageProbeLocationName};
3278         }
3279         else {
3280             $index    = $out->{'Index'};
3281             $status   = $out->{'Status'};
3282             $reading  = $out->{'Reading'};
3283             $location = $out->{'Probe Name'};
3284         }
3285
3286         next VOLT if blacklisted('volt', $index);
3287         $count{volt}++;
3288
3289         my $msg = sprintf 'Voltage sensor %d [%s] is %s',
3290           $index, $location, $reading;
3291         my $err = $snmp ? $probestatus2nagios{$status} : $status2nagios{$status};
3292         report('chassis', $msg, $err, $index);
3293     }
3294     return;
3295 }
3296
3297
3298 #-----------------------------------------
3299 # CHASSIS: Check batteries
3300 #-----------------------------------------
3301 sub check_batteries {
3302     return if blacklisted('bp', 'all');
3303
3304     my $index    = undef;
3305     my $status   = undef;
3306     my $reading  = undef;
3307     my $location = undef;
3308     my @output = ();
3309
3310     if ($snmp) {
3311         my %bat_oid
3312           = (
3313              '1.3.6.1.4.1.674.10892.1.600.50.1.2.1' => 'batteryIndex',
3314              '1.3.6.1.4.1.674.10892.1.600.50.1.5.1' => 'batteryStatus',
3315              '1.3.6.1.4.1.674.10892.1.600.50.1.6.1' => 'batteryReading',
3316              '1.3.6.1.4.1.674.10892.1.600.50.1.7.1' => 'batteryLocationName',
3317             );
3318         my $result = undef;
3319         if ($opt{use_get_table}) {
3320             my $batteryTable = '1.3.6.1.4.1.674.10892.1.600.50.1';
3321             $result = $snmp_session->get_table(-baseoid => $batteryTable);
3322         }
3323         else {
3324             $result = $snmp_session->get_entries(-columns => [keys %bat_oid]);
3325         }
3326
3327         # No batteries is OK
3328         return 0 if !defined $result;
3329
3330         @output = @{ get_snmp_output($result, \%bat_oid) };
3331     }
3332     else {
3333         @output = @{ run_omreport("$omopt_chassis batteries") };
3334     }
3335
3336     my %bat_reading
3337       = (
3338          1 => 'Predictive Failure',
3339          2 => 'Failed',
3340          4 => 'Presence Detected',
3341         );
3342
3343   BATTERY:
3344     foreach my $out (@output) {
3345         if ($snmp) {
3346             $index    = $out->{batteryIndex} - 1;
3347             $status   = $snmp_status{$out->{batteryStatus}};
3348             $reading  = get_hashval($out->{batteryReading}, \%bat_reading);
3349             $location = $out->{batteryLocationName};
3350         }
3351         else {
3352             $index    = $out->{'Index'};
3353             $status   = $out->{'Status'};
3354             $reading  = $out->{'Reading'};
3355             $location = $out->{'Probe Name'};
3356         }
3357
3358         next BATTERY if blacklisted('bp', $index);
3359         $count{bat}++;
3360
3361         my $msg = sprintf 'Battery probe %d [%s] is %s',
3362           $index, $location, $reading;
3363         report('chassis', $msg, $status2nagios{$status}, $index);
3364     }
3365     return;
3366 }
3367
3368
3369 #-----------------------------------------
3370 # CHASSIS: Check amperage probes (power monitoring)
3371 #-----------------------------------------
3372 sub check_pwrmonitoring {
3373     return if blacklisted('amp', 'all');
3374
3375     my $index    = undef;
3376     my $status   = undef;
3377     my $reading  = undef;
3378     my $location = undef;
3379     my $max_crit = undef;
3380     my $max_warn = undef;
3381     my $unit     = undef;
3382     my @output = ();
3383
3384     if ($snmp) {
3385         my %amp_oid
3386           = (
3387              '1.3.6.1.4.1.674.10892.1.600.30.1.2.1'  => 'amperageProbeIndex',
3388              '1.3.6.1.4.1.674.10892.1.600.30.1.5.1'  => 'amperageProbeStatus',
3389              '1.3.6.1.4.1.674.10892.1.600.30.1.6.1'  => 'amperageProbeReading',
3390              '1.3.6.1.4.1.674.10892.1.600.30.1.7.1'  => 'amperageProbeType',
3391              '1.3.6.1.4.1.674.10892.1.600.30.1.8.1'  => 'amperageProbeLocationName',
3392              '1.3.6.1.4.1.674.10892.1.600.30.1.10.1' => 'amperageProbeUpperCriticalThreshold',
3393              '1.3.6.1.4.1.674.10892.1.600.30.1.11.1' => 'amperageProbeUpperNonCriticalThreshold',
3394              '1.3.6.1.4.1.674.10892.1.600.30.1.16.1' => 'amperageProbeDiscreteReading',
3395             );
3396         my $result = undef;
3397         if ($opt{use_get_table}) {
3398             my $amperageProbeTable = '1.3.6.1.4.1.674.10892.1.600.30.1';
3399             $result = $snmp_session->get_table(-baseoid => $amperageProbeTable);
3400         }
3401         else {
3402             $result = $snmp_session->get_entries(-columns => [keys %amp_oid]);
3403         }
3404
3405         # No pwrmonitoring is OK
3406         return 0 if !defined $result;
3407
3408         @output = @{ get_snmp_output($result, \%amp_oid) };
3409     }
3410     else {
3411         @output = @{ run_omreport("$omopt_chassis pwrmonitoring") };
3412     }
3413
3414     my %amp_type   # Amperage probe types
3415       = (
3416          1  => 'amperageProbeTypeIsOther',            # other than following values
3417          2  => 'amperageProbeTypeIsUnknown',          # unknown
3418          3  => 'amperageProbeTypeIs1Point5Volt',      # 1.5 amperage probe
3419          4  => 'amperageProbeTypeIs3Point3volt',      # 3.3 amperage probe
3420          5  => 'amperageProbeTypeIs5Volt',            # 5 amperage probe
3421          6  => 'amperageProbeTypeIsMinus5Volt',       # -5 amperage probe
3422          7  => 'amperageProbeTypeIs12Volt',           # 12 amperage probe
3423          8  => 'amperageProbeTypeIsMinus12Volt',      # -12 amperage probe
3424          9  => 'amperageProbeTypeIsIO',               # I/O probe
3425          10 => 'amperageProbeTypeIsCore',             # Core probe
3426          11 => 'amperageProbeTypeIsFLEA',             # FLEA (standby) probe
3427          12 => 'amperageProbeTypeIsBattery',          # Battery probe
3428          13 => 'amperageProbeTypeIsTerminator',       # SCSI Termination probe
3429          14 => 'amperageProbeTypeIs2Point5Volt',      # 2.5 amperage probe
3430          15 => 'amperageProbeTypeIsGTL',              # GTL (ground termination logic) probe
3431          16 => 'amperageProbeTypeIsDiscrete',         # amperage probe with discrete reading
3432          23 => 'amperageProbeTypeIsPowerSupplyAmps',  # Power Supply probe with reading in Amps
3433          24 => 'amperageProbeTypeIsPowerSupplyWatts', # Power Supply probe with reading in Watts
3434          25 => 'amperageProbeTypeIsSystemAmps',       # System probe with reading in Amps
3435          26 => 'amperageProbeTypeIsSystemWatts',      # System probe with reading in Watts
3436         );
3437
3438     my %amp_discrete
3439       = (
3440          1 => 'Good',
3441          2 => 'Bad',
3442         );
3443
3444     my %amp_unit
3445       = (
3446          'amperageProbeTypeIsPowerSupplyAmps'  => 'hA',  # tenths of Amps
3447          'amperageProbeTypeIsSystemAmps'       => 'hA',  # tenths of Amps
3448          'amperageProbeTypeIsPowerSupplyWatts' => 'W',   # Watts
3449          'amperageProbeTypeIsSystemWatts'      => 'W',   # Watts
3450          'amperageProbeTypeIsDiscrete'         => q{},   # discrete reading, no unit
3451         );
3452
3453   AMP:
3454     foreach my $out (@output) {
3455         if ($snmp) {
3456             $index    = $out->{amperageProbeIndex} - 1;
3457             $status   = $snmp_status{$out->{amperageProbeStatus}};
3458             $reading  = get_hashval($out->{amperageProbeType}, \%amp_type) eq 'amperageProbeTypeIsDiscrete'
3459               ? get_hashval($out->{amperageProbeDiscreteReading}, \%amp_discrete)
3460                 : $out->{amperageProbeReading};
3461             $location = $out->{amperageProbeLocationName};
3462             $max_crit = exists $out->{amperageProbeUpperCriticalThreshold}
3463               ? $out->{amperageProbeUpperCriticalThreshold} : 0;
3464             $max_warn = exists $out->{amperageProbeUpperNonCriticalThreshold}
3465               ? $out->{amperageProbeUpperNonCriticalThreshold} : 0;
3466             $unit     = exists $amp_unit{$amp_type{$out->{amperageProbeType}}}
3467               ? $amp_unit{$amp_type{$out->{amperageProbeType}}} : 'mA';
3468             if ($unit eq 'hA') {
3469                 $reading  /= 10;
3470                 $max_crit /= 10;
3471                 $max_warn /= 10;
3472                 $unit      = 'A';
3473             }
3474         }
3475         else {
3476             $index    = $out->{'Index'};
3477             next AMP if (!defined $index || $index !~ m/^\d+$/x);
3478             $status   = $out->{'Status'};
3479             $reading  = $out->{'Reading'};
3480             $location = $out->{'Probe Name'};
3481             $max_crit = $out->{'Failure Threshold'} ne '[N/A]'
3482               ? $out->{'Failure Threshold'} : 0;
3483             $max_warn = $out->{'Warning Threshold'} ne '[N/A]'
3484               ? $out->{'Warning Threshold'} : 0;
3485             $reading  =~ s{\A (\d+.*?)\s+([a-zA-Z]+) \s*\z}{$1}xms;
3486             $unit     = $2;
3487             $max_warn =~ s{\A (\d+.*?)\s+[a-zA-Z]+ \s*\z}{$1}xms;
3488             $max_crit =~ s{\A (\d+.*?)\s+[a-zA-Z]+ \s*\z}{$1}xms;
3489         }
3490
3491         next AMP if blacklisted('amp', $index);
3492         next AMP if $index !~ m{\A \d+ \z}xms;
3493         $count{amp}++;
3494
3495         my $msg = sprintf 'Amperage probe %d [%s] reads %s %s',
3496           $index, $location, $reading, $unit, $status;
3497         report('chassis', $msg, $status2nagios{$status}, $index);
3498
3499         # Collect performance data
3500         if (defined $opt{perfdata}) {
3501             next AMP if $reading !~ m{\A \d+(\.\d+)? \z}xms; # discrete reading (not number)
3502             my $pname = lc $location;
3503             $pname =~ s{\s}{_}gxms;
3504             my $pkey = join q{_}, 'pwr_mon', $index, $pname;
3505             my $pval = join q{;}, "$reading$unit", $max_warn, $max_crit;
3506             $perfdata{$pkey} = $pval;
3507         }
3508     }
3509
3510     # Collect EXTRA performance data not found at first run. This is a
3511     # rather ugly hack
3512     if (defined $opt{perfdata} && !$snmp) {
3513         my $found = 0;
3514         my $index = 0;
3515         my %used  = ();
3516
3517         # find used indexes
3518         foreach (keys %perfdata) {
3519             if (m/\A pwr_mon_(\d+)/xms) {
3520                 $used{$1} = 1;
3521             }
3522         }
3523
3524       AMP2:
3525         foreach my $line (@{ run_command("$omreport $omopt_chassis pwrmonitoring -fmt ssv") }) {
3526             chop $line;
3527             if ($line eq 'Location;Reading') {
3528                 $found = 1;
3529                 next AMP2;
3530             }
3531             if ($line eq q{}) {
3532                 $found = 0;
3533                 next AMP2;
3534             }
3535             if ($found and $line =~ m/\A ([^;]+?) ; (\d*\.\d+) \s ([AW]) \z/xms) {
3536                 my $aname = lc $1;
3537                 my $aval = $2;
3538                 my $aunit = $3;
3539                 $aname =~ s{\s}{_}gxms;
3540
3541                 # don't use an existing index
3542                 while (exists $used{$index}) { ++$index; }
3543
3544                 $perfdata{"pwr_mon_${index}_${aname}"} = "$aval$aunit;0;0";
3545                 ++$index;
3546             }
3547         }
3548     }
3549
3550     return;
3551 }
3552
3553
3554 #-----------------------------------------
3555 # CHASSIS: Check intrusion
3556 #-----------------------------------------
3557 sub check_intrusion {
3558     return if blacklisted('intr', 'all');
3559
3560     my $index    = undef;
3561     my $status   = undef;
3562     my $reading  = undef;
3563     my @output = ();
3564
3565     if ($snmp) {
3566         my %int_oid
3567           = (
3568              '1.3.6.1.4.1.674.10892.1.300.70.1.2.1' => 'intrusionIndex',
3569              '1.3.6.1.4.1.674.10892.1.300.70.1.5.1' => 'intrusionStatus',
3570              '1.3.6.1.4.1.674.10892.1.300.70.1.6.1' => 'intrusionReading',
3571             );
3572         my $result = undef;
3573         if ($opt{use_get_table}) {
3574             my $intrusionTable = '1.3.6.1.4.1.674.10892.1.300.70.1';
3575             $result = $snmp_session->get_table(-baseoid => $intrusionTable);
3576         }
3577         else {
3578             $result = $snmp_session->get_entries(-columns => [keys %int_oid]);
3579         }
3580
3581         # No intrusion is OK
3582         return 0 if !defined $result;
3583
3584         @output = @{ get_snmp_output($result, \%int_oid) };
3585     }
3586     else {
3587         @output = @{ run_omreport("$omopt_chassis intrusion") };
3588     }
3589
3590     my %int_reading
3591       = (
3592          1 => 'Not Breached',          # chassis not breached and no uncleared breaches
3593          2 => 'Breached',              # chassis currently breached
3594          3 => 'Breached Prior',        # chassis breached prior to boot and has not been cleared
3595          4 => 'Breach Sensor Failure', # intrusion sensor has failed
3596         );
3597
3598   INTRUSION:
3599     foreach my $out (@output) {
3600         if ($snmp) {
3601             $index    = $out->{intrusionIndex} - 1;
3602             $status   = $snmp_status{$out->{intrusionStatus}};
3603             $reading  = get_hashval($out->{intrusionReading}, \%int_reading);
3604         }
3605         else {
3606             $index    = $out->{'Index'};
3607             $status   = $out->{'Status'};
3608             $reading  = $out->{'State'};
3609         }
3610
3611         next INTRUSION if blacklisted('intr', $index);
3612         $count{intr}++;
3613
3614         if ($status ne 'Ok') {
3615             my $msg = sprintf 'Chassis intrusion %d detected: %s',
3616               $index, $reading;
3617             report('chassis', $msg, $E_WARNING, $index);
3618         }
3619         # Ok
3620         else {
3621             my $msg = sprintf 'Chassis intrusion %d detection: %s (%s)',
3622               $index, $status, $reading;
3623             report('chassis', $msg, $E_OK, $index);
3624         }
3625     }
3626     return;
3627 }
3628
3629
3630 #-----------------------------------------
3631 # CHASSIS: Check alert log
3632 #-----------------------------------------
3633 sub check_alertlog {
3634     return if $snmp; # Not supported with SNMP
3635
3636     my @output = @{ run_omreport("$omopt_system alertlog") };
3637     foreach my $out (@output) {
3638         ++$count{alert}{$out->{Severity}};
3639     }
3640
3641     # Create error messages and set exit value if appropriate
3642     my $err = 0;
3643     if ($count{alert}{'Critical'} > 0)        { $err = $E_CRITICAL; }
3644     elsif ($count{alert}{'Non-Critical'} > 0) { $err = $E_WARNING;  }
3645
3646     my $msg = sprintf 'Alert log content: %d critical, %d non-critical, %d ok',
3647       $count{alert}{'Critical'}, $count{alert}{'Non-Critical'}, $count{alert}{'Ok'};
3648     report('other', $msg, $err);
3649
3650     return;
3651 }
3652
3653 #-----------------------------------------
3654 # CHASSIS: Check ESM log overall health
3655 #-----------------------------------------
3656 sub check_esmlog_health {
3657     my $health = 'Ok';
3658
3659     if ($snmp) {
3660         my $systemStateEventLogStatus = '1.3.6.1.4.1.674.10892.1.200.10.1.41.1';
3661         my $result = $snmp_session->get_request(-varbindlist => [$systemStateEventLogStatus]);
3662         if (!defined $result) {
3663             my $msg = sprintf 'SNMP ERROR [esmhealth]: %s',
3664               $snmp_session->error;
3665             report('other', $msg, $E_UNKNOWN);
3666         }
3667         $health = $snmp_status{$result->{$systemStateEventLogStatus}};
3668     }
3669     else {
3670         foreach (@{ run_command("$omreport $omopt_system esmlog -fmt ssv") }) {
3671             if (m/\A Health;(.+) \z/xms) {
3672                 $health = $1;
3673                 chop $health;
3674                 last;
3675             }
3676         }
3677     }
3678
3679     # If the overall health of the ESM log is other than "Ok", the
3680     # fill grade of the log is more than 80% and the log should be
3681     # cleared
3682     if ($health eq 'Ok') {
3683         my $msg = sprintf 'ESM log health is Ok (less than 80%% full)';
3684         report('other', $msg, $E_OK);
3685     }
3686     elsif ($health eq 'Critical') {
3687         my $msg = sprintf 'ESM log is 100%% full';
3688         report('other', $msg, $status2nagios{$health});
3689     }
3690     else {
3691         my $msg = sprintf 'ESM log is more than 80%% full';
3692         report('other', $msg, $status2nagios{$health});
3693     }
3694
3695     return;
3696 }
3697
3698 #-----------------------------------------
3699 # CHASSIS: Check ESM log
3700 #-----------------------------------------
3701 sub check_esmlog {
3702     my @output = ();
3703
3704     if ($snmp) {
3705         my %esm_oid
3706           = (
3707              '1.3.6.1.4.1.674.10892.1.300.40.1.7.1'  => 'eventLogSeverityStatus',
3708             );
3709         my $result = $snmp_session->get_entries(-columns => [keys %esm_oid]);
3710
3711         # No entries is OK
3712         return if !defined $result;
3713
3714         @output = @{ get_snmp_output($result, \%esm_oid) };
3715         foreach my $out (@output) {
3716             ++$count{esm}{$snmp_status{$out->{eventLogSeverityStatus}}};
3717         }
3718     }
3719     else {
3720         @output = @{ run_omreport("$omopt_system esmlog") };
3721         foreach my $out (@output) {
3722             ++$count{esm}{$out->{Severity}};
3723         }
3724     }
3725
3726     # Create error messages and set exit value if appropriate
3727     my $err = 0;
3728     if ($count{esm}{'Critical'} > 0)        { $err = $E_CRITICAL; }
3729     elsif ($count{esm}{'Non-Critical'} > 0) { $err = $E_WARNING;  }
3730
3731     my $msg = sprintf 'ESM log content: %d critical, %d non-critical, %d ok',
3732       $count{esm}{'Critical'}, $count{esm}{'Non-Critical'}, $count{esm}{'Ok'};
3733     report('other', $msg, $err);
3734
3735     return;
3736 }
3737
3738 #
3739 # Handy function for checking all storage components
3740 #
3741 sub check_storage {
3742     check_controllers();
3743     check_physical_disks();
3744     check_virtual_disks();
3745     check_cache_battery();
3746     check_connectors();
3747     check_enclosures();
3748     check_enclosure_fans();
3749     check_enclosure_pwr();
3750     check_enclosure_temp();
3751     check_enclosure_emms();
3752     return;
3753 }
3754
3755
3756
3757 #---------------------------------------------------------------------
3758 # Info functions
3759 #---------------------------------------------------------------------
3760
3761 #
3762 # Fetch output from 'omreport chassis info', put in sysinfo hash
3763 #
3764 sub get_omreport_chassis_info {
3765     if (open my $INFO, '-|', "$omreport $omopt_chassis info -fmt ssv") {
3766         my @lines = <$INFO>;
3767         close $INFO;
3768         foreach (@lines) {
3769             next if !m/\A (Chassis\sModel|Chassis\sService\sTag|Model|Service\sTag)/xms;
3770             my ($key, $val) = split /;/xms;
3771             $key =~ s{\s+\z}{}xms; # remove trailing whitespace
3772             $val =~ s{\s+\z}{}xms; # remove trailing whitespace
3773             if ($key eq 'Chassis Model' or $key eq 'Model') {
3774                 $sysinfo{model}  = $val;
3775             }
3776             if ($key eq 'Chassis Service Tag' or $key eq 'Service Tag') {
3777                 $sysinfo{serial} = $val;
3778             }
3779         }
3780     }
3781     return;
3782 }
3783
3784 #
3785 # Fetch output from 'omreport chassis bios', put in sysinfo hash
3786 #
3787 sub get_omreport_chassis_bios {
3788     if (open my $BIOS, '-|', "$omreport $omopt_chassis bios -fmt ssv") {
3789         my @lines = <$BIOS>;
3790         close $BIOS;
3791         foreach (@lines) {
3792             next if !m/;/xms;
3793             my ($key, $val) = split /;/xms;
3794             $key =~ s{\s+\z}{}xms; # remove trailing whitespace
3795             $val =~ s{\s+\z}{}xms; # remove trailing whitespace
3796             $sysinfo{bios}     = $val if $key eq 'Version';
3797             $sysinfo{biosdate} = $val if $key eq 'Release Date';
3798         }
3799     }
3800     return;
3801 }
3802
3803 #
3804 # Fetch output from 'omreport system operatingsystem', put in sysinfo hash
3805 #
3806 sub get_omreport_system_operatingsystem {
3807     if (open my $VER, '-|', "$omreport $omopt_system operatingsystem -fmt ssv") {
3808         my @lines = <$VER>;
3809         close $VER;
3810         foreach (@lines) {
3811             next if !m/;/xms;
3812             my ($key, $val) = split /;/xms;
3813             $key =~ s{\s+\z}{}xms; # remove trailing whitespace
3814             $val =~ s{\s+\z}{}xms; # remove trailing whitespace
3815             if ($key eq 'Operating System') {
3816                 $sysinfo{osname} = $val;
3817             }
3818             elsif ($key eq 'Operating System Version') {
3819                 $sysinfo{osver}  = $val;
3820             }
3821         }
3822     }
3823     return;
3824 }
3825
3826 #
3827 # Fetch output from 'omreport about', put in sysinfo hash
3828 #
3829 sub get_omreport_about {
3830     if (open my $OM, '-|', "$omreport about -fmt ssv") {
3831         my @lines = <$OM>;
3832         close $OM;
3833         foreach (@lines) {
3834             if (m/\A Version;(.+) \z/xms) {
3835                 $sysinfo{om} = $1;
3836                 chomp $sysinfo{om};
3837             }
3838         }
3839     }
3840     return;
3841 }
3842
3843 #
3844 # Fetch chassis info via SNMP, put in sysinfo hash
3845 #
3846 sub get_snmp_chassis_info {
3847     my %chassis_oid
3848       = (
3849          '1.3.6.1.4.1.674.10892.1.300.10.1.9.1'  => 'chassisModelName',
3850          '1.3.6.1.4.1.674.10892.1.300.10.1.11.1' => 'chassisServiceTagName',
3851         );
3852
3853     my $chassisInformationTable = '1.3.6.1.4.1.674.10892.1.300.10.1';
3854     my $result = $snmp_session->get_table(-baseoid => $chassisInformationTable);
3855
3856     if (defined $result) {
3857         foreach my $oid (keys %{ $result }) {
3858             if (exists $chassis_oid{$oid} and $chassis_oid{$oid} eq 'chassisModelName') {
3859                 $sysinfo{model} = $result->{$oid};
3860                 $sysinfo{model} =~ s{\s+\z}{}xms; # remove trailing whitespace
3861             }
3862             elsif (exists $chassis_oid{$oid} and $chassis_oid{$oid} eq 'chassisServiceTagName') {
3863                 $sysinfo{serial} = $result->{$oid};
3864             }
3865         }
3866     }
3867     else {
3868         my $msg = sprintf 'SNMP ERROR getting chassis info: %s',
3869           $snmp_session->error;
3870         report('other', $msg, $E_UNKNOWN);
3871     }
3872     return;
3873 }
3874
3875 #
3876 # Fetch BIOS info via SNMP, put in sysinfo hash
3877 #
3878 sub get_snmp_chassis_bios {
3879     my %bios_oid
3880       = (
3881          '1.3.6.1.4.1.674.10892.1.300.50.1.7.1.1' => 'systemBIOSReleaseDateName',
3882          '1.3.6.1.4.1.674.10892.1.300.50.1.8.1.1' => 'systemBIOSVersionName',
3883         );
3884
3885     my $systemBIOSTable = '1.3.6.1.4.1.674.10892.1.300.50.1';
3886     my $result = $snmp_session->get_table(-baseoid => $systemBIOSTable);
3887
3888     if (defined $result) {
3889         foreach my $oid (keys %{ $result }) {
3890             if (exists $bios_oid{$oid} and $bios_oid{$oid} eq 'systemBIOSReleaseDateName') {
3891                 $sysinfo{biosdate} = $result->{$oid};
3892                 $sysinfo{biosdate} =~ s{\A (\d{4})(\d{2})(\d{2}).*}{$2/$3/$1}xms;
3893             }
3894             elsif (exists $bios_oid{$oid} and $bios_oid{$oid} eq 'systemBIOSVersionName') {
3895                 $sysinfo{bios} = $result->{$oid};
3896             }
3897         }
3898     }
3899     else {
3900         my $msg = sprintf 'SNMP ERROR getting BIOS info: %s',
3901           $snmp_session->error;
3902         report('other', $msg, $E_UNKNOWN);
3903     }
3904     return;
3905 }
3906
3907 #
3908 # Fetch OS info via SNMP, put in sysinfo hash
3909 #
3910 sub get_snmp_system_operatingsystem {
3911     my %os_oid
3912       = (
3913          '1.3.6.1.4.1.674.10892.1.400.10.1.6.1' => 'operatingSystemOperatingSystemName',
3914          '1.3.6.1.4.1.674.10892.1.400.10.1.7.1' => 'operatingSystemOperatingSystemVersionName',
3915         );
3916
3917     my $operatingSystemTable = '1.3.6.1.4.1.674.10892.1.400.10.1';
3918     my $result = $snmp_session->get_table(-baseoid => $operatingSystemTable);
3919
3920     if (defined $result) {
3921         foreach my $oid (keys %{ $result }) {
3922             if (exists $os_oid{$oid} and $os_oid{$oid} eq 'operatingSystemOperatingSystemName') {
3923                 $sysinfo{osname} = ($result->{$oid});
3924             }
3925             elsif (exists $os_oid{$oid} and $os_oid{$oid} eq 'operatingSystemOperatingSystemVersionName') {
3926                 $sysinfo{osver} = $result->{$oid};
3927             }
3928         }
3929     }
3930     else {
3931         my $msg = sprintf 'SNMP ERROR getting OS info: %s',
3932           $snmp_session->error;
3933         report('other', $msg, $E_UNKNOWN);
3934     }
3935     return;
3936 }
3937
3938 #
3939 # Fetch OMSA version via SNMP, put in sysinfo hash
3940 #
3941 sub get_snmp_about {
3942     my %omsa_oid
3943       = (
3944          '1.3.6.1.4.1.674.10892.1.100.10.0' => 'systemManagementSoftwareGlobalVersionName',
3945         );
3946     my $systemManagementSoftwareGroup = '1.3.6.1.4.1.674.10892.1.100';
3947     my $result = $snmp_session->get_table(-baseoid => $systemManagementSoftwareGroup);
3948     if (defined $result) {
3949         foreach my $oid (keys %{ $result }) {
3950             if (exists $omsa_oid{$oid} and $omsa_oid{$oid} eq 'systemManagementSoftwareGlobalVersionName') {
3951                 $sysinfo{om} = ($result->{$oid});
3952             }
3953         }
3954     }
3955     else {
3956         my $msg = sprintf 'SNMP ERROR getting OMSA info: %s',
3957           $snmp_session->error;
3958         report('other', $msg, $E_UNKNOWN);
3959     }
3960     return;
3961 }
3962
3963 #
3964 # Collects some information about the system
3965 #
3966 sub get_sysinfo
3967 {
3968     # Get system model and serial number
3969     $snmp ? get_snmp_chassis_info() : get_omreport_chassis_info();
3970
3971     # Get BIOS information. Only if needed
3972     if ( $opt{okinfo} >= 1
3973          or $opt{debug}
3974          or (defined $opt{postmsg} and $opt{postmsg} =~ m/[%][bd]/xms) ) {
3975         $snmp ? get_snmp_chassis_bios() : get_omreport_chassis_bios();
3976     }
3977
3978     # Get OMSA information. Only if needed
3979     if ($opt{okinfo} >= 3 or $opt{debug}) {
3980         $snmp ? get_snmp_about() : get_omreport_about();
3981     }
3982
3983     # Return now if debug
3984     return if $opt{debug};
3985
3986     # Get OS information. Only if needed
3987     if (defined $opt{postmsg} and $opt{postmsg} =~ m/[%][or]/xms) {
3988         $snmp ? get_snmp_system_operatingsystem() : get_omreport_system_operatingsystem();
3989     }
3990
3991     return;
3992 }
3993
3994
3995 # Helper function for running omreport when the results are strictly
3996 # name=value pairs.
3997 sub run_omreport_info {
3998     my $command = shift;
3999     my %output  = ();
4000     my @keys    = ();
4001
4002     # Run omreport and fetch output
4003     my $rawtext = slurp_command("$omreport $command -fmt ssv 2>&1");
4004
4005     # Parse output, store in array
4006     for ((split /\n/xms, $rawtext)) {
4007         if (m/\A Error/xms) {
4008             my $msg = "Problem running 'omreport $command': $_";
4009             report('other', $msg, $E_UNKNOWN);
4010         }
4011         next if !m/;/xms;  # ignore lines with less than two fields
4012         my @vals = split m/;/xms;
4013         $output{$vals[0]} = $vals[1];
4014     }
4015
4016     # Finally, return the collected information
4017     return \%output;
4018 }
4019
4020 # Get various firmware information (BMC, RAC)
4021 sub get_firmware_info {
4022     my @snmp_output = ();
4023     my %nrpe_output = ();
4024
4025     if ($snmp) {
4026         my %fw_oid
4027           = (
4028              '1.3.6.1.4.1.674.10892.1.300.60.1.7.1'  => 'firmwareType',
4029              '1.3.6.1.4.1.674.10892.1.300.60.1.8.1'  => 'firmwareTypeName',
4030              '1.3.6.1.4.1.674.10892.1.300.60.1.11.1' => 'firmwareVersionName',
4031             );
4032
4033         my $firmwareTable = '1.3.6.1.4.1.674.10892.1.300.60.1';
4034         my $result = $snmp_session->get_table(-baseoid => $firmwareTable);
4035
4036         # Some don't have this OID, this is ok
4037         if (!defined $result) {
4038             return;
4039         }
4040
4041         @snmp_output = @{ get_snmp_output($result, \%fw_oid) };
4042     }
4043     else {
4044         %nrpe_output = %{ run_omreport_info("$omopt_chassis info") };
4045     }
4046
4047     my %fw_type  # Firmware types
4048       = (
4049          1  => 'other',                              # other than following values
4050          2  => 'unknown',                            # unknown
4051          3  => 'systemBIOS',                         # System BIOS
4052          4  => 'embeddedSystemManagementController', # Embedded System Management Controller
4053          5  => 'powerSupplyParallelingBoard',        # Power Supply Paralleling Board
4054          6  => 'systemBackPlane',                    # System (Primary) Backplane
4055          7  => 'powerVault2XXSKernel',               # PowerVault 2XXS Kernel
4056          8  => 'powerVault2XXSApplication',          # PowerVault 2XXS Application
4057          9  => 'frontPanel',                         # Front Panel Controller
4058          10 => 'baseboardManagementController',      # Baseboard Management Controller
4059          11 => 'hotPlugPCI',                         # Hot Plug PCI Controller
4060          12 => 'sensorData',                         # Sensor Data Records
4061          13 => 'peripheralBay',                      # Peripheral Bay Backplane
4062          14 => 'secondaryBackPlane',                 # Secondary Backplane for ESM 2 systems
4063          15 => 'secondaryBackPlaneESM3And4',         # Secondary Backplane for ESM 3 and 4 systems
4064          16 => 'rac',                                # Remote Access Controller
4065          17 => 'imc'                                 # Integrated Management Controller
4066         );
4067
4068
4069     if ($snmp) {
4070         foreach my $out (@snmp_output) {
4071             if ($fw_type{$out->{firmwareType}} eq 'baseboardManagementController') {
4072                 $sysinfo{'bmc'} = 1;
4073                 $sysinfo{'bmc_fw'} = $out->{firmwareVersionName};
4074             }
4075             elsif ($fw_type{$out->{firmwareType}} =~ m{\A rac|imc \z}xms) {
4076                 my $name = $out->{firmwareTypeName}; $name =~ s/\s//gxms;
4077                 $sysinfo{'rac'} = 1;
4078                 $sysinfo{'rac_name'} = $name;
4079                 $sysinfo{'rac_fw'} = $out->{firmwareVersionName};
4080             }
4081         }
4082     }
4083     else {
4084         foreach my $key (keys %nrpe_output) {
4085             next if !defined $nrpe_output{$key};
4086             if ($key eq 'BMC Version' or $key eq 'Baseboard Management Controller Version') {
4087                 $sysinfo{'bmc'} = 1;
4088                 $sysinfo{'bmc_fw'} = $nrpe_output{$key};
4089             }
4090             elsif ($key =~ m{\A (i?DRAC)\s*(\d?)\s+Version}xms) {
4091                 my $name = "$1$2";
4092                 $sysinfo{'rac'} = 1;
4093                 $sysinfo{'rac_fw'} = $nrpe_output{$key};
4094                 $sysinfo{'rac_name'} = $name;
4095             }
4096         }
4097     }
4098
4099     return;
4100 }
4101
4102
4103
4104 #=====================================================================
4105 # Main program
4106 #=====================================================================
4107
4108 # Here we do the actual checking of components
4109 # Check global status if applicable
4110 if ($global) {
4111     $globalstatus = check_global();
4112 }
4113
4114 # Do multiple selected checks
4115 if ($check{storage})     { check_storage();       }
4116 if ($check{memory})      { check_memory();        }
4117 if ($check{fans})        { check_fans();          }
4118 if ($check{power})       { check_powersupplies(); }
4119 if ($check{temp})        { check_temperatures();  }
4120 if ($check{cpu})         { check_processors();    }
4121 if ($check{voltage})     { check_volts();         }
4122 if ($check{batteries})   { check_batteries();     }
4123 if ($check{amperage})    { check_pwrmonitoring(); }
4124 if ($check{intrusion})   { check_intrusion();     }
4125 if ($check{alertlog})    { check_alertlog();      }
4126 if ($check{esmlog})      { check_esmlog();        }
4127 if ($check{esmhealth})   { check_esmlog_health(); }
4128
4129
4130 #---------------------------------------------------------------------
4131 # Finish up
4132 #---------------------------------------------------------------------
4133
4134 # Counter variable
4135 %nagios_alert_count
4136   = (
4137      'OK'       => 0,
4138      'WARNING'  => 0,
4139      'CRITICAL' => 0,
4140      'UNKNOWN'  => 0,
4141     );
4142
4143 # Get system information
4144 get_sysinfo();
4145
4146 # Get firmware info if requested via option
4147 if ($opt{okinfo} >= 1) {
4148     get_firmware_info();
4149 }
4150
4151 # Close SNMP session
4152 if ($snmp) {
4153     $snmp_session->close;
4154 }
4155
4156 # Print messages
4157 if ($opt{debug}) {
4158     print "   System:      $sysinfo{model}\n";
4159     print "   ServiceTag:  $sysinfo{serial}";
4160     print q{ } x (25 - length $sysinfo{serial}), "OMSA version:    $sysinfo{om}\n";
4161     print "   BIOS/date:   $sysinfo{bios} $sysinfo{biosdate}";
4162     print q{ } x (25 - length "$sysinfo{bios} $sysinfo{biosdate}"), "Plugin version:  $VERSION\n";
4163     if ($#report_storage >= 0) {
4164         print "-----------------------------------------------------------------------------\n";
4165         print "   Storage Components                                                        \n";
4166         print "=============================================================================\n";
4167         print "  STATE  |    ID    |  MESSAGE TEXT                                          \n";
4168         print "---------+----------+--------------------------------------------------------\n";
4169         foreach (@report_storage) {
4170             my ($msg, $level, $nexus) = @{$_};
4171             print q{ } x (8 - length $reverse_exitcode{$level}) . "$reverse_exitcode{$level} | "
4172               . q{ } x (8 - length $nexus) . "$nexus | $msg\n";
4173             $nagios_alert_count{$reverse_exitcode{$level}}++;
4174         }
4175     }
4176     if ($#report_chassis >= 0) {
4177         print "-----------------------------------------------------------------------------\n";
4178         print "   Chassis Components                                                        \n";
4179         print "=============================================================================\n";
4180         print "  STATE  |  ID  |  MESSAGE TEXT                                              \n";
4181         print "---------+------+------------------------------------------------------------\n";
4182         foreach (@report_chassis) {
4183             my ($msg, $level, $nexus) = @{$_};
4184             print q{ } x (8 - length $reverse_exitcode{$level}) . "$reverse_exitcode{$level} | "
4185               . q{ } x (4 - length $nexus) . "$nexus | $msg\n";
4186             $nagios_alert_count{$reverse_exitcode{$level}}++;
4187         }
4188     }
4189     if ($#report_other >= 0) {
4190         print "-----------------------------------------------------------------------------\n";
4191         print "   Other messages                                                            \n";
4192         print "=============================================================================\n";
4193         print "  STATE  |  MESSAGE TEXT                                                     \n";
4194         print "---------+-------------------------------------------------------------------\n";
4195         foreach (@report_other) {
4196             my ($msg, $level, $nexus) = @{$_};
4197             print q{ } x (8 - length $reverse_exitcode{$level}) . "$reverse_exitcode{$level} | $msg\n";
4198             $nagios_alert_count{$reverse_exitcode{$level}}++;
4199         }
4200     }
4201 }
4202 else {
4203     my $c = 0;  # counter to determine linebreaks
4204
4205     # Run through each message, sorted by severity level
4206   ALERT:
4207     foreach (sort {$a->[1] < $b->[1]} (@report_storage, @report_chassis, @report_other)) {
4208         my ($msg, $level, $nexus) = @{ $_ };
4209         next ALERT if $level == $E_OK;
4210
4211         if (defined $opt{only}) {
4212             # If user wants only critical alerts
4213             next ALERT if ($opt{only} eq 'critical' and $level == $E_WARNING);
4214
4215             # If user wants only warning alerts
4216             next ALERT if ($opt{only} eq 'warning' and $level == $E_CRITICAL);
4217         }
4218
4219         # Prefix with service tag if specified with option '-i|--info'
4220         if ($opt{info}) {
4221             if (defined $opt{htmlinfo}) {
4222                 $msg = '[<a href="' . warranty_url($sysinfo{serial})
4223                   . "\">$sysinfo{serial}</a>] " . $msg;
4224             }
4225             else {
4226                 $msg = "[$sysinfo{serial}] " . $msg;
4227             }
4228         }
4229
4230         # Prefix with nagios level if specified with option '--state'
4231         $msg = $reverse_exitcode{$level} . ": $msg" if $opt{state};
4232
4233         # Prefix with one-letter nagios level if specified with option '--short-state'
4234         $msg = (substr $reverse_exitcode{$level}, 0, 1) . ": $msg" if $opt{shortstate};
4235
4236         ($c++ == 0) ? print $msg : print $linebreak, $msg;
4237
4238         $nagios_alert_count{$reverse_exitcode{$level}}++;
4239     }
4240 }
4241
4242 # Determine our exit code
4243 $exit_code = $E_OK;
4244 $exit_code = $E_UNKNOWN  if $nagios_alert_count{'UNKNOWN'} > 0;
4245 $exit_code = $E_WARNING  if $nagios_alert_count{'WARNING'} > 0;
4246 $exit_code = $E_CRITICAL if $nagios_alert_count{'CRITICAL'} > 0;
4247
4248 # Global status via SNMP.. extra safety check
4249 if ($globalstatus != $E_OK && $exit_code == $E_OK && !defined $opt{only}) {
4250     print "OOPS! Something is wrong with this server, but I don't know what. ";
4251     print "The global system health status is $reverse_exitcode{$globalstatus}, ";
4252     print "but every component check is OK. This may be a bug in the Nagios plugin, ";
4253     print "please file a bug report.\n";
4254     exit $E_UNKNOWN;
4255 }
4256
4257 # Print OK message
4258 if ($exit_code == $E_OK && defined $opt{only} && $opt{only} !~ m{\A critical|warning|chassis \z}xms && !$opt{debug}) {
4259     my %okmsg
4260       = ( 'storage'     => "STORAGE OK - $count{pdisk} physical drives, $count{vdisk} logical drives",
4261           'fans'        => $count{fan} == 0 && $blade ? 'OK - blade system with no fan probes' : "FANS OK - $count{fan} fan probes checked",
4262           'temp'        => "TEMPERATURES OK - $count{temp} temperature probes checked",
4263           'memory'      => "MEMORY OK - $count{dimm} memory modules checked",
4264           'power'       => $count{power} == 0 ? 'OK - no instrumented power supplies found' : "POWER OK - $count{power} power supplies checked",
4265           'cpu'         => "PROCESSORS OK - $count{cpu} processors checked",
4266           'voltage'     => "VOLTAGE OK - $count{volt} voltage probes checked",
4267           'batteries'   => $count{bat} == 0 ? 'OK - no batteries found' : "BATTERIES OK - $count{bat} batteries checked",
4268           'amperage'    => $count{amp} == 0 ? 'OK - no power monitoring probes found' : "AMPERAGE OK - $count{amp} amperage (power monitoring) probes checked",
4269           'intrusion'   => $count{intr} == 0 ? 'OK - no intrusion detection probes found' : "INTRUSION OK - $count{intr} intrusion detection probes checked",
4270           'alertlog'    => $snmp ? 'OK - not supported via snmp' : "OK - Alert Log content: $count{alert}{Ok} ok, $count{alert}{'Non-Critical'} warning and $count{alert}{Critical} critical",
4271           'esmlog'      => "OK - ESM Log content: $count{esm}{Ok} ok, $count{esm}{'Non-Critical'} warning and $count{esm}{Critical} critical",
4272           'esmhealth'   => "ESM LOG OK - less than 80% used",
4273         );
4274
4275     print $okmsg{$opt{only}};
4276 }
4277 elsif ($exit_code == $E_OK && !$opt{debug}) {
4278     if (defined $opt{htmlinfo}) {
4279         printf q{OK - System: '<a href="%s">%s</a>', SN: '<a href="%s">%s</a>', hardware working fine},
4280           documentation_url($sysinfo{model}), $sysinfo{model},
4281             warranty_url($sysinfo{serial}), $sysinfo{serial};
4282     }
4283     else {
4284         printf q{OK - System: '%s', SN: '%s', hardware working fine},
4285           $sysinfo{model}, $sysinfo{serial};
4286     }
4287
4288     if ($check{storage}) {
4289         printf ', %d logical drives, %d physical drives',
4290           $count{vdisk}, $count{pdisk};
4291     }
4292     else {
4293         print ', not checking storage';
4294     }
4295
4296     if ($opt{okinfo} >= 1) {
4297         print $linebreak;
4298         printf q{----- BIOS='%s %s'}, $sysinfo{bios}, $sysinfo{biosdate};
4299
4300         if ($sysinfo{rac}) {
4301             printf q{, %s='%s'}, $sysinfo{rac_name}, $sysinfo{rac_fw};
4302         }
4303         if ($sysinfo{bmc}) {
4304             printf q{, BMC='%s'}, $sysinfo{bmc_fw};
4305         }
4306     }
4307
4308     if ($opt{okinfo} >= 2) {
4309         if ($check{storage}) {
4310             my @storageprint = ();
4311             foreach my $id (sort keys %{ $sysinfo{controller} }) {
4312                 chomp $sysinfo{controller}{$id}{driver};
4313                 my $msg = sprintf q{----- Ctrl %s [%s]: Fw='%s', Dr='%s'},
4314                   $sysinfo{controller}{$id}{id}, $sysinfo{controller}{$id}{name},
4315                     $sysinfo{controller}{$id}{firmware}, $sysinfo{controller}{$id}{driver};
4316                 if (defined $sysinfo{controller}{$id}{storport}) {
4317                     $msg .= sprintf q{, Storport: '%s'}, $sysinfo{controller}{$id}{storport};
4318                 }
4319                 push @storageprint, $msg;
4320             }
4321             foreach my $id (sort keys %{ $sysinfo{enclosure} }) {
4322                 push @storageprint, sprintf q{----- Encl %s [%s]: Fw='%s'},
4323                   $sysinfo{enclosure}{$id}->{id}, $sysinfo{enclosure}{$id}->{name},
4324                     $sysinfo{enclosure}{$id}->{firmware};
4325             }
4326
4327             # print stuff
4328             foreach my $line (@storageprint) {
4329                 print $linebreak, $line;
4330             }
4331         }
4332     }
4333
4334     if ($opt{okinfo} >= 3) {
4335         print "$linebreak----- OpenManage Server Administrator (OMSA) version: '$sysinfo{om}'";
4336     }
4337
4338 }
4339 else {
4340     if ($opt{extinfo}) {
4341         print $linebreak;
4342         if (defined $opt{htmlinfo}) {
4343             printf '------ SYSTEM: <a href="%s">%s</a>, SN: <a href="%s">%s</a>',
4344               documentation_url($sysinfo{model}), $sysinfo{model},
4345                 warranty_url($sysinfo{serial}), $sysinfo{serial};
4346         }
4347         else {
4348             printf '------ SYSTEM: %s, SN: %s',
4349               $sysinfo{model}, $sysinfo{serial};
4350         }
4351     }
4352     if (defined $opt{postmsg}) {
4353         my $post = undef;
4354         if (-f $opt{postmsg}) {
4355             open my $POST, '<', $opt{postmsg}
4356               or ( print $linebreak
4357                    and print "ERROR: Couldn't open post message file $opt{postmsg}: $!\n"
4358                    and exit $E_UNKNOWN );
4359             $post = <$POST>;
4360             close $POST;
4361             chomp $post;
4362         }
4363         else {
4364             $post = $opt{postmsg};
4365         }
4366         if (defined $post) {
4367             print $linebreak;
4368             $post =~ s{[%]s}{$sysinfo{serial}}gxms;
4369             $post =~ s{[%]m}{$sysinfo{model}}gxms;
4370             $post =~ s{[%]b}{$sysinfo{bios}}gxms;
4371             $post =~ s{[%]d}{$sysinfo{biosdate}}gxms;
4372             $post =~ s{[%]o}{$sysinfo{osname}}gxms;
4373             $post =~ s{[%]r}{$sysinfo{osver}}gxms;
4374             $post =~ s{[%]p}{$count{pdisk}}gxms;
4375             $post =~ s{[%]l}{$count{vdisk}}gxms;
4376             $post =~ s{[%]n}{$linebreak}gxms;
4377             $post =~ s{[%]{2}}{%}gxms;
4378             print $post;
4379         }
4380     }
4381 }
4382
4383 # Print any perl warnings that have occured
4384 if (@perl_warnings) {
4385     foreach (@perl_warnings) {
4386         chop @$_;
4387         print "${linebreak}INTERNAL ERROR: @$_";
4388         if (defined $opt{htmlinfo}) {
4389             # check_openmanage.html#internal-error-blah-blah
4390             print ' [<a href="http://bit.ly/aMDzE1">more info</a>]';
4391         }
4392     }
4393     $exit_code = $E_UNKNOWN;
4394 }
4395
4396 # Reset the WARN signal
4397 $SIG{__WARN__} = $original_sigwarn;
4398
4399 # Print performance data
4400 if (defined $opt{perfdata} && !$opt{debug} && %perfdata) {
4401     my $lb = $opt{perfdata} eq 'multiline' ? "\n" : q{ };  # line break for perfdata
4402     print q{|};
4403
4404     sub perfdata {
4405         my %order
4406           = (
4407              fan       => 0,
4408              pwr       => 1,
4409              temp      => 2,
4410              enclosure => 3,
4411             );
4412         return ($order{(split /_/, $a, 2)[0]} cmp $order{(split /_/, $b, 2)[0]}) || $a cmp $b;
4413     }
4414
4415     print join $lb, map { "'$_'=$perfdata{$_}" } sort perfdata keys %perfdata;
4416 }
4417
4418 # Print a linebreak at the end
4419 print "\n" if !$opt{debug};
4420
4421 # Exit with proper exit code
4422 exit $exit_code;