From d995675cdaa4aa669502ae698b36f2d2c47bc4bf Mon Sep 17 00:00:00 2001 From: Russ Allbery Date: Tue, 19 Oct 2010 09:48:49 -0700 Subject: [PATCH] More correctly check and report syntax errors Print an UNKNOWN status on standard output on syntax errors in all scripts, rather than reporting the problem only to standard error. Check that the host to check was specified and report a syntax error if it wasn't. Thanks, Tobias Wolter. --- NEWS | 5 +++++ check_afsspace | 21 ++++++++++++++------- check_bos | 19 +++++++++++++------ check_rxdebug | 21 ++++++++++++++------- check_udebug | 19 ++++++++++++++----- 5 files changed, 60 insertions(+), 25 deletions(-) diff --git a/NEWS b/NEWS index ee8362d..641eab0 100644 --- a/NEWS +++ b/NEWS @@ -9,3 +9,8 @@ afs-monitor 2.0 (unreleased) salvage), check_bos now reports a warning stating that, rather than a critical error showing the auxiliary status line. Reported by Steve Rader. + + Print an UNKNOWN status on standard output on syntax errors in all + scripts, rather than reporting the problem only to standard error. + Check that the host to check was specified and report a syntax error + if it wasn't. Thanks, Tobias Wolter. diff --git a/check_afsspace b/check_afsspace index 1104402..29da098 100755 --- a/check_afsspace +++ b/check_afsspace @@ -46,6 +46,15 @@ $VOS ||= '/usr/bin/vos'; # Implementation ############################################################################## +# Report a syntax error and exit. We do this via stdout in order to satisfy +# the Nagios plugin output requirements, but also report a more conventional +# error via stderr in case people are calling this outside of Nagios. +sub syntax { + print "AFS UNKNOWN - ", join ('', @_), "\n"; + warn "$0: ", join ('', @_), "\n"; + exit 3; +} + # Parse command line options. my ($help, $host, $version); Getopt::Long::config ('bundling', 'no_ignore_case'); @@ -54,7 +63,8 @@ GetOptions ('c|critical=i' => \$CRITICAL, 'h|help' => \$help, 't|timeout=i' => \$TIMEOUT, 'V|version' => \$version, - 'w|warning=i' => \$WARNINGS) or exit 3; + 'w|warning=i' => \$WARNINGS) + or syntax ("invalid option"); if ($help) { print "Feeding myself to perldoc, please wait....\n"; exec ('perldoc', '-t', $0) or die "Cannot fork: $!\n"; @@ -63,13 +73,10 @@ if ($help) { print "check_afsspace $version\n"; exit 0; } -if (@ARGV) { - warn "Usage: $0 [-hv] [-c ] [-w ] -H \n"; - exit 3; -} +syntax ("extra arguments on command line") if @ARGV; +syntax ("host to check not specified") unless (defined $host); if ($WARNINGS > $CRITICAL) { - warn "$0: warning level $WARNINGS greater than critical level $CRITICAL\n"; - exit 3; + syntax ("warning level $WARNINGS greater than critical level $CRITICAL"); } # Set up the alarm. diff --git a/check_bos b/check_bos index e0363b8..67a06eb 100755 --- a/check_bos +++ b/check_bos @@ -57,13 +57,23 @@ our @OKAY = ( # Implementation ############################################################################## +# Report a syntax error and exit. We do this via stdout in order to satisfy +# the Nagios plugin output requirements, but also report a more conventional +# error via stderr in case people are calling this outside of Nagios. +sub syntax { + print "BOS UNKNOWN - ", join ('', @_), "\n"; + warn "$0: ", join ('', @_), "\n"; + exit 3; +} + # Parse command line options. my ($help, $host, $version); Getopt::Long::config ('bundling', 'no_ignore_case'); GetOptions ('H|hostname=s' => \$host, 'h|help' => \$help, 't|timeout=i' => \$TIMEOUT, - 'V|version' => \$version) or exit 3; + 'V|version' => \$version) + or syntax ("invalid option"); if ($help) { print "Feeding myself to perldoc, please wait....\n"; exec ('perldoc', '-t', $0) or die "Cannot fork: $!\n"; @@ -72,11 +82,8 @@ if ($help) { print "check_bos $version\n"; exit 0; } -if (@ARGV) { - print "Usage: $0 [-hv] [-t ] -H \n"; - warn "Usage: $0 [-hv] [-t ] -H \n"; - exit 3; -} +syntax ("extra arguments on command line") if @ARGV; +syntax ("host to check not specified") unless (defined $host); # Set up the alarm. $SIG{ALRM} = sub { diff --git a/check_rxdebug b/check_rxdebug index 1d138b5..4e497fd 100755 --- a/check_rxdebug +++ b/check_rxdebug @@ -48,6 +48,15 @@ $RXDEBUG ||= '/usr/bin/rxdebug'; # Implementation ############################################################################## +# Report a syntax error and exit. We do this via stdout in order to satisfy +# the Nagios plugin output requirements, but also report a more conventional +# error via stderr in case people are calling this outside of Nagios. +sub syntax { + print "AFS UNKNOWN - ", join ('', @_), "\n"; + warn "$0: ", join ('', @_), "\n"; + exit 3; +} + # Parse command line options. my ($help, $host, $version); Getopt::Long::config ('bundling', 'no_ignore_case'); @@ -56,7 +65,8 @@ GetOptions ('c|critical=i' => \$CRITICAL, 'h|help' => \$help, 't|timeout=i' => \$TIMEOUT, 'V|version' => \$version, - 'w|warning=i' => \$WARNINGS) or exit 3; + 'w|warning=i' => \$WARNINGS) + or syntax ("invalid option"); if ($help) { print "Feeding myself to perldoc, please wait....\n"; exec ('perldoc', '-t', $0) or die "Cannot fork: $!\n"; @@ -65,13 +75,10 @@ if ($help) { print "check_rxdebug $version\n"; exit 0; } -if (@ARGV) { - warn "Usage: $0 [-hv] [-c ] [-w ] -H \n"; - exit 3; -} +syntax ("extra arguments on command line") if @ARGV; +syntax ("host to check not specified") unless (defined $host); if ($WARNINGS > $CRITICAL) { - warn "$0: warning level $WARNINGS greater than critical level $CRITICAL\n"; - exit 3; + syntax ("warning level $WARNINGS greater than critical level $CRITICAL"); } # Set up the alarm. diff --git a/check_udebug b/check_udebug index 00db9b2..a486eb8 100755 --- a/check_udebug +++ b/check_udebug @@ -40,6 +40,15 @@ $UDEBUG ||= '/usr/bin/udebug'; # Implementation ############################################################################## +# Report a syntax error and exit. We do this via stdout in order to satisfy +# the Nagios plugin output requirements, but also report a more conventional +# error via stderr in case people are calling this outside of Nagios. +sub syntax { + print "UBIK UNKNOWN - ", join ('', @_), "\n"; + warn "$0: ", join ('', @_), "\n"; + exit 3; +} + # Parse command line options. my ($help, $host, $port, $version); Getopt::Long::config ('bundling', 'no_ignore_case'); @@ -47,7 +56,8 @@ GetOptions ('H|hostname=s' => \$host, 'h|help' => \$help, 'p|port=i' => \$port, 't|timeout=i' => \$TIMEOUT, - 'V|version' => \$version) or exit 3; + 'V|version' => \$version) + or syntax ("invalid option"); if ($help) { print "Feeding myself to perldoc, please wait....\n"; exec ('perldoc', '-t', $0) or die "Cannot fork: $!\n"; @@ -56,10 +66,9 @@ if ($help) { print "check_udebug $version\n"; exit 0; } -if (@ARGV || !(defined ($host) && defined ($port))) { - warn "Usage: $0 [-hv] [-t ] -H -p \n"; - exit 3; -} +syntax ("extra arguments on command line") if @ARGV; +syntax ("host to check not specified") unless (defined $host); +syntax ("port to check not specified") unless (defined $port); # Set up the alarm. $SIG{ALRM} = sub { -- 2.39.5