Last active
February 2, 2024 15:17
-
-
Save OddBloke/022b2d76c0c32a941d92cb1a68535585 to your computer and use it in GitHub Desktop.
big-graf: use grafterm to display a single full-terminal Prometheus graph
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
#!/bin/sh -eu | |
write() { | |
_TARGET=$1 | |
_ADDRESS="${BIG_GRAF_PROMETHEUS_ADDRESS:-"http://localhost:9090"}" | |
_TITLE="${GRAPH_TITLE:-""}" | |
_QUERY="$(echo "${GRAPH_QUERY:-"up"}" | sed 's,",\\",g')" | |
_LEGEND="${GRAPH_LEGEND:-"{{ .instance }}"}" | |
_UNIT="${GRAPH_UNIT:-""}" | |
_DECIMALS="${GRAPH_DECIMALS:-0}" | |
cat << EOT > "$_TARGET" | |
{ | |
"version": "v1", | |
"datasources": { | |
"ds": { | |
"prometheus": { | |
"address": "${_ADDRESS}" | |
} | |
} | |
}, | |
"dashboard": { | |
"widgets": [ | |
{ | |
"title": "${_TITLE}", | |
"gridPos": { "w": 100 }, | |
"graph": { | |
"queries": [ | |
{ | |
"datasourceID": "ds", | |
"expr": "${_QUERY}", | |
"legend": "${_LEGEND}" | |
} | |
], | |
"visualization": { | |
"yAxis": { "unit": "${_UNIT}", "decimals": ${_DECIMALS} } | |
} | |
} | |
} | |
] | |
} | |
} | |
EOT | |
} | |
help() { | |
cat << EOH | |
$0: generate a grafterm configuration and invoke grafterm with it | |
It reads the following environment variables: | |
BIG_GRAF_PROMETHEUS_ADDRESS | |
The Prometheus instance to query (default: http://localhost:9090) | |
GRAPH_TITLE | |
The title of the graph (default: no title) | |
GRAPH_QUERY | |
The query to execute against the Prometheus instance (default: "up") | |
GRAPH_LEGEND | |
The legend to use for the graph, uses Go's templating language against | |
the query result (default: "{{ .instance }}") | |
GRAPH_UNIT | |
The unit to use for the Y-axis of the graph, see grafterm's documentation | |
for the options (default: no units) | |
GRAPH_DECIMALS | |
The number of decimals for the Y-axis of the graph (default: 0) | |
Any arguments are passed through to grafterm itself: see 'grafterm --help' | |
for available options, including those related to query time range. | |
EOH | |
} | |
main() { | |
tmpfile="$(mktemp)" | |
cleanup() { | |
[ -e "$tmpfile" ] && rm "$tmpfile" | |
} | |
trap cleanup EXIT | |
write "$tmpfile" | |
grafterm -c "$tmpfile" $@ | |
} | |
case "$0 $@" in | |
*" -h"*|*" --help"*) | |
help | |
exit 0 | |
;; | |
esac | |
main $@ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment