m110 / grafcli

Grafana CLI for quick and easy dashboards management.
MIT License
168 stars 20 forks source link
backup cli dashboard grafana python

grafcli

Build Status

Grafana CLI for fast and easy dashboards management.

Please note that grafcli has been tested for a while, but it still can have some minor defects. All help by contributions and suggestions is welcomed! Remember to back up your dashboards first.

Also note that grafcli was created when grafana itself lacked some features, like exports or API. Although it's still nice to have some of those in form of a CLI application.

Credit goes to b3niup for the original idea!

Featuring:

Why?

How?

Grafcli connects to Grafana HTTP API or directly to one of the backends (Elastic, SQLite, MySQL, PostgreSQL) and modifies the dashboards. This is all hidden behind an interface you already know well, similar to *nix filesystem.

Requirements

Installation

pip3 install grafcli

or get the source and run:

python3 setup.py install

Then define your hosts in the config file (see below for details).

cp /etc/grafcli/grafcli.conf.example /etc/grafcli/grafcli.conf

You will need at least one of the backend libraries listed above (except sqlite3, which comes with Python).

TODO

Usage

Navigation

Use cd and ls commands to list nodes and move around.

[/]> cd templates
[/templates]> ls
dashboards
rows
panels
[/templates]> ls dashboards
example_dashboard
another_dashboard
[/templates]> ls dashboards/example_dashboard
1-Example-Row
2-Another-Row
3-Yet-Another-Row
[/templates]> ls dashboards/example_dashboard/1-Example-Row
1-Example-Panel
2-Another-Panel

Directories

In the root directory, you will find three basic directories:

Management

Most of the arguments here are paths to a dashboard, row or panel.

Configuration

Grafcli will attempt to read ./grafcli.conf, ~/.grafcli.conf and /etc/grafcli/grafcli.conf in that order.

Here is the configuration file explained.

[grafcli]
# Your favorite editor - this name will act as a command!
editor = vim
# Executable used as merge tool. Paths will be passed as arguments.
mergetool = vimdiff
# Commands history file. Leave empty to disable.
history = ~/.grafcli_history
# Additional verbosity, if needed.
verbose = off
# Answer 'yes' to all overwrite prompts.
force = on

[resources]
# Directory where all local data will be stored (including backups).
data-dir = ~/.grafcli

# List of remote Grafana hosts.
# The key names do not matter, as long as matching section exists.
# Set the value to off to disable the host.
[hosts]
host.example.com = on

[host.example.com]
type = elastic
# In case of more hosts, use comma-separated values.
hosts = host1.example.com,host2.example.com
port = 9200
index = grafana-dash
ssl = off
# HTTP user and password, if any.
user =
password =

You can use other backends as well.

HTTP API:

[api.example.com]
type = api
url = http://localhost:3000/api
# Use either user and password or just the token
user =
password =
# token can also be stored in the GRAFANA_API_TOKEN environment variable
token =

MySQL:

[mysql.example.com]
type = mysql
host = mysql.example.com
port = 3306
user = grafana
password =
database = grafana

PostgreSQL:

[postgresql.example.com]
type = postgresql
host = postgresql.example.com
port = 5432
user = grafana
password =
database = grafana

SQLite:

[sqlite.example.com]
type = sqlite
path = /opt/grafana/data/grafana.db

Tips

Batch mode

Any command can be passed directly as arguments to grafcli, which will exit just after executing it. If you run it without arguments, you will get to interactive mode (preferable choice in most cases).

Batch mode:

$ grafcli ls remote
host.example.com
another.example.com
$ 

Interactive mode:

$ grafcli
[/]> ls remote
host.example.com
another.example.com
[/]> 

Short names

All rows and panels names start with a number and it may seem that typing all that stuff gets boring soon. There are completions available (triggered by the TAB key) to help you with that.

It is enough to provide just the number of the row or panel. So instead of typing:

[/]> cp /templates/dashboards/dashboard/1-Top-Row/1-Top-Panel /remote/example/dashboard/1-Top-Row

You can just do:

[/]> cp /templates/dashboards/dashboard/1/1 /remote/example/dashboard/1

But then again, TAB-completions make it easy enough to type full names.

Examples

Some of the common operations.

[/]> template remote/example/main_dashboard
[/templates/dashboards]> cp main_dashboard new_dashboard
[/]> cp templates/dashboards/new_dashboard remote/main_dashboard
[/templates/dashboards]> cp main_dashboard/1-Top-Row new_dashboard
[/templates/dashboards]> cp main_dashboard/1-Top-Row new_dashboard/2-Some-Existing-Row
[/templates/dashboards]> cp main_dashboard/1-Top-Row/1-Top-Panel new_dashboard/1-Top-Row
[/templates/dashboards]> cp main_dashboard/1-Top-Row/1-Top-Panel new_dashboard/1-Top-Row/2-Second-Panel
[/] backup remote/example ~/backup.tgz
[/] restore ~/backup.tgz remote/example
[/]> import ~/dashboard.json templates/dashboards/dashboard
[/]> export templates/dashboards/dashboard ~/dashboard.json

Development

Running tests

Unit tests can be run by:

make unittests

To run integration tests:

make integration

The first run of integration tests can take a bit longer, since images will be built and downloaded.