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

58 lines
1.4 KiB
Bash
Executable File

#!/bin/bash
# Check OPNsense network traffic for a host.
usage() {
echo "Usage: $0 --api <api_url> --user <username> --password <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
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."
else
echo "Failed to acknowledge the alerts. Status code: $response"
if $EXIT_ON_FAILURE; then
exit 1
fi
fi
sleep 60
done