]> git.uio.no Git - check_openmanage.git/blobdiff - check_openmanage
bugfix
[check_openmanage.git] / check_openmanage
index d986761b15616efc6debbdf2f23dcb4a6f433c45..a610353e11aacf08c530d3e9bc1b798a8b58297b 100755 (executable)
@@ -51,7 +51,7 @@ $SIG{__WARN__} = sub { push @perl_warnings, [@_]; };
 
 # Version and similar info
 $NAME    = 'check_openmanage';
-$VERSION = '3.7.0-beta11';
+$VERSION = '3.7.0';
 $AUTHOR  = 'Trond H. Amundsen';
 $CONTACT = 't.h.amundsen@usit.uio.no';
 
@@ -138,6 +138,7 @@ END_LICENSE
         'debug'             => 0,        # debugging / verbose output
         'help'              => 0,        # display help output
         'perfdata'          => undef,    # output performance data
+        'legacy_perfdata'   => 0,        # legacy performance data output
         'info'              => 0,        # display servicetag
         'extinfo'           => 0,        # display extra info
         'htmlinfo'          => undef,    # html tags in output
@@ -181,6 +182,7 @@ GetOptions('b|blacklist=s'      => \@{ $opt{blacklist} },
           'h|help'             => \$opt{help},
           'V|version'          => \$opt{version},
           'p|perfdata:s'       => \$opt{perfdata},
+          'legacy-perfdata'    => \$opt{legacy_perfdata},
           'i|info'             => \$opt{info},
           'e|extinfo'          => \$opt{extinfo},
           'I|htmlinfo:s'       => \$opt{htmlinfo},
@@ -480,23 +482,27 @@ sub parse_configfile {
     # Mapping between command line options and the corresponding
     # config file options
     our %opt2config
-      = ( 'info'           => 'output_servicetag',
-         'extinfo'        => 'output_sysinfo',
-         'state'          => 'output_servicestate',
-         'shortstate'     => 'output_servicestate_abbr',
-         'show_blacklist' => 'output_blacklist',
-         'htmlinfo'       => 'output_html',
-         'okinfo'         => 'output_ok_verbosity',
-         'protocol'       => 'snmp_version',
-         'community'      => 'snmp_community',
-         'port'           => 'snmp_port',
-         'ipv6'           => 'snmp_use_ipv6',
-         'tcp'            => 'snmp_use_tcp',
-         'warning'        => 'temp_threshold_warning',
-         'critical'       => 'temp_threshold_critical',
-         'all'            => 'check_everything',
-         'perfdata'       => 'performance_data',
-         'tempunit'       => 'temperature_unit',
+      = ( 'info'            => 'output_servicetag',
+         'extinfo'         => 'output_sysinfo',
+         'postmsg'         => 'output_post_message',
+         'state'           => 'output_servicestate',
+         'shortstate'      => 'output_servicestate_abbr',
+         'show_blacklist'  => 'output_blacklist',
+         'htmlinfo'        => 'output_html',
+         'okinfo'          => 'output_ok_verbosity',
+         'protocol'        => 'snmp_version',
+         'community'       => 'snmp_community',
+         'port'            => 'snmp_port',
+         'ipv6'            => 'snmp_use_ipv6',
+         'tcp'             => 'snmp_use_tcp',
+         'warning'         => 'temp_threshold_warning',
+         'critical'        => 'temp_threshold_critical',
+         'all'             => 'check_everything',
+         'perfdata'        => 'performance_data',
+         'tempunit'        => 'temperature_unit',
+         'timeout'         => 'timeout',
+         'blacklist'       => 'blacklist',
+         'legacy_perfdata' => 'legacy_performance_data',
        );
 
     # Load the perl module
@@ -513,6 +519,32 @@ sub parse_configfile {
       or do { report('other', (sprintf q{Couldn't read configuration file: %s}, Config::Tiny->errstr()), $E_UNKNOWN);
              return; };
 
+    # Syntax check
+    foreach my $section (keys %{ $tiny }) {
+      KEYWORD:
+       foreach my $keyword (keys %{ $tiny->{$section} }) {
+           next KEYWORD if $keyword eq 'check_everything';
+           if ($keyword =~ m{\A check_(.+)}xms) {
+               my $c = $1;
+               foreach my $cl (keys %check) {
+                   next KEYWORD if $c eq $cl;
+               }
+           }
+           else {
+             LEGAL:
+               foreach my $legal (keys %opt2config) {
+                   next KEYWORD if $keyword eq $opt2config{$legal};
+               }
+           }
+           if ($section eq '_') {
+               report('other', qq{CONFIG ERROR: In the global section: Unknown statement "$keyword"}, $E_UNKNOWN);
+           }
+           else {
+               report('other', qq{CONFIG ERROR: Unknown statement "$keyword" in section "$section"}, $E_UNKNOWN);
+           }
+       }
+    }
+
     # Adjust checks according to statements in the configuration file
     sub configfile_adjust_checks {
        my $keyword = shift;
@@ -695,6 +727,16 @@ sub parse_configfile {
        return;
     }
 
+    # Set postmsg string from config file
+    sub configfile_set_postmsg {
+       my $keyword = shift;
+       my $conf = $opt2config{postmsg};
+       if (defined $tiny->{$keyword}->{$conf} and $tiny->{$keyword}->{$conf} ne q{}) {
+           $opt{postmsg} = $tiny->{$keyword}->{$conf}; # array ref
+       }
+       return;
+    }
+
     # Sections in the config file to check for statements
     my @sections = ();
 
@@ -733,6 +775,7 @@ sub parse_configfile {
        configfile_set_boolean($sect, 'show_blacklist');
        configfile_set_boolean($sect, 'ipv6');
        configfile_set_boolean($sect, 'tcp');
+       configfile_set_boolean($sect, 'legacy_perfdata');
        configfile_set_snmp_version($sect);
        configfile_set_snmp_community($sect);
        configfile_set_snmp_port($sect);
@@ -740,6 +783,7 @@ sub parse_configfile {
        configfile_set_temp_threshold($sect, 'critical');
        configfile_set_perfdata($sect);
        configfile_set_tempunit($sect);
+       configfile_set_postmsg($sect);
     }
 
     return;
@@ -1459,13 +1503,13 @@ sub temp_from_celsius {
     my $to = shift;
 
     if ($to eq 'F') {
-       return ceil($x * 9/5 + 32);
+       return sprintf '%.1f', ($x * 9/5 + 32);
     }
     elsif ($to eq 'K') {
-       return ceil($x + 273.15);
+       return sprintf '%.1f', ($x + 273.15);
     }
     elsif ($to eq 'R') {
-       return ceil($x * 9/5 + 32 + 459.67);
+       return sprintf '%.1f', ($x * 9/5 + 32 + 459.67);
     }
     return $x;
 }
@@ -2826,25 +2870,25 @@ sub check_enclosure_temp {
            report('storage', $msg, $E_OK, $nexus);
        }
        elsif ($status ne 'Ok' and $max_crit ne '[N/A]' and $reading > $max_crit) {
-           my $msg = sprintf '%s in enclosure %s [%s] is critically high at %d %s',
+           my $msg = sprintf '%s in enclosure %s [%s] is critically high at %s %s',
              $name, $encl_id, $encl_name, $reading, $opt{tempunit};
            my $err = $snmp ? $probestatus2nagios{$status} : $status2nagios{$status};
            report('chassis', $msg, $err, $nexus);
        }
        elsif ($status ne 'Ok' and $max_warn ne '[N/A]' and $reading > $max_warn) {
-           my $msg = sprintf '%s in enclosure %s [%s] is too high at %d %s',
+           my $msg = sprintf '%s in enclosure %s [%s] is too high at %s %s',
              $name, $encl_id, $encl_name, $reading, $opt{tempunit};
            my $err = $snmp ? $probestatus2nagios{$status} : $status2nagios{$status};
            report('chassis', $msg, $err, $nexus);
        }
        elsif ($status ne 'Ok' and $min_crit ne '[N/A]' and $reading < $min_crit) {
-           my $msg = sprintf '%s in enclosure %s [%s] is critically low at %d %s',
+           my $msg = sprintf '%s in enclosure %s [%s] is critically low at %s %s',
              $name, $encl_id, $encl_name, $reading, $opt{tempunit};
            my $err = $snmp ? $probestatus2nagios{$status} : $status2nagios{$status};
            report('chassis', $msg, $err, $nexus);
        }
        elsif ($status ne 'Ok' and $min_warn ne '[N/A]' and $reading < $min_warn) {
-           my $msg = sprintf '%s in enclosure %s [%s] is too low at %d %s',
+           my $msg = sprintf '%s in enclosure %s [%s] is too low at %s %s',
              $name, $encl_id, $encl_name, $reading, $opt{tempunit};
            my $err = $snmp ? $probestatus2nagios{$status} : $status2nagios{$status};
            report('chassis', $msg, $err, $nexus);
@@ -2875,7 +2919,7 @@ sub check_enclosure_temp {
            if (defined $reading && $reading ne '[N/A]') {
                # take into account that with certain states the
                # reading doesn't exist or is not an integer
-               $msg .= sprintf ' reads %d %s', $reading, $opt{tempunit};
+               $msg .= sprintf ' reads %s %s', $reading, $opt{tempunit};
                if ($min_warn eq '[N/A]' or $min_crit eq '[N/A]') {
                    $msg .= sprintf ' (max=%s/%s)', $max_warn, $max_crit;
                }
@@ -2894,14 +2938,21 @@ sub check_enclosure_temp {
        if (defined $opt{perfdata} && $reading ne '[N/A]') {
            my $index = $name;
            $index =~ s{\A Temperature\sProbe\s(\d+) \z}{$1}gxms;
+           my $legacy_name = $name;
+           $legacy_name =~ s{\A Temperature\sProbe\s(\d+) \z}{temp_$1}gxms;
+           my $legacy_label = lc "enclosure_${encl_id}_${legacy_name}";
+           my $legacy_mini = $legacy_label;
+            $legacy_mini =~ s{enclosure_(.+?)_temp_(.+?)}{e$1t$2}xms;
            push @perfdata, {
-                            type  => 'E',
-                            id    => $opt{perfdata} eq 'minimal' ? "${encl_id}_t${index}" : "${encl_id}_temp_${index}",
-                            unit  => $opt{tempunit},
-                            label => q{},
-                            value => $reading,
-                            warn  => $max_warn,
-                            crit  => $max_crit,
+                            type   => 'E',
+                            id     => $opt{perfdata} eq 'minimal' ? "${encl_id}_t${index}" : "${encl_id}_temp_${index}",
+                            unit   => $opt{tempunit},
+                            label  => q{},
+                            legacy => $legacy_label,
+                            mini   => $legacy_mini,
+                            value  => $reading,
+                            warn   => $max_warn,
+                            crit   => $max_crit,
                            };
        }
     }
@@ -3231,15 +3282,18 @@ sub check_fans {
            my $pname = $location;
            $pname =~ s{\s}{_}gxms;
            $pname =~ s{proc_}{cpu#}xms;
+           my $legacy_pname = $pname;
            $pname =~ s{_rpm\z}{}ixms;
            push @perfdata, {
-                            type  => 'F',
-                            id    => $index,
-                            unit  => 'rpm',
-                            label => $pname,
-                            value => $reading,
-                            warn  => $max_warn,
-                            crit  => $max_crit,
+                            type   => 'F',
+                            id     => $index,
+                            unit   => 'rpm',
+                            label  => $pname,
+                            legacy => lc "fan_${index}_${legacy_pname}",
+                             mini   => "f$index",
+                            value  => $reading,
+                            warn   => $max_warn,
+                            crit   => $max_crit,
                            };
        }
     }
@@ -3500,55 +3554,55 @@ sub check_temperatures {
            # First check according to custom thresholds
            if (exists $crit_threshold{$index}{max} and $reading > $crit_threshold{$index}{max}) {
                # Custom critical MAX
-               my $msg = sprintf 'Temperature Probe %d [%s] reads %d %s (custom max=%d)',
+               my $msg = sprintf 'Temperature Probe %d [%s] reads %s %s (custom max=%s)',
                  $index, $location, $reading, $opt{tempunit}, $crit_threshold{$index}{max};
                report('chassis', $msg, $E_CRITICAL, $index);
            }
            elsif (exists $warn_threshold{$index}{max} and $reading > $warn_threshold{$index}{max}) {
                # Custom warning MAX
-               my $msg = sprintf 'Temperature Probe %d [%s] reads %d %s (custom max=%d)',
+               my $msg = sprintf 'Temperature Probe %d [%s] reads %s %s (custom max=%s)',
                  $index, $location, $reading, $opt{tempunit}, $warn_threshold{$index}{max};
                report('chassis', $msg, $E_WARNING, $index);
            }
            elsif (exists $crit_threshold{$index}{min} and $reading < $crit_threshold{$index}{min}) {
                # Custom critical MIN
-               my $msg = sprintf 'Temperature Probe %d [%s] reads %d %s (custom min=%d)',
+               my $msg = sprintf 'Temperature Probe %d [%s] reads %s %s (custom min=%s)',
                  $index, $location, $reading, $opt{tempunit}, $crit_threshold{$index}{min};
                report('chassis', $msg, $E_CRITICAL, $index);
            }
            elsif (exists $warn_threshold{$index}{min} and $reading < $warn_threshold{$index}{min}) {
                # Custom warning MIN
-               my $msg = sprintf 'Temperature Probe %d [%s] reads %d %s (custom min=%d)',
+               my $msg = sprintf 'Temperature Probe %d [%s] reads %s %s (custom min=%s)',
                  $index, $location, $reading, $opt{tempunit}, $warn_threshold{$index}{min};
                report('chassis', $msg, $E_WARNING, $index);
            }
            elsif ($status ne 'Ok' and $max_crit ne '[N/A]' and $reading > $max_crit) {
-               my $msg = sprintf 'Temperature Probe %d [%s] is critically high at %d %s',
+               my $msg = sprintf 'Temperature Probe %d [%s] is critically high at %s %s',
                  $index, $location, $reading, $opt{tempunit};
                my $err = $snmp ? $probestatus2nagios{$status} : $status2nagios{$status};
                report('chassis', $msg, $err, $index);
            }
            elsif ($status ne 'Ok' and $max_warn ne '[N/A]' and $reading > $max_warn) {
-               my $msg = sprintf 'Temperature Probe %d [%s] is too high at %d %s',
+               my $msg = sprintf 'Temperature Probe %d [%s] is too high at %s %s',
                  $index, $location, $reading, $opt{tempunit};
                my $err = $snmp ? $probestatus2nagios{$status} : $status2nagios{$status};
                report('chassis', $msg, $err, $index);
            }
            elsif ($status ne 'Ok' and $min_crit ne '[N/A]' and $reading < $min_crit) {
-               my $msg = sprintf 'Temperature Probe %d [%s] is critically low at %d %s',
+               my $msg = sprintf 'Temperature Probe %d [%s] is critically low at %s %s',
                  $index, $location, $reading, $opt{tempunit};
                my $err = $snmp ? $probestatus2nagios{$status} : $status2nagios{$status};
                report('chassis', $msg, $err, $index);
            }
            elsif ($status ne 'Ok' and $min_warn ne '[N/A]' and $reading < $min_warn) {
-               my $msg = sprintf 'Temperature Probe %d [%s] is too low at %d %s',
+               my $msg = sprintf 'Temperature Probe %d [%s] is too low at %s %s',
                  $index, $location, $reading, $opt{tempunit};
                my $err = $snmp ? $probestatus2nagios{$status} : $status2nagios{$status};
                report('chassis', $msg, $err, $index);
            }
            # Ok
            else {
-               my $msg = sprintf 'Temperature Probe %d [%s] reads %d %s',
+               my $msg = sprintf 'Temperature Probe %d [%s] reads %s %s',
                  $index, $location, $reading, $opt{tempunit};
                if ($min_warn eq '[N/A]' and $min_crit eq '[N/A]') {
                    $msg .= sprintf ' (max=%s/%s)', $max_warn, $max_crit;
@@ -3565,16 +3619,18 @@ sub check_temperatures {
            if (defined $opt{perfdata}) {
                my $pname = $location;
                $pname =~ s{\s}{_}gxms;
-               $pname =~ s{_temp\z}{}xms;
+               $pname =~ s{_temp\z}{}ixms;
                $pname =~ s{proc_}{cpu#}xms;
                push @perfdata, {
-                                type  => 'T',
-                                id    => $index,
-                                unit  => $opt{tempunit},
-                                label => $pname,
-                                value => $reading,
-                                warn  => $max_warn,
-                                crit  => $max_crit,
+                                type   => 'T',
+                                id     => $index,
+                                unit   => $opt{tempunit},
+                                label  => $pname,
+                                legacy => lc "temp_${index}_${pname}",
+                                 mini   => "t$index",
+                                value  => $reading,
+                                warn   => $max_warn,
+                                crit   => $max_crit,
                                };
            }
        }
@@ -3956,20 +4012,20 @@ sub check_volts {
        report('chassis', $msg, $err, $index);
 
        # Collect performance data
-       if (defined $opt{perfdata}) {
+       if (defined $opt{perfdata} and !$opt{legacy_perfdata}) {
            $reading =~ s{\s+V\z}{}xms;  # remove unit
            $reading =~ s{\.000\z}{}xms; # if integer
            next VOLT if $reading !~ m{\A \d+(\.\d+)? \z}xms; # discrete reading (not number)
            my $label = join q{_}, $location;
            $label =~ s{\s}{_}gxms;
            push @perfdata, {
-                            type  => 'V',
-                            id    => $index,
-                            unit  => 'V',
-                            label => $label,
-                            value => $reading,
-                            warn  => 0,
-                            crit  => 0,
+                            type   => 'V',
+                            id     => $index,
+                            unit   => 'V',
+                            label  => $label,
+                            value  => $reading,
+                            warn   => 0,
+                            crit   => 0,
                            };
        }
     }
@@ -4204,13 +4260,15 @@ sub check_pwrmonitoring {
            my $label = join q{_},  $location;
            $label =~ s{\s}{_}gxms;
            push @perfdata, {
-                            type  => $unit,
-                            id    => $index,
-                            unit  => $unit,
-                            label => $label,
-                            value => $reading,
-                            warn  => $max_warn,
-                            crit  => $max_crit,
+                            type   => $unit,
+                            id     => $index,
+                            unit   => $unit,
+                            label  => $label,
+                            legacy => (join q{_}, 'pwr_mon', $index, lc $label),
+                             mini   => "p${index}" . lc $unit,
+                            value  => $reading,
+                            warn   => $max_warn,
+                            crit   => $max_crit,
                            };
        }
     }
@@ -4232,7 +4290,7 @@ sub check_pwrmonitoring {
       AMP2:
        foreach my $line (@{ run_command("$omreport $omopt_chassis pwrmonitoring -fmt ssv") }) {
            chop $line;
-           if ($line eq 'Location;Reading') {
+           if ($line eq 'Location;Reading' or $line eq 'Amperage') {
                $found = 1;
                next AMP2;
            }
@@ -4250,13 +4308,15 @@ sub check_pwrmonitoring {
                while (exists $used{$index}) { ++$index; }
 
                push @perfdata, {
-                                type  => $aunit,
-                                id    => $index,
-                                unit  => $aunit,
-                                label => $aname,
-                                value => $aval,
-                                warn  => 0,
-                                crit  => 0,
+                                type   => $aunit,
+                                id     => $index,
+                                unit   => $aunit,
+                                label  => $aname,
+                                legacy => "pwr_mon_${index}_${aname}",
+                                 mini   => "p${index}a",
+                                value  => $aval,
+                                warn   => 0,
+                                crit   => 0,
                                };
                ++$index;
            }
@@ -5067,7 +5127,7 @@ else {
        # Prefix with service tag if specified with option '-i|--info'
        if ($opt{info}) {
            if (defined $opt{htmlinfo}) {
-               $msg = '[<a href="' . warranty_url($sysinfo{serial})
+               $msg = '[<a target="_blank" href="' . warranty_url($sysinfo{serial})
                  . "\">$sysinfo{serial}</a>] " . $msg;
            }
            else {
@@ -5135,7 +5195,7 @@ if ($exit_code == $E_OK && defined $opt{only} && $opt{only} !~ m{\A critical|war
 }
 elsif ($exit_code == $E_OK && !$opt{debug}) {
     if (defined $opt{htmlinfo}) {
-       printf q{OK - System: '<a href="%s">%s%s</a>', SN: '<a href="%s">%s</a>'},
+       printf q{OK - System: '<a target="_blank" href="%s">%s%s</a>', SN: '<a target="_blank" href="%s">%s</a>'},
          documentation_url($sysinfo{model}), $sysinfo{model}, $sysinfo{rev},
            warranty_url($sysinfo{serial}), $sysinfo{serial};
     }
@@ -5221,7 +5281,7 @@ else {
     if ($opt{extinfo}) {
        print $linebreak;
        if (defined $opt{htmlinfo}) {
-           printf '------ SYSTEM: <a href="%s">%s%s</a>, SN: <a href="%s">%s</a>',
+           printf '------ SYSTEM: <a target="_blank" href="%s">%s%s</a>, SN: <a target="_blank" href="%s">%s</a>',
              documentation_url($sysinfo{model}), $sysinfo{model}, $sysinfo{rev},
                warranty_url($sysinfo{serial}), $sysinfo{serial};
        }
@@ -5293,12 +5353,25 @@ if (defined $opt{perfdata} && !$opt{debug} && @perfdata) {
              $a->{label} cmp $b->{label};
     }
 
+    # LEGACY sort routine for performance data
+    sub perfsort_legacy {
+       my %order = ( fan => 0, pwr => 1, tem => 2, enc => 3, );
+       return ($order{(substr $a->{legacy}, 0, 3)} cmp $order{(substr $b->{legacy}, 0, 3)}) ||
+         $a->{legacy} cmp $b->{legacy};
+    }
+
     # Print performance data sorted
-    if ($opt{perfdata} eq 'minimal') {
-       print join $lb, map { "$_->{type}$_->{id}=$_->{value}$_->{unit};$_->{warn};$_->{crit}" } sort perfsort @perfdata;
+    if ($opt{legacy_perfdata}) {
+       my $type = $opt{perfdata} eq 'minimal' ? 'mini' : 'legacy';
+       print join $lb, map { "$_->{$type}=$_->{value};$_->{warn};$_->{crit}" } sort perfsort_legacy @perfdata;
     }
     else {
-       print join $lb, map { "$_->{type}$_->{id}_$_->{label}=$_->{value}$_->{unit};$_->{warn};$_->{crit}" } sort perfsort @perfdata;
+       if ($opt{perfdata} eq 'minimal') {
+           print join $lb, map { "$_->{type}$_->{id}=$_->{value}$_->{unit};$_->{warn};$_->{crit}" } sort perfsort @perfdata;
+       }
+       else {
+           print join $lb, map { "$_->{type}$_->{id}_$_->{label}=$_->{value}$_->{unit};$_->{warn};$_->{crit}" } sort perfsort @perfdata;
+       }
     }
 }