Created
December 9, 2016 13:47
-
Star
(123)
You must be signed in to star a gist -
Fork
(44)
You must be signed in to fork a gist
-
-
Save cherti/61ec48deaaab7d288c9fcf17e700853a to your computer and use it in GitHub Desktop.
send a dummy alert to prometheus-alertmanager
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
name=$RANDOM | |
url='http://localhost:9093/api/v1/alerts' | |
echo "firing up alert $name" | |
# change url o | |
curl -XPOST $url -d "[{ | |
\"status\": \"firing\", | |
\"labels\": { | |
\"alertname\": \"$name\", | |
\"service\": \"my-service\", | |
\"severity\":\"warning\", | |
\"instance\": \"$name.example.net\" | |
}, | |
\"annotations\": { | |
\"summary\": \"High latency is high!\" | |
}, | |
\"generatorURL\": \"http://prometheus.int.example.net/<generating_expression>\" | |
}]" | |
echo "" | |
echo "press enter to resolve alert" | |
read | |
echo "sending resolve" | |
curl -XPOST $url -d "[{ | |
\"status\": \"resolved\", | |
\"labels\": { | |
\"alertname\": \"$name\", | |
\"service\": \"my-service\", | |
\"severity\":\"warning\", | |
\"instance\": \"$name.example.net\" | |
}, | |
\"annotations\": { | |
\"summary\": \"High latency is high!\" | |
}, | |
\"generatorURL\": \"http://prometheus.int.example.net/<generating_expression>\" | |
}]" | |
echo "" |
You can pass different severity as argument to check different channels, for example:
sh test.sh 'critical'
#!/bin/bash
# Set default values
name=$RANDOM
url='https://alertmanager.local/api/v1/alerts'
summary='Testing summary!'
instance="$name.example.net"
default_severity='warning'
# Function to send alert
send_alert() {
local status=$1
local custom_severity=$2
local current_severity=${custom_severity:-$default_severity}
curl -XPOST $url -d "[
{
\"status\": \"$status\",
\"labels\": {
\"alertname\": \"$name\",
\"service\": \"my-service\",
\"severity\":\"$current_severity\",
\"instance\": \"$instance\"
},
\"annotations\": {
\"summary\": \"$summary\"
},
\"generatorURL\": \"https://prometheus.local/<generating_expression>\"
}
]"
echo ""
}
# Main script
echo "Firing up alert $name"
send_alert "firing" "$1"
read -p "Press enter to resolve alert"
echo "Sending resolve"
send_alert "resolved" "$1"
You can pass different severity as argument to check different channels, for example:
sh test.sh 'critical'
#!/bin/bash # Set default values name=$RANDOM url='https://alertmanager.local/api/v1/alerts' summary='Testing summary!' instance="$name.example.net" default_severity='warning' # Function to send alert send_alert() { local status=$1 local custom_severity=$2 local current_severity=${custom_severity:-$default_severity} curl -XPOST $url -d "[ { \"status\": \"$status\", \"labels\": { \"alertname\": \"$name\", \"service\": \"my-service\", \"severity\":\"$current_severity\", \"instance\": \"$instance\" }, \"annotations\": { \"summary\": \"$summary\" }, \"generatorURL\": \"https://prometheus.local/<generating_expression>\" } ]" echo "" } # Main script echo "Firing up alert $name" send_alert "firing" "$1" read -p "Press enter to resolve alert" echo "Sending resolve" send_alert "resolved" "$1"
should change from v1
to v2
and add an header:
-H "Content-Type: application/json" \
Thanks, this is very helpful!
Minor changes were needed to work with newer versions:
- Change the API version of the alertmanager to v2.
- Add a header to the curl calls:
-H "Content-Type: application/json"
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
tks