aboutsummaryrefslogblamecommitdiff
path: root/nagios-acknowledge
blob: 51fdce81998d2e8b4e3e1c8dd05cce3c7556b59c (plain) (tree)































                                                                              
                 




























                                                                             

































                                                                       
#!/usr/bin/env bash

#
# nagios-acknowledge(1) -- Shortcut to acknowledge problems in Nagios, because
# it's annoying to do with the web interface for large sets of hosts or
# services.
#
#     $ nac <host>[/<service>] [optional comment]
#
# Good for for/while loops:
#
#   for hostname in hosta hostb hostc; do nac "$hostname" ... ; done
#   while read -r hostname; do nac "$hostname" ... ; done < downtime-hostnames
#
# By default, does not send ACKNOWLEDGEMENT notifications. Use -n if you do
# want that.
#
# Author: Tom Ryder <tom@sanctum.geek.nz>
# Copyright: 2014 Sanctum
#

# Name self
self=nagios-acknowledge

# Usage printing function
usage() {
    printf 'USAGE: %s [-n] <host[/service]> [comment]\n' "$self"
}

# Default to not notifying
notify=0

# Process options
OPTIND=1
while getopts 'hn' opt ; do
    case "$opt" in
        n)
            notify=1
            ;;
        h)
            usage
            exit 0
            ;;
        '?')
            usage >&2
            exit 1
            ;;
    esac
done
shift "$((OPTIND-1))"

# Bail if no arguments left; we need at least the host/service name
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)
spec=$1
sticky=1
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

# 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

# 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