Metadata-Version: 2.4
Name: grafana-snapshots
Version: 0.3.1
Summary: A Python-based application to build Grafana snapshots using the Grafana API and grafana-client python interface
Home-page: https://github.com/grafana-toolbox/grafana-snapshots
Author: author
Author-email: jfpik78@gmail.com
Maintainer: peekjef72
Maintainer-email: jfpik78@gmail.com
License: Apache-2.0 license
Project-URL: Source, https://github.com/grafana-toolbox/grafana-snapshots-tool
Project-URL: Tracker, https://github.com/grafana-toolbox/grafana-snapshots-tool/issues
Keywords: grafana http api grafana-client grafana-api http-client grafana-utils grafana-automation grafana-toolbox grafana-snapshots
Classifier: Development Status :: 5 - Production/Stable
Classifier: Environment :: Console
Classifier: Environment :: Web Environment
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Education
Classifier: Intended Audience :: Information Technology
Classifier: Intended Audience :: Manufacturing
Classifier: Intended Audience :: Science/Research
Classifier: Intended Audience :: System Administrators
Classifier: Intended Audience :: Telecommunications Industry
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.7
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: Implementation :: CPython
Classifier: Programming Language :: Python :: Implementation :: PyPy
Classifier: Topic :: Communications
Classifier: Topic :: Database
Classifier: Topic :: Internet
Classifier: Topic :: Internet :: WWW/HTTP
Classifier: Topic :: Scientific/Engineering
Classifier: Topic :: Scientific/Engineering :: Human Machine Interfaces
Classifier: Topic :: Scientific/Engineering :: Information Analysis
Classifier: Topic :: Scientific/Engineering :: Interface Engine/Protocol Translator
Classifier: Topic :: Scientific/Engineering :: Visualization
Classifier: Topic :: Software Development
Classifier: Topic :: Software Development :: Libraries
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Topic :: System :: Archiving
Classifier: Topic :: System :: Networking :: Monitoring
Classifier: Topic :: Utilities
Classifier: Operating System :: MacOS
Classifier: Operating System :: OS Independent
Classifier: Operating System :: POSIX
Classifier: Operating System :: Unix
Requires-Python: >=3.7
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: grafana-client<6,>=5
Requires-Dist: Jinja2<3.2,>=3
Requires-Dist: python-dateutil<3
Requires-Dist: PyYAML<7,>=5
Provides-Extra: test
Requires-Dist: parameterized<1,>=0.8; extra == "test"
Requires-Dist: codecov<3; extra == "test"
Requires-Dist: coverage[toml]<8; extra == "test"
Requires-Dist: unittest-xml-reporting<4; extra == "test"
Requires-Dist: requests-mock<2; extra == "test"
Provides-Extra: develop
Requires-Dist: build<2; extra == "develop"
Requires-Dist: poethepoet<1; extra == "develop"
Requires-Dist: pip-review<2; extra == "develop"
Requires-Dist: twine<7; extra == "develop"
Dynamic: author
Dynamic: author-email
Dynamic: description
Dynamic: description-content-type
Dynamic: home-page
Dynamic: license-file
Dynamic: requires-dist
Dynamic: summary

# Grafana Snapshots

Create Grafana snapshots **including data(!)**.

The application uses the [Grafana HTTP API] and its Python client [grafana-client].

The aim of this tool is to:
1. Easily build snapshots from existing Grafana dashboard.
1. Export the snapshots to a local storage in JSON format so it can be sent, imported an visualized to a remote Grafana.
1. 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 package

Install from PyPI.
```bash
pip3 install grafana-snapshots
```

Install from repository.
```bash
pip install git+https://github.com/grafana-toolbox/grafana-snapshots.git
```

## 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**:

```bash
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':

```bash
$ 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':

```bash
$ 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

```bash
$ 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.


[grafana-client]: https://github.com/grafana-toolbox/grafana-client
[Grafana HTTP API]: https://grafana.com/docs/grafana/latest/http_api/
