Skip to main content

Generate Grafana dashboards with YAML

Project description

grafana-dashboard-builder

PyPI version Build Status Coverage Status

Introduction

grafana-dashboard-builder is an open-source tool for easier creation of Grafana dashboards. It is written in Python and uses YAML descriptors for dashboard templates.

This project has been inspired by Jenkins Job Builder that allows users to describe Jenkins jobs with human-readable format. grafana-dashboard-builder aims to provide similar simplicity to Grafana dashboard creation and to give users easy way how they can create dashboard templates filled with different configuration.

Installation

To install:

pip install grafana-dashboard-builder

or

uv run pip install -e .

An alternative to installing it into the global site-packages is to add it to your user local directory (usually ~/.local).

pip install --user grafana-dashboard-builder

Usage

After installation you'll find grafana-dashboard-builder on your path. Help can be printed by --help command-line option.

usage: grafana-dashboard-builder [-h] -p PATH [PATH ...] [--project PROJECT] [-o OUT] [-c CONFIG]
                                 [--context CONTEXT] [--plugins PLUGINS [PLUGINS ...]]
                                 [--exporter EXPORTERS [EXPORTERS ...]]

optional arguments:
  -h, --help            show this help message and exit
  -p PATH [PATH ...], --path PATH [PATH ...]
                        List of path to YAML definition files
  --project PROJECT     (deprecated, use path) Location of the file containing
                        project definition.
  -o OUT, --out OUT     (deprecated, use config file and file exporter) Path
                        to output folder
  -c CONFIG, --config CONFIG
                        Configuration file containing fine-tuned setup of
                        builder's components.
  --context CONTEXT     YAML structure defining parameters for dashboard
                        definition. Effectively overrides any parameter
                        defined on project level.
  --plugins PLUGINS [PLUGINS ...]
                        List of external component plugins to load
  --exporter EXPORTERS [EXPORTERS ...]
                        List of dashboard exporters

To start you need to create project configuration that needs to be in one YAML document. And some examples with current can be found in sample project:

grafana-dashboard-builder -p ./samples/project.yaml --exporter file --config ./samples/config.yaml

Exporters

grafana-dashboard-builder provides several builtin exporters that can be enabled through --exporter option. Configuration for all of them is to be provided in configuration file given in --config option. Look at sample config.

File exporter

File exporter is used when you want to store dashboards as JSON files on your local disk.

file:
  output_folder: /some/directory/on/my/disk

To use file exporter run grafana-dashboard-builder with --exporter file option.

Grafana Elastic Search

grafana-dashboard-builder currently supports persisting dashboards to Elastic Search used by Grafana prior to version 2.0

To configure Elastic Search endpoint put following structure to your configuration file:

elastic-search:
  host: https://this-is-my-domain.com
  password: my_password
  username: my_username

With this configuration your dashboard will be uploaded to https://this-is-my-domain.com/es/grafana-dash/dashboard/dashboard_name

If you do not want to store your credentials in the configuration file you can use environment variables ES_PASSWORD and ES_USERNAME.

To use elastic search exporter run grafana-dashboard-builder with --exporter elastic-search option.

Grafana API

grafana-dashboard-builder currently supports Grafana version 2.0 API.

To configure Grafana endpoint put following structure to your configuration file:

grafana:
  host: https://this-is-my-domain.com
  password: my_password
  username: my_username

With this configuration your dashboard will be POSTed to https://this-is-my-domain.com/api/dashboards/db

If you do not want to store your credentials in the configuration file you can use environment variables GRAFANA_PASSWORD and GRAFANA_USERNAME.

You can use Organization API Key to access Grafana API:

Set token in your configuration file:

grafana:
  host: https://this-is-my-domain.com
  token: eyJrIjoiOGNTW...o2b2123kO==

Or in GRAFANA_TOKEN environment variable.

Read more about authentication in Grafana docs.

To use Grafana exporter run grafana-dashboard-builder with --exporter grafana option.

Supported data stores

At this moment grafana-dashboard-builder supports following data stores:

YAML definition format

