Created
November 26, 2020 14:16
-
-
Save LukeChannings/c4537bbcfced8c822916b6efa51d737c to your computer and use it in GitHub Desktop.
scriptable-prometheus-example.js
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
const PROMETHEUS_ENDPOINT = 'prometheus.example.com' | |
const fetchMetric = async (query) => { | |
const url = `https://${PROMETHEUS_ENDPOINT}/api/v1/query?query=${encodeURIComponent(query)}` | |
const req = new Request(url) | |
return req.loadJSON() | |
} | |
const getPVCAvailability = (pvc) => { | |
return fetchMetric(`kubelet_volume_stats_available_bytes{persistentvolumeclaim="${pvc}"}`) | |
} | |
const filmsPvc = await getPVCAvailability('films') | |
let size = '' | |
if (Array.isArray(filmsPvc?.data?.result)) { | |
const [, bytes] = filmsPvc.data.result[0]?.value ?? [] | |
size = `${(bytes / 1000 ** 3).toFixed(2)} GB` | |
} else { | |
size = 'ERROR' | |
} | |
let listWidget = new ListWidget() | |
const valueText = listWidget.addText(size) | |
valueText.centerAlignText() | |
valueText.textColor = Color.dynamic(new Color('#333'), new Color('#fff')) | |
valueText.font = new Font('HelveticaNeue-Bold', 22) | |
listWidget.addSpacer(5) | |
const titleText = listWidget.addText("Free (Suplex)") | |
titleText.centerAlignText() | |
titleText.textColor = Color.dynamic(new Color('#aaa'), new Color('#c9c9c9')) | |
titleText.font = new Font('HelveticaNeue-Bold', 14) | |
Script.setWidget(listWidget) | |
Script.complete() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment