From 2dd6caaa9b9b40b6f7ce690753d669ff918b1981 Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Wed, 26 Nov 2014 17:56:23 +1300 Subject: First commit of project --- LICENSE | 22 ++++++++++++++++ README.markdown | 18 +++++++++++++ nscaw | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 119 insertions(+) create mode 100644 LICENSE create mode 100644 README.markdown create mode 100755 nscaw diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..d77e562 --- /dev/null +++ b/LICENSE @@ -0,0 +1,22 @@ +The MIT License (MIT) + +Copyright (c) 2014 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..17dad48 --- /dev/null +++ b/README.markdown @@ -0,0 +1,18 @@ +nscaw +===== + +Command wrapper that sends an appropriate passive check to an NSCA server using +`send_nsca`, depending on the command's outcome. + +Make sure `send_nsca` is in `$PATH` and that `NSCAW_SERVER` is set. + + $ nscaw IMPORTANT_JOB -- important-job -o options args ... + +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/nscaw b/nscaw new file mode 100755 index 0000000..dc35637 --- /dev/null +++ b/nscaw @@ -0,0 +1,79 @@ +#!/usr/bin/env bash + +# +# Command wrapper that sends an appropriate passive check to an NSCA server +# using send_nsca, depending on the command's outcome. +# +# Make sure send_nsca is in $PATH and that NSCAW_SERVER is set. +# +# $ nscaw IMPORTANT_JOB -- important-job -o options args ... +# +# Author: Tom Ryder +# License: MIT +# + +# Name ourself +self=nscaw + +# If there's a defaults file with environment variables for us, source it +if [[ -r /etc/default/"$self" ]] ; then + source /etc/default/"$self" +fi + +# Define a function to explain how to use this script +usage() { + printf 'Usage: %s SERVICE -- COMMAND...\n' \ + "$self" +} + +# Respond to requests for help with usage (exit success) +case $1 in + -h|--help) + usage + exit 0 + ;; +esac + +# Check that at least three arguments are present and in the correct form +if (( $# < 3 )) || [[ $2 != -- ]] ; then + usage >&2 + exit 1 +fi + +# Pull the service from the first argument and shift the first two arguments +# off the arguments array; the rest of the array should be the command to run +service=$1 +shift 2 + +# Figure out our hostname; most of the time `hostname -s` will be fine, but +# it can be overridden with the value of NSCAW_HOSTNAME if defined +hostname=${NSCAW_HOSTNAME:-$(hostname -s)} + +# Attempt to run command +"$@" + +# Decide return code and message based on command exit value +case $? in + 0) + code=0 # OK + message=$(printf '%s: Command "%s" ran successfully' \ + "$self" "$*") + ;; + 127) + code=3 # UNKNOWN + message=$(printf '%s: Command "%s" could not be found' \ + "$self" "$*") + ;; + *) + code=2 # CRITICAL + message=$(printf '%s: Command "%s" ran with errors' \ + "$self" "$*") + ;; +esac + +# Format the passive check and pipe it into send_nsca; note that we ignore the +# stdout of send_nsca as it's just a diagnostic message +printf '%s\t%s\t%u\t%s\n' \ + "$hostname" "$service" "$code" "$message" | + send_nsca -H "${NSCAW_SERVER:?}" >/dev/null + -- cgit v1.2.3