A Clojure script/project designed to help dealing with Grafana.
-
Copying a grafana dashboard from one instance to another instance.
-
Copying alert-rules associated with this grafana dashboard.
-
Copying library-panels associated with this grafana dashboard.
-
Adjusting library-panels where a specific target structure needs repetition with different datasources.
If you check out this repository you can use babashka.
This project contains two main-functions:
active-grafana.main-copy
active-grafana.main-adjust
With babaskha-tasks (see bb.edn or https://book.babashka.org/#tasks) you can start at the root of the project with:
active-grafana $ bb copy -h
or
active-grafana $ bb adjust -h
If this does not work try:
active-grafana $ bb -m active-grafana.main-copy -- -h
active-grafana $ bb -m active-grafana.main-adjust -- -h
The help shows variables like URL, FROM_URL or TO_MESSAGE, which can
alternatively be provided by environment variables. For example:
$ export FROM_URL=http://localhost:3000
$ export FROM_TOKEN=glsa_FcOPTbFuJ9ZO0q6AzdTSoKjVLHaxsBw5_4ece1975
active-grafana $ TO_URL=http://localhost:3001 \
TO_TOKEN=glsa_sr9e9M1JI0ARODVP347uVKm7L1wqKvGa_3543afe4 \
TO_MESSAGE="Changes to the speed-check-panel." \
bb -m active-grafana.main-copy -- \
-b -a --board-uid=b3b41ced-1237-45a1-9f63-08d8b4191c57 \
--to-board-folder-uid=afb10bf4-f0b1-4e2b-af04-1061844be119 \
--to-alerts-folder-uid=afb10bf4-f0b1-4e2b-af04-1061844be119
All variables can be found in active-grafana.settings.
If you check out this repository you can use lein.
This project contains two main-functions:
active-grafana.main-copy
active-grafana.main-adjust
To start, type at the root of the repository:
For main-copy:
active-grafana $ lein run -m active-grafana.main-copy -- -h
or
active-grafana $ lein with-profile as-copy run -- -h
For main-adjust:
active-grafana $ lein run -m active-grafana.main-adjust -- -h
or
active-grafana $ lein with-profile as-adjust run -- -h
The help shows variables like URL, FROM_URL or TO_MESSAGE, which can
alternatively be provided by environment variables. For example:
$ export FROM_URL=http://localhost:3000
$ export FROM_TOKEN=glsa_FcOPTbFuJ9ZO0q6AzdTSoKjVLHaxsBw5_4ece1975
active-grafana $ TO_URL=http://localhost:3001 \
TO_TOKEN=glsa_sr9e9M1JI0ARODVP347uVKm7L1wqKvGa_3543afe4 \
TO_MESSAGE="Changes to the speed-check-panel." \
lein run -m active-grafana.main-copy -- \
-b -a --board-uid=b3b41ced-1237-45a1-9f63-08d8b4191c57 \
--to-board-folder-uid=afb10bf4-f0b1-4e2b-af04-1061844be119 \
--to-alerts-folder-uid=afb10bf4-f0b1-4e2b-af04-1061844be119
All variables can be found in active-grafana.settings.
This project can also be used as a babashka-pod.
The pod-interface can be found in active-grafana.pod.
A usage example with docker.
Check out this repository as active-grafana.`
active-grafana $ docker build -t active-grafana -f Dockerfile-pod .
active-grafana $ docker run active-grafana
active-grafana $ docker run active-grafana copy --help
active-grafana $ docker run active-grafana adjust --help
With the help of GraalVM we can create a native image.
A usage example with docker.
Check out this repository as active-grafana.`
active-grafana $ docker build -t active-grafana-copy -f Dockerfile-standalone-copy .
active-grafana $ docker run -it active-grafana-copy sh
# ./active-grafana-copy --help
Note: The Dockerfile contains this line:
RUN wget https://github.com/active-group/active-grafana/releases/download/v0.1/active-grafana-copy.jar
Check https://github.com/active-group/active-grafana/releases for other releases.
active-grafana $ docker build -t active-grafana-adjust -f Dockerfile-standalone-adjust .
active-grafana $ docker run -it active-grafana-adjust sh
# ./active-grafana-adjust --help
Note: The Dockerfile contains this line:
RUN wget https://github.com/active-group/active-grafana/releases/download/v0.1/active-grafana-adjust.jar
Check https://github.com/active-group/active-grafana/releases for other releases.
The standard way of adjusting a library-panel is:
- get the library-panel with the given
PANEL_UID - take the first
targetas reference target - create the new target list by repeating the reference target adjusted for the
datasource-uid and the reference-id based on the provided
DATASOURCE-UIDS. - update the library panel with the newly created target list
Sometimes one or more targets need more adjustments than the datasource-uid and
the reference-id. Use the --i-am-an-expert argument to achieve more advanced
adjustments. Example:
adjust --adjust --url=<grafana-url> --token=<grafana-token> --panel-uid=<panel-uid> \
--datasource-uids="<datasource-uid-1>,<datasource-uid-2>,...,<datasource-uid-n>" \
--i-am-an-expert="{:path \"<path-to-f-target->target>\" :data <data>}"
The i-am-an-expert argument gets transformed to a map using
clojure.edn/read-string. The path is processed with (load-string (slurp path)). That is, the path should lead to a file containing a function like the
following:
(defn f-target->target
[reference-target uid data]
(assoc (assoc-in reference-target ["datasource" "uid"] uid) "refId" (get data uid)))
The data is provided to this loaded function, e.g.:
{"<datasource-uid-1>" "ref-id-1"
"<datasource-uid-2>" "ref-id-2"
"..." "..."
"<datasource-uid-n>" "ref-id-n"}
The result of this example is, that the target does not use the datasource-uid as reference-id, but a reference-id provided as {"uid" "ref-id"}-map via the command-line.
Note: make sure to escape strings in the --i-am-an-expert
command-line-argument in the proper way, e.g.:
adjust --adjust ... --i-am-an-expert="{:path \"file.path\" :data {\"uid-1\" \"my-uid-1-data\"}}"
-
The application uses the environment variable
BABASHKA_POD(as described here) to determine, whether the application should behave as a pod. -
Error handling: If something goes wrong, you are immediately provided with the plain grafana-api-error-messages. Currently, there is no fallback or further help provided.
-
Testing: There is no testing within the project.
-
Logging and Debugging: The current logging and debugging system is rudimentary and might even be confusing.
Copyright © 2023-2026 Active Group GmbH
This program and the accompanying materials are made available under the terms of the Eclipse Public License 2.0 which is available at http://www.eclipse.org/legal/epl-2.0.
This Source Code may also be made available under the following Secondary Licenses when the conditions for such availability set forth in the Eclipse Public License, v. 2.0 are satisfied: GNU General Public License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any later version, with the GNU Classpath Exception which is available at https://www.gnu.org/software/classpath/license.html.