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-downtime | |
parent | Remove silly symlinks (diff) | |
download | nagscripts-mk-livestatus.tar.gz nagscripts-mk-livestatus.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-downtime')
-rwxr-xr-x | nagios-downtime | 100 |
1 files changed, 63 insertions, 37 deletions
diff --git a/nagios-downtime b/nagios-downtime index 9a886e0..fdf9988 100755 --- a/nagios-downtime +++ b/nagios-downtime @@ -5,17 +5,20 @@ # because it's annoying to do with the web interface for large sets of hosts # or services. # -# $ ndt <host>[/<service>] <start> <end> [optional comment] +# $ ndt (<host[/service][,host[/service],...>|-) <start> <end> [comment]\n' "$self" # -# Good for for/while loops: +# You can specify multiple objects by separating them with commas: # -# for hostname in hosta hostb hostc; do ndt "$hostname" ... ; done -# while read -r hostname; do ndt "$hostname" ... ; done < downtime-hosts +# $ ndt abc-example-ap-1,abc-example-ap-2/VOLTAGE 9am 10am 'Power problems at abc-example' +# +# Even easier is to pipe them into stdin by specifying - as the object: +# +# $ nds abc-example | ndt - 9am 10am 'Power problems at abc-example' # # Assumes date(1) with +%s format and --date option (probably only GNU date). # # Author: Tom Ryder <tom@sanctum.geek.nz> -# Copyright: 2014 Sanctum +# Copyright: 2016 # # Name self @@ -23,7 +26,7 @@ self=nagios-downtime # Usage printing function usage() { - printf 'USAGE: %s <host[/service]> <start> <end> [comment]\n' "$self" + printf 'USAGE: %s [<host[/service][,host[/service],...>|-] <start> <end> [comment]\n' "$self" } # Process options (just help at the moment) @@ -59,42 +62,65 @@ author=${SUDO_USER:-$USER} comment=${4:-'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 -# Quietly replace semicolons in comment with commas -comment=${comment//;/,} + # 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) + ;; -# Attempt to parse start and end dates; fail usefully if the call doesn't work -if ! dtstart=$(date +%s --date "$2") ; then - printf '%s: Could not parse start date %s' "$self" "$2" >&2 - exit 1 -fi -if ! dtend=$(date +%s --date "$3") ; then - printf '%s: Could not parse end date %s' "$self" "$3" >&2 + # 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 +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 -fi +done -# Write command and print message if it fails; succeed silently -if [[ $service ]] ; then - cmd=$(printf '[%lu] SCHEDULE_SVC_DOWNTIME;%s;%s;%s;%s;%u;%u;%u;%s;%s' \ - "$now" "$host" "$service" "$dtstart" "$dtend" \ - "$fixed" "$trigger" "$duration" "$author" "$comment") -else - cmd=$(printf '[%lu] SCHEDULE_HOST_DOWNTIME;%s;%s;%s;%u;%u;%u;%s;%s' \ - "$now" "$host" "$dtstart" "$dtend" \ - "$fixed" "$trigger" "$duration" "$author" "$comment") -fi +# Attempt to parse start and end dates; fail if the call doesn't work +dta=$(date +%s --date "$2") || exit +dtb=$(date +%s --date "$3") || exit -# Attempt to write command to file -if ! printf '%s\n' "$cmd" > "$cmdfile" ; then - printf '%s: Failed to write command to file\n' "$self" >&2 +# If the end time is less than right now, this is probably a mistake +dtn=$(date +%s) +if ((dtn > dtb)) ; then + printf '%s: Refusing to schedule downtime ending in the past (%s)\n' \ + "$self" "$(date -d @"$dtb" +%c)" exit 1 fi +# Quietly replace semicolons in comment with commas +comment=${comment//;/,} + +# 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] SCHEDULE_SVC_DOWNTIME;%s;%s;%s;%s;%u;%u;%u;%s;%s' \ + "$now" "$host" "$service" "$dta" "$dtb" \ + "$fixed" "$trigger" "$duration" "$author" "$comment") + ;; + *) + host=$object + cmd=$(printf '[%lu] SCHEDULE_HOST_DOWNTIME;%s;%s;%s;%u;%u;%u;%s;%s' \ + "$now" "$host" "$dta" "$dtb" \ + "$fixed" "$trigger" "$duration" "$author" "$comment") + ;; + esac + printf '%s\n' "$cmd" > "$cmdfile" || exit +done |