Tool to help you manage your Grafana dashboards using Git.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. package main
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "flag"
  6. "fmt"
  7. "io/ioutil"
  8. "os"
  9. "git"
  10. "grafana"
  11. )
  12. type diffVersion struct {
  13. oldVersion int
  14. newVersion int
  15. }
  16. var (
  17. grafanaURL = flag.String("grafana-url", "", "Base URL of the Grafana instance")
  18. grafanaAPIKey = flag.String("api-key", "", "API key to use in authenticated requests")
  19. clonePath = flag.String("clone-path", "/tmp/grafana-dashboards", "Path to directory where the repo will be cloned")
  20. repoURL = flag.String("git-repo", "", "SSH URL for the Git repository, without the user part")
  21. privateKeyPath = flag.String("private-key", "", "Path to the private key used to talk with the Git remote")
  22. )
  23. func main() {
  24. dv := make(map[string]diffVersion)
  25. flag.Parse()
  26. if *grafanaURL == "" {
  27. println("Error: No Grafana URL provided")
  28. flag.Usage()
  29. os.Exit(1)
  30. }
  31. if *grafanaAPIKey == "" {
  32. println("Error: No Grafana API key provided")
  33. flag.Usage()
  34. os.Exit(1)
  35. }
  36. if *repoURL == "" {
  37. println("Error: No Git repository provided")
  38. flag.Usage()
  39. os.Exit(1)
  40. }
  41. if *privateKeyPath == "" {
  42. println("Error: No private key provided")
  43. flag.Usage()
  44. os.Exit(1)
  45. }
  46. dbVersions, err := getDashboardsVersions()
  47. if err != nil {
  48. panic(err)
  49. }
  50. repo, err := git.Sync(
  51. *repoURL,
  52. *clonePath,
  53. *privateKeyPath,
  54. )
  55. if err != nil {
  56. panic(err)
  57. }
  58. w, err := repo.Worktree()
  59. if err != nil {
  60. panic(err)
  61. }
  62. client := grafana.NewClient(*grafanaURL, *grafanaAPIKey)
  63. uris, err := client.GetDashboardsURIs()
  64. if err != nil {
  65. panic(err)
  66. }
  67. for _, uri := range uris {
  68. dashboard, err := client.GetDashboard(uri)
  69. if err != nil {
  70. panic(err)
  71. }
  72. version, ok := dbVersions[dashboard.Slug]
  73. if !ok || dashboard.Version > version {
  74. slugExt := dashboard.Slug + ".json"
  75. if err = rewriteFile(
  76. *clonePath+"/"+slugExt,
  77. dashboard.RawJSON,
  78. ); err != nil {
  79. panic(err)
  80. }
  81. if _, err = w.Add(slugExt); err != nil {
  82. panic(err)
  83. }
  84. dv[dashboard.Slug] = diffVersion{
  85. oldVersion: version,
  86. newVersion: dashboard.Version,
  87. }
  88. }
  89. }
  90. status, err := w.Status()
  91. if err != nil {
  92. panic(err)
  93. }
  94. if !status.IsClean() {
  95. if err = writeVersions(dbVersions, dv); err != nil {
  96. panic(err)
  97. }
  98. if _, err = w.Add("versions.json"); err != nil {
  99. panic(err)
  100. }
  101. if _, err = git.Commit(getCommitMessage(dv), w); err != nil {
  102. panic(err)
  103. }
  104. }
  105. if err = git.Push(repo, *privateKeyPath); err != nil {
  106. panic(err)
  107. }
  108. }
  109. func rewriteFile(filename string, content []byte) error {
  110. if err := os.Remove(filename); err != nil {
  111. pe, ok := err.(*os.PathError)
  112. if !ok || pe.Err.Error() != "no such file or directory" {
  113. return err
  114. }
  115. }
  116. indentedContent, err := indent(content)
  117. if err != nil {
  118. return err
  119. }
  120. return ioutil.WriteFile(filename, indentedContent, 0644)
  121. }
  122. func indent(srcJSON []byte) (indentedJSON []byte, err error) {
  123. buf := bytes.NewBuffer(nil)
  124. if err = json.Indent(buf, srcJSON, "", "\t"); err != nil {
  125. return
  126. }
  127. indentedJSON, err = ioutil.ReadAll(buf)
  128. return
  129. }
  130. func getCommitMessage(dv map[string]diffVersion) string {
  131. message := "Updated dashboards\n"
  132. for slug, diff := range dv {
  133. message += fmt.Sprintf("%s: %d => %d\n", slug, diff.oldVersion, diff.newVersion)
  134. }
  135. return message
  136. }