Skip to main content

Grafana dashboards as Python code

Project description

Description

This module aims to generate Grafana dashboards from Python scripts. The module is tested with Grafana version 9. Older versions of Grafana might have another API and another JSON structure and are thus unsupported.

Use Case

According to their website, Grafana is a web-based application to query, visualize, alert on, and understand your data no matter where it’s stored. With Grafana you can create, explore, and share all of your data through beautiful, flexible dashboards.

The configuration is done through a GUI, which is very user friendly. Developing a dashboard can be done via trial and error or on progressive insight. The configurations are stored in a Sqlite3 database. There is a basic version control and the dashboards and panels are portable via json. The json of a dashboard or a panel can be inspected via the GUI. Grafana has a web API that can be used to interact with the installation. Also provisioning can be used. Then the generated config files will have to be stored at the appropriate location.

But, sometimes we want more…

  • a robust configuration that can be backup-ed

    • The database can get corruption.

    • There can arise issues during upgrades.

  • manageable via GIT

  • roll-out dashboards and panels over different installations

  • roll-out changes easily over all panels and dashboards

    • change the foreground color over all panels and all dashboards

    • change a data source over all panels and all dashboards

    • add a timezone offset in all queries

  • have a unified look and feel

  • have a compact notation (json files are very verbose and lengthy)

Solution: Config in code

  • Grafana configuration in Python

    • set of Python dataclasses

    • supporting multiple panel types

    • supporting multiple data sources

    • supporting other configurations (alarming, …)

  • User functions

    • implement some communication via the Grafana API

    • download a dashboard from a Grafana installation as json

    • upload a dashboard to a Grafana installation as json

    • convert a python script to uploadable json

    • convert a json to a python script

the grafanacode tool

As already said, the configuration will be done in Python. Although this is all relative simple code, some knowledge of python will be useful. We will use Python 3 with some additional libraries. Under the hood we make extensive use of the attrs library. If you install it via pip (see below), these dependencies will be installed together with the tool,

Usage

This module contains a number of functionalities:

  • Save one or more Grafana dashboards.

  • Export one or more Grafana dashboards to Python code (*).

  • Generate one or more Grafana dashboards.

  • Upload one or more Grafana dashboards to a running system.

(*) The export is not complete. Some additional work needs to be done before the code can run. But the script contains already most of the code

Take a look at the included examples for more info.

IMPORTANT NOTE:

In order to reduce typing there is only one namespace: grafanacode. So when in the documentation there is: grafanacode.plugins.panel_base.PropGridPos, you have to type grafanacode.PropGridPos. Or when you from grafanacode import *, you simply type PropGridPos. I know this is strongly disadvised. Nevertheless, in this case, importing the grafanacode objects in your namespace implies little risk. On the other side, t makes the code much more compact and readable, which is one of the main aims of this module.

Installation

grafanacode is just a Python package, so:

pip install grafanacode

Support

This library is under development. So breaking changes are always possible.

grafanacode works with Python 3.9 and 3.10.

Development setup

Every Python development system will do, but on Windows OS, I prefer Anaconda.

To install Anaconda:

  • Download Anaconda from https://www.anaconda.com/; chose windows installer

  • Double click to install; Accept all defaults (personally I install this at another location).

  • Start Anaconda

  • Click on Anaconda Navigator

  • Setup a virtual environment; I called this grafanacode

  • Click on environments

  • Click on create at the bottom

  • Fill in the popup; chose Python 3

  • Select environment

  • Start a Command prompt
    • Click on the green play button

    • Chose open terminal

    • In the command prompt, between brackets, you can see the virtual environment you are in.

  • Install dependencies

    pip install attrs
    pip install requests
    pip install sphinx-rtd-theme
    pip install sphinx-toolbox
    pip install pprinter
    pip install pylint
    pip install docutils==0.16
    pip install hypothesis

Generate dashboards

Take a look at the example scripts.

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

grafanacode-0.5.7.tar.gz (59.5 kB view details)

Uploaded Source

Built Distribution

grafanacode-0.5.7-py3-none-any.whl (91.8 kB view details)

Uploaded Python 3

File details

Details for the file grafanacode-0.5.7.tar.gz.

File metadata

  • Download URL: grafanacode-0.5.7.tar.gz
  • Upload date:
  • Size: 59.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.7.15

File hashes

Hashes for grafanacode-0.5.7.tar.gz
Algorithm Hash digest
SHA256 74824ff669e4460885dbf3ce3d56afbfb34e412bf103eab118254dccca366e60
MD5 4c94a2b58ff6c58b924e75a68dded1e4
BLAKE2b-256 05b1cb9f739dd14d02e0a4480907e9a56e15d125ed65d731d4bd44cb74358dae

See more details on using hashes here.

File details

Details for the file grafanacode-0.5.7-py3-none-any.whl.

File metadata

  • Download URL: grafanacode-0.5.7-py3-none-any.whl
  • Upload date:
  • Size: 91.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.7.15

File hashes

Hashes for grafanacode-0.5.7-py3-none-any.whl
Algorithm Hash digest
SHA256 6d48304a1e268bae5007e0f4388706455bd13af57f1227621abfadefb0f305b2
MD5 00b93ca12ebd950d9ef92208589f5be5
BLAKE2b-256 366cd4c75fba03f5576dd7491f811b2c01af3aff96e23a3403eecc26f4b2aaa4

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