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