From c925d90c841c262514d67da3f1d51e0528ce09ba Mon Sep 17 00:00:00 2001 From: Napsty Date: Tue, 29 Apr 2014 14:38:25 +0200 Subject: [PATCH 01/11] Trying to fix issue #3 --- check_smart.pl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/check_smart.pl b/check_smart.pl index a3aafe5..1182e21 100755 --- a/check_smart.pl +++ b/check_smart.pl @@ -363,7 +363,7 @@ warn "(debug) final status/output:\n" if $opt_debug; - if($exit_status ne 'OK'){ + if($exit_status_local ne 'OK'){ if ($opt_g) { $status_string_local = $label.join(', ', @error_messages); $status_string .= $status_string_local.$Terminator; From df10d17eb511af65f2ede850cf2ed05a515081c9 Mon Sep 17 00:00:00 2001 From: Napsty Date: Tue, 29 Apr 2014 14:53:13 +0200 Subject: [PATCH 02/11] Trying to fix issue #3 --- check_smart.pl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/check_smart.pl b/check_smart.pl index 1182e21..272c927 100755 --- a/check_smart.pl +++ b/check_smart.pl @@ -361,9 +361,9 @@ warn "(debug) FINAL STATUS: $exit_status_local\n" if $opt_debug; warn "###########################################################\n\n\n" if $opt_debug; - warn "(debug) final status/output:\n" if $opt_debug; + warn "(debug) final status/output: $exit_status\n" if $opt_debug; - if($exit_status_local ne 'OK'){ + if($exit_status ne 'OK'){ if ($opt_g) { $status_string_local = $label.join(', ', @error_messages); $status_string .= $status_string_local.$Terminator; From 46dc45b93ddb1aed5d4c050267f32d401a384761 Mon Sep 17 00:00:00 2001 From: Napsty Date: Tue, 29 Apr 2014 15:15:35 +0200 Subject: [PATCH 03/11] Trying to fix issue #3 --- check_smart.pl | 1 + 1 file changed, 1 insertion(+) diff --git a/check_smart.pl b/check_smart.pl index 272c927..ef57ed3 100755 --- a/check_smart.pl +++ b/check_smart.pl @@ -418,5 +418,6 @@ sub escalate_status { return if $exit_status eq 'WARNING'; return if $exit_status eq 'CRITICAL'; } + $exit_status_local = $requested_status; $exit_status = $requested_status; } From 22c7b0d339663fc658c2bb4f2a74d627d68e9851 Mon Sep 17 00:00:00 2001 From: Napsty Date: Tue, 29 Apr 2014 15:21:03 +0200 Subject: [PATCH 04/11] Trying to fix issue #3 --- check_smart.pl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/check_smart.pl b/check_smart.pl index ef57ed3..e938347 100755 --- a/check_smart.pl +++ b/check_smart.pl @@ -363,7 +363,7 @@ warn "(debug) final status/output: $exit_status\n" if $opt_debug; - if($exit_status ne 'OK'){ + if($exit_status_local ne 'OK'){ if ($opt_g) { $status_string_local = $label.join(', ', @error_messages); $status_string .= $status_string_local.$Terminator; @@ -382,6 +382,7 @@ } } + } $status_string =~ s/$Terminator$//; From f663940419d839ad94852319015c7498d8390d2b Mon Sep 17 00:00:00 2001 From: Napsty Date: Tue, 29 Apr 2014 16:09:19 +0200 Subject: [PATCH 05/11] Trying to fix issue #3 --- check_smart.pl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/check_smart.pl b/check_smart.pl index e938347..ea68569 100755 --- a/check_smart.pl +++ b/check_smart.pl @@ -420,5 +420,5 @@ sub escalate_status { return if $exit_status eq 'CRITICAL'; } $exit_status_local = $requested_status; - $exit_status = $requested_status; + # $exit_status = $requested_status; } From e4ef6803686eea8d5c368ca5883be7e532dcca1d Mon Sep 17 00:00:00 2001 From: Napsty Date: Tue, 29 Apr 2014 16:30:54 +0200 Subject: [PATCH 06/11] Trying to fix issue #3 --- check_smart.pl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/check_smart.pl b/check_smart.pl index ea68569..8ad8f03 100755 --- a/check_smart.pl +++ b/check_smart.pl @@ -114,6 +114,7 @@ my $smart_command = 'sudo smartctl'; my $exit_status = 'OK'; +my $exit_status_local = 'OK'; my $status_string = ''; my $perf_string = ''; my $Terminator=' --- '; @@ -122,7 +123,7 @@ foreach $device ( split(":",$device) ){ my @error_messages = qw//; my($status_string_local)=''; - my($exit_status_local)='OK'; + #my($exit_status_local)='OK'; my($tag,$label); if ($opt_g){ From 223292740156bae5c5cc083f053e9088d9bb10b2 Mon Sep 17 00:00:00 2001 From: Napsty Date: Mon, 5 May 2014 12:01:11 +0200 Subject: [PATCH 07/11] Trying to fix issue #3 --- check_smart.pl | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/check_smart.pl b/check_smart.pl index 8ad8f03..43f7ba6 100755 --- a/check_smart.pl +++ b/check_smart.pl @@ -113,8 +113,8 @@ my $smart_command = 'sudo smartctl'; -my $exit_status = 'OK'; -my $exit_status_local = 'OK'; +my $exit_status = ''; +my($exit_status_local)='OK'; my $status_string = ''; my $perf_string = ''; my $Terminator=' --- '; @@ -123,7 +123,6 @@ foreach $device ( split(":",$device) ){ my @error_messages = qw//; my($status_string_local)=''; - #my($exit_status_local)='OK'; my($tag,$label); if ($opt_g){ @@ -359,11 +358,9 @@ $perf_string = join(' ', @perfdata); warn "###########################################################\n" if $opt_debug; - warn "(debug) FINAL STATUS: $exit_status_local\n" if $opt_debug; + warn "(debug) LOCAL STATUS: $exit_status_local\n" if $opt_debug; warn "###########################################################\n\n\n" if $opt_debug; - warn "(debug) final status/output: $exit_status\n" if $opt_debug; - if($exit_status_local ne 'OK'){ if ($opt_g) { $status_string_local = $label.join(', ', @error_messages); @@ -372,6 +369,7 @@ else { $status_string = join(', ', @error_messages); } + $exit_status = '$exit_status_local'; } else { if ($opt_g) { @@ -381,11 +379,13 @@ else { $status_string = "no SMART errors detected. ".join(', ', @error_messages); } + $exit_status = "$exit_status_local"; } - } + warn "(debug) final status/output: $exit_status\n" if $opt_debug; + $status_string =~ s/$Terminator$//; print "$exit_status: $status_string|$perf_string\n"; exit $ERRORS{$exit_status}; @@ -420,6 +420,6 @@ sub escalate_status { return if $exit_status eq 'WARNING'; return if $exit_status eq 'CRITICAL'; } + $exit_status = $requested_status; $exit_status_local = $requested_status; - # $exit_status = $requested_status; } From 79837b9554edd3f03c99bcc73fb9edc19f8966dd Mon Sep 17 00:00:00 2001 From: Napsty Date: Mon, 5 May 2014 14:19:14 +0200 Subject: [PATCH 08/11] Trying to fix issue #3 --- check_smart.pl | 82 ++++++++++++++++++++++++++++---------------------- 1 file changed, 46 insertions(+), 36 deletions(-) diff --git a/check_smart.pl b/check_smart.pl index 43f7ba6..d86aec2 100755 --- a/check_smart.pl +++ b/check_smart.pl @@ -36,7 +36,7 @@ $ENV{'BASH_ENV'}=''; $ENV{'ENV'}=''; -use vars qw($opt_b $opt_d $opt_g $opt_debug $opt_h $opt_i $opt_v); +use vars qw($opt_b $opt_d $opt_g $opt_debug $opt_h $opt_i $opt_s $opt_v); Getopt::Long::Configure('bundling'); GetOptions( "debug" => \$opt_debug, @@ -45,6 +45,7 @@ "g=s" => \$opt_g, "global=s" => \$opt_g, "h" => \$opt_h, "help" => \$opt_h, "i=s" => \$opt_i, "interface=s" => \$opt_i, + "s=s" => \$opt_s, "separator=s"=> \$opt_s, "v" => \$opt_v, "version" => \$opt_v, ); @@ -113,18 +114,24 @@ my $smart_command = 'sudo smartctl'; -my $exit_status = ''; -my($exit_status_local)='OK'; -my $status_string = ''; my $perf_string = ''; -my $Terminator=' --- '; +my $Separator = (defined($opt_s)?$opt_s:' --- '); +my $exit_status = 'OK'; +my $exit_status_local ; +my $status_string = ''; +# do not allow some perl pattern sign to be used as separators +$Separator = ' --- ' if ( $Separator =~ m/\+/ ); +$Separator = ' --- ' if ( $Separator =~ m/\*/); foreach $device ( split(":",$device) ){ my @error_messages = qw//; my($status_string_local)=''; my($tag,$label); + # always start with an OK status for new devices in case of -g + $exit_status_local = 'OK'; + if ($opt_g){ # we had a pattern based on $opt_g $tag = $device; @@ -255,7 +262,7 @@ my ($attribute_name, $when_failed, $raw_value) = ($1, $2, $3); if ($when_failed ne '-'){ push(@error_messages, "Attribute $attribute_name failed at $when_failed"); - escalate_status('WARNING'); + escalate_status('WARNING'); warn "(debug) parsed SMART attribute $attribute_name with error condition:\n$when_failed\n\n" if $opt_debug; } # some attributes produce questionable data; no need to graph them @@ -269,7 +276,7 @@ if ($opt_b) { if (($raw_value > 0) && ($raw_value >= $opt_b)) { push(@error_messages, "$raw_value Sectors pending re-allocation"); - escalate_status('WARNING'); + escalate_status('WARNING'); warn "(debug) Current_Pending_Sector is non-zero ($raw_value)\n\n" if $opt_debug; } elsif (($raw_value > 0) && ($raw_value < $opt_b)) { @@ -362,31 +369,27 @@ warn "###########################################################\n\n\n" if $opt_debug; if($exit_status_local ne 'OK'){ + if ($opt_g) { + $status_string_local = $label.join(', ', @error_messages); + $status_string .= $status_string_local.$Separator; + } else { + $status_string = join(', ', @error_messages); + } + + } else { if ($opt_g) { - $status_string_local = $label.join(', ', @error_messages); - $status_string .= $status_string_local.$Terminator; - } - else { - $status_string = join(', ', @error_messages); - } - $exit_status = '$exit_status_local'; - } - else { - if ($opt_g) { - $status_string_local = $label."Device is clean"; - $status_string .= $status_string_local.$Terminator; - } - else { - $status_string = "no SMART errors detected. ".join(', ', @error_messages); + $status_string_local = $label."Device is clean"; + $status_string .= $status_string_local.$Separator; + } else { + $status_string = "no SMART errors detected. ".join(', ', @error_messages); } - $exit_status = "$exit_status_local"; } } - warn "(debug) final status/output: $exit_status\n" if $opt_debug; +warn "(debug) final status/output: $exit_status\n" if $opt_debug; -$status_string =~ s/$Terminator$//; +$status_string =~ s/$Separator$//; print "$exit_status: $status_string|$perf_string\n"; exit $ERRORS{$exit_status}; @@ -402,6 +405,7 @@ sub print_help { print "\n"; print "Other options\n"; print " -i/--interface: device's interface type\n"; + print " -s/--separator: used with -g to separate device results\n"; print " (See http://sourceforge.net/apps/trac/smartmontools/wiki/Supported_RAID-Controllers for interface convention)\n"; print " -b/--bad: Threshold value (integer) when to warn for N bad entries\n"; print " -h/--help: this help\n"; @@ -411,15 +415,21 @@ sub print_help { # escalate an exit status IFF it's more severe than the previous exit status sub escalate_status { - my $requested_status = shift; - # no test for 'CRITICAL'; automatically escalates upwards - if ($requested_status eq 'WARNING') { - return if $exit_status eq 'CRITICAL'; - } - if ($requested_status eq 'UNKNOWN') { - return if $exit_status eq 'WARNING'; - return if $exit_status eq 'CRITICAL'; - } - $exit_status = $requested_status; - $exit_status_local = $requested_status; + my($requested_status) = @_; + my($init_gsts,$init_lsts) = (1,1); + + # no test for 'CRITICAL'; automatically escalates upwards + if ($requested_status eq 'WARNING') { + $init_gsts=0 if $exit_status eq 'CRITICAL'; + $init_lsts=0 if $exit_status_local eq 'CRITICAL'; + } + if ($requested_status eq 'UNKNOWN') { + $init_gsts=0 if $exit_status eq 'WARNING'; + $init_lsts=0 if $exit_status_local eq 'WARNING'; + + $init_gsts=0 if $exit_status eq 'CRITICAL'; + $init_lsts=0 if $exit_status_local eq 'CRITICAL'; + } + $exit_status = $requested_status if $init_gsts; + $exit_status_local = $requested_status if $init_lsts; } From a1da0b9b8d00cfa419cdcea907fd59e2ef3d065b Mon Sep 17 00:00:00 2001 From: Napsty Date: Mon, 5 May 2014 14:24:01 +0200 Subject: [PATCH 09/11] Trying to fix issue #3 --- check_smart.pl | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/check_smart.pl b/check_smart.pl index d86aec2..be7bc80 100755 --- a/check_smart.pl +++ b/check_smart.pl @@ -120,10 +120,6 @@ my $exit_status_local ; my $status_string = ''; -# do not allow some perl pattern sign to be used as separators -$Separator = ' --- ' if ( $Separator =~ m/\+/ ); -$Separator = ' --- ' if ( $Separator =~ m/\*/); - foreach $device ( split(":",$device) ){ my @error_messages = qw//; my($status_string_local)=''; @@ -389,7 +385,10 @@ warn "(debug) final status/output: $exit_status\n" if $opt_debug; -$status_string =~ s/$Separator$//; +if ( $opt_g && $status_string ne ''){ + $status_string = substr($status_string,0,length($status_string)-length($Separator)) ; +} + print "$exit_status: $status_string|$perf_string\n"; exit $ERRORS{$exit_status}; From df2d7bbd8fd3bd23f0a4b2fab7d1c8648350b72a Mon Sep 17 00:00:00 2001 From: Napsty Date: Mon, 5 May 2014 15:23:32 +0200 Subject: [PATCH 10/11] Trying to fix issue #3 (Patch by Caspar Smit) --- check_smart.pl | 80 ++++++++++++++++++++++---------------------------- 1 file changed, 35 insertions(+), 45 deletions(-) diff --git a/check_smart.pl b/check_smart.pl index be7bc80..269d29b 100755 --- a/check_smart.pl +++ b/check_smart.pl @@ -36,7 +36,7 @@ $ENV{'BASH_ENV'}=''; $ENV{'ENV'}=''; -use vars qw($opt_b $opt_d $opt_g $opt_debug $opt_h $opt_i $opt_s $opt_v); +use vars qw($opt_b $opt_d $opt_g $opt_debug $opt_h $opt_i $opt_v); Getopt::Long::Configure('bundling'); GetOptions( "debug" => \$opt_debug, @@ -45,7 +45,6 @@ "g=s" => \$opt_g, "global=s" => \$opt_g, "h" => \$opt_h, "help" => \$opt_h, "i=s" => \$opt_i, "interface=s" => \$opt_i, - "s=s" => \$opt_s, "separator=s"=> \$opt_s, "v" => \$opt_v, "version" => \$opt_v, ); @@ -114,18 +113,17 @@ my $smart_command = 'sudo smartctl'; -my $perf_string = ''; -my $Separator = (defined($opt_s)?$opt_s:' --- '); my $exit_status = 'OK'; -my $exit_status_local ; +my $exit_status_local = 'OK'; my $status_string = ''; +my $perf_string = ''; +my $Terminator = ' --- '; + foreach $device ( split(":",$device) ){ my @error_messages = qw//; my($status_string_local)=''; my($tag,$label); - - # always start with an OK status for new devices in case of -g $exit_status_local = 'OK'; if ($opt_g){ @@ -258,7 +256,7 @@ my ($attribute_name, $when_failed, $raw_value) = ($1, $2, $3); if ($when_failed ne '-'){ push(@error_messages, "Attribute $attribute_name failed at $when_failed"); - escalate_status('WARNING'); + escalate_status('WARNING'); warn "(debug) parsed SMART attribute $attribute_name with error condition:\n$when_failed\n\n" if $opt_debug; } # some attributes produce questionable data; no need to graph them @@ -272,7 +270,7 @@ if ($opt_b) { if (($raw_value > 0) && ($raw_value >= $opt_b)) { push(@error_messages, "$raw_value Sectors pending re-allocation"); - escalate_status('WARNING'); + escalate_status('WARNING'); warn "(debug) Current_Pending_Sector is non-zero ($raw_value)\n\n" if $opt_debug; } elsif (($raw_value > 0) && ($raw_value < $opt_b)) { @@ -361,34 +359,33 @@ $perf_string = join(' ', @perfdata); warn "###########################################################\n" if $opt_debug; - warn "(debug) LOCAL STATUS: $exit_status_local\n" if $opt_debug; + warn "(debug) LOCAL STATUS: $exit_status_local, FINAL STATUS: $exit_status\n" if $opt_debug; warn "###########################################################\n\n\n" if $opt_debug; if($exit_status_local ne 'OK'){ - if ($opt_g) { - $status_string_local = $label.join(', ', @error_messages); - $status_string .= $status_string_local.$Separator; - } else { - $status_string = join(', ', @error_messages); - } - - } else { if ($opt_g) { - $status_string_local = $label."Device is clean"; - $status_string .= $status_string_local.$Separator; - } else { - $status_string = "no SMART errors detected. ".join(', ', @error_messages); + $status_string_local = $label.join(', ', @error_messages); + $status_string .= $status_string_local.$Terminator; + } + else { + $status_string = join(', ', @error_messages); + } + } + else { + if ($opt_g) { + $status_string_local = $label."Device is clean"; + $status_string .= $status_string_local.$Terminator; + } + else { + $status_string = "no SMART errors detected. ".join(', ', @error_messages); } } } -warn "(debug) final status/output: $exit_status\n" if $opt_debug; - -if ( $opt_g && $status_string ne ''){ - $status_string = substr($status_string,0,length($status_string)-length($Separator)) ; -} + warn "(debug) final status/output: $exit_status\n" if $opt_debug; +$status_string =~ s/$Terminator$//; print "$exit_status: $status_string|$perf_string\n"; exit $ERRORS{$exit_status}; @@ -404,7 +401,6 @@ sub print_help { print "\n"; print "Other options\n"; print " -i/--interface: device's interface type\n"; - print " -s/--separator: used with -g to separate device results\n"; print " (See http://sourceforge.net/apps/trac/smartmontools/wiki/Supported_RAID-Controllers for interface convention)\n"; print " -b/--bad: Threshold value (integer) when to warn for N bad entries\n"; print " -h/--help: this help\n"; @@ -414,21 +410,15 @@ sub print_help { # escalate an exit status IFF it's more severe than the previous exit status sub escalate_status { - my($requested_status) = @_; - my($init_gsts,$init_lsts) = (1,1); - - # no test for 'CRITICAL'; automatically escalates upwards - if ($requested_status eq 'WARNING') { - $init_gsts=0 if $exit_status eq 'CRITICAL'; - $init_lsts=0 if $exit_status_local eq 'CRITICAL'; - } - if ($requested_status eq 'UNKNOWN') { - $init_gsts=0 if $exit_status eq 'WARNING'; - $init_lsts=0 if $exit_status_local eq 'WARNING'; - - $init_gsts=0 if $exit_status eq 'CRITICAL'; - $init_lsts=0 if $exit_status_local eq 'CRITICAL'; - } - $exit_status = $requested_status if $init_gsts; - $exit_status_local = $requested_status if $init_lsts; + my $requested_status = shift; + # no test for 'CRITICAL'; automatically escalates upwards + if ($requested_status eq 'WARNING') { + return if ($exit_status|$exit_status_local) eq 'CRITICAL'; + } + if ($requested_status eq 'UNKNOWN') { + return if ($exit_status|$exit_status_local) eq 'WARNING'; + return if ($exit_status|$exit_status_local) eq 'CRITICAL'; + } + $exit_status = $requested_status; + $exit_status_local = $requested_status; } From ce5956aa7bafc9183b9ad155437d70a037098b2f Mon Sep 17 00:00:00 2001 From: Napsty Date: Mon, 5 May 2014 15:30:28 +0200 Subject: [PATCH 11/11] Bouncing to version 5.2 --- check_smart.pl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/check_smart.pl b/check_smart.pl index 269d29b..dd5d0a4 100755 --- a/check_smart.pl +++ b/check_smart.pl @@ -19,6 +19,7 @@ # Mar 19, 2014: Claudio Kuenzler - bugfix in defect list perfdata (rev 4.2) # Apr 22, 2014: Jerome Lauret - implemented -g to do a global lookup (rev 5.0) # Apr 25, 2014: Claudio Kuenzler - cleanup, merge Jeromes code, perfdata output fix (rev 5.1) +# May 5, 2014: Caspar Smit - Fixed output bug in global check / issue #3 (rev 5.2) use strict; use Getopt::Long; @@ -26,7 +27,7 @@ use File::Basename qw(basename); my $basename = basename($0); -my $revision = '$Revision: 5.1 $'; +my $revision = '$Revision: 5.2 $'; use FindBin; use lib $FindBin::Bin;