From 5a3415fd9bdf8fbdd11c87223949075212b57f03 Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Fri, 13 Aug 2021 16:13:39 +1200 Subject: Build and shebang nscaw from source file --- .gitignore | 1 + Makefile | 15 +++++++++--- nscaw | 81 -------------------------------------------------------------- nscaw.bash | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 94 insertions(+), 84 deletions(-) create mode 100644 .gitignore delete mode 100755 nscaw create mode 100644 nscaw.bash diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b7c2666 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/nscaw diff --git a/Makefile b/Makefile index 9814ba5..3944539 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,16 @@ .POSIX: -.PHONY: all install +.PHONY: all install clean .SUFFIXES: +.SUFFIXES: .bash +ALL = nscaw +BASH = /bin/bash PREFIX = /usr/local -all: -install: +all: $(ALL) +.bash: + $(BASH) -c : + awk -v interpreter=$(BASH) 'NR == 1 { $$1 ="#!" interpreter } 1' $< > $@ + chmod +x ./$@ +install: nscaw cp -- nscaw $(PREFIX)/bin +clean: + rm -f -- $(ALL) diff --git a/nscaw b/nscaw deleted file mode 100755 index de8364f..0000000 --- a/nscaw +++ /dev/null @@ -1,81 +0,0 @@ -#!/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)} - -# Figure out our username -username=$(whoami) - -# Attempt to run command within a time wrapper -# -exec 3>&1 4>&2 -TIMEFORMAT=$'real %3lR, user %3lU, sys %3lS\n' \ - time=$( { time "$@" 1>&3 2>&4 ; } 2>&1) ; ret=$? -exec 3>&- 4>&- - -# Decide return code and message based on command exit value -case $ret in - 0) - code=0 # OK - message=$(printf '%s: `%s` succeeded: %s' \ - "$self" "$*" "$time") - ;; - *) - code=2 # CRITICAL - message=$(printf '%s: `%s` failed (exit %d), USER %s, PATH=%s: %s' \ - "$self" "$*" "$ret" "$username" "$PATH" "$time") - ;; -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:-nsca}" \ - -c "${NSCAW_CONFIG:-/etc/send_nsca.cfg}" >/dev/null diff --git a/nscaw.bash b/nscaw.bash new file mode 100644 index 0000000..e5651d7 --- /dev/null +++ b/nscaw.bash @@ -0,0 +1,81 @@ +#!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)} + +# Figure out our username +username=$(whoami) + +# Attempt to run command within a time wrapper +# +exec 3>&1 4>&2 +TIMEFORMAT=$'real %3lR, user %3lU, sys %3lS\n' \ + time=$( { time "$@" 1>&3 2>&4 ; } 2>&1) ; ret=$? +exec 3>&- 4>&- + +# Decide return code and message based on command exit value +case $ret in + 0) + code=0 # OK + message=$(printf '%s: `%s` succeeded: %s' \ + "$self" "$*" "$time") + ;; + *) + code=2 # CRITICAL + message=$(printf '%s: `%s` failed (exit %d), USER %s, PATH=%s: %s' \ + "$self" "$*" "$ret" "$username" "$PATH" "$time") + ;; +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:-nsca}" \ + -c "${NSCAW_CONFIG:-/etc/send_nsca.cfg}" >/dev/null -- cgit v1.2.3