From e765a4a966b934dd98c6254398d03eaf9fd47ed0 Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Fri, 27 Oct 2017 20:47:59 +1300 Subject: Backported to 5.6, warn/die behaviour fixed --- README.markdown | 6 ++--- check_nrpe_cluster | 64 +++++++++++++++++++++++++++--------------------------- 2 files changed, 35 insertions(+), 35 deletions(-) diff --git a/README.markdown b/README.markdown index 6751251..2abc6a6 100644 --- a/README.markdown +++ b/README.markdown @@ -1,8 +1,8 @@ check\_nrpe\_cluster ==================== -Run two or more NRPE checks and return a status based on -their aggregated results. +Run two or more NRPE checks and return a status based on their aggregated +results. Thanks ------ @@ -16,4 +16,4 @@ License Copyright (c) [Tom Ryder][1]. Distributed under [MIT License][2]. [1]: https://sanctum.geek.nz/ -[2]: http://opensource.org/licenses/MIT +[2]: https://opensource.org/licenses/MIT diff --git a/check_nrpe_cluster b/check_nrpe_cluster index dca66be..171d172 100755 --- a/check_nrpe_cluster +++ b/check_nrpe_cluster @@ -5,7 +5,8 @@ # results, similar to check_cluster. fork(3)s ahoy! # # Author: Tom Ryder -# Copyright: 2017 +# Copyright: 2017 Tom Ryder +# License: MIT (see LICENSE) # package Monitoring::Plugin::NRPE::Cluster; @@ -15,42 +16,41 @@ use warnings; use utf8; # Require at least this Perl version -use 5.010; - -# Decree package version -our $VERSION = 2.01; +# Nothing in here should need a modern Perl +use 5.006; # Import required modules -use Carp; # core -use Const::Fast; # dpkg: libconst-fast-perl -use English qw(-no_match_vars); # dpkg: perl-core -use IPC::Run3; # dpkg: libipc-run3-perl -use Monitoring::Plugin; # cpanm Monitoring::Plugin +use English qw(-no_match_vars); +use IPC::Run3; +use Monitoring::Plugin qw(%ERRORS); + +# Decree package version +our $VERSION = '2.02'; # Add warning and critical options -const our @OPTS => ( +our @OPTS = ( { - spec => 'warning|w=s', - help => "-w, --warning=THRESHOLD\n" - . ' Warning threshold for the number of OK checks', + spec => 'warning|w=s', + help => 'Warning threshold for the number of OK checks', + label => 'THRESHOLD', }, { - spec => 'critical|c=s', - help => "-c, --critical=THRESHOLD\n" - . ' Critical threshold for the number of OK checks', + spec => 'critical|c=s', + help => 'Critical threshold for the number of OK checks', + label => 'THRESHOLD', }, ); # Regular expressions -const our %RES => ( +our %RES = ( # Single HOSTNAME:CHECK pair from the command line host_check_pair => qr{ - \A # Start of string - ([^:,]++) # Hostname - : # Colon - ([^:,]++) # Check command - \z # End of string + \A # Start of string + ([^:,]+) # Hostname + : # Colon + ([^:,]+) # Check command + \z # End of string }msx, # Junk to remove from stdout @@ -70,15 +70,15 @@ sub build { my $def = shift; # Split HOST:CHECK definition string up, check it meets the minimum - ( my @defs = split /,/msx, $def ) >= 2 - or croak 'Need at least two HOST:CHECK pairs'; + ( my @defs = split m/,/msx, $def ) >= 2 + or die "Need at least two HOST:CHECK pairs\n"; # Build a list of check hashrefs with hostname and check command name my @checks; for my $def (@defs) { my %check; @check{qw(host check)} = $def =~ $RES{host_check_pair} - or croak "Malformed HOST:CHECK string: $def"; + or die "Malformed HOST:CHECK string: $def\n"; push @checks, \%check; } @@ -91,9 +91,10 @@ sub fetch { my ( $mp, $checks ) = @_; # Figure out where check_nrpe should be, and ensure it's there - my $pdir = $ENV{NAGIOS_PLUGINS_DIR} // '/usr/local/nagios/libexec'; + my $pdir = $ENV{NAGIOS_PLUGINS_DIR} || '/usr/local/nagios/libexec'; my $nrpe = "$pdir/check_nrpe"; - -x $nrpe or croak "$nrpe does not exist"; + -e $nrpe or die "$nrpe does not exist\n"; + -x $nrpe or die "$nrpe is not executable\n"; # Iterate through the checks and collect exit value and output for my $check ( @{$checks} ) { @@ -138,9 +139,8 @@ sub check { $out =~ s{ $RES{stdout_junk} }{...}msx; # Add the check's details including the truncated output - $mp->add_message( $code, - "$check->{host}:$check->{check} <$check->{exit}> $out", - ); + $mp->add_message( $ERRORS{OK}, + "$check->{host}:$check->{check} <$check->{exit}> $out" ); } # Form messages and exit @@ -191,7 +191,7 @@ sub run { # Validate and build the list of checks from the remaining arguments @ARGV == 1 - or croak 'Need a comma-separated HOST:CHECK list'; + or die "Need a comma-separated HOST:CHECK list\n"; my $checks = build $ARGV[0]; # Start counting down to timeout -- cgit v1.2.3