From 541322969e27b07566c55448d749bc64ab7ae5c0 Mon Sep 17 00:00:00 2001 From: Stephane Lapie Date: Wed, 7 Jun 2017 15:12:53 +0900 Subject: Fix regexp for nullmailer "mailq" output As it currently is, the regular expression does not match mailq command output on a Debian Jessie setup. Three erroneous behaviors fixed : - Meaningful lines do not end with the "bytes" word - There might be one or more space before the byte count, not 2 - Time match was completely broken, it only accounted for 0-29 minutes and 0-29 seconds. --- plugins-scripts/check_mailq.pl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'plugins-scripts') diff --git a/plugins-scripts/check_mailq.pl b/plugins-scripts/check_mailq.pl index 32f498d3..1fc15ffc 100755 --- a/plugins-scripts/check_mailq.pl +++ b/plugins-scripts/check_mailq.pl @@ -533,7 +533,7 @@ elsif ( $mailq eq "nullmailer" ) { while () { #2006-06-22 16:00:00 282 bytes - if (/^[1-9][0-9]*-[01][0-9]-[0-3][0-9]\s[0-2][0-9]\:[0-2][0-9]\:[0-2][0-9]\s{2}[0-9]+\sbytes$/) { + if (/^[1-9][0-9]*-[01][0-9]-[0-3][0-9]\s[0-2][0-9]\:[0-5][0-9]\:[0-5][0-9]\s+[0-9]+\sbytes/) { $msg_q++ ; } } -- cgit v1.2.3 From 96c8dd89353be67d44ce7a21b90f621ad89a2984 Mon Sep 17 00:00:00 2001 From: Bernd Arnold Date: Wed, 30 May 2018 19:03:43 +0200 Subject: My version of check_uptime Derived from check_mailq --- plugins-scripts/check_uptime | 256 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 256 insertions(+) create mode 100755 plugins-scripts/check_uptime (limited to 'plugins-scripts') diff --git a/plugins-scripts/check_uptime b/plugins-scripts/check_uptime new file mode 100755 index 00000000..4bc95282 --- /dev/null +++ b/plugins-scripts/check_uptime @@ -0,0 +1,256 @@ +#!/usr/bin/perl -w + +# check_uptime - check uptime to see how long the system is running. +# + +# License Information: +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 +# USA +# +############################################################################ + +use POSIX; +use strict; +use Getopt::Long; +use vars qw($opt_V $opt_h $opt_v $verbose $PROGNAME $opt_w $opt_c + $opt_f $opt_s + $status $state $msg); +use FindBin; +use lib "$FindBin::Bin"; +use lib "/usr/lib/monitoring-plugins/"; +use utils qw(%ERRORS &print_revision &support &usage ); + +sub print_help (); +sub print_usage (); +sub process_arguments (); + +#$ENV{'PATH'}='/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin'; +$ENV{'BASH_ENV'}=''; +$ENV{'ENV'}=''; +$PROGNAME = "check_uptime"; +$state = $ERRORS{'UNKNOWN'}; + + +# Process arguments + +Getopt::Long::Configure('bundling'); +$status = process_arguments(); +if ($status){ + print "ERROR: processing arguments\n"; + exit $ERRORS{"UNKNOWN"}; +} + + +# Get uptime info from file + +my $uptime_file = "/proc/uptime"; + +if ( ! -r $uptime_file ) { + print "ERROR: file '$uptime_file' is not readable\n"; + exit $ERRORS{"UNKNOWN"}; +} + +if ( ! open FILE, "<", $uptime_file ) { + print "ERROR: cannot read from file '$uptime_file'\n"; + exit $ERRORS{"UNKNOWN"}; +} + +chomp( my $file_content = ); +close FILE; + +print "$uptime_file: $file_content\n" if $verbose; + +# Get first digit value (without fraction) +my ( $uptime_seconds ) = $file_content =~ /^([\d]+)/; + +# Bail out if value is not numeric +if ( $uptime_seconds !~ /^\d+$/ ) { + print "ERROR: no numeric value: $uptime_seconds\n"; + exit $ERRORS{"UNKNOWN"}; +} + + +# Do calculations for a "pretty" format (2 weeks, 5 days, ...) + +my ( $secs, $mins, $hours, $days, $weeks ); +$secs = $uptime_seconds; +if ( $secs > 100 ) { + $mins = int( $secs / 60 ); + $secs -= $mins * 60; +} +if ( $mins > 100 ) { + $hours = int( $mins / 60 ); + $mins -= $hours * 60; +} +if ( $hours > 48 ) { + $days = int( $hours / 24 ); + $hours -= $days * 24; +} +if ( $days > 14 ) { + $weeks = int( $days / 7 ); + $days -= $weeks * 7; +} + +my $pretty_uptime = ""; +$pretty_uptime .= sprintf( "%d week%s, ", $weeks, $weeks == 1 ? "" : "s" ) if $weeks; +$pretty_uptime .= sprintf( "%d day%s, ", $days, $days == 1 ? "" : "s" ) if $days; +$pretty_uptime .= sprintf( "%d hour%s, ", $hours, $hours == 1 ? "" : "s" ) if $hours; +$pretty_uptime .= sprintf( "%d minute%s, ", $mins, $mins == 1 ? "" : "s" ) if $mins; +# Replace last occurence of comma with "and" +$pretty_uptime =~ s/, $/ and /; +# Always print the seconds (though it may be 0 seconds) +$pretty_uptime .= sprintf( "%d second%s", $secs, $secs == 1 ? "" : "s" ); + + +# Default to catch errors in program +my $state_str = "UNKNOWN"; + +# Check values +if ( $uptime_seconds > $opt_c ) { + $state_str = "CRITICAL"; +} elsif ( $uptime_seconds > $opt_w ) { + $state_str = "WARNING"; +} else { + $state_str = "OK"; +} + +$msg = "$state_str: "; + +$msg .= "uptime is $uptime_seconds seconds. "; +$msg .= "Running for $pretty_uptime. " if $opt_f; +if ( $opt_s ) { + chomp( my $up_since = `uptime -s` ); + $msg .= "Running since $up_since. "; +} + +$state = $ERRORS{$state_str}; + +# Perfdata support +print "$msg|uptime=$uptime_seconds;$opt_w;$opt_c;0\n"; +exit $state; + + +##################################### +#### subs + + +sub process_arguments(){ + GetOptions + ("V" => \$opt_V, "version" => \$opt_V, + "v" => \$opt_v, "verbose" => \$opt_v, + "h" => \$opt_h, "help" => \$opt_h, + "w=s" => \$opt_w, "warning=s" => \$opt_w, # warning if above this number + "c=s" => \$opt_c, "critical=s" => \$opt_c, # critical if above this number + "f" => \$opt_f, "for" => \$opt_f, # show "running for ..." + "s" => \$opt_s, "since" => \$opt_s, # show "running since ..." + ); + + if ($opt_V) { + print_revision($PROGNAME,'2.2'); + exit $ERRORS{'UNKNOWN'}; + } + + if ($opt_h) { + print_help(); + exit $ERRORS{'UNKNOWN'}; + } + + if (defined $opt_v) { + $verbose = $opt_v; + } + + unless ( defined $opt_w && defined $opt_c ) { + print_usage(); + exit $ERRORS{'UNKNOWN'}; + } + + # Check if suffix is present + # Calculate parameter to seconds (to get an integer value finally) + # s = seconds + # m = minutes + # h = hours + # d = days + # w = weeks + my %factor = ( "s" => 1, + "m" => 60, + "h" => 60 * 60, + "d" => 60 * 60 * 24, + "w" => 60 * 60 * 24 * 7, + ); + if ( $opt_w =~ /^(\d+)([a-z])$/ ) { + my $value = $1; + my $suffix = $2; + print "warning: value=$value, suffix=$suffix\n" if $verbose; + if ( ! defined $factor{$suffix} ) { + print "Error: wrong suffix ($suffix) for warning"; + exit $ERRORS{'UNKNOWN'}; + } + $opt_w = $value * $factor{$suffix}; + } + if ( $opt_c =~ /^(\d+)([a-z])$/ ) { + my $value = $1; + my $suffix = $2; + print "critical: value=$value, suffix=$suffix\n" if $verbose; + if ( ! defined $factor{$suffix} ) { + print "Error: wrong suffix ($suffix) for critical"; + exit $ERRORS{'UNKNOWN'}; + } + $opt_c = $value * $factor{$suffix}; + } + + if ( $opt_w !~ /^\d+$/ ) { + print "Warning (-w) is not numeric\n"; + exit $ERRORS{'UNKNOWN'}; + } + if ( $opt_c !~ /^\d+$/ ) { + print "Critical (-c) is not numeric\n"; + exit $ERRORS{'UNKNOWN'}; + } + + if ( $opt_w >= $opt_c) { + print "Warning (-w) cannot be greater than Critical (-c)!\n"; + exit $ERRORS{'UNKNOWN'}; + } + + return $ERRORS{'OK'}; +} + +sub print_usage () { + print "Usage: $PROGNAME -w -c [-v]\n"; +} + +sub print_help () { + print_revision($PROGNAME,'2.2'); + print "Copyright (c) 2002 Subhendu Ghosh/Carlos Canau/Benjamin Schmid\n"; + print "Copyright (c) 2018 Bernd Arnold\n"; + print "\n"; + print_usage(); + print "\n"; + print " Checks the uptime of the system using $uptime_file\n"; + print "\n"; + print "-w (--warning) = Min. number of uptime to generate warning\n"; + print "-c (--critical) = Min. number of uptime to generate critical alert ( w < c )\n"; + print "-f (--for) = Show uptime in a pretty format (Running for x weeks, x days, ...)\n"; + print "-s (--since) = Show last boot in yyyy-mm-dd HH:MM:SS format (output from 'uptime -s')\n"; + print "-h (--help)\n"; + print "-V (--version)\n"; + print "-v (--verbose) = debugging output\n"; + print "\n\n"; + print "Note: -w and -c are required arguments.\n"; + print " You can suffix both values with s for seconds (default), m (minutes), h (hours), d (days) or w (weeks).\n"; + print ""; + print "\n\n"; + support(); +} -- cgit v1.2.3 From de0872e2d71caec807c659c7f518adcbefe7e665 Mon Sep 17 00:00:00 2001 From: Bernd Arnold Date: Wed, 30 May 2018 19:11:06 +0200 Subject: Replaced my values with macros --- plugins-scripts/check_uptime | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'plugins-scripts') diff --git a/plugins-scripts/check_uptime b/plugins-scripts/check_uptime index 4bc95282..09600df4 100755 --- a/plugins-scripts/check_uptime +++ b/plugins-scripts/check_uptime @@ -1,4 +1,4 @@ -#!/usr/bin/perl -w +#!@PERL@ -w # check_uptime - check uptime to see how long the system is running. # @@ -29,14 +29,13 @@ use vars qw($opt_V $opt_h $opt_v $verbose $PROGNAME $opt_w $opt_c $status $state $msg); use FindBin; use lib "$FindBin::Bin"; -use lib "/usr/lib/monitoring-plugins/"; use utils qw(%ERRORS &print_revision &support &usage ); sub print_help (); sub print_usage (); sub process_arguments (); -#$ENV{'PATH'}='/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin'; +$ENV{'PATH'}='@TRUSTED_PATH@'; $ENV{'BASH_ENV'}=''; $ENV{'ENV'}=''; $PROGNAME = "check_uptime"; @@ -158,7 +157,7 @@ sub process_arguments(){ ); if ($opt_V) { - print_revision($PROGNAME,'2.2'); + print_revision($PROGNAME,'@NP_VERSION@'); exit $ERRORS{'UNKNOWN'}; } @@ -232,7 +231,7 @@ sub print_usage () { } sub print_help () { - print_revision($PROGNAME,'2.2'); + print_revision($PROGNAME,'@NP_VERSION@'); print "Copyright (c) 2002 Subhendu Ghosh/Carlos Canau/Benjamin Schmid\n"; print "Copyright (c) 2018 Bernd Arnold\n"; print "\n"; -- cgit v1.2.3 From 44816b4979cdad1698179e749726b8f45e5587d2 Mon Sep 17 00:00:00 2001 From: Bernd Arnold Date: Sun, 3 Jun 2018 16:05:54 +0200 Subject: Rename to .pl All other check script also have the .pl ending --- plugins-scripts/check_uptime | 255 ---------------------------------------- plugins-scripts/check_uptime.pl | 255 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 255 insertions(+), 255 deletions(-) delete mode 100755 plugins-scripts/check_uptime create mode 100755 plugins-scripts/check_uptime.pl (limited to 'plugins-scripts') diff --git a/plugins-scripts/check_uptime b/plugins-scripts/check_uptime deleted file mode 100755 index 09600df4..00000000 --- a/plugins-scripts/check_uptime +++ /dev/null @@ -1,255 +0,0 @@ -#!@PERL@ -w - -# check_uptime - check uptime to see how long the system is running. -# - -# License Information: -# This program is free software; you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation; either version 2 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 -# USA -# -############################################################################ - -use POSIX; -use strict; -use Getopt::Long; -use vars qw($opt_V $opt_h $opt_v $verbose $PROGNAME $opt_w $opt_c - $opt_f $opt_s - $status $state $msg); -use FindBin; -use lib "$FindBin::Bin"; -use utils qw(%ERRORS &print_revision &support &usage ); - -sub print_help (); -sub print_usage (); -sub process_arguments (); - -$ENV{'PATH'}='@TRUSTED_PATH@'; -$ENV{'BASH_ENV'}=''; -$ENV{'ENV'}=''; -$PROGNAME = "check_uptime"; -$state = $ERRORS{'UNKNOWN'}; - - -# Process arguments - -Getopt::Long::Configure('bundling'); -$status = process_arguments(); -if ($status){ - print "ERROR: processing arguments\n"; - exit $ERRORS{"UNKNOWN"}; -} - - -# Get uptime info from file - -my $uptime_file = "/proc/uptime"; - -if ( ! -r $uptime_file ) { - print "ERROR: file '$uptime_file' is not readable\n"; - exit $ERRORS{"UNKNOWN"}; -} - -if ( ! open FILE, "<", $uptime_file ) { - print "ERROR: cannot read from file '$uptime_file'\n"; - exit $ERRORS{"UNKNOWN"}; -} - -chomp( my $file_content = ); -close FILE; - -print "$uptime_file: $file_content\n" if $verbose; - -# Get first digit value (without fraction) -my ( $uptime_seconds ) = $file_content =~ /^([\d]+)/; - -# Bail out if value is not numeric -if ( $uptime_seconds !~ /^\d+$/ ) { - print "ERROR: no numeric value: $uptime_seconds\n"; - exit $ERRORS{"UNKNOWN"}; -} - - -# Do calculations for a "pretty" format (2 weeks, 5 days, ...) - -my ( $secs, $mins, $hours, $days, $weeks ); -$secs = $uptime_seconds; -if ( $secs > 100 ) { - $mins = int( $secs / 60 ); - $secs -= $mins * 60; -} -if ( $mins > 100 ) { - $hours = int( $mins / 60 ); - $mins -= $hours * 60; -} -if ( $hours > 48 ) { - $days = int( $hours / 24 ); - $hours -= $days * 24; -} -if ( $days > 14 ) { - $weeks = int( $days / 7 ); - $days -= $weeks * 7; -} - -my $pretty_uptime = ""; -$pretty_uptime .= sprintf( "%d week%s, ", $weeks, $weeks == 1 ? "" : "s" ) if $weeks; -$pretty_uptime .= sprintf( "%d day%s, ", $days, $days == 1 ? "" : "s" ) if $days; -$pretty_uptime .= sprintf( "%d hour%s, ", $hours, $hours == 1 ? "" : "s" ) if $hours; -$pretty_uptime .= sprintf( "%d minute%s, ", $mins, $mins == 1 ? "" : "s" ) if $mins; -# Replace last occurence of comma with "and" -$pretty_uptime =~ s/, $/ and /; -# Always print the seconds (though it may be 0 seconds) -$pretty_uptime .= sprintf( "%d second%s", $secs, $secs == 1 ? "" : "s" ); - - -# Default to catch errors in program -my $state_str = "UNKNOWN"; - -# Check values -if ( $uptime_seconds > $opt_c ) { - $state_str = "CRITICAL"; -} elsif ( $uptime_seconds > $opt_w ) { - $state_str = "WARNING"; -} else { - $state_str = "OK"; -} - -$msg = "$state_str: "; - -$msg .= "uptime is $uptime_seconds seconds. "; -$msg .= "Running for $pretty_uptime. " if $opt_f; -if ( $opt_s ) { - chomp( my $up_since = `uptime -s` ); - $msg .= "Running since $up_since. "; -} - -$state = $ERRORS{$state_str}; - -# Perfdata support -print "$msg|uptime=$uptime_seconds;$opt_w;$opt_c;0\n"; -exit $state; - - -##################################### -#### subs - - -sub process_arguments(){ - GetOptions - ("V" => \$opt_V, "version" => \$opt_V, - "v" => \$opt_v, "verbose" => \$opt_v, - "h" => \$opt_h, "help" => \$opt_h, - "w=s" => \$opt_w, "warning=s" => \$opt_w, # warning if above this number - "c=s" => \$opt_c, "critical=s" => \$opt_c, # critical if above this number - "f" => \$opt_f, "for" => \$opt_f, # show "running for ..." - "s" => \$opt_s, "since" => \$opt_s, # show "running since ..." - ); - - if ($opt_V) { - print_revision($PROGNAME,'@NP_VERSION@'); - exit $ERRORS{'UNKNOWN'}; - } - - if ($opt_h) { - print_help(); - exit $ERRORS{'UNKNOWN'}; - } - - if (defined $opt_v) { - $verbose = $opt_v; - } - - unless ( defined $opt_w && defined $opt_c ) { - print_usage(); - exit $ERRORS{'UNKNOWN'}; - } - - # Check if suffix is present - # Calculate parameter to seconds (to get an integer value finally) - # s = seconds - # m = minutes - # h = hours - # d = days - # w = weeks - my %factor = ( "s" => 1, - "m" => 60, - "h" => 60 * 60, - "d" => 60 * 60 * 24, - "w" => 60 * 60 * 24 * 7, - ); - if ( $opt_w =~ /^(\d+)([a-z])$/ ) { - my $value = $1; - my $suffix = $2; - print "warning: value=$value, suffix=$suffix\n" if $verbose; - if ( ! defined $factor{$suffix} ) { - print "Error: wrong suffix ($suffix) for warning"; - exit $ERRORS{'UNKNOWN'}; - } - $opt_w = $value * $factor{$suffix}; - } - if ( $opt_c =~ /^(\d+)([a-z])$/ ) { - my $value = $1; - my $suffix = $2; - print "critical: value=$value, suffix=$suffix\n" if $verbose; - if ( ! defined $factor{$suffix} ) { - print "Error: wrong suffix ($suffix) for critical"; - exit $ERRORS{'UNKNOWN'}; - } - $opt_c = $value * $factor{$suffix}; - } - - if ( $opt_w !~ /^\d+$/ ) { - print "Warning (-w) is not numeric\n"; - exit $ERRORS{'UNKNOWN'}; - } - if ( $opt_c !~ /^\d+$/ ) { - print "Critical (-c) is not numeric\n"; - exit $ERRORS{'UNKNOWN'}; - } - - if ( $opt_w >= $opt_c) { - print "Warning (-w) cannot be greater than Critical (-c)!\n"; - exit $ERRORS{'UNKNOWN'}; - } - - return $ERRORS{'OK'}; -} - -sub print_usage () { - print "Usage: $PROGNAME -w -c [-v]\n"; -} - -sub print_help () { - print_revision($PROGNAME,'@NP_VERSION@'); - print "Copyright (c) 2002 Subhendu Ghosh/Carlos Canau/Benjamin Schmid\n"; - print "Copyright (c) 2018 Bernd Arnold\n"; - print "\n"; - print_usage(); - print "\n"; - print " Checks the uptime of the system using $uptime_file\n"; - print "\n"; - print "-w (--warning) = Min. number of uptime to generate warning\n"; - print "-c (--critical) = Min. number of uptime to generate critical alert ( w < c )\n"; - print "-f (--for) = Show uptime in a pretty format (Running for x weeks, x days, ...)\n"; - print "-s (--since) = Show last boot in yyyy-mm-dd HH:MM:SS format (output from 'uptime -s')\n"; - print "-h (--help)\n"; - print "-V (--version)\n"; - print "-v (--verbose) = debugging output\n"; - print "\n\n"; - print "Note: -w and -c are required arguments.\n"; - print " You can suffix both values with s for seconds (default), m (minutes), h (hours), d (days) or w (weeks).\n"; - print ""; - print "\n\n"; - support(); -} diff --git a/plugins-scripts/check_uptime.pl b/plugins-scripts/check_uptime.pl new file mode 100755 index 00000000..09600df4 --- /dev/null +++ b/plugins-scripts/check_uptime.pl @@ -0,0 +1,255 @@ +#!@PERL@ -w + +# check_uptime - check uptime to see how long the system is running. +# + +# License Information: +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 +# USA +# +############################################################################ + +use POSIX; +use strict; +use Getopt::Long; +use vars qw($opt_V $opt_h $opt_v $verbose $PROGNAME $opt_w $opt_c + $opt_f $opt_s + $status $state $msg); +use FindBin; +use lib "$FindBin::Bin"; +use utils qw(%ERRORS &print_revision &support &usage ); + +sub print_help (); +sub print_usage (); +sub process_arguments (); + +$ENV{'PATH'}='@TRUSTED_PATH@'; +$ENV{'BASH_ENV'}=''; +$ENV{'ENV'}=''; +$PROGNAME = "check_uptime"; +$state = $ERRORS{'UNKNOWN'}; + + +# Process arguments + +Getopt::Long::Configure('bundling'); +$status = process_arguments(); +if ($status){ + print "ERROR: processing arguments\n"; + exit $ERRORS{"UNKNOWN"}; +} + + +# Get uptime info from file + +my $uptime_file = "/proc/uptime"; + +if ( ! -r $uptime_file ) { + print "ERROR: file '$uptime_file' is not readable\n"; + exit $ERRORS{"UNKNOWN"}; +} + +if ( ! open FILE, "<", $uptime_file ) { + print "ERROR: cannot read from file '$uptime_file'\n"; + exit $ERRORS{"UNKNOWN"}; +} + +chomp( my $file_content = ); +close FILE; + +print "$uptime_file: $file_content\n" if $verbose; + +# Get first digit value (without fraction) +my ( $uptime_seconds ) = $file_content =~ /^([\d]+)/; + +# Bail out if value is not numeric +if ( $uptime_seconds !~ /^\d+$/ ) { + print "ERROR: no numeric value: $uptime_seconds\n"; + exit $ERRORS{"UNKNOWN"}; +} + + +# Do calculations for a "pretty" format (2 weeks, 5 days, ...) + +my ( $secs, $mins, $hours, $days, $weeks ); +$secs = $uptime_seconds; +if ( $secs > 100 ) { + $mins = int( $secs / 60 ); + $secs -= $mins * 60; +} +if ( $mins > 100 ) { + $hours = int( $mins / 60 ); + $mins -= $hours * 60; +} +if ( $hours > 48 ) { + $days = int( $hours / 24 ); + $hours -= $days * 24; +} +if ( $days > 14 ) { + $weeks = int( $days / 7 ); + $days -= $weeks * 7; +} + +my $pretty_uptime = ""; +$pretty_uptime .= sprintf( "%d week%s, ", $weeks, $weeks == 1 ? "" : "s" ) if $weeks; +$pretty_uptime .= sprintf( "%d day%s, ", $days, $days == 1 ? "" : "s" ) if $days; +$pretty_uptime .= sprintf( "%d hour%s, ", $hours, $hours == 1 ? "" : "s" ) if $hours; +$pretty_uptime .= sprintf( "%d minute%s, ", $mins, $mins == 1 ? "" : "s" ) if $mins; +# Replace last occurence of comma with "and" +$pretty_uptime =~ s/, $/ and /; +# Always print the seconds (though it may be 0 seconds) +$pretty_uptime .= sprintf( "%d second%s", $secs, $secs == 1 ? "" : "s" ); + + +# Default to catch errors in program +my $state_str = "UNKNOWN"; + +# Check values +if ( $uptime_seconds > $opt_c ) { + $state_str = "CRITICAL"; +} elsif ( $uptime_seconds > $opt_w ) { + $state_str = "WARNING"; +} else { + $state_str = "OK"; +} + +$msg = "$state_str: "; + +$msg .= "uptime is $uptime_seconds seconds. "; +$msg .= "Running for $pretty_uptime. " if $opt_f; +if ( $opt_s ) { + chomp( my $up_since = `uptime -s` ); + $msg .= "Running since $up_since. "; +} + +$state = $ERRORS{$state_str}; + +# Perfdata support +print "$msg|uptime=$uptime_seconds;$opt_w;$opt_c;0\n"; +exit $state; + + +##################################### +#### subs + + +sub process_arguments(){ + GetOptions + ("V" => \$opt_V, "version" => \$opt_V, + "v" => \$opt_v, "verbose" => \$opt_v, + "h" => \$opt_h, "help" => \$opt_h, + "w=s" => \$opt_w, "warning=s" => \$opt_w, # warning if above this number + "c=s" => \$opt_c, "critical=s" => \$opt_c, # critical if above this number + "f" => \$opt_f, "for" => \$opt_f, # show "running for ..." + "s" => \$opt_s, "since" => \$opt_s, # show "running since ..." + ); + + if ($opt_V) { + print_revision($PROGNAME,'@NP_VERSION@'); + exit $ERRORS{'UNKNOWN'}; + } + + if ($opt_h) { + print_help(); + exit $ERRORS{'UNKNOWN'}; + } + + if (defined $opt_v) { + $verbose = $opt_v; + } + + unless ( defined $opt_w && defined $opt_c ) { + print_usage(); + exit $ERRORS{'UNKNOWN'}; + } + + # Check if suffix is present + # Calculate parameter to seconds (to get an integer value finally) + # s = seconds + # m = minutes + # h = hours + # d = days + # w = weeks + my %factor = ( "s" => 1, + "m" => 60, + "h" => 60 * 60, + "d" => 60 * 60 * 24, + "w" => 60 * 60 * 24 * 7, + ); + if ( $opt_w =~ /^(\d+)([a-z])$/ ) { + my $value = $1; + my $suffix = $2; + print "warning: value=$value, suffix=$suffix\n" if $verbose; + if ( ! defined $factor{$suffix} ) { + print "Error: wrong suffix ($suffix) for warning"; + exit $ERRORS{'UNKNOWN'}; + } + $opt_w = $value * $factor{$suffix}; + } + if ( $opt_c =~ /^(\d+)([a-z])$/ ) { + my $value = $1; + my $suffix = $2; + print "critical: value=$value, suffix=$suffix\n" if $verbose; + if ( ! defined $factor{$suffix} ) { + print "Error: wrong suffix ($suffix) for critical"; + exit $ERRORS{'UNKNOWN'}; + } + $opt_c = $value * $factor{$suffix}; + } + + if ( $opt_w !~ /^\d+$/ ) { + print "Warning (-w) is not numeric\n"; + exit $ERRORS{'UNKNOWN'}; + } + if ( $opt_c !~ /^\d+$/ ) { + print "Critical (-c) is not numeric\n"; + exit $ERRORS{'UNKNOWN'}; + } + + if ( $opt_w >= $opt_c) { + print "Warning (-w) cannot be greater than Critical (-c)!\n"; + exit $ERRORS{'UNKNOWN'}; + } + + return $ERRORS{'OK'}; +} + +sub print_usage () { + print "Usage: $PROGNAME -w -c [-v]\n"; +} + +sub print_help () { + print_revision($PROGNAME,'@NP_VERSION@'); + print "Copyright (c) 2002 Subhendu Ghosh/Carlos Canau/Benjamin Schmid\n"; + print "Copyright (c) 2018 Bernd Arnold\n"; + print "\n"; + print_usage(); + print "\n"; + print " Checks the uptime of the system using $uptime_file\n"; + print "\n"; + print "-w (--warning) = Min. number of uptime to generate warning\n"; + print "-c (--critical) = Min. number of uptime to generate critical alert ( w < c )\n"; + print "-f (--for) = Show uptime in a pretty format (Running for x weeks, x days, ...)\n"; + print "-s (--since) = Show last boot in yyyy-mm-dd HH:MM:SS format (output from 'uptime -s')\n"; + print "-h (--help)\n"; + print "-V (--version)\n"; + print "-v (--verbose) = debugging output\n"; + print "\n\n"; + print "Note: -w and -c are required arguments.\n"; + print " You can suffix both values with s for seconds (default), m (minutes), h (hours), d (days) or w (weeks).\n"; + print ""; + print "\n\n"; + support(); +} -- cgit v1.2.3 From d5fbf8ae93c511e9bb9e0ee4304625d4b4023622 Mon Sep 17 00:00:00 2001 From: Bernd Arnold Date: Sun, 3 Jun 2018 16:42:34 +0200 Subject: Added suffix "s" for seconds in perfdata output --- plugins-scripts/check_uptime.pl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'plugins-scripts') diff --git a/plugins-scripts/check_uptime.pl b/plugins-scripts/check_uptime.pl index 09600df4..8c1f3d20 100755 --- a/plugins-scripts/check_uptime.pl +++ b/plugins-scripts/check_uptime.pl @@ -137,7 +137,7 @@ if ( $opt_s ) { $state = $ERRORS{$state_str}; # Perfdata support -print "$msg|uptime=$uptime_seconds;$opt_w;$opt_c;0\n"; +print "$msg|uptime=${uptime_seconds}s;$opt_w;$opt_c;0\n"; exit $state; -- cgit v1.2.3 From 250adb31ef1849adea8b3bd7f66e92df1bbdd431 Mon Sep 17 00:00:00 2001 From: Bernd Arnold Date: Sun, 3 Jun 2018 16:51:43 +0200 Subject: Added test file --- plugins-scripts/t/check_uptime.t | 73 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 plugins-scripts/t/check_uptime.t (limited to 'plugins-scripts') diff --git a/plugins-scripts/t/check_uptime.t b/plugins-scripts/t/check_uptime.t new file mode 100644 index 00000000..410a0800 --- /dev/null +++ b/plugins-scripts/t/check_uptime.t @@ -0,0 +1,73 @@ +#!/usr/bin/perl -w -I .. +# +# check_uptime tests +# +# + +use strict; +use Test::More tests => 21; +use NPTest; + +my $result; + +$result = NPTest->testCmd( + "./check_uptime" + ); +cmp_ok( $result->return_code, '==', 3, "Missing parameters" ); +like ( $result->output, '/^Usage: check_uptime -w/', "Output for missing parameters correct" ); + +$result = NPTest->testCmd( + "./check_uptime --help" + ); +cmp_ok( $result->return_code, '==', 3, "Help output requested" ); +like ( $result->output, '/ABSOLUTELY NO WARRANTY/', "Output for help correct" ); + +$result = NPTest->testCmd( + "./check_uptime -w 5 -c 2" + ); +cmp_ok( $result->return_code, '==', 3, "Warning greater than critical" ); +like ( $result->output, '/^Warning .*cannot be greater than Critical/', "Output for warning greater than critical correct" ); + +$result = NPTest->testCmd( + "./check_uptime -c 1000 -W 100 2>&1" + ); +like ( $result->output, '/^Unknown option: W/', "Output with wrong parameter is correct" ); + +$result = NPTest->testCmd( + "./check_uptime -f -w 1 -c 2" + ); +cmp_ok( $result->return_code, '==', 2, "Uptime higher than 2 seconds" ); +like ( $result->output, '/Running for \d+/', "Output for the f parameter correct" ); + +$result = NPTest->testCmd( + "./check_uptime -s -w 1 -c 2" + ); +cmp_ok( $result->return_code, '==', 2, "Uptime higher than 2 seconds" ); +like ( $result->output, '/Running since \d+/', "Output for the s parameter correct" ); + +$result = NPTest->testCmd( + "./check_uptime -w 1 -c 2" + ); +cmp_ok( $result->return_code, '==', 2, "Uptime higher than 2 seconds" ); +like ( $result->output, '/^CRITICAL: uptime is \d+ seconds/', "Output for uptime higher than 2 seconds correct" ); + +$result = NPTest->testCmd( + "./check_uptime -w 1 -c 9999w" + ); +cmp_ok( $result->return_code, '==', 1, "Uptime lower than 9999 weeks" ); +like ( $result->output, '/^WARNING: uptime is \d+ seconds/', "Output for uptime lower than 9999 weeks correct" ); + +$result = NPTest->testCmd( + "./check_uptime -w 9998w -c 9999w" + ); +cmp_ok( $result->return_code, '==', 0, "Uptime lower than 9998 weeks" ); +like ( $result->output, '/^OK: uptime is \d+ seconds/', "Output for uptime lower than 9998 weeks correct" ); +like ( $result->output, '/\|uptime=[0-9]+s;6046790400;6047395200;/', "Checking for performance output" ); + +$result = NPTest->testCmd( + "./check_uptime -w 111222d -c 222333d" + ); +cmp_ok( $result->return_code, '==', 0, "Uptime lower than 111222 days" ); +like ( $result->output, '/^OK: uptime is \d+ seconds/', "Output for uptime lower than 111222 days correct" ); +like ( $result->output, '/\|uptime=[0-9]+s;9609580800;19209571200;/', "Checking for performance output" ); + -- cgit v1.2.3 From 554b702f9d65fdfe640f20633543e00cd79d64ac Mon Sep 17 00:00:00 2001 From: Bernd Arnold Date: Sun, 3 Jun 2018 20:55:42 +0200 Subject: Fix: uptime_file variable was declared too late When called with --help, the following error was shown: Use of uninitialized value $uptime_file in concatenation (.) or string at --- plugins-scripts/check_uptime.pl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'plugins-scripts') diff --git a/plugins-scripts/check_uptime.pl b/plugins-scripts/check_uptime.pl index 8c1f3d20..27dc93ff 100755 --- a/plugins-scripts/check_uptime.pl +++ b/plugins-scripts/check_uptime.pl @@ -1,4 +1,4 @@ -#!@PERL@ -w +#!/usr/bin/perl -w # check_uptime - check uptime to see how long the system is running. # @@ -41,6 +41,8 @@ $ENV{'ENV'}=''; $PROGNAME = "check_uptime"; $state = $ERRORS{'UNKNOWN'}; +my $uptime_file = "/proc/uptime"; + # Process arguments @@ -54,8 +56,6 @@ if ($status){ # Get uptime info from file -my $uptime_file = "/proc/uptime"; - if ( ! -r $uptime_file ) { print "ERROR: file '$uptime_file' is not readable\n"; exit $ERRORS{"UNKNOWN"}; -- cgit v1.2.3 From 5a73671ec6299c480c90ec5b9f0f3d58397fbcfb Mon Sep 17 00:00:00 2001 From: Bernd Arnold Date: Sat, 9 Jun 2018 10:02:23 +0200 Subject: Fix: Initialize values Otherwise, there's a warning about unitialized values: Use of uninitialized value $hours in numeric gt (>) at ... Use of uninitialized value $days in numeric gt (>) at ... --- plugins-scripts/check_uptime.pl | 1 + 1 file changed, 1 insertion(+) (limited to 'plugins-scripts') diff --git a/plugins-scripts/check_uptime.pl b/plugins-scripts/check_uptime.pl index 27dc93ff..20234c88 100755 --- a/plugins-scripts/check_uptime.pl +++ b/plugins-scripts/check_uptime.pl @@ -85,6 +85,7 @@ if ( $uptime_seconds !~ /^\d+$/ ) { my ( $secs, $mins, $hours, $days, $weeks ); $secs = $uptime_seconds; +$mins = $hours = $days = $weeks = 0; if ( $secs > 100 ) { $mins = int( $secs / 60 ); $secs -= $mins * 60; -- cgit v1.2.3 From 9ee884b3de6c95f871a714dfc67cfb9b5e11724f Mon Sep 17 00:00:00 2001 From: Bernd Arnold Date: Wed, 13 Jun 2018 15:57:16 +0200 Subject: Fix: Use macro for perl binary I've messed that up in the previous commit 554b702f9d65fdfe640f20633543e00cd79d64ac. --- plugins-scripts/check_uptime.pl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'plugins-scripts') diff --git a/plugins-scripts/check_uptime.pl b/plugins-scripts/check_uptime.pl index 20234c88..88170307 100755 --- a/plugins-scripts/check_uptime.pl +++ b/plugins-scripts/check_uptime.pl @@ -1,4 +1,4 @@ -#!/usr/bin/perl -w +#!@PERL@ -w # check_uptime - check uptime to see how long the system is running. # -- cgit v1.2.3 From a784b19d6fc193e4c98ec6b2248bf4e252a9a0d7 Mon Sep 17 00:00:00 2001 From: Bernd Arnold Date: Wed, 13 Jun 2018 16:10:17 +0200 Subject: Modified alignment --- plugins-scripts/check_uptime.pl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'plugins-scripts') diff --git a/plugins-scripts/check_uptime.pl b/plugins-scripts/check_uptime.pl index 88170307..2b230f5c 100755 --- a/plugins-scripts/check_uptime.pl +++ b/plugins-scripts/check_uptime.pl @@ -192,7 +192,7 @@ sub process_arguments(){ if ( $opt_w =~ /^(\d+)([a-z])$/ ) { my $value = $1; my $suffix = $2; - print "warning: value=$value, suffix=$suffix\n" if $verbose; + print "warning: value=$value, suffix=$suffix\n" if $verbose; if ( ! defined $factor{$suffix} ) { print "Error: wrong suffix ($suffix) for warning"; exit $ERRORS{'UNKNOWN'}; @@ -202,7 +202,7 @@ sub process_arguments(){ if ( $opt_c =~ /^(\d+)([a-z])$/ ) { my $value = $1; my $suffix = $2; - print "critical: value=$value, suffix=$suffix\n" if $verbose; + print "critical: value=$value, suffix=$suffix\n" if $verbose; if ( ! defined $factor{$suffix} ) { print "Error: wrong suffix ($suffix) for critical"; exit $ERRORS{'UNKNOWN'}; -- cgit v1.2.3 From db499b6f5b82aff65171ac4cfa5b936ba1e5e5ad Mon Sep 17 00:00:00 2001 From: Bernd Arnold Date: Wed, 13 Jun 2018 16:56:22 +0200 Subject: Introducing ranges for warning and critical Works as before: -w 1w -c 2w New (as before, but also warn if uptime < 5m, and crit if uptime < 2m): -w 5m:1w -c 2m:2w (idea by @sni) Also refactored the time calculation, if a suffix is present: New sub calc_as_seconds($) --- plugins-scripts/check_uptime.pl | 135 +++++++++++++++++++++++++++------------ plugins-scripts/t/check_uptime.t | 2 +- 2 files changed, 95 insertions(+), 42 deletions(-) (limited to 'plugins-scripts') diff --git a/plugins-scripts/check_uptime.pl b/plugins-scripts/check_uptime.pl index 2b230f5c..f2b47be5 100755 --- a/plugins-scripts/check_uptime.pl +++ b/plugins-scripts/check_uptime.pl @@ -26,6 +26,8 @@ use strict; use Getopt::Long; use vars qw($opt_V $opt_h $opt_v $verbose $PROGNAME $opt_w $opt_c $opt_f $opt_s + $lower_warn_threshold $upper_warn_threshold + $lower_crit_threshold $upper_crit_threshold $status $state $msg); use FindBin; use lib "$FindBin::Bin"; @@ -118,10 +120,19 @@ $pretty_uptime .= sprintf( "%d second%s", $secs, $secs == 1 ? "" : "s" ); my $state_str = "UNKNOWN"; # Check values -if ( $uptime_seconds > $opt_c ) { +my $out_of_bounds_text = ""; +if ( $uptime_seconds > $upper_crit_threshold ) { $state_str = "CRITICAL"; -} elsif ( $uptime_seconds > $opt_w ) { + $out_of_bounds_text = "Exceeds upper crit threshold"; +} elsif ( $uptime_seconds < $lower_crit_threshold ) { + $state_str = "CRITICAL"; + $out_of_bounds_text = "Exceeds lower crit threshold"; +} elsif ( $uptime_seconds > $upper_warn_threshold ) { + $state_str = "WARNING"; + $out_of_bounds_text = "Exceeds upper warn threshold"; +} elsif ( $uptime_seconds < $lower_warn_threshold ) { $state_str = "WARNING"; + $out_of_bounds_text = "Exceeds lower warn threshold"; } else { $state_str = "OK"; } @@ -129,6 +140,7 @@ if ( $uptime_seconds > $opt_c ) { $msg = "$state_str: "; $msg .= "uptime is $uptime_seconds seconds. "; +$msg .= "$out_of_bounds_text. " if $out_of_bounds_text; $msg .= "Running for $pretty_uptime. " if $opt_f; if ( $opt_s ) { chomp( my $up_since = `uptime -s` ); @@ -138,7 +150,7 @@ if ( $opt_s ) { $state = $ERRORS{$state_str}; # Perfdata support -print "$msg|uptime=${uptime_seconds}s;$opt_w;$opt_c;0\n"; +print "$msg|uptime=${uptime_seconds}s;$upper_warn_threshold;$upper_crit_threshold;0\n"; exit $state; @@ -176,51 +188,56 @@ sub process_arguments(){ exit $ERRORS{'UNKNOWN'}; } - # Check if suffix is present - # Calculate parameter to seconds (to get an integer value finally) - # s = seconds - # m = minutes - # h = hours - # d = days - # w = weeks - my %factor = ( "s" => 1, - "m" => 60, - "h" => 60 * 60, - "d" => 60 * 60 * 24, - "w" => 60 * 60 * 24 * 7, - ); - if ( $opt_w =~ /^(\d+)([a-z])$/ ) { - my $value = $1; - my $suffix = $2; - print "warning: value=$value, suffix=$suffix\n" if $verbose; - if ( ! defined $factor{$suffix} ) { - print "Error: wrong suffix ($suffix) for warning"; - exit $ERRORS{'UNKNOWN'}; - } - $opt_w = $value * $factor{$suffix}; + # Check if a range was supplied ("lowvalue:highvalue") for warning and critical + # Otherwise, set 0 as the lower threshold and the parameter value as upper threshold + # (the uptime should always be positive, so there should be no issue) + if ( $opt_w =~ /^(.+):(.+)$/ ) { + $lower_warn_threshold = $1; + $upper_warn_threshold = $2; + } else { + $lower_warn_threshold = 0; + $upper_warn_threshold = $opt_w; + } + if ( $opt_c =~ /^(.+):(.+)$/ ) { + $lower_crit_threshold = $1; + $upper_crit_threshold = $2; + } else { + $lower_crit_threshold = 0; + $upper_crit_threshold = $opt_c; + } + + # Set as seconds (calculate if suffix present) + $lower_warn_threshold = calc_as_seconds( $lower_warn_threshold ); + $lower_crit_threshold = calc_as_seconds( $lower_crit_threshold ); + $upper_warn_threshold = calc_as_seconds( $upper_warn_threshold ); + $upper_crit_threshold = calc_as_seconds( $upper_crit_threshold ); + + # Check for numeric value of warning parameter + if ( $lower_warn_threshold !~ /^\d+$/ ) { + print "Lower warning (-w) is not numeric\n"; + exit $ERRORS{'UNKNOWN'}; } - if ( $opt_c =~ /^(\d+)([a-z])$/ ) { - my $value = $1; - my $suffix = $2; - print "critical: value=$value, suffix=$suffix\n" if $verbose; - if ( ! defined $factor{$suffix} ) { - print "Error: wrong suffix ($suffix) for critical"; - exit $ERRORS{'UNKNOWN'}; - } - $opt_c = $value * $factor{$suffix}; + if ( $upper_warn_threshold !~ /^\d+$/ ) { + print "Upper warning (-w) is not numeric\n"; + exit $ERRORS{'UNKNOWN'}; } - - if ( $opt_w !~ /^\d+$/ ) { - print "Warning (-w) is not numeric\n"; + # Check for numeric value of critical parameter + if ( $lower_crit_threshold !~ /^\d+$/ ) { + print "Lower critical (-c) is not numeric\n"; exit $ERRORS{'UNKNOWN'}; } - if ( $opt_c !~ /^\d+$/ ) { - print "Critical (-c) is not numeric\n"; + if ( $upper_crit_threshold !~ /^\d+$/ ) { + print "Upper critical (-c) is not numeric\n"; exit $ERRORS{'UNKNOWN'}; } - if ( $opt_w >= $opt_c) { - print "Warning (-w) cannot be greater than Critical (-c)!\n"; + # Check boundaries + if ( $upper_warn_threshold >= $upper_crit_threshold ) { + print "Upper Warning (-w) cannot be greater than Critical (-c)!\n"; + exit $ERRORS{'UNKNOWN'}; + } + if ( $lower_warn_threshold < $lower_crit_threshold ) { + print "Lower Warning (-w) cannot be greater than Critical (-c)!\n"; exit $ERRORS{'UNKNOWN'}; } @@ -254,3 +271,39 @@ sub print_help () { print "\n\n"; support(); } + +sub calc_as_seconds () { + + my $parameter = shift; + + # Check if suffix is present + # Calculate parameter to seconds (to get an integer value finally) + # If no suffix is present, just return the value + + # Possible suffixes: + # s = seconds + # m = minutes + # h = hours + # d = days + # w = weeks + my %factor = ( "s" => 1, + "m" => 60, + "h" => 60 * 60, + "d" => 60 * 60 * 24, + "w" => 60 * 60 * 24 * 7, + ); + + if ( $parameter =~ /^(\d+)([a-z])$/ ) { + my $value = $1; + my $suffix = $2; + print "detected: value=$value, suffix=$suffix\n" if $verbose; + if ( ! defined $factor{$suffix} ) { + print "Error: wrong suffix ($suffix) for value '$parameter'"; + exit $ERRORS{'UNKNOWN'}; + } + $parameter = $value * $factor{$suffix}; + } + + return $parameter; + +} diff --git a/plugins-scripts/t/check_uptime.t b/plugins-scripts/t/check_uptime.t index 410a0800..4606718a 100644 --- a/plugins-scripts/t/check_uptime.t +++ b/plugins-scripts/t/check_uptime.t @@ -26,7 +26,7 @@ $result = NPTest->testCmd( "./check_uptime -w 5 -c 2" ); cmp_ok( $result->return_code, '==', 3, "Warning greater than critical" ); -like ( $result->output, '/^Warning .*cannot be greater than Critical/', "Output for warning greater than critical correct" ); +like ( $result->output, '/^Upper Warning .*cannot be greater than Critical/', "Output for warning greater than critical correct" ); $result = NPTest->testCmd( "./check_uptime -c 1000 -W 100 2>&1" -- cgit v1.2.3 From 558090a7d884a265aa909d894672154892a0baae Mon Sep 17 00:00:00 2001 From: Bernd Arnold Date: Wed, 13 Jun 2018 17:13:20 +0200 Subject: Added tests for range values --- plugins-scripts/t/check_uptime.t | 58 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) (limited to 'plugins-scripts') diff --git a/plugins-scripts/t/check_uptime.t b/plugins-scripts/t/check_uptime.t index 4606718a..c395307c 100644 --- a/plugins-scripts/t/check_uptime.t +++ b/plugins-scripts/t/check_uptime.t @@ -5,7 +5,7 @@ # use strict; -use Test::More tests => 21; +use Test::More tests => 40; use NPTest; my $result; @@ -71,3 +71,59 @@ cmp_ok( $result->return_code, '==', 0, "Uptime lower than 111222 days" ); like ( $result->output, '/^OK: uptime is \d+ seconds/', "Output for uptime lower than 111222 days correct" ); like ( $result->output, '/\|uptime=[0-9]+s;9609580800;19209571200;/', "Checking for performance output" ); +# Same as before, hopefully uptime is higher than 2 seconds so no warning +$result = NPTest->testCmd( + "./check_uptime -w 2:111222d -c 1:222333d" + ); +cmp_ok( $result->return_code, '==', 0, "Uptime lower than 111222 days, and higher 2 seconds" ); +like ( $result->output, '/^OK: uptime is \d+ seconds/', "Output for uptime lower than 111222 days, and higher 2 seconds correct" ); +like ( $result->output, '/\|uptime=[0-9]+s;9609580800;19209571200;/', "Checking for performance output" ); + +# Same as before, now the low warning should trigger +$result = NPTest->testCmd( + "./check_uptime -w 111221d:111222d -c 1:222333d" + ); +cmp_ok( $result->return_code, '==', 1, "Uptime lower than 111221 days raises warning" ); +like ( $result->output, '/^WARNING: uptime is \d+ seconds/', "Output for uptime lower than 111221 days correct" ); +like ( $result->output, '/Exceeds lower warn threshold/', "Exceeds text correct" ); +like ( $result->output, '/\|uptime=[0-9]+s;9609580800;19209571200;/', "Checking for performance output" ); + +# Same as before, now the low critical should trigger +$result = NPTest->testCmd( + "./check_uptime -w 111221d:111222d -c 111220d:222333d" + ); +cmp_ok( $result->return_code, '==', 2, "Uptime lower than 111220 days raises critical" ); +like ( $result->output, '/^CRITICAL: uptime is \d+ seconds/', "Output for uptime lower than 111220 days correct" ); +like ( $result->output, '/Exceeds lower crit threshold/', "Exceeds text correct" ); +like ( $result->output, '/\|uptime=[0-9]+s;9609580800;19209571200;/', "Checking for performance output" ); + + +# +# Range values using ":" without two parts ("a:b") is invalid +# Strings without two parts are always considered as upper threshold +# + +$result = NPTest->testCmd( + "./check_uptime -w 2: -c 1:4" + ); +cmp_ok( $result->return_code, '==', 3, "Wrong parameter format raises unknown" ); +like ( $result->output, '/^Upper warning .* is not numeric/', "Output for wrong parameter format correct" ); + +$result = NPTest->testCmd( + "./check_uptime -w 2:3 -c 1:" + ); +cmp_ok( $result->return_code, '==', 3, "Wrong parameter format raises unknown" ); +like ( $result->output, '/^Upper critical .* is not numeric/', "Output for wrong parameter format correct" ); + +$result = NPTest->testCmd( + "./check_uptime -w :3 -c 1:4" + ); +cmp_ok( $result->return_code, '==', 3, "Wrong parameter format raises unknown" ); +like ( $result->output, '/^Upper warning .* is not numeric/', "Output for wrong parameter format correct" ); + +$result = NPTest->testCmd( + "./check_uptime -w 2:3 -c :4" + ); +cmp_ok( $result->return_code, '==', 3, "Wrong parameter format raises unknown" ); +like ( $result->output, '/^Upper critical .* is not numeric/', "Output for wrong parameter format correct" ); + -- cgit v1.2.3 From cf157ef84502ed86b006fbb6289b7ec94e8f0eb3 Mon Sep 17 00:00:00 2001 From: Bernd Arnold Date: Wed, 13 Jun 2018 17:28:53 +0200 Subject: Refactoring Better alignment. Avoid duplications ("Exceeds ... threshold"). --- plugins-scripts/check_uptime.pl | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'plugins-scripts') diff --git a/plugins-scripts/check_uptime.pl b/plugins-scripts/check_uptime.pl index f2b47be5..1f844ff6 100755 --- a/plugins-scripts/check_uptime.pl +++ b/plugins-scripts/check_uptime.pl @@ -107,9 +107,9 @@ if ( $days > 14 ) { my $pretty_uptime = ""; $pretty_uptime .= sprintf( "%d week%s, ", $weeks, $weeks == 1 ? "" : "s" ) if $weeks; -$pretty_uptime .= sprintf( "%d day%s, ", $days, $days == 1 ? "" : "s" ) if $days; +$pretty_uptime .= sprintf( "%d day%s, ", $days, $days == 1 ? "" : "s" ) if $days; $pretty_uptime .= sprintf( "%d hour%s, ", $hours, $hours == 1 ? "" : "s" ) if $hours; -$pretty_uptime .= sprintf( "%d minute%s, ", $mins, $mins == 1 ? "" : "s" ) if $mins; +$pretty_uptime .= sprintf( "%d minute%s, ", $mins, $mins == 1 ? "" : "s" ) if $mins; # Replace last occurence of comma with "and" $pretty_uptime =~ s/, $/ and /; # Always print the seconds (though it may be 0 seconds) @@ -123,16 +123,16 @@ my $state_str = "UNKNOWN"; my $out_of_bounds_text = ""; if ( $uptime_seconds > $upper_crit_threshold ) { $state_str = "CRITICAL"; - $out_of_bounds_text = "Exceeds upper crit threshold"; + $out_of_bounds_text = "upper crit"; } elsif ( $uptime_seconds < $lower_crit_threshold ) { $state_str = "CRITICAL"; - $out_of_bounds_text = "Exceeds lower crit threshold"; + $out_of_bounds_text = "lower crit"; } elsif ( $uptime_seconds > $upper_warn_threshold ) { $state_str = "WARNING"; - $out_of_bounds_text = "Exceeds upper warn threshold"; + $out_of_bounds_text = "upper warn"; } elsif ( $uptime_seconds < $lower_warn_threshold ) { $state_str = "WARNING"; - $out_of_bounds_text = "Exceeds lower warn threshold"; + $out_of_bounds_text = "lower warn"; } else { $state_str = "OK"; } @@ -140,7 +140,7 @@ if ( $uptime_seconds > $upper_crit_threshold ) { $msg = "$state_str: "; $msg .= "uptime is $uptime_seconds seconds. "; -$msg .= "$out_of_bounds_text. " if $out_of_bounds_text; +$msg .= "Exceeds $out_of_bounds_text threshold. " if $out_of_bounds_text; $msg .= "Running for $pretty_uptime. " if $opt_f; if ( $opt_s ) { chomp( my $up_since = `uptime -s` ); -- cgit v1.2.3 From ae7c16306c9562d98caa843a550687cd618e36af Mon Sep 17 00:00:00 2001 From: Sven Nierlein Date: Wed, 13 Jun 2018 20:23:07 +0200 Subject: add check_uptime to makefile --- plugins-scripts/Makefile.am | 2 ++ 1 file changed, 2 insertions(+) (limited to 'plugins-scripts') diff --git a/plugins-scripts/Makefile.am b/plugins-scripts/Makefile.am index ea65aed1..088a4459 100644 --- a/plugins-scripts/Makefile.am +++ b/plugins-scripts/Makefile.am @@ -16,11 +16,13 @@ VPATH=$(top_srcdir) $(top_srcdir)/plugins-scripts $(top_srcdir)/plugins-scripts/ libexec_SCRIPTS = check_breeze check_disk_smb check_flexlm check_ircd \ check_log check_oracle check_rpc check_sensors check_wave \ check_ifstatus check_ifoperstatus check_mailq check_file_age \ + check_uptime \ utils.sh utils.pm EXTRA_DIST=check_breeze.pl check_disk_smb.pl check_flexlm.pl check_ircd.pl \ check_log.sh check_oracle.sh check_rpc.pl check_sensors.sh \ check_ifstatus.pl check_ifoperstatus.pl check_wave.pl check_mailq.pl check_file_age.pl \ + check_uptime.pl \ utils.sh.in utils.pm.in t EDIT = sed \ -- cgit v1.2.3 From 0b9c85109b3b929993f0afa16cb069e67e94e872 Mon Sep 17 00:00:00 2001 From: Sven Nierlein Date: Wed, 13 Jun 2018 20:23:21 +0200 Subject: fix typo in check_uptime --- plugins-scripts/check_uptime.pl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'plugins-scripts') diff --git a/plugins-scripts/check_uptime.pl b/plugins-scripts/check_uptime.pl index 1f844ff6..9679a7cd 100755 --- a/plugins-scripts/check_uptime.pl +++ b/plugins-scripts/check_uptime.pl @@ -124,7 +124,7 @@ my $out_of_bounds_text = ""; if ( $uptime_seconds > $upper_crit_threshold ) { $state_str = "CRITICAL"; $out_of_bounds_text = "upper crit"; -} elsif ( $uptime_seconds < $lower_crit_threshold ) { +} elsif ( $uptime_seconds < $lower_crit_threshold ) { $state_str = "CRITICAL"; $out_of_bounds_text = "lower crit"; } elsif ( $uptime_seconds > $upper_warn_threshold ) { -- cgit v1.2.3 From b40d660bb38a7d43934c6c63962e3c50f524e55a Mon Sep 17 00:00:00 2001 From: Bernd Arnold Date: Sun, 17 Jun 2018 09:19:55 +0200 Subject: Remove useless (empty) print --- plugins-scripts/check_uptime.pl | 1 - 1 file changed, 1 deletion(-) (limited to 'plugins-scripts') diff --git a/plugins-scripts/check_uptime.pl b/plugins-scripts/check_uptime.pl index 9679a7cd..b91ceea5 100755 --- a/plugins-scripts/check_uptime.pl +++ b/plugins-scripts/check_uptime.pl @@ -267,7 +267,6 @@ sub print_help () { print "\n\n"; print "Note: -w and -c are required arguments.\n"; print " You can suffix both values with s for seconds (default), m (minutes), h (hours), d (days) or w (weeks).\n"; - print ""; print "\n\n"; support(); } -- cgit v1.2.3 From 28f4d144f286e115eb2502c6c9e130d26eaba441 Mon Sep 17 00:00:00 2001 From: Bernd Arnold Date: Sun, 17 Jun 2018 09:20:16 +0200 Subject: Added help text for range support --- plugins-scripts/check_uptime.pl | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'plugins-scripts') diff --git a/plugins-scripts/check_uptime.pl b/plugins-scripts/check_uptime.pl index b91ceea5..0a13cc0e 100755 --- a/plugins-scripts/check_uptime.pl +++ b/plugins-scripts/check_uptime.pl @@ -267,6 +267,12 @@ sub print_help () { print "\n\n"; print "Note: -w and -c are required arguments.\n"; print " You can suffix both values with s for seconds (default), m (minutes), h (hours), d (days) or w (weeks).\n"; + print "\n"; + print "Range support: You may specify a range for both warning and critical thresholds.\n"; + print " This works without additional Perl modules.\n"; + print "Example: ./check_uptime -w 10m:4w -c 1m:8w\n"; + print " Results in a critical state when uptime is below 60 seconds or higher than 8 weeks,\n"; + print " and in a warning state when uptime is below 10 minutes or above 4 weeks.\n"; print "\n\n"; support(); } -- cgit v1.2.3 From 37f9b45266acfeb45157d3c27a65979b33c57df7 Mon Sep 17 00:00:00 2001 From: Bernd Arnold Date: Sun, 17 Jun 2018 09:36:13 +0200 Subject: Drop uptime binary call No need to call /bin/uptime, since the string can be generated with strftime(...). --- plugins-scripts/check_uptime.pl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'plugins-scripts') diff --git a/plugins-scripts/check_uptime.pl b/plugins-scripts/check_uptime.pl index 0a13cc0e..ed859ab3 100755 --- a/plugins-scripts/check_uptime.pl +++ b/plugins-scripts/check_uptime.pl @@ -143,7 +143,7 @@ $msg .= "uptime is $uptime_seconds seconds. "; $msg .= "Exceeds $out_of_bounds_text threshold. " if $out_of_bounds_text; $msg .= "Running for $pretty_uptime. " if $opt_f; if ( $opt_s ) { - chomp( my $up_since = `uptime -s` ); + my $up_since = strftime( "%Y-%m-%d %H:%M:%S", localtime( time - $uptime_seconds ) ); $msg .= "Running since $up_since. "; } -- cgit v1.2.3 From 6a8564fb44ab6914dcc8ec63e8af6477803da080 Mon Sep 17 00:00:00 2001 From: Bernd Arnold Date: Sun, 17 Jun 2018 09:42:25 +0200 Subject: Fixed error message, added comment --- plugins-scripts/check_uptime.pl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'plugins-scripts') diff --git a/plugins-scripts/check_uptime.pl b/plugins-scripts/check_uptime.pl index ed859ab3..4c9f22da 100755 --- a/plugins-scripts/check_uptime.pl +++ b/plugins-scripts/check_uptime.pl @@ -236,8 +236,9 @@ sub process_arguments(){ print "Upper Warning (-w) cannot be greater than Critical (-c)!\n"; exit $ERRORS{'UNKNOWN'}; } + # No "<=" since both values are zero if no range (only upper threshold values) is given if ( $lower_warn_threshold < $lower_crit_threshold ) { - print "Lower Warning (-w) cannot be greater than Critical (-c)!\n"; + print "Lower Warning (-w) cannot be less than Critical (-c)!\n"; exit $ERRORS{'UNKNOWN'}; } -- cgit v1.2.3 From 660e0cf4e2968c97adeabf9ba91a54f1bd4ad240 Mon Sep 17 00:00:00 2001 From: Sven Nierlein Date: Tue, 4 Dec 2018 14:08:21 +0100 Subject: tests: skip some tests if Monitoring::Plugin::Range isn't available The check_file_age uses Monitoring::Plugin::Range internally. Skip thoses tests if the module isn't available. --- plugins-scripts/t/check_file_age.t | 103 +++++++++++++------------------------ 1 file changed, 36 insertions(+), 67 deletions(-) (limited to 'plugins-scripts') diff --git a/plugins-scripts/t/check_file_age.t b/plugins-scripts/t/check_file_age.t index ebf673f5..8b876708 100644 --- a/plugins-scripts/t/check_file_age.t +++ b/plugins-scripts/t/check_file_age.t @@ -20,105 +20,74 @@ my $temp_link = "/tmp/check_file_age.link.tmp"; unlink $temp_file, $temp_link; -$result = NPTest->testCmd( - "./check_file_age" - ); +$result = NPTest->testCmd("./check_file_age"); cmp_ok( $result->return_code, '==', 3, "Missing parameters" ); like ( $result->output, $unknownOutput, "Output for unknown correct" ); -$result = NPTest->testCmd( - "./check_file_age -f $temp_file" - ); +$result = NPTest->testCmd("./check_file_age -f $temp_file"); cmp_ok( $result->return_code, '==', 2, "File not exists" ); like ( $result->output, $criticalOutput, "Output for file missing correct" ); write_chars(100); -$result = NPTest->testCmd( - "./check_file_age -f $temp_file" - ); +$result = NPTest->testCmd("./check_file_age -f $temp_file"); cmp_ok( $result->return_code, '==', 0, "File is new enough" ); like ( $result->output, $successOutput, "Output for success correct" ); sleep 2; -$result = NPTest->testCmd( - "./check_file_age -f $temp_file -w 1" - ); +$result = NPTest->testCmd("./check_file_age -f $temp_file -w 1"); cmp_ok( $result->return_code, '==', 1, "Warning for file over 1 second old" ); like ( $result->output, $warningOutput, "Output for warning correct" ); -$result = NPTest->testCmd( - "./check_file_age -f $temp_file -w 0:1" - ); -cmp_ok( $result->return_code, '==', 1, "Warning for file over 1 second old by range" ); -like ( $result->output, $warningOutput, "Output for warning by range correct" ); - -$result = NPTest->testCmd( - "./check_file_age -f $temp_file -c 1" - ); +$result = NPTest->testCmd("./check_file_age -f $temp_file -c 1"); cmp_ok( $result->return_code, '==', 2, "Critical for file over 1 second old" ); like ( $result->output, $criticalOutput, "Output for critical correct" ); -$result = NPTest->testCmd( - "./check_file_age -f $temp_file -c 0:1" - ); -cmp_ok( $result->return_code, '==', 2, "Critical for file over 1 second old by range" ); -like ( $result->output, $criticalOutput, "Output for critical by range correct" ); - -$result = NPTest->testCmd( - "./check_file_age -f $temp_file -c 1000 -W 100" - ); +$result = NPTest->testCmd("./check_file_age -f $temp_file -c 1000 -W 100"); cmp_ok( $result->return_code, '==', 0, "Checking file size" ); -$result = NPTest->testCmd( - "./check_file_age -f $temp_file -c 0:1000 -W 0:100" - ); -cmp_ok( $result->return_code, '==', 0, "Checking file size by range" ); - -$result = NPTest->testCmd( - "./check_file_age -f $temp_file -c 1000 -W 100" - ); +$result = NPTest->testCmd("./check_file_age -f $temp_file -c 1000 -W 100"); like( $result->output, $performanceOutput, "Checking for performance Output" ); -$result = NPTest->testCmd( - "./check_file_age -f $temp_file -c 1000 -W 100" - ); +$result = NPTest->testCmd("./check_file_age -f $temp_file -c 1000 -W 100"); like( $result->output, $performanceOutput, "Checking for performance Output from range" ); -$result = NPTest->testCmd( - "./check_file_age -f /non/existent --ignore-missing" - ); +$result = NPTest->testCmd("./check_file_age -f /non/existent --ignore-missing"); cmp_ok( $result->return_code, '==', 0, "Honours --ignore-missing" ); -$result = NPTest->testCmd( - "./check_file_age -f $temp_file -c 1000 -W 101" - ); +$result = NPTest->testCmd("./check_file_age -f $temp_file -c 1000 -W 101"); cmp_ok( $result->return_code, '==', 1, "One byte too short" ); -$result = NPTest->testCmd( - "./check_file_age -f $temp_file -c 1000 -W 101:" - ); -cmp_ok( $result->return_code, '==', 1, "One byte too short by range" ); - -$result = NPTest->testCmd( - "./check_file_age -f $temp_file -c 1000 -C 101" - ); +$result = NPTest->testCmd("./check_file_age -f $temp_file -c 1000 -C 101"); cmp_ok( $result->return_code, '==', 2, "One byte too short - critical" ); -$result = NPTest->testCmd( - "./check_file_age -f $temp_file -c 1000 -C 101:" - ); -cmp_ok( $result->return_code, '==', 2, "One byte too short by range - critical" ); +SKIP: { + eval 'use Monitoring::Plugin::Range'; + skip "Monitoring::Plugin::Range module require", 9 if $@; + + $result = NPTest->testCmd("./check_file_age -f $temp_file -w 0:1"); + cmp_ok( $result->return_code, '==', 1, "Warning for file over 1 second old by range" ); + like ( $result->output, $warningOutput, "Output for warning by range correct" ); + + $result = NPTest->testCmd("./check_file_age -f $temp_file -c 0:1"); + cmp_ok( $result->return_code, '==', 2, "Critical for file over 1 second old by range" ); + like ( $result->output, $criticalOutput, "Output for critical by range correct" ); + + $result = NPTest->testCmd("./check_file_age -f $temp_file -c 0:1000 -W 0:100"); + cmp_ok( $result->return_code, '==', 0, "Checking file size by range" ); + + $result = NPTest->testCmd("./check_file_age -f $temp_file -c 1000 -W 101:"); + cmp_ok( $result->return_code, '==', 1, "One byte too short by range" ); + + $result = NPTest->testCmd("./check_file_age -f $temp_file -c 1000 -W 0:99"); + cmp_ok( $result->return_code, '==', 1, "One byte too long by range" ); -$result = NPTest->testCmd( - "./check_file_age -f $temp_file -c 1000 -W 0:99" - ); -cmp_ok( $result->return_code, '==', 1, "One byte too long by range" ); + $result = NPTest->testCmd("./check_file_age -f $temp_file -c 1000 -C 101:"); + cmp_ok( $result->return_code, '==', 2, "One byte too short by range - critical" ); -$result = NPTest->testCmd( - "./check_file_age -f $temp_file -c 1000 -C 0:99" - ); -cmp_ok( $result->return_code, '==', 2, "One byte too long by range - critical" ); + $result = NPTest->testCmd("./check_file_age -f $temp_file -c 1000 -C 0:99"); + cmp_ok( $result->return_code, '==', 2, "One byte too long by range - critical" ); +}; symlink $temp_file, $temp_link or die "Cannot create symlink"; $result = NPTest->testCmd("./check_file_age -f $temp_link -c 10"); -- cgit v1.2.3 From aa79ab278ffc9bf3d41207d97bdfb04be35b5c74 Mon Sep 17 00:00:00 2001 From: Sven Nierlein Date: Tue, 8 Dec 2020 16:24:10 +0100 Subject: check_mailq: restore accidentially removed options with https://github.com/monitoring-plugins/monitoring-plugins/issues/381 -W and -C have been removed. It would have been sufficient to only remove the long options. Restoring short options.. - fixes https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=954961 --- plugins-scripts/check_mailq.pl | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'plugins-scripts') diff --git a/plugins-scripts/check_mailq.pl b/plugins-scripts/check_mailq.pl index 32f498d3..aac1310e 100755 --- a/plugins-scripts/check_mailq.pl +++ b/plugins-scripts/check_mailq.pl @@ -568,7 +568,9 @@ sub process_arguments(){ "w=i" => \$opt_w, "warning=i" => \$opt_w, # warning if above this number "c=i" => \$opt_c, "critical=i" => \$opt_c, # critical if above this number "t=i" => \$opt_t, "timeout=i" => \$opt_t, - "s" => \$opt_s, "sudo" => \$opt_s + "s" => \$opt_s, "sudo" => \$opt_s, + "W=i" => \$opt_W, # warning if above this number + "C=i" => \$opt_C, # critical if above this number ); if ($opt_V) { @@ -662,8 +664,8 @@ sub print_help () { print " Feedback/patches to support non-sendmail mailqueue welcome\n\n"; print "-w (--warning) = Min. number of messages in queue to generate warning\n"; print "-c (--critical) = Min. number of messages in queue to generate critical alert ( w < c )\n"; - print "-W (--Warning) = Min. number of messages for same domain in queue to generate warning\n"; - print "-C (--Critical) = Min. number of messages for same domain in queue to generate critical alert ( W < C )\n"; + print "-W = Min. number of messages for same domain in queue to generate warning\n"; + print "-C = Min. number of messages for same domain in queue to generate critical alert ( W < C )\n"; print "-t (--timeout) = Plugin timeout in seconds (default = $utils::TIMEOUT)\n"; print "-M (--mailserver) = [ sendmail | qmail | postfix | exim | nullmailer ] (default = autodetect)\n"; print "-s (--sudo) = Use sudo to call the mailq command\n"; -- cgit v1.2.3 From 3abcf83af41af9e56382b8e93a48d8d3ce1f18e8 Mon Sep 17 00:00:00 2001 From: rincewind Date: Fri, 2 Jul 2021 10:37:35 +0200 Subject: Remove modified note, since this is a git repository --- plugins-scripts/check_log.sh | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'plugins-scripts') diff --git a/plugins-scripts/check_log.sh b/plugins-scripts/check_log.sh index d28c8d0a..4330235e 100755 --- a/plugins-scripts/check_log.sh +++ b/plugins-scripts/check_log.sh @@ -1,8 +1,7 @@ #!/bin/sh # # Log file pattern detector plugin for monitoring -# Written by Ethan Galstad (nagios@nagios.org) -# Last Modified: 07-31-1999 +# Written originally by Ethan Galstad (nagios@nagios.org) # # Usage: ./check_log # -- cgit v1.2.3 From 14e1d7f6af409c28dab4732357a2b355a1ada85a Mon Sep 17 00:00:00 2001 From: rincewind Date: Fri, 2 Jul 2021 17:39:47 +0200 Subject: Apply shellcheck --- plugins-scripts/check_log.sh | 58 ++++++++++++++++++++++---------------------- 1 file changed, 29 insertions(+), 29 deletions(-) (limited to 'plugins-scripts') diff --git a/plugins-scripts/check_log.sh b/plugins-scripts/check_log.sh index 4330235e..8a79704a 100755 --- a/plugins-scripts/check_log.sh +++ b/plugins-scripts/check_log.sh @@ -59,11 +59,11 @@ PATH="@TRUSTED_PATH@" export PATH -PROGNAME=`basename $0` -PROGPATH=`echo $0 | sed -e 's,[\\/][^\\/][^\\/]*$,,'` +PROGNAME=$(basename "$0") +PROGPATH=$(echo "$0" | sed -e 's,[\\/][^\\/][^\\/]*$,,') REVISION="@NP_VERSION@" -. $PROGPATH/utils.sh +. "$PROGPATH"/utils.sh print_usage() { echo "Usage: $PROGNAME -F logfile -O oldlog -q query" @@ -72,7 +72,7 @@ print_usage() { } print_help() { - print_revision $PROGNAME $REVISION + print_revision "$PROGNAME" $REVISION echo "" print_usage echo "" @@ -86,7 +86,7 @@ print_help() { if [ $# -lt 1 ]; then print_usage - exit $STATE_UNKNOWN + exit "$STATE_UNKNOWN" fi # Grab the command line arguments @@ -99,19 +99,19 @@ while test -n "$1"; do case "$1" in --help) print_help - exit $STATE_OK + exit "$STATE_OK" ;; -h) print_help - exit $STATE_OK + exit "$STATE_OK" ;; --version) - print_revision $PROGNAME $REVISION - exit $STATE_OK + print_revision "$PROGNAME" $REVISION + exit "$STATE_OK" ;; -V) - print_revision $PROGNAME $REVISION - exit $STATE_OK + print_revision "$PROGNAME" $REVISION + exit "$STATE_OK" ;; --filename) logfile=$2 @@ -148,7 +148,7 @@ while test -n "$1"; do *) echo "Unknown argument: $1" print_usage - exit $STATE_UNKNOWN + exit "$STATE_UNKNOWN" ;; esac shift @@ -156,22 +156,22 @@ done # If the source log file doesn't exist, exit -if [ ! -e $logfile ]; then +if [ ! -e "$logfile" ]; then echo "Log check error: Log file $logfile does not exist!" - exit $STATE_UNKNOWN -elif [ ! -r $logfile ] ; then + exit "$STATE_UNKNOWN" +elif [ ! -r "$logfile" ] ; then echo "Log check error: Log file $logfile is not readable!" - exit $STATE_UNKNOWN + exit "$STATE_UNKNOWN" fi # If the old log file doesn't exist, this must be the first time # we're running this test, so copy the original log file over to # the old diff file and exit -if [ ! -e $oldlog ]; then - cat $logfile > $oldlog +if [ ! -e "$oldlog" ]; then + cat "$logfile" > "$oldlog" echo "Log check data initialized..." - exit $STATE_OK + exit "$STATE_OK" fi # The old log file exists, so compare it to the original log now @@ -179,24 +179,24 @@ fi # The temporary file that the script should use while # processing the log file. if [ -x /bin/mktemp ]; then - tempdiff=`/bin/mktemp /tmp/check_log.XXXXXXXXXX` + tempdiff=$(/bin/mktemp /tmp/check_log.XXXXXXXXXX) else - tempdiff=`/bin/date '+%H%M%S'` + tempdiff=$(/bin/date '+%H%M%S') tempdiff="/tmp/check_log.${tempdiff}" - touch $tempdiff - chmod 600 $tempdiff + touch "$tempdiff" + chmod 600 "$tempdiff" fi -diff $logfile $oldlog | grep -v "^>" > $tempdiff +diff "$logfile" "$oldlog" | grep -v "^>" > "$tempdiff" # Count the number of matching log entries we have -count=`grep -c "$query" $tempdiff` +count=$(grep -c "$query" "$tempdiff") # Get the last matching entry in the diff file -lastentry=`grep "$query" $tempdiff | tail -1` +lastentry=$(grep "$query" "$tempdiff" | tail -1) -rm -f $tempdiff -cat $logfile > $oldlog +rm -f "$tempdiff" +cat "$logfile" > "$oldlog" if [ "$count" = "0" ]; then # no matches, exit with no error echo "Log check ok - 0 pattern matches found" @@ -206,4 +206,4 @@ else # Print total matche count and the last entry we found exitstatus=$STATE_CRITICAL fi -exit $exitstatus +exit "$exitstatus" -- cgit v1.2.3 From c2aa1a5fa2dd96c7186704901d33721a63b9cd03 Mon Sep 17 00:00:00 2001 From: rincewind Date: Fri, 2 Jul 2021 18:01:47 +0200 Subject: Add extended and perl regex --- plugins-scripts/check_log.sh | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) (limited to 'plugins-scripts') diff --git a/plugins-scripts/check_log.sh b/plugins-scripts/check_log.sh index 8a79704a..6e9fbca5 100755 --- a/plugins-scripts/check_log.sh +++ b/plugins-scripts/check_log.sh @@ -145,6 +145,22 @@ while test -n "$1"; do exitstatus=$2 shift ;; + --extended-regex) + ERE=1 + shift + ;; + -e) + ERE=1 + shift + ;; + --perl-regex) + PRE=1 + shift + ;; + -p) + PRE=1 + shift + ;; *) echo "Unknown argument: $1" print_usage @@ -154,6 +170,20 @@ while test -n "$1"; do shift done +# Parameter sanity check +if [ $ERE ] && [ $PRE ] ; then + echo "Can not use extended and perl regex at the same time" + exit "$STATE_UNKNOWN" +fi + +if [ $ERE ]; then + GREP="grep -E" +fi + +if [ $PRE ]; then + GREP="grep -P" +fi + # If the source log file doesn't exist, exit if [ ! -e "$logfile" ]; then @@ -190,10 +220,10 @@ fi diff "$logfile" "$oldlog" | grep -v "^>" > "$tempdiff" # Count the number of matching log entries we have -count=$(grep -c "$query" "$tempdiff") +count=$($GREP -c "$query" "$tempdiff") # Get the last matching entry in the diff file -lastentry=$(grep "$query" "$tempdiff" | tail -1) +lastentry=$($GREP "$query" "$tempdiff" | tail -1) rm -f "$tempdiff" cat "$logfile" > "$oldlog" -- cgit v1.2.3 From 53b77dee91f780b4d78839f881c642189b829e3c Mon Sep 17 00:00:00 2001 From: rincewind Date: Fri, 2 Jul 2021 18:38:12 +0200 Subject: Add -a option to print all matching lines and -p and -e options for perl and extended RE --- plugins-scripts/check_log.sh | 51 ++++++++++++++++++++++++++++++++------------ 1 file changed, 37 insertions(+), 14 deletions(-) (limited to 'plugins-scripts') diff --git a/plugins-scripts/check_log.sh b/plugins-scripts/check_log.sh index 6e9fbca5..8af07b5e 100755 --- a/plugins-scripts/check_log.sh +++ b/plugins-scripts/check_log.sh @@ -69,6 +69,11 @@ print_usage() { echo "Usage: $PROGNAME -F logfile -O oldlog -q query" echo "Usage: $PROGNAME --help" echo "Usage: $PROGNAME --version" + echo "" + echo "Other parameters:" + echo " -a|--all : Print all matching lines" + echo " -p|--perl-regex : Use perl style regular expressions in the query" + echo " -e|--extended-regex : Use extended style regular expressions in the query (not necessary for GNU grep)" } print_help() { @@ -115,35 +120,35 @@ while test -n "$1"; do ;; --filename) logfile=$2 - shift + shift 2 ;; -F) logfile=$2 - shift + shift 2 ;; --oldlog) oldlog=$2 - shift + shift 2 ;; -O) oldlog=$2 - shift + shift 2 ;; --query) query=$2 - shift + shift 2 ;; -q) query=$2 - shift + shift 2 ;; -x) exitstatus=$2 - shift + shift 2 ;; --exitstatus) exitstatus=$2 - shift + shift 2 ;; --extended-regex) ERE=1 @@ -161,13 +166,20 @@ while test -n "$1"; do PRE=1 shift ;; + --all) + ALL=1 + shift + ;; + -a) + ALL=1 + shift + ;; *) echo "Unknown argument: $1" print_usage exit "$STATE_UNKNOWN" ;; esac - shift done # Parameter sanity check @@ -176,6 +188,8 @@ if [ $ERE ] && [ $PRE ] ; then exit "$STATE_UNKNOWN" fi +GREP="grep" + if [ $ERE ]; then GREP="grep -E" fi @@ -219,11 +233,20 @@ fi diff "$logfile" "$oldlog" | grep -v "^>" > "$tempdiff" -# Count the number of matching log entries we have -count=$($GREP -c "$query" "$tempdiff") +if [ $ALL ]; then + # Get the last matching entry in the diff file + entry=$($GREP "$query" "$tempdiff") -# Get the last matching entry in the diff file -lastentry=$($GREP "$query" "$tempdiff" | tail -1) + # Count the number of matching log entries we have + count=$(echo "$entry" | wc -l) + +else + # Count the number of matching log entries we have + count=$($GREP -c "$query" "$tempdiff") + + # Get the last matching entry in the diff file + entry=$($GREP "$query" "$tempdiff" | tail -1) +fi rm -f "$tempdiff" cat "$logfile" > "$oldlog" @@ -232,7 +255,7 @@ if [ "$count" = "0" ]; then # no matches, exit with no error echo "Log check ok - 0 pattern matches found" exitstatus=$STATE_OK else # Print total matche count and the last entry we found - echo "($count) $lastentry" + echo "($count) $entry" exitstatus=$STATE_CRITICAL fi -- cgit v1.2.3 From 10db89344adc7dad1d1ec4ac00e4fadc6b79fa72 Mon Sep 17 00:00:00 2001 From: rincewind Date: Mon, 5 Jul 2021 09:56:19 +0200 Subject: Add quoting for the remaining variables --- plugins-scripts/check_log.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'plugins-scripts') diff --git a/plugins-scripts/check_log.sh b/plugins-scripts/check_log.sh index 8af07b5e..382bd72c 100755 --- a/plugins-scripts/check_log.sh +++ b/plugins-scripts/check_log.sh @@ -77,7 +77,7 @@ print_usage() { } print_help() { - print_revision "$PROGNAME" $REVISION + print_revision "$PROGNAME" "$REVISION" echo "" print_usage echo "" @@ -111,11 +111,11 @@ while test -n "$1"; do exit "$STATE_OK" ;; --version) - print_revision "$PROGNAME" $REVISION + print_revision "$PROGNAME" "$REVISION" exit "$STATE_OK" ;; -V) - print_revision "$PROGNAME" $REVISION + print_revision "$PROGNAME" "$REVISION" exit "$STATE_OK" ;; --filename) -- cgit v1.2.3 From 1fb635d8691180b312537a4540feb6f947986e86 Mon Sep 17 00:00:00 2001 From: Peter Newman Date: Thu, 22 Jul 2021 13:33:14 +0100 Subject: Re-attach a comment to where it actually belongs --- plugins-scripts/check_ifoperstatus.pl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'plugins-scripts') diff --git a/plugins-scripts/check_ifoperstatus.pl b/plugins-scripts/check_ifoperstatus.pl index 9ede1633..c190ce95 100755 --- a/plugins-scripts/check_ifoperstatus.pl +++ b/plugins-scripts/check_ifoperstatus.pl @@ -124,10 +124,10 @@ if (!defined($session)) { ## map ifdescr to ifindex - should look at being able to cache this value if (defined $ifdescr || defined $iftype) { - # escape "/" in ifdescr - very common in the Cisco world if (defined $iftype) { $status=fetch_ifindex($snmpIfType, $iftype); } else { + # escape "/" in ifdescr - very common in the Cisco world $ifdescr =~ s/\//\\\//g; $status=fetch_ifindex($snmpIfDescr, $ifdescr); # if using on device with large number of interfaces # recommend use of SNMP v2 (get-bulk) -- cgit v1.2.3 From ead405515fb4bf84ce18d2fe3f621058f09a8aa2 Mon Sep 17 00:00:00 2001 From: Lorenz Kästle Date: Mon, 18 Oct 2021 10:41:44 +0200 Subject: Restrict the nickname length of the test user for check_ircd check_ircd was using the string `ircd` plus the PID as a nickname for connecting to a IRC network by default. This caused errors, when the PID was too high and the network restricted the length of the nickname to 9 characters. This patch "fixes" this by just cutting it of, if it gets too big. --- plugins-scripts/check_ircd.pl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'plugins-scripts') diff --git a/plugins-scripts/check_ircd.pl b/plugins-scripts/check_ircd.pl index 22d21c2e..e7ab80c2 100755 --- a/plugins-scripts/check_ircd.pl +++ b/plugins-scripts/check_ircd.pl @@ -69,7 +69,7 @@ $ENV{'ENV'}=''; # -----------------------------------------------------------------[ Global ]-- $PROGNAME = "check_ircd"; -my $NICK="ircd$$"; +my $NICK="ircd" . $$ % 10000; my $USER_INFO="monitor localhost localhost : "; # -------------------------------------------------------------[ connection ]-- -- cgit v1.2.3 From 03028d23b6c59bcb4000bc70209593afd798dfe3 Mon Sep 17 00:00:00 2001 From: Lorenz Kästle Date: Mon, 18 Oct 2021 13:16:10 +0200 Subject: Add comment to make the purpose of the nickname fix more obvious --- plugins-scripts/check_ircd.pl | 2 ++ 1 file changed, 2 insertions(+) (limited to 'plugins-scripts') diff --git a/plugins-scripts/check_ircd.pl b/plugins-scripts/check_ircd.pl index e7ab80c2..d869ae7b 100755 --- a/plugins-scripts/check_ircd.pl +++ b/plugins-scripts/check_ircd.pl @@ -69,6 +69,8 @@ $ENV{'ENV'}=''; # -----------------------------------------------------------------[ Global ]-- $PROGNAME = "check_ircd"; +# nickname shouldn't be longer than 9 chars, this might happen with large PIDs +# To prevent this, we cut of the part over 10000 my $NICK="ircd" . $$ % 10000; my $USER_INFO="monitor localhost localhost : "; -- cgit v1.2.3 From 5e2c3992204466d621e521695c4250f1d5480e77 Mon Sep 17 00:00:00 2001 From: RincewindsHat <12514511+RincewindsHat@users.noreply.github.com> Date: Mon, 29 Nov 2021 17:27:57 +0100 Subject: Make size parameter a little bit more intelligible --- plugins-scripts/check_file_age.pl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'plugins-scripts') diff --git a/plugins-scripts/check_file_age.pl b/plugins-scripts/check_file_age.pl index 01b854a6..26281ddd 100755 --- a/plugins-scripts/check_file_age.pl +++ b/plugins-scripts/check_file_age.pl @@ -147,7 +147,7 @@ sub print_help () { print "\n"; print " -i | --ignore-missing : return OK if the file does not exist\n"; print " File must be no more than this many seconds old (default: warn 240 secs, crit 600)\n"; - print " File must be at least this many bytes long (default: crit 0 bytes)\n\n"; + print " File must be at least this many bytes long (default: file size is ignored (0 bytes))\n\n"; print " Both and can specify a range using the standard plugin syntax\n"; print " If any of the warning and critical arguments are in range syntax (not just bare numbers)\n"; print " then all warning and critical arguments will be interpreted as ranges.\n"; -- cgit v1.2.3