aboutsummaryrefslogtreecommitdiff
path: root/nagios-downtime
diff options
context:
space:
mode:
Diffstat (limited to 'nagios-downtime')
-rwxr-xr-xnagios-downtime100
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