Skip to content

Instantly share code, notes, and snippets.

@warlord0
Last active February 25, 2021 11:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save warlord0/cc76a7fce81d1a7d8cd304f020128790 to your computer and use it in GitHub Desktop.
Save warlord0/cc76a7fce81d1a7d8cd304f020128790 to your computer and use it in GitHub Desktop.
Icinga2 Bash Script for Downtime
#!/bin/bash
# Icinga2 Maintenance mode / Downtime
#
# https://icinga.com/docs/icinga2/latest/doc/12-icinga2-api/#icinga2-api-actions
# Program details
PROGNAME=$(basename $0)
RELEASE="Revision 1.0.0"
AUTHOR="(c) 2020 Paul Bargewell (paul.bargewell@opusvl.com)"
CURL=`which curl`
JQ=`which jq`
JO=`which jo`
if [[ -z "$CURL" ]]; then
echo "Missing curl"
exit 1
fi
# JQ for nice JSON return format
if [[ -z "$JQ" ]]; then
echo "Missing jq"
exit 1
fi
# JO for creating the JSON request data
if [[ -z "$JO" ]]; then
echo "Missing jo"
exit 1
fi
# Functions plugin usage
print_release() {
echo "$RELEASE $AUTHOR"
}
DURATION='1 hour' # Default duration
URL="https://icinga2:5665" # Default URL
STATE="down" # Default state
print_usage() {
echo ""
echo "$PROGNAME $RELEASE -Add a scheduled downtime to icinga2 host/service"
echo ""
echo "Usage: $PROGNAME"
echo ""
echo " -h | --help Show this page"
echo ""
echo " -d | --duration Downtime duration, eg. '1 hour', '15 min' (default: '1 hour'"
echo ""
echo " -H | --host Name of host to add downtime (required)"
echo ""
echo " -s | --service Name of service to add downtime"
echo ""
echo " -U | --url Icinga2 API URL"
echo ""
echo " -v | --version Version"
echo ""
echo "Usage: $PROGNAME --help"
echo ""
exit 0
}
print_help() {
print_usage
echo ""
echo "Add a scheduled downtime to icinga2 host/service"
echo ""
exit 0
}
# Parse parameters
while [ $# -gt 0 ]; do
case "$1" in
-h | --help)
print_help
exit 0
;;
-v | --version)
print_release
exit 0
;;
-H | --host)
shift
HOST=$1
;;
-s | --service)
shift
SERVICE=$1
;;
-d | --duration)
shift
DURATION=$1
;;
-c | --comment)
shift
COMMENT=$1
;;
-u | --username)
shift
USERNAME=$1
;;
-p | --password)
shift
PASSWORD=$1
;;
-U | --url)
shift
URL=$1
;;
--state)
shift
STATE=$1
;;
*)
echo "Unknown argument: $1"
print_usage
;;
esac
shift
done
if [[ -z "${HOST}" ]]; then
echo "Must specify a host"
exit 1
fi
if [[ -z "${USERNAME}" || -z "${PASSWORD}" ]]; then
echo "Must specify a username and password"
exit 1
fi
if [[ ! -z "$SERVICE" ]]; then
TYPE="Service"
SERVICE=" && service.name==\"${SERVICE}\""
else
TYPE="Host"
fi
if [[ "${STATE}" == "down" ]]; then
NOW=`date +"%Y-%m-%d %T"`
NOW_EPOCH=`date -d "$NOW" +%s`
ENDS=`date -d "${DURATION}" +"%Y-%m-%d %T"`
ENDS_EPOCH=`date -d "$ENDS" +%s`
DURATION_SECONDS=`expr $ENDS_EPOCH - $NOW_EPOCH`
if [[ -z "$COMMENT" ]]; then
COMMENT="Scheduled downtime between $NOW until $ENDS ( $DURATION )"
fi
ACTION="schedule-downtime"
JSON=$(${JO} -p author=$(id -un) comment="${COMMENT}" start_time=$(date -d "$NOW" +%s) \
end_time=$(date -d "$ENDS" +%s) duration=${DURATION_SECONDS} type=${TYPE} \
filter="host.name==\"${HOST}\"${SERVICE}" )
else
# Remove downtimes for the specified host / service
ACTION="remove-downtime"
JSON=$(${JO} -p author=$(id -un) \
type="${TYPE}" \
filter="host.name==\"${HOST}\"${SERVICE}" )
fi
${CURL} -u ${USERNAME}:${PASSWORD} \
-k --silent "${URL}/v1/actions/${ACTION}" -H "Accept: application/json" \
-d "${JSON}" -X POST | ${JQ}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment