diff options
author | Tom Ryder <tom@sanctum.geek.nz> | 2016-09-04 13:31:05 +1200 |
---|---|---|
committer | Tom Ryder <tom@sanctum.geek.nz> | 2016-09-04 13:38:22 +1200 |
commit | e7b180da0f0e4eab3b7c2b08ba405654baf82588 (patch) | |
tree | c5349e668f9f8329c5a636889d05c979f7c77619 /nagios-clear | |
parent | Require at least one host (diff) | |
download | nagscripts-e7b180da0f0e4eab3b7c2b08ba405654baf82588.tar.gz nagscripts-e7b180da0f0e4eab3b7c2b08ba405654baf82588.zip |
Upgrading to latest version
These are sanitized from the versions we use at work, so maintaining
them is becoming a bit of a pain. I may have to work out some reasonable
submodule system.
Diffstat (limited to 'nagios-clear')
-rwxr-xr-x | nagios-clear | 117 |
1 files changed, 117 insertions, 0 deletions
diff --git a/nagios-clear b/nagios-clear new file mode 100755 index 0000000..e8a6de8 --- /dev/null +++ b/nagios-clear @@ -0,0 +1,117 @@ +#!/usr/bin/env bash + +# +# nagios-clear(1) -- Shortcut to removing all active downtime and +# acknowledgements from a host. +# +self=nagios-clear + +# Usage printing function +usage() { + printf 'USAGE: %s [<host[/service][,host[/service],...>|-] [comment]\n' "$self" +} + +# Process options (just help at the moment) +OPTIND=1 +while getopts 'h' opt ; do + case "$opt" in + h) + usage + exit 0 + ;; + '?') + usage >&2 + exit 1 + ;; + esac +done +shift "$((OPTIND-1))" + +# Bail if too few arguments left; we need at least an object name; the comma is +# optional +if ! (($#)) ; then + usage >&2 + exit 1 +fi + +# Define relatively fixed/guaranteed fields for Nagios command; note that the +# comment has a default of 'no comment given' +now=$(date +%s) +author=${SUDO_USER:-"$USER"} +spec=$1 +comment=${2:-'no comment given'} +cmdfile=${NAGCMD_FILE:-/usr/local/nagios/var/rw/nagios.cmd} + +# Define the path to the Livestatus socket +socket=${MK_LIVESTATUS_SOCKET:-/usr/local/nagios/var/rw/live} + +# How to get the objects depends on the spec (the first argument) +declare -a objects +case $spec in + + # If the spec is just "-", we just read unique objects from stdin + -) + while read -r object ; do + [[ $object ]] || continue + objects[${#objects[@]}]=$object + done < <(sort -u) + ;; + + # If the spec is anything else, we break it up with commas and read the + # objects that way + *) + IFS=, read -a objects -r < <(printf '%s\n' "$spec") + ;; +esac + +# There must be at least one object +if ! ((${#objects[@]})) ; then + printf '%s: At least one host/service must be given\n' \ + "$self" >&2 + exit 1 +fi + +# All the hosts or services must exist, just to be strict +for object in "${objects[@]}" ; do + nagios-exists "$object" && continue + printf '%s: Host/service %s does not seem to exist\n' \ + "$self" "$object" >&2 + exit 1 +done + +# Write commands to remove acknowledgements for each of the objects +for object in "${objects[@]}" ; do + case $object in + */*) + host=${object%/*} + service=${object##*/} + printf '[%lu] REMOVE_SVC_ACKNOWLEDGEMENT;%s;%s\n' \ + "$now" "$host" "$service" + unixcat "$socket" <<EOF | +GET downtimes +Columns: id +Filter: host_name = $host +Filter: service_description = $service +EOF + while read -r id ; do + printf '[%lu] DEL_SVC_DOWNTIME;%u\n' \ + "$now" "$id" + done + ;; + *) + host=$object + printf '[%lu] REMOVE_HOST_ACKNOWLEDGEMENT;%s\n' \ + "$now" "$host" + unixcat "$socket" <<EOF | +GET downtimes +Columns: id +Filter: host_name = $host +Filter: service_description = '' +EOF + while read -r id ; do + printf '[%lu] DEL_HOST_DOWNTIME;%u\n' \ + "$now" "$id" + done + ;; + esac +done > "$cmdfile" |