Advertisement
devinteske

Dashboard Backup Makefile

Aug 23rd, 2022 (edited)
2,281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Make 3.36 KB | None | 0 0
  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. #   U dashboard.meta.json
  19. #
  20. # NB: The `U' stands for `updated' (familiar to users of sccs/rcs/cvs)
  21. #
  22. # Where `dashboard' is the name of the dashboard (specifically its internal
  23. # ``slug'' name) and dashboard.meta contains the metadata properties.
  24. #
  25. # This backup script requires (sorted alphabetically):
  26. #   cat [coreutils]
  27. #   cmp [diffutils]
  28. #   curl
  29. #   jq
  30. #   make
  31. #   mv [coreutils]
  32. #   rm [coreutils]
  33. #   sh (any flavor)
  34. #   xargs [findutils]
  35. #
  36. # NB: RedHat/CentOS package listed in brackets if different than utility name.
  37. #
  38. ############################################################ CONFIGURATION
  39.  
  40. #
  41. # Grafana to backup
  42. # NB: HOST must be fully qualified
  43. # NB: Access is via HTTPS on configured PORT
  44. #
  45. HOST = ip_or_hostname
  46. PORT = 3000
  47.  
  48. #
  49. # API Key for accessing the above Grafana
  50. # NB: Required
  51. #
  52. API_KEY = unquoted_api_key_here
  53.  
  54. ############################################################ FUNCTIONS
  55.  
  56. DIE = sh -c 'fmt="$$1"; \
  57.     if [ "$$fmt" ]; then \
  58.         shift 1; \
  59.         printf "$$fmt\n" "$$@" >&2; \
  60.     fi; \
  61.     exit 1; \
  62. ' sh
  63.  
  64. API_GET = sh -c 'uri="$$1" output="$${2:-/dev/stdout}"; \
  65.     curl -sLo- -H "Authorization: Bearer $(API_KEY)" \
  66.         "https://$(HOST):$(PORT)/api/$$uri" > "$$output"; \
  67. ' sh
  68.  
  69. JSON_PUT = sh -c 'input="$$1"; \
  70.     json=$$( cat "$$input" ); \
  71.     slug=$$( echo "$$json" | jq -r .meta.slug ); \
  72.     output1=".$$slug.json"; \
  73.     output2=".$$slug.meta.json"; \
  74.     echo "$$json" | jq .dashboard > "$$output1"; \
  75.     echo "$$json" | jq .meta > "$$output2"; \
  76.     rm -f "$$input"; \
  77.     echo "$$slug"; \
  78. ' sh
  79.  
  80. ############################################################ TARGETS
  81.  
  82. all:
  83.     @set -e; \
  84.     json=$$( $(API_GET) search ); \
  85.     case "$$json" in \
  86.     "") $(DIE) "API search failed" ;; \
  87.     {*}) msgtype=$$( echo "$$json" | jq -r '.message|type' ); \
  88.         [ "$$msgtype" = "null" ] || $(DIE) "%s: %s" \
  89.             "$(HOST)" "$$( echo "$$json" | jq -r .message )"; \
  90.     esac; \
  91.     uids=$$( echo "$$json" | \
  92.         jq -r 'map(select(.type=="dash-db"))|.[].uid' ); \
  93.     [ "$$uids" ] || $(DIE) "No dashboards found"; \
  94.     echo "$$uids" | \
  95.         xargs -P99 -rn1 -I%%% $(API_GET) dashboards/uid/%%% .%%%.tmp; \
  96.     trap "rm -f .*.tmp" EXIT; \
  97.     slugs=$$( echo "$$uids" | \
  98.         xargs -P99 -rn1 -I%%% $(JSON_PUT) .%%%.tmp ); \
  99.     trap "rm -f .*.json" EXIT; \
  100.     for slug in $$slugs; do \
  101.         ! cmp -s ".$$slug.json" "$$slug.json" 2> /dev/null || \
  102.             continue; \
  103.         echo "U $$slug.json"; \
  104.         mv -f ".$$slug.json" "$$slug.json"; \
  105.         echo "U $$slug.meta.json"; \
  106.         mv -f ".$$slug.meta.json" "$$slug.meta.json"; \
  107.     done
  108.  
  109. ################################################################################
  110. # END
  111. ################################################################################
Tags: Grafana
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement