diff options
author | Tom Ryder <tom@sanctum.geek.nz> | 2016-06-20 13:08:15 +1200 |
---|---|---|
committer | Tom Ryder <tom@sanctum.geek.nz> | 2016-06-20 13:08:15 +1200 |
commit | cdc34fa78b326bd241aae7399190c739e25bf025 (patch) | |
tree | 2d12fdf161b74dcde13baf33bb46f0137f443952 /nagios-acknowledge | |
parent | Remove silly symlinks (diff) | |
download | nagscripts-3023a404a59a8ec51e9ebe0b5fea3a4f9f8ddf0c.tar.gz nagscripts-3023a404a59a8ec51e9ebe0b5fea3a4f9f8ddf0c.zip |
Update to latest version with MKLivestatusmk-livestatus
No longer using NDOUtils at work, these are much nicer and more likely
to work with any given Nagios installation now anyway.
Added nagios-data-search, nagios-downtstream-list, nagios-exists,
nagios-problem-list, and nagios-unhandled-list.
Diffstat (limited to 'nagios-acknowledge')
-rwxr-xr-x | nagios-acknowledge | 86 |
1 files changed, 59 insertions, 27 deletions
diff --git a/nagios-acknowledge b/nagios-acknowledge index 51fdce8..f9b1081 100755 --- a/nagios-acknowledge +++ b/nagios-acknowledge @@ -5,18 +5,21 @@ # it's annoying to do with the web interface for large sets of hosts or # services. # -# $ nac <host>[/<service>] [optional comment] +# $ nac (<host[/service][,host[/service],...>|-) [optional comment] # -# Good for for/while loops: +# You can specify multiple objects by separating them with commas: # -# for hostname in hosta hostb hostc; do nac "$hostname" ... ; done -# while read -r hostname; do nac "$hostname" ... ; done < downtime-hostnames +# $ nac abc-example-ap-1,abc-example-ap-2/VOLTAGE 'Power problems at abc-example' +# +# Even easier is to pipe them into stdin by specifying - as the object: +# +# $ nul | grep abc-example | nac - 'Power problems at abc-example' # # By default, does not send ACKNOWLEDGEMENT notifications. Use -n if you do # want that. # # Author: Tom Ryder <tom@sanctum.geek.nz> -# Copyright: 2014 Sanctum +# Copyright: 2016 # # Name self @@ -24,16 +27,16 @@ self=nagios-acknowledge # Usage printing function usage() { - printf 'USAGE: %s [-n] <host[/service]> [comment]\n' "$self" + printf 'USAGE: %s [-n] (<host[/service][,host[/service],...>|-) [comment]\n' "$self" } # Default to not notifying notify=0 -# Process options +# Figure out whether we should notify OPTIND=1 while getopts 'hn' opt ; do - case "$opt" in + case $opt in n) notify=1 ;; @@ -60,37 +63,66 @@ fi now=$(date +%s) spec=$1 sticky=1 +notify=$notify persistent=1 author=${SUDO_USER:-$USER} comment=${2:-'no comment given'} cmdfile=${NAGCMD_FILE:-/usr/local/nagios/var/rw/nagios.cmd} -# If a service name is specified after a slash, figure that out -if [[ $spec == */* ]] ; then - host=${spec%/*} - service=${spec##*/} -else - host=$spec - service= -fi +# 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 + +# All the hosts or services must exist, just to be strict; they don't +# necessarily have to be in a PROBLEM state though +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 # Quietly replace semicolons in comment with commas comment=${comment//;/,} -# Write command and print message if it fails; succeed silently -if [[ $service ]] ; then - cmd=$(printf '[%lu] ACKNOWLEDGE_SVC_PROBLEM;%s;%s;%u;%u;%u;%s;%s' \ - "$now" "$host" "$service" \ - "$sticky" "$notify" "$persistent" "$author" "$comment") -else - cmd=$(printf '[%lu] ACKNOWLEDGE_HOST_PROBLEM;%s;%u;%u;%u;%s;%s' \ - "$now" "$host" \ - "$sticky" "$notify" "$persistent" "$author" "$comment") -fi +# Write commands to schedule downtime for each of the objects, bail if a single +# one of them fails +for object in "${objects[@]}" ; do + case $object in + */*) + host=${object%/*} + service=${object##*/} + cmd=$(printf '[%lu] ACKNOWLEDGE_SVC_PROBLEM;%s;%s;%u;%u;%u;%s;%s' \ + "$now" "$host" "$service" \ + "$sticky" "$notify" "$persistent" "$author" "$comment") + ;; + *) + host=$object + cmd=$(printf '[%lu] ACKNOWLEDGE_HOST_PROBLEM;%s;%u;%u;%u;%s;%s' \ + "$now" "$host" \ + "$sticky" "$notify" "$persistent" "$author" "$comment") + ;; + esac + printf '%s\n' "$cmd" > "$cmdfile" || exit +done # Attempt to write command to file if ! printf '%s\n' "$cmd" > "$cmdfile" ; then printf '%s: Failed to write command to file\n' "$self" >&2 exit 1 fi - |