Skip to content

Instantly share code, notes, and snippets.

@justinledwards
Created June 24, 2025 20:40
Show Gist options
  • Save justinledwards/40cc7375ae0113f0691334b8c9341b24 to your computer and use it in GitHub Desktop.
Save justinledwards/40cc7375ae0113f0691334b8c9341b24 to your computer and use it in GitHub Desktop.
Prometheus Alertmanager Test (Dadjoke Version)
#!/bin/bash
# This script fetches a random dad joke and then sends it as a
# test alert to Alertmanager.
echo "Fetching a random dad joke from icanhazdadjoke.com..."
# Fetch the joke as plain text and store it in a variable.
# The User-Agent is requested by the API's documentation.
JOKE=$(curl -s -H "Accept: text/plain" -H "User-Agent: Prometheus Alert Test (https://example.com)" https://icanhazdadjoke.com/)
# Check if the joke was fetched successfully
if [ -z "$JOKE" ]; then
echo "Failed to fetch a joke. Please check your internet connection."
exit 1
fi
echo "Joke fetched: $JOKE"
echo "Sending test alert to Alertmanager..."
# Construct the JSON payload using a heredoc for readability.
# The ${JOKE} variable is embedded directly into the description.
PAYLOAD=$(cat <<EOF
[
{
"startsAt": "$(date -u +"%Y-%m-%dT%H:%M:%SZ")",
"annotations": {
"summary": "This is not a drill... It's a dad joke.",
"description": "${JOKE}"
},
"labels": {
"alertname": "DadJokeEmergency",
"instance": "comedy-central",
"job": "joke-delivery-service",
"severity": "none"
}
}
]
EOF
)
# Send the final payload to Alertmanager
curl -X POST -H "Content-Type: application/json" http://localhost:9093/api/v2/alerts -d "${PAYLOAD}"
echo -e "\nTest alert sent!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment