This article explains how to find out the id of a Datasource in Grafana when you know its name.
The article is based on the following documentation: https://grafana.com/docs/grafana/latest/developers/http_api/data_source/#get-a-single-data-source-by-name
\uD83D\uDCD8 Instructions
Use the following script to reach Grafana API by IP with username and password.
Note: values in this script are only an example. Before running this script on your machine, first updategrafana_host
,grafana_port
,grafana_username
,grafana_password,
anddatasource_name
with your own actual values.Code Block language bash #!/bin/bash #Get a single data source by name #GET /api/datasources/name/:name grafana_host=10.2.7.243100 grafana_port=80 grafana_username='admin' grafana_password='promadminz-operatorpassword' datasource_name='hivemq-2021prometheus-metr1' curl --location \ --header 'Content-Type: application/json' \ --header 'Accept: application/json' \ "http://${grafana_username}:${grafana_password}@${grafana_host}:${grafana_port}/api/datasources/name/${datasource_name}"
Use the
id
value as Datasource id.If all credentials are valid, the output of the script is JSON like this:
Code Block language json { "id": 5211, "uid": "bbEC2jv4kabcd1efgh", "orgId": 1, "name": "hivemq-2021prometheus-metr1", "type": "prometheus", "typeName": "Prometheus", "typeLogoUrl": "public/app/plugins/datasource/prometheus/img/prometheus_logo.svg", "access": "proxy", "url": "http://vmiprometheus-2021kube-victoria-metrics-single-server.hivemq-2021-metr.svc.cluster.local:8428prometheus-prometheus:9090/", "password": "", "user": "", "database": "", "basicAuth": false, "basicAuthUser": "", "basicAuthPassword": "", "withCredentials": false, "isDefault": falsetrue, "jsonData": {}, "secureJsonFields": {}, "version": 1, "readOnly": falsetrue }
If the Grafana IP is incorrect you will get an error like this:
Code Block language text curl: (7) Failed to connect to 10.2.7.242100 port 80 after 6058 ms: Couldn't connect to server
If user credentials are invalid you will get an error like this:
Code Block language json { "message": "invalid username or password" }
If the request is not authorized you will get an error like this:
Code Block language json { "message": "Unauthorized" }
To list all Datasources:
Code Block language bash curl --location \ --header 'Content-Type: application/json' \ --header 'Accept: application/json' \ "http://${grafana_username}:${grafana_password}@${grafana_host}:${grafana_port}/api/datasources"
\uD83D\uDCCB Related articles
Filter by label (Content by label) | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|