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 diff --git a/plugins-scripts/check_uptime b/plugins-scripts/check_uptime new file mode 100755 index 0000000..4bc9528 --- /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 v0.10-9-g596f 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 diff --git a/plugins-scripts/check_uptime b/plugins-scripts/check_uptime index 4bc9528..09600df 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 v0.10-9-g596f 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 diff --git a/plugins-scripts/check_uptime b/plugins-scripts/check_uptime deleted file mode 100755 index 09600df..0000000 --- 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 0000000..09600df --- /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 v0.10-9-g596f 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 diff --git a/plugins-scripts/check_uptime.pl b/plugins-scripts/check_uptime.pl index 09600df..8c1f3d2 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 v0.10-9-g596f 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 diff --git a/plugins-scripts/t/check_uptime.t b/plugins-scripts/t/check_uptime.t new file mode 100644 index 0000000..410a080 --- /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 v0.10-9-g596f 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 diff --git a/plugins-scripts/check_uptime.pl b/plugins-scripts/check_uptime.pl index 8c1f3d2..27dc93f 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 v0.10-9-g596f 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 ... diff --git a/plugins-scripts/check_uptime.pl b/plugins-scripts/check_uptime.pl index 27dc93f..20234c8 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 v0.10-9-g596f 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. diff --git a/plugins-scripts/check_uptime.pl b/plugins-scripts/check_uptime.pl index 20234c8..8817030 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 v0.10-9-g596f From a784b19d6fc193e4c98ec6b2248bf4e252a9a0d7 Mon Sep 17 00:00:00 2001 From: Bernd Arnold Date: Wed, 13 Jun 2018 16:10:17 +0200 Subject: Modified alignment diff --git a/plugins-scripts/check_uptime.pl b/plugins-scripts/check_uptime.pl index 8817030..2b230f5 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 v0.10-9-g596f 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($) diff --git a/plugins-scripts/check_uptime.pl b/plugins-scripts/check_uptime.pl index 2b230f5..f2b47be 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 410a080..4606718 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 v0.10-9-g596f 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 diff --git a/plugins-scripts/t/check_uptime.t b/plugins-scripts/t/check_uptime.t index 4606718..c395307 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 v0.10-9-g596f 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"). diff --git a/plugins-scripts/check_uptime.pl b/plugins-scripts/check_uptime.pl index f2b47be..1f844ff 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 v0.10-9-g596f 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 diff --git a/plugins-scripts/Makefile.am b/plugins-scripts/Makefile.am index ea65aed..088a445 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 v0.10-9-g596f 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 diff --git a/plugins-scripts/check_uptime.pl b/plugins-scripts/check_uptime.pl index 1f844ff..9679a7c 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 v0.10-9-g596f 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 diff --git a/plugins-scripts/check_uptime.pl b/plugins-scripts/check_uptime.pl index 9679a7c..b91ceea 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 v0.10-9-g596f 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 diff --git a/plugins-scripts/check_uptime.pl b/plugins-scripts/check_uptime.pl index b91ceea..0a13cc0 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 v0.10-9-g596f 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(...). diff --git a/plugins-scripts/check_uptime.pl b/plugins-scripts/check_uptime.pl index 0a13cc0..ed859ab 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 v0.10-9-g596f 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 diff --git a/plugins-scripts/check_uptime.pl b/plugins-scripts/check_uptime.pl index ed859ab..4c9f22d 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 v0.10-9-g596f