icinga2-checks/Other/auto-acknowledge-apt.sh

63 lines
1.6 KiB
Bash
Raw Normal View History

2024-02-25 20:52:48 -07:00
#!/bin/bash
usage() {
2024-02-25 21:08:32 -07:00
echo "Usage: $0 --api <api_url> --user <username> --password <password> [--fail]"
2024-02-25 20:52:48 -07:00
exit 1
}
2024-02-25 21:08:32 -07:00
EXIT_ON_FAILURE=false
2024-02-25 20:52:48 -07:00
while [ "$1" != "" ]; do
case $1 in
--api ) shift
api=$1
;;
--user ) shift
user=$1
;;
--password ) shift
password=$1
;;
2024-02-25 21:11:23 -07:00
--fail )
2024-02-25 21:08:32 -07:00
EXIT_ON_FAILURE=true
;;
2024-02-25 20:52:48 -07:00
* ) usage
esac
shift
done
if [ -z "$api" ] || [ -z "$user" ] || [ -z "$password" ]; then
2024-02-25 21:11:23 -07:00
echo "Missing required details."
2024-02-25 20:52:48 -07:00
usage
2024-02-28 09:54:57 -07:00
exit 1
2024-02-25 20:52:48 -07:00
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")
2024-02-25 21:01:11 -07:00
if [ "$response" == "200" ] || [ "$response" == "409" ]; then
# 409 is okay because that means it's already been acknowledged.
2024-02-25 20:52:48 -07:00
echo "All pending alerts have been acknowledged."
2024-02-27 22:08:06 -07:00
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."
2024-02-25 20:52:48 -07:00
else
echo "Failed to acknowledge the alerts. Status code: $response"
2024-02-25 21:08:32 -07:00
if $EXIT_ON_FAILURE; then
exit 1
fi
2024-02-25 20:52:48 -07:00
fi
sleep 60
done