grafana-cli
is a command line tool which allows you to execute actions against Grafana api server such as:
- List grafana alerts
- Pause grafana alerts
- Resume grafana alerts
Clone this repo:
git clone https://github.com/orojina/grafana-cli.git
Copy the .grafana
folder into your home directory:
cd grafana-cli
cp -r .grafana ~
cp grafana-cli.sh ~
Update the configuration files for each environment (if you have only one Grafana instance you can use the prod configuration file). The config files must follow the following format; all fields are mandatory except for the GF_EXCLUDE_ALERTS
variable which is optional.
NOTE: The image shows the following sections:
This section contains:
- The name of the Grafana environment (
dev
|qa
|prod
) - The grafana API server URL
- The authorization header to be used on every HTTP request
IMPORTANT: Make sure there is a line break between organizations, so that the confirgurations can be parsed successfully.
NOTE: Don't change the section name; keep it as [Environment]
These sections are intended to store the Grafana tokens for each Grafana Organization. In this example we are declaring 2 Grafana Organizations ORGANIZATION_A
& ORGANIZATION_B
but you can have more organizations and name them as you wish. The name you use as the section name is the name of the organization you will be using to load the environment variables using the following command in your terminal:
source ~/grafana-cli.sh config set-env dev ORGANIZATION_A
NOTE: The Grafana token must have the Grafana admin
role of that organization.
IMPORTANT:
- You should ask within your team if an API KEY already exists for your team usage.
- If you need to create a new API Token, please refer to the Grafana documentation for instructions on how to create the Grafana token. Be aware that you need to have Organization admin rights to create a new API Token.
- Make sure the token has the
admin
role attached.
grafana-cli
requires jq to be installed.
Use the following command to see a list of available commands and how to use them
~/grafana-cli.sh -h
- Load environment variables into your terminal session
source ~/grafana-cli.sh config set-env prod ORGANIZATION_A
- Get a list of Grafana alerts
~/grafana-cli.sh alerts
Pro Tip: use the following command to watch
the status of your alerts; the alert list will be refreshed every 10 secs:
watch -c -n 10 "~/grafana-cli.sh alerts"
- Pause all alerts in the Grafana Organization
~/grafana-cli.sh alerts pause
- Pause specific alerts by alertId
Use the -i
option followed by the alert IDs you want grafana-cli
to pause
~/grafana-cli.sh alerts pause -i 2,8,9
- Add annotations
Use the `-a` option followed by any text to add an annotation as part of the `pause/resume` action
~/grafana-cli.sh alerts pause -i 2,8,9 -a "Pausing alerts due maintenance activities"
- Add alerts to the exclusion list
grafana-cli
allows you to set an exclusion list, which basically tells grafana-cli
to ignore those alerts when pausing/resuming alerts.
All you need to do is to set the GF_EXCLUDE_ALERTS
variable with the alert IDs you dont want grafana-cli
to touch.
Note: Whenever you change a config file, you need to make sure to source the confige file in your terminal window, otherwise changes wont be applied.
source ~/grafana-cli.sh config set-env dev ORGANIZATION_A
~/grafana-cli.sh alerts pause
Note: To resume alerts just change the pause command from pause
to resume