Skip to main content

A Python-based application to build Grafana snapshots using the Grafana API and grafana-client python interface

Project description

Grafana Snapshots Tool

A python3 bases application to build grafana snapshots that contains data(!) using Grafana API and a python interface grafana-client

The aim of this tool is to:

  1. Easily build snapshots from existing Grafana dashboard.
  2. Export the snapshots to a local storage in JSON format so it can be sent, imported an visualized to a remote Grafana.
  3. Import a snapshot in JSON format to a Grafana.

The development of this tool began when we discovered that there was no solution to automate the creation of snapshots from Grafana; only the functionality from the GUI was operational. That was a sticking point for us since it prevented us from being able to provide reports other than mannually build dashboards with statics screenshots. With this tool, we are able to build static dashboards, also called snapshots, that can be shared and visualized in grafana. It can be also used to store particular situation even if the data window is out of the scope of the retention of the datasource.

Install using this repo

pip install git+https://github.com/peekjef72/grafana-snapshots-tool.git

Install pypi package

install from pypi

pip3 install grafana-snapshots-tool 

Requirements:

  • bash
  • python >3.6
  • python modules:
    • python_dateutil
    • setuptools
    • PyYaml
    • jinja2
    • grafana-client 2.0.0 what will pull the dependencies
      • requests
      • idna
      • urllib3
      • certifi
      • chardet
  • Access to a Grafana API server.
  • A Token of an Admin role (grafana APIKey).

Configuration

The configuration is stored in a YAML file.

It contains 3 parts:

  • general: for script env.
    • debug: enable verbose (debug) trace (for dev only...)
    • snapshot_suffix: when generating or exporting a dashboard to snapshot, append that suffix to the snapshot name or file name. The suffix can contain plain text and pattern that is translated with strftime command.
    • output_path: where to store the exported snapshots.
  • grafana: for grafana access settings
    • label: a label to refer a grafana server. There must be at least 'default' label. It is used to reference several Grafana server so you can generate from one and to import to another using different 'label'.
      • protocol, host, port: use to build the access url
      • verify_ssl: to check ssl certificate or not
      • token: APIKEY with admin right from Grafana to access the REST API.
      • search_api_limit: the maximum element to retrieve on search over API.
  • context: to define default values for dashboards, time_from, time_to and values for variables that the data exposed in the dashboard are depending from. It is an object. Add a sub-object identified by the dashboard name, for each dashboard you want to add default value. Each object can contain:
    • time_from and time_to
    • an object called vars: the definitions of each variable with it's corresponding value.

Usages

build a directory structure:

  • grafana-snapshosts/
    • conf/grafana-snapshots.json where your main configuration file is
    • snapshots/ where your exported snapshots will be stored.

usage:

grafana-snapshots -h
usage: grafana-snapshots [-h] [-b BASE_PATH] [-c CONFIG_FILE]
                         [-d DASHBOARD_NAME] [-F GRAFANA_FOLDER]
                         [-f TIME_FROM] [-g GRAFANA_LABEL] [-i IMPORT_FILE]
                         [-o CONTEXT_NAME] [-p] [-s SNAPSHOT_NAME]
                         [-t TIME_TO] [-v] [-V]
                         [ACTION]

then enter into your directory and type in you commands.

Example:

  • generate a snapshot for the dashboard 'My dashboard' with default values for 'from' (now - 5 min) to 'now':
$ grafana-snapshots -d "My dashboard"
OK: new snapshot 'My_dashboard_202010241750.json' created.

then you can go into Grafana Gui and find the snapshot in dashboard/Manage/Snapshots part.

  • export the dashboard 'My dashboard' with data from: 'now-1d' to 'now':
$ grafana-snapshots -d "My dashboard" -f "now-1d" export
OK: snapshot exported to './snapshots/My_dashboard_202010241750.json' exported.

then you can find the created file in the "snapshots" dir.

  • import the file './snapshots/My_dashboard_202010241750.json' into Grafana
$ grafana-snapshots -f './snapshots/My_dashboard_202010241750.json' import
OK: snapshot './snapshots/My_dashboard_202010241750.json' imported.

then you can go into Grafana Gui and find the snapshot in dashboard/Manage/Snapshots part.

TODO - Known Limitations:

  • actual snapshots can only contain a single set of variables/values.
  • currently snapshots for Table doesn't work.
  • repeat is not supported
  • overrides are not used to modify values !
  • changes with new version of Grafana (>7.5) have several impacts on the rendering of panels; a code review is required to improve the result.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

grafana-snapshots-0.2.1.tar.gz (21.6 kB view details)

Uploaded Source

Built Distribution

grafana_snapshots-0.2.1-py3-none-any.whl (23.8 kB view details)

Uploaded Python 3

File details

Details for the file grafana-snapshots-0.2.1.tar.gz.

File metadata

  • Download URL: grafana-snapshots-0.2.1.tar.gz
  • Upload date:
  • Size: 21.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.8.3 keyring/23.4.1 rfc3986/1.5.0 colorama/0.4.4 CPython/3.6.8

File hashes

Hashes for grafana-snapshots-0.2.1.tar.gz
Algorithm Hash digest
SHA256 b50a8ccceb6dacae3494d03e8ad434b4ac2c73b22a6af31fb880301378c0e657
MD5 305142d948e1e095fbb1f5e334011f91
BLAKE2b-256 75d2d51f2e3910f6b22b89957962e460df370c3a5fcb9f948b533f8ea00bc9a9

See more details on using hashes here.

File details

Details for the file grafana_snapshots-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: grafana_snapshots-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 23.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.8.3 keyring/23.4.1 rfc3986/1.5.0 colorama/0.4.4 CPython/3.6.8

File hashes

Hashes for grafana_snapshots-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a75c019e13675a43227e866500a7f8e470dc4907970ac077a1e0595521e7a4d4
MD5 bc5c22d16cec2ad11fc3b062a1190adb
BLAKE2b-256 06e44639c1b79adaeeb8661a2b096f8d874a3e0afe9096ddedd3695c56c7eb8e

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page