packages/graf: init

This commit is contained in:
Max Headroom 2023-05-31 00:30:29 +02:00
parent 6624e2b7c7
commit 8696bf632e
3 changed files with 50 additions and 0 deletions

View file

@ -50,6 +50,8 @@
inherit (config.dream2nix.outputs.excalidraw.packages) excalidraw;
in excalidraw // { webroot = "${excalidraw}/${excalidraw.webPath}"; };
graf = pkgs.callPackage ./tools/graf { };
grafana = pkgs.callPackage ./monitoring/grafana { };
ipfs = pkgs.callPackage ./networking/ipfs { };

View file

@ -0,0 +1,14 @@
{
writeShellApplication,
curl, gum, jq
}:
writeShellApplication {
name = "graf";
runtimeInputs = [
curl
gum
jq
];
text = builtins.readFile ./graf.sh;
}

View file

@ -0,0 +1,34 @@
export GRAFANA_HOST="${GRAFANA_HOST:-https://monitoring.privatevoid.net}"
export GRAF_TOKEN_FILE="${GRAF_TOKEN_FILE:-$HOME/.config/graf/token}"
export GRAF_DASHBOARDS_DIR="$REPO_ROOT/cluster/services/monitoring/provisioning/objects/dashboards"
api() {
reqpath="$1"
shift
gum spin --title="Querying Grafana API..." --show-output -- curl -H @"$GRAF_TOKEN_FILE" -sSL --fail-with-body "$GRAFANA_HOST/api$reqpath" "$@"
}
cmd_export() {
dashboardUid="$(api '/search?type=dash-db' | jq -r '.[] | "\(.uid) | \(.title)"' | gum choose | cut -d " " -f1)"
dashboardLoc="$GRAF_DASHBOARDS_DIR/dashboard-$dashboardUid.json"
api "/dashboards/uid/$dashboardUid" | jq '{ dashboard: .dashboard, folderId: .meta.folderId, overwrite: true }' > "$dashboardLoc"
gum format "## Successfully exported dashboard to *$(realpath --relative-to="$REPO_ROOT" "${dashboardLoc}")*"
}
cmd_import() {
dashboardUid="$(cat cluster/services/monitoring/dashboards/dashboard-*.json | jq -r '"\(.dashboard.uid) | \(.dashboard.title)"' | gum choose | cut -d " " -f1)"
dashboardLoc="$GRAF_DASHBOARDS_DIR/dashboard-$dashboardUid.json"
dashboardUrl="$(api /dashboards/import -H "Content-Type: application/json" --data @"$dashboardLoc" | jq -r .importedUrl)"
gum format "## Successfully imported dashboard to ${GRAFANA_HOST}${dashboardUrl}"
}
cmd="$1"
shift
case $cmd in
export)
cmd_export "$@";;
import)
cmd_import "$@";;
*)
echo Unknown command: "$cmd";;
esac