#!/bin/bash usage() { echo "Usage: $0 --api --user --password [--fail]" exit 1 } EXIT_ON_FAILURE=false while [ "$1" != "" ]; do case $1 in --api ) shift api=$1 ;; --user ) shift user=$1 ;; --password ) shift password=$1 ;; --fail ) EXIT_ON_FAILURE=true ;; * ) usage esac shift done if [ -z "$api" ] || [ -z "$user" ] || [ -z "$password" ]; then echo "Missing required details." usage exit 1 fi # Acknowledge all services that meet this filter. payload='{ "type": "Service", "filter": "service.name == \"apt\" && service.acknowledgement == 0", "author": "Auto-Acknowledgement Script", "comment": "This alert has been automatically acknowledged.", "notify": true, "pretty": true }' while true; do response=$(curl -s -o /dev/null -w "%{http_code}" -X POST -H "Accept: application/json" -u "$user:$password" -d "$payload" -k "$api/v1/actions/acknowledge-problem") if [ "$response" == "200" ] || [ "$response" == "409" ]; then # 409 is okay because that means it's already been acknowledged. echo "All pending alerts have been acknowledged." elif [ "$response" == "000" ]; then echo "ERROR: failed to reach Icinga2 server. Sleeping 60s..." sleep 60 elif [ "$response" == "404" ]; then echo "Icinga reported no services to acknowledge." else echo "Failed to acknowledge the alerts. Status code: $response" if $EXIT_ON_FAILURE; then exit 1 fi fi sleep 60 done