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