diff options
Diffstat (limited to 'nagios-force-check')
-rwxr-xr-x | nagios-force-check | 89 |
1 files changed, 59 insertions, 30 deletions
diff --git a/nagios-force-check b/nagios-force-check index 8c19620..ef12e35 100755 --- a/nagios-force-check +++ b/nagios-force-check @@ -4,10 +4,11 @@ # nagios-force-check(1) -- Force an immediate check of a nominated host or # service. # -# $ nfc <host>[/<service>] +# $ nac <host>[/<service>] +# # # Author: Tom Ryder <tom@sanctum.geek.nz> -# Copyright: 2014 Sanctum +# Copyright: 2016 # # Name self @@ -15,13 +16,17 @@ self=nagios-force-check # Usage printing function usage() { - printf 'USAGE: %s [-n] <host[/service]>\n' "$self" + printf 'USAGE: %s (-a | <host[/service]> [<host[/service]..])\n' "$self" } # Handle options, just -h help at the moment OPTIND=1 -while getopts 'h' opt ; do +check_all_problems=0 +while getopts 'ah' opt ; do case "$opt" in + a) + check_all_problems=1 + ;; h) usage exit 0 @@ -34,8 +39,13 @@ while getopts 'h' opt ; do done shift "$((OPTIND-1))" -# Bail if no arguments left; we need at least the host/service name -if ! (($#)) ; then +# Bail if -a was selected but there are more arguments +if ((check_all_problems)) && (($#)) ; then + usage >&2 + exit 1 + +# Bail if -a wasn't selected and there are no arguments +elif ! ((check_all_problems)) && ! (($#)) ; then usage >&2 exit 1 fi @@ -43,35 +53,54 @@ fi # Define relatively fixed/guaranteed fields for Nagios command; note that the # comment has a default of 'no comment given' now=$(date +%s) -spec=$1 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##*/} +# Specs are either arguments or all unhandled problems +declare -a specs +if ((check_all_problems)) ; then + while read -r object ; do + specs=("${specs[@]}" "$object") + done < <(nagios-problem-list) else - host=$spec - service= + specs=("$@") fi -# Write command and print message if it fails; succeed silently -declare -a cmds -if [[ $service ]] ; then - cmds=("${cmds[@]}" "$(printf '[%lu] SCHEDULE_FORCED_SVC_CHECK;%s;%s;%lu' \ - "$now" "$host" "$service" "$now")") -else - cmds=("${cmds[@]}" "$(printf '[%lu] SCHEDULE_FORCED_HOST_CHECK;%s;%lu' \ - "$now" "$host" "$now")") - cmds=("${cmds[@]}" "$(printf '[%lu] SCHEDULE_HOST_SVC_CHECKS;%s;%lu' \ - "$now" "$host" "$now")") -fi +# Iterate through the specs given and run the force recheck command +for spec in "${specs[@]}" ; do -# Attempt to write command to file -for cmd in "${cmds[@]}" ; do - if ! printf '%s\n' "$cmd" >> "$cmdfile" ; then - printf '%s: Failed to write command to file\n' "$self" >&2 - exit 1 + # Check the host or service exists + if ! nagios-exists "$spec" ; then + printf '%s: Host/service %s does not seem to exist\n' \ + "$self" "$spec" >&2 + fi + + # If a service name is specified after a slash, figure that out + if [[ $spec == */* ]] ; then + host=${spec%/*} + service=${spec##*/} + else + host=$spec + service= fi -done + # Write command and print message if it fails; succeed silently + declare -a cmds + cmds=() + if [[ $service ]] ; then + cmds=("${cmds[@]}" "$(printf '[%lu] SCHEDULE_SVC_CHECK;%s;%s;%lu' \ + "$now" "$host" "$service" "$now")") + else + cmds=("${cmds[@]}" "$(printf '[%lu] SCHEDULE_HOST_CHECK;%s;%lu' \ + "$now" "$host" "$now")") + cmds=("${cmds[@]}" "$(printf '[%lu] SCHEDULE_HOST_SVC_CHECKS;%s;%lu' \ + "$now" "$host" "$now")") + fi + + # Attempt to write commands to file + for cmd in "${cmds[@]}" ; do + if ! printf '%s\n' "$cmd" >> "$cmdfile" ; then + printf '%s: Failed to write command to file\n' "$self" >&2 + exit 1 + fi + done +done |