Created
October 31, 2016 03:49
-
-
Save gnilchee/f392531c52546d4346910c6c4da9b8c1 to your computer and use it in GitHub Desktop.
a simple check to register passing, warn, critical, with consul using http api
This file contains hidden or 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
#!/usr/bin/env bash | |
# Assign service variables | |
CONSUL_URL= | |
CHECK_ENDPOINT= | |
CONSUL_DC="dc1" | |
NODE_NAME="$(hostname -f)" | |
NODE_ADDRESS="$(hostname -I| awk '{print $1}')" | |
SERVICE_ID="Redis1" | |
SERVICE_NAME="Redis" | |
SERVICE_PORT=6379 | |
CHECK_ID="service:$SERVICE_NAME" | |
CHECK_NAME="Redis health check" | |
CHECK_NOTES="Script based health check" | |
# Actual Check | |
if [ $(curl -sI $CHECK_ENDPOINT| head -1 | awk '{print $2}') -eq 200 ]; then | |
CHECK_RESULT="passing" | |
else | |
CHECK_RESULT="critical" | |
fi | |
# Payload | |
read -r -d '' CONSUL_JSON <<EOF | |
{ | |
"Datacenter": "$CONSUL_DC", | |
"Node": "$NODE_NAME", | |
"Address": "$NODE_ADDRESS", | |
"TaggedAddresses": { | |
"lan": "$NODE_ADDRESS" | |
}, | |
"Service": { | |
"ID": "$SERVICE_ID", | |
"Service": "$SERVICE_NAME", | |
"Tags": [ | |
"master", | |
"v1" | |
], | |
"Address": "$NODE_ADDRESS", | |
"Port": $SERVICE_PORT | |
}, | |
"Check": { | |
"Node": "$NODE_NAME", | |
"CheckID": "$CHECK_ID", | |
"Name": "$CHECK_NAME", | |
"Notes": "$CHECK_NOTES", | |
"Status": "$CHECK_RESULT", | |
"ServiceID": "$SERVICE_ID" | |
} | |
} | |
EOF | |
# Notify Consul | |
curl -X PUT -d "$CONSUL_JSON" http://$CONSUL_URL/v1/catalog/register |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment