From f11f226ddf5b4968e6ca1dba958aa73cc0685682 Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Thu, 26 Mar 2015 00:17:13 +1300 Subject: First commit --- LICENSE | 22 +++++++++++ README.markdown | 14 +++++++ check_nrpe_cluster | 108 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 144 insertions(+) create mode 100644 LICENSE create mode 100644 README.markdown create mode 100755 check_nrpe_cluster diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..6420c68 --- /dev/null +++ b/LICENSE @@ -0,0 +1,22 @@ +The MIT License (MIT) + +Copyright (c) 2015 Tom Ryder + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + diff --git a/README.markdown b/README.markdown new file mode 100644 index 0000000..f30ec40 --- /dev/null +++ b/README.markdown @@ -0,0 +1,14 @@ +check\_nrpe\_cluster +==================== + +Run two or more NRPE checks and return a status based on +their aggregated results. + +License +------- + +Copyright (c) [Tom Ryder][1]. Distributed under [MIT License][2]. + +[1]: https://sanctum.geek.nz/ +[2]: http://opensource.org/licenses/MIT + diff --git a/check_nrpe_cluster b/check_nrpe_cluster new file mode 100755 index 0000000..67ce1f6 --- /dev/null +++ b/check_nrpe_cluster @@ -0,0 +1,108 @@ +#!/usr/bin/env perl + +# +# Run two or more NRPE checks and return a status based on their aggregated +# results, similar to check_cluster. fork(3)s ahoy! +# +# Author: Tom Ryder +# Copyright: 2015 +# +# $Id$ +# +package Nagios::Plugin::NRPE::Cluster; + +# Force me to write this properly +use strict; +use warnings; +use utf8; +use autodie qw(:all); + +# Require at least Perl 5.10 +use 5.010; + +# Decree package version +our $VERSION = 1.0; + +# Import required modules +use English qw(-no_match_vars); # dpkg: perl-core +use IPC::Run3; # dpkg: libipc-run3-perl +use Nagios::Plugin; # dpkg: libnagios-plugin-perl + +# Find path to Nagios' plugins +my $plugins_dir = + exists $ENV{NAGIOS_PLUGINS_DIR} + ? $ENV{NAGIOS_PLUGINS_DIR} + : '/usr/local/nagios/libexec'; + +# Build Nagios::Plugin object +my $np = Nagios::Plugin->new( + usage => 'Usage: %s [-w THRESHOLD] [-c THRESHOLD] ' + . 'HOSTNAME1:CHECK1,HOSTNAME2:CHECK2[,HOSTNAME3:CHECK3...]', + version => $VERSION, +); + +# Add warning and critical options +$np->add_arg( + spec => 'warning|w=s', + help => "-w, --warning=THRESHOLD\n" + . ' Warning threshold for the number of OK checks', +); +$np->add_arg( + spec => 'critical|c=s', + help => "-c, --critical=THRESHOLD\n" + . ' Critical threshold for the number of OK checks', +); + +# Read options +$np->getopts(); + +# Need one of --warning or --critical +if ( !$np->opts->warning && !$np->opts->critical ) { + $np->nagios_die(q{Need one/both of --warning or --critical}); +} + +# Verify we have at least two host:check pairs +my @pairs; +if (@ARGV) { + @pairs = split m{,}msx, $ARGV[0]; +} +if ( @pairs < 2 ) { + $np->nagios_die(q{Need at least two HOSTNAME:CHECK definitions}); +} + +# Verify all of the arguments are in the expected format +my @invalids = grep { !m{[^:]+:[^:]+}msx } @ARGV; +if (@invalids) { + $np->nagios_die( q{Argument(s) %s are not in HOSTNAME:CHECK format}, + join q{,}, @invalids ); +} + +# Start counting down to timeout +alarm $np->opts->timeout; + +# Run the checks and collect a list of successes and failures +my ( @pass, @fail ); +foreach my $pair (@pairs) { + my ( $hostname, $command ) = split m{:}msx, $pair; + my $cmd = [ $plugins_dir . '/check_nrpe', '-H', $hostname, '-c', $command ]; + run3 $cmd, \undef, \undef, \undef; + if ( $CHILD_ERROR == 0 ) { + push @pass, $pair; + } + else { + push @fail, $pair; + } +} + +# Compare the check results to the failures +my $code = $np->check_threshold( + check => scalar @pass, + warning => $np->opts->warning, + critical => $np->opts->critical, +); +$np->nagios_exit( $code, sprintf 'All checks run, %u passes, %u failures', + scalar @pass, scalar @fail ); + +# This should never happen, but if it does we may as well be explicit about it +$np->nagios_die(q{Couldn't work out how to aggregate checks}); + -- cgit v1.2.3