Skip to content

Instantly share code, notes, and snippets.

@literalplus
Last active July 4, 2019 14:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save literalplus/b672bfd798844d72b1c035272d8c3336 to your computer and use it in GitHub Desktop.
Save literalplus/b672bfd798844d72b1c035272d8c3336 to your computer and use it in GitHub Desktop.
Collection of Bash scripts to simplify publishing annotations to Grafana.

Grafana Annotations via Bash scripts

Ever wanted to annotate Grafana panels with when your backups are executed or whatever? Have some possibly useful scripts. These create region annotations, so you're supposed to call the script again with the update operation to set the end time. If you don't, they will be zero-length, which does look a little weird in the interface. Alternatively, you can just change isRegion to false. Note that it really does need the full annotation JSON to update because Grafana replaces the document entirely. If we were to only send the new end time, all other info (description, etc.) would be deleted.

Also, you need to have jq installed for this to work.

License

This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.

In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to <http://unlicense.org>
#!/bin/bash
if [ "$#" -ne 4 ]; then
echo "$0 dashboardId panelId tag text" >&2
exit 2
fi
DASHBOARD_ID="$1"
PANEL_ID="$2"
TAG="$3"
TEXT="$4"
END_ID="$5"
CURRENT_TIME="$(date +%s)"
CURRENT_TS="$CURRENT_TIME""000"
BODY=$(cat <<EOF
{
"dashboardId": $DASHBOARD_ID,
"panelId": $PANEL_ID,
"isRegion": true,
"tags": ["$TAG"],
"text": "$TEXT",
"time": $CURRENT_TS,
"timeEnd": $CURRENT_TS
}
EOF
)
echo "$BODY"
#!/bin/bash
# Get API tokens from the Grafana admin page at..."API Tokens"
# You probably want to make this script readable to root only or something
AUTH="Authorization: Bearer INSERT API TOKEN HERE"
GRAFANA_BASE="https://grafana.example.com"
CURRENT_TIME="$(date +%s)"
CURRENT_TS="$CURRENT_TIME""000"
case $1 in
new)
if [ "$#" -ne 2 ]; then
echo "$0 $1 annotationSpec" >&2
exit 2
fi
BODY="$2"
URL="$GRAFANA_BASE""/api/annotations"
curl -H "$AUTH" -H "Content-Type: application/json" -XPOST -d "$BODY" "$URL"
;;
update)
if [ "$#" -ne 3 ]; then
echo "$0 $1 id annotationSpec" >&2
exit 2
fi
BODY_RAW="$3"
ANN_ID="$2"
URL="$GRAFANA_BASE""/api/annotations/""$ANN_ID"
BODY=$(echo "$BODY_RAW" | jq ". += {timeEnd: $CURRENT_TS}")
curl -H "$AUTH" -H "Content-Type: application/json" -XPUT -d "$BODY" "$URL"
;;
*)
echo "Invalid operation, try new or update." >&2
exit 2
esac
#!/bin/bash
ANN=$(grafana-make-annotation 2 1 test "My awesome bash script that takes time to execute")
ANN_ID=$(grafana-event new "$ANN" 2>/dev/null | jq ".id")
sleep 500s # or whatever you want to do
grafana-event update $ANN_ID "$ANN" >/dev/null 2>&1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment