Package: grafana-snapshots
Grafana Snapshots ToolA python3 bases application to build grafana snapshots that contains data(!) usingGrafana APIand a python interfacegrafana-clientThe aim of this tool is to:Easily build snapshots from existing Grafana dashboard.Export the snapshots to a local storage in JSON format so it can be sent, imported an visualized to a remote Grafana.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 repopipinstallgit+https://github.com/peekjef72/grafana-snapshots-tool.gitInstall pypi packageinstall from pypipip3installgrafana-snapshots-toolRequirements:bashpython >3.6python modules:python_dateutilsetuptoolsPyYamljinja2grafana-client 2.0.0 what will pull the dependenciesrequestsidnaurllib3certifichardetAccess to a Grafana API server.ATokenof anAdminrole (grafana APIKey).ConfigurationThe 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 settingslabel: 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 urlverify_ssl: to check ssl certificate or nottoken: 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_fromandtime_toan object calledvars: the definitions of each variable with it's corresponding value.Usagesbuild a directory structure:grafana-snapshosts/conf/grafana-snapshots.json
where your main configuration file issnapshots/
where your exported snapshots will be stored.usage:grafana-snapshots-h
usage:grafana-snapshots[-h][-bBASE_PATH][-cCONFIG_FILE][-dDASHBOARD_NAME][-FGRAFANA_FOLDER][-fTIME_FROM][-gGRAFANA_LABEL][-iIMPORT_FILE][-oCONTEXT_NAME][-p][-sSNAPSHOT_NAME][-tTIME_TO][-v][-V][ACTION]then enter into your directory and type in you commands.Example:generatea snapshot for the dashboard 'My dashboard' with default values for 'from' (now - 5 min) to 'now':$grafana-snapshots-d"My dashboard"OK:newsnapshot'My_dashboard_202010241750.json'created.then you can go into Grafana Gui and find the snapshot in dashboard/Manage/Snapshots part.exportthe dashboard 'My dashboard' with data from: 'now-1d' to 'now':$grafana-snapshots-d"My dashboard"-f"now-1d"exportOK:snapshotexportedto'./snapshots/My_dashboard_202010241750.json'exported.then you can find the created file in the "snapshots" dir.importthe 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 supportedoverrides 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.