Skip to content

Instantly share code, notes, and snippets.

@carinadigital
Last active January 25, 2024 11:26
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save carinadigital/fd2960fdccd77dbdabc849656c43a070 to your computer and use it in GitHub Desktop.
Save carinadigital/fd2960fdccd77dbdabc849656c43a070 to your computer and use it in GitHub Desktop.
Send a dummy alert to Alertmanager. Based on https://gist.github.com/cherti/61ec48deaaab7d288c9fcf17e700853a by @cherti
#!/usr/bin/env bash
name=fooAlert-$RANDOM
url='http://localhost:9093/api/v1/alerts'
bold=$(tput bold)
normal=$(tput sgr0)
generate_post_data() {
cat <<EOF
[{
"status": "$1",
"labels": {
"alertname": "${name}",
"service": "my-service",
"severity":"warning",
"instance": "${name}.example.net",
"namespace": "foo-bar",
"label_costcentre": "FOO"
},
"annotations": {
"summary": "High latency is high!"
},
"generatorURL": "http://local-example-alert/$name"
$2
$3
}]
EOF
}
echo "${bold}Firing alert ${name} ${normal}"
printf -v startsAt ',"startsAt" : "%s"' $(date --rfc-3339=seconds | sed 's/ /T/')
POSTDATA=$(generate_post_data 'firing' "${startsAt}")
curl $url --data "$POSTDATA" --trace-ascii /dev/stdout
echo -e "\n"
echo "${bold}Press enter to resolve alert ${name} ${normal}"
read
echo "${bold}Sending resolved ${normal}"
printf -v endsAt ',"endsAt" : "%s"' $(date --rfc-3339=seconds | sed 's/ /T/')
POSTDATA=$(generate_post_data 'firing' "${startsAt}" "${endsAt}")
curl $url --data "$POSTDATA" --trace-ascii /dev/stdout
echo -e "\n"
@rakafo
Copy link

rakafo commented Apr 14, 2022

very useful - thank you!

@kholisrag
Copy link

extremely useful, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment