Created
May 30, 2018 19:47
-
-
Save groner/e3b8c93d4b088f617d625b20d3d08112 to your computer and use it in GitHub Desktop.
prometheus query helpers (uses curl and jq)
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
prom-query () { | |
local tmp=$(mktemp -t prom-query.XXXXXX) | |
curl -Ggs cakepile.local:8001/api/v1/namespaces/monitoring/services/prometheus-k8s:web/proxy/api/v1/query --data-urlencode query="$1" >$tmp | |
if jq -e '.status=="error"' <$tmp >/dev/null; then | |
jq <$tmp >&2 -r .error | |
rm -f $tmp | |
return 1 | |
fi | |
jq <$tmp '.data.result[]' | |
rm -f $tmp | |
} | |
prom-drop-labels () { | |
local jlabels=$(printf '%s\0' "$@" | jq -Rc 'split("\u0000") | [.[] | {key: ., value: 1}] | from_entries'); | |
jq --argjson labels "$jlabels" '.metric |= ([to_entries[] | select(.key | in($labels) | not)] | from_entries)' | |
} | |
prom-keep () { | |
local jlabels=$(printf '%s\0' "$@" | jq -Rc 'split("\u0000") | [.[] | {key: ., value: 1}] | from_entries'); | |
jq --argjson labels "$jlabels" '.metric |= ([to_entries[] | select(.key | in($labels))] | from_entries)' | |
} | |
prom-list () { | |
jq -r '"\(.metric | "\(.__name__//""){\(del(.__name__) | [to_entries[] | "\(.key)=\(.value | @json)"] | join(","))}")\t\(.value[0] | todate)\t\(.value[1])"' | |
} | |
prom-list-labels () { | |
jq -r '.metric | "\(.__name__//""){\(del(.__name__) | [to_entries[] | "\(.key)=\(.value | @json)"] | join(","))}"' | sort -u | |
} | |
prom-list-names () { | |
jq -r '.metric.__name__' | sort -u | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment