How to find out unused datasources?

Hi, our company’ grafana has several hundreds datasources. I’m not sure how many of them are being used (by dashboards/panels/variables). Are there any existing methods or tools which can help to find out unused datasources so that we can delete them ? Thanks !

1 Like

Use GitHub - panodata/grafana-wtf: Grep through all Grafana entities in the spirit of git-wtf. and search datasource names. Or use Grafana API and search them in dashboard models (that’s what grafana wtf is doing )

1 Like

Thank you, I will try it.

Would HashiCorp’s Consul work well for this? Maybe someone knows? I haven’t tried it yet but I hear it’s the go-to for service discoverability in the DevOps space. I’m hoping to be testing the service in the next 2 months. I’ll keep data sources in mind, so I can test if it can scan for these as well and integrate data source discoverability into Grafana.

Dear @chenlujjj and @jangaraj,

thanks for sharing your needs and for mentioning grafana-wtf in this context. With [1], grafana-wtf now has a corresponding feature and version 0.11.0 has just been released [2].

Finding out about unused data sources might now be as easy as

# Install program [3].
pip install grafana-wtf

# Set URL to Grafana.
export GRAFANA_URL=https://grafana.example.org/

# Display all data sources and the dashboards using them, as well as unused data sources.
grafana-wtf datasource-breakdown --format=yaml

# Display names of unused datasources as a flat list.
grafana-wtf datasource-breakdown --format=json | jq -r '.unused[].datasource.name'

Let me know if this fits your bill already, if it will work flawlessly on a large Grafana instance, and if you think this feature could receive any further improvements. Thanks!

With kind regards,
Andreas.

[1] Add feature to display datasource breakdown by amotl · Pull Request #16 · panodata/grafana-wtf · GitHub
[2] Release 0.11.0 · panodata/grafana-wtf · GitHub
[3] We strongly recommend to use a Python virtualenv and/or the pip install --user option to install the program, instead of installing it globally. In order to do so, you would invoke those commands beforehand:

python3 -m venv .venv
source .venv/bin/activate
pip install grafana-wtf
2 Likes

Thank you @amotl . I will try this tool next week when I back to work.
A few questions:

  1. After installation, grafana-wtf --version still print 0.9.0 …
$ pip install grafana-wtf --upgrade                                                                                                                                                                                 py tmp at 20:54:58
Collecting grafana-wtf
  Using cached grafana-wtf-0.11.0.tar.gz (28 kB)
Collecting six
  Using cached six-1.16.0-py2.py3-none-any.whl (11 kB)
...

$ grafana-wtf --version                                                                                                                                                                                             
grafana-wtf 0.9.0
  1. Have you considered about this scenario – data sources used in variables? Not checked out code yet. :slight_smile:

Another feature request is to find out invalid data souces.
By invalid I mean the data source cannot pass test when press “Save & Test” button in its page.

I have written a tiny go script to do this. Maybe it can be added to grafana-wtf :slight_smile:

1 Like

Hi @chenlujjj,

thanks for reporting back your observations. I will go through each item one by one.

Installation problem

I can’t reproduce this within a fresh virtualenv on my workstation.

(.venv) sink:testdrive amo$ pip install grafana-wtf --upgrade
(.venv) sink:testdrive amo$ grafana-wtf --version
grafana-wtf 0.11.0

a) Are you also using a Python virtualenv for installing grafana-wtf into?
b) Maybe you installed the new version into a virtualenv, but the system is still referring to a previous “global” installation? What does which grafana-wtf say on your end?

Collecting datasources from all domains of a dashboard

Thanks for asking, I believe I did. The procedure collects data sources from the panels, annotations and templating slots.

I think the templating slot is actually what we are usually referring to as variables, right?

However, this section has to be improved, as just discovered and reported by @jangaraj at AttributeError: 'Munch' object has no attribute 'datasource' for datasource-breakdown · Issue #17 · panodata/grafana-wtf · GitHub.

Finding unhealthy data sources

Thank you for suggesting that, I’ve just created a dedicated issue to track this at Finding unhealthy data sources · Issue #19 · panodata/grafana-wtf · GitHub.

With kind regards,
Andreas.

Hi again,

grafana-wtf version 0.12.0 has just been released. The program is now also available as a container image, which might make its installation more effortless, see also How to run grafana-wtf from container image.

Please note that regarding your feature request How to find unused data sources?, the corresponding subcommand has been renamed to explore datasources.

Example

# Display all data sources and the dashboards using them, as well as unused data sources.
grafana-wtf explore datasources --format=yaml

# Display names of unused datasources as a flat list.
grafana-wtf explore datasources --format=json | jq -r '.unused[].datasource.name'

New features

Other than this, two other subcommands have been added, namely info and explore dashboards. I will be happy receive any feedback or requests about improvements to them.

With kind regards,
Andreas.

3 Likes

It turns out that the installation problem is caused by my operation mistake. :joy: It’s solved now.

1 Like

Thank you for the work ! I will try this new feature today and give feedback. :slightly_smiling_face:

Feedback: the command grafana-wtf explore datasources --format=json | jq -r '.unused[].datasource.name' works like a charm. It outputs the same result as my script in aspect of prometheus type data sources, and the log and work style are far better. :100:

2 Likes