Each component follows the same configuration format. Top level must contain 2 fields - name and component type. Under component type is wrapped definition of the component.

- name: some-name
  component-type:
    component-param1: param-value
    component-param2: other-value

Components can be defined in multiple source files that are passed through --path option. If a path is directory it is recursively walked and all files are processed.

Components

Components define basic building blocks such as rows, graphs and template queries. They can be defined in-place or be named and reused within other components and dashboards.

Components can define parameters that can be passed from parent component to its children.

- name: graph-name
  panels:
    - graph:
        target: target
        y_formats: [bytes, short]
        span: 4
- name: row-name
  rows:
    - row:
        title: Placeholder row
        panels:
            - graph-name
            - graph:
                target: target
                y_formats: [bytes, short]
                span: 4

Another component is template queries that allow you to define just one query string for hierarchical variables. Each query part that starts with $ sign will appear as one variable.

- name: template-name
  templates:
    - query:
        query: '{metric-prefix}.$component.$application'

Dashboard

Dashboard is top-level object composed of several components.

- name: overview
  dashboard:
    title: overview dashboard
    time_options: [1h]
    refresh_intervals: [5m]
    templates:
      - template-name:
            metric-prefix: '{metric-prefix}'
    time:
      from: now-12h
      to: now
    rows:
      - row-name

Project

Project is an entry point for builder and defines which dashboards will be generated and provides parameters to them.

- name: Example project
  project:
    dashboard-prefix: MyApp
    metric-prefix: metric.prefix
    dashboards:
        - overview

The biggest benefit of grafana-dashboard-builder is that you can generate several dashboards from one dashboard template just by defining multiple values for a parameter that is contained in dashboard name. Following project will generate 2 dashboards named prefix1-dashboard and prefix2-dashboard.

- name: Example project
  project:
    dashboard-prefix:
      - prefix1
      - prefix2
    dashboards:
      - '{dashboard-prefix}-dashboard'

External context definition

Thanks to project component you can use one dashboard template and configure it with different parameters. But what if you need to use different params based on the Grafana you are uploading dashboards to. That's why you can define configuration externally to your projects and dashboard templates.

You can reference configuration stored in YAML with -config option or even inline it to --context option. External configuration file can look like:

context:
  region: eu
  default-datacenter: cze

Run tox test suite

tox .

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_dashboard_builder-0.10.0a3.tar.gz (22.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

grafana_dashboard_builder-0.10.0a3-py3-none-any.whl (40.4 kB view details)

Uploaded Python 3

File details

Details for the file grafana_dashboard_builder-0.10.0a3.tar.gz.

File metadata

File hashes

Hashes for grafana_dashboard_builder-0.10.0a3.tar.gz
Algorithm Hash digest
SHA256 9b52b26041d1897b3c2270e4678259e6bc84687aff22495510765dc84b1e9d55
MD5 5aad70ce678f9df7c2695d18dd4a4074
BLAKE2b-256 c916571e74b0f6f6ee6d4c6089af1ea54ed04ff2d06ac792b919bb272b9a4332

See more details on using hashes here.

Provenance

The following attestation bundles were made for grafana_dashboard_builder-0.10.0a3.tar.gz:

Publisher: publish-to-pypi.yml on jakubplichta/grafana-dashboard-builder

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file grafana_dashboard_builder-0.10.0a3-py3-none-any.whl.

File metadata

File hashes

Hashes for grafana_dashboard_builder-0.10.0a3-py3-none-any.whl
Algorithm Hash digest
SHA256 03a64bc969f803a332dfb156ec8c29b462944fddf77017929e81def1f43431d8
MD5 56377a7728159f832f89723dc0a1f4ed
BLAKE2b-256 d8eb01501be21190edc718b3b55d3080d72bcd82aed703356211e225a5140388

See more details on using hashes here.

Provenance

The following attestation bundles were made for grafana_dashboard_builder-0.10.0a3-py3-none-any.whl:

Publisher: publish-to-pypi.yml on jakubplichta/grafana-dashboard-builder

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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