· 3 years ago · Aug 23, 2022, 03:10 PM
1############################################################ IDENT(1)
2#
3# $Title: Script to make backup of Grafana dashboards $
4# $Copyright: 2019-2022 Devin Teske. All rights reserved. $
5# $FrauBSD$
6#
7############################################################ INFORMATION
8#
9# Accesses a remote Grafana instance via HTTPS using an API key (created by an
10# administrator of said Grafana instance) to download the JSON models of all
11# dashboards, saved to the current working directory (CWD).
12#
13# Each time `make' is run, each Grafana dashboard is downloaded and compared to
14# the existing JSON model (if previously downloaded) in CWD. If the model has
15# changed, the message is printed:
16#
17# U dashboard.json
18#
19# NB: The `U' stands for `updated' (familiar to users of sccs/rcs/cvs)
20#
21# Where `dashboard' is the name of the dashboard (specifically its internal
22# ``slug'' name).
23#
24# This backup script requires (sorted alphabetically):
25# cat [coreutils]
26# cmp [diffutils]
27# curl
28# jq
29# make
30# mv [coreutils]
31# rm [coreutils]
32# sh (any flavor)
33# xargs [findutils]
34#
35# NB: RedHat/CentOS package listed in brackets if different than utility name.
36#
37############################################################ CONFIGURATION
38
39#
40# Grafana to backup
41# NB: HOST must be fully qualified
42# NB: Access is via HTTPS on configured PORT
43#
44HOST = vmon.global.voleon.net
45PORT = 443
46
47#
48# API Key for accessing the above Grafana
49# NB: Required
50#
51API_KEY = eyJrIjoiZ1N6TjQ3OExkdnZVaVI2NTZFMkRjbGxoQ0RPSlV4blYiLCJuIjoiYmFja3VwcyIsImlkIjoxfQ==
52
53############################################################ FUNCTIONS
54
55DIE = sh -c 'fmt="$$1"; \
56 if [ "$$fmt" ]; then \
57 shift 1; \
58 printf "$$fmt\n" "$$@" >&2; \
59 fi; \
60 exit 1; \
61' sh
62
63API_GET = sh -c 'uri="$$1" output="$${2:-/dev/stdout}"; \
64 curl -sLo- -H "Authorization: Bearer $(API_KEY)" \
65 "https://$(HOST):$(PORT)/api/$$uri" > "$$output"; \
66' sh
67
68JSON_PUT = sh -c 'input="$$1"; \
69 json=$$( cat "$$input" ); \
70 slug=$$( echo "$$json" | jq -r .meta.slug ); \
71 output1=".$$slug.json"; \
72 output2=".$$slug.meta.json"; \
73 echo "$$json" | jq .dashboard > "$$output1"; \
74 echo "$$json" | jq .meta > "$$output2"; \
75 rm -f "$$input"; \
76 echo "$$slug"; \
77' sh
78
79############################################################ TARGETS
80
81all:
82 @set -e; \
83 json=$$( $(API_GET) search ); \
84 case "$$json" in \
85 "") $(DIE) "API search failed" ;; \
86 {*}) msgtype=$$( echo "$$json" | jq -r '.message|type' ); \
87 [ "$$msgtype" = "null" ] || $(DIE) "%s: %s" \
88 "$(HOST)" "$$( echo "$$json" | jq -r .message )"; \
89 esac; \
90 uids=$$( echo "$$json" | \
91 jq -r 'map(select(.type=="dash-db"))|.[].uid' ); \
92 [ "$$uids" ] || $(DIE) "No dashboards found"; \
93 echo "$$uids" | \
94 xargs -P99 -rn1 -I%%% $(API_GET) dashboards/uid/%%% .%%%.tmp; \
95 trap "rm -f .*.tmp" EXIT; \
96 slugs=$$( echo "$$uids" | \
97 xargs -P99 -rn1 -I%%% $(JSON_PUT) .%%%.tmp ); \
98 trap "rm -f .*.json" EXIT; \
99 for slug in $$slugs; do \
100 ! cmp -s ".$$slug.json" "$$slug.json" 2> /dev/null || \
101 continue; \
102 echo "U $$slug.json"; \
103 mv -f ".$$slug.json" "$$slug.json"; \
104 echo "U $$slug.meta.json"; \
105 mv -f ".$$slug.meta.json" "$$slug.meta.json"; \
106 done
107
108################################################################################
109# END
110################################################################################