Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  1. 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 update grafana_host, grafana_port, grafana_username, grafana_password, and datasource_name with your own actual values.

    Code Block
    languagebash
    #!/bin/bash
    
    #Get a single data source by name
    #GET /api/datasources/name/:name
    
    grafana_host=10.2.7.100
    grafana_port=80
    grafana_username='admin'
    grafana_password='adminz-password'
    datasource_name='hivemq-prometheus-1'
    
    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}"
  2. Use the id value as Datasource id.

  3. If all credentials are valid, the output of the script is JSON like this:

    Code Block
    languagejson
    {
      "id": 11,
      "uid": "abcd1efgh",
      "orgId": 1,
      "name": "hivemq-prometheus-1",
      "type": "prometheus",
      "typeName": "Prometheus",
      "typeLogoUrl": "public/app/plugins/datasource/prometheus/img/prometheus_logo.svg",
      "access": "proxy",
      "url": "http://prometheus-kube-prometheus-prometheus:9090/",
      "password": "",
      "user": "",
      "database": "",
      "basicAuth": false,
      "isDefault": true,
      "jsonData": {},
      "readOnly": true
    }

  4. If the Grafana IP is incorrect you will get an error like this:

    Code Block
    languagetext
    curl: (7) Failed to connect to 10.2.7.100 port 80 after 6058 ms: Couldn't connect to server

  5. If user credentials are invalid you will get an error like this:

    Code Block
    languagejson
    {
      "message": "invalid username or password"
    }

  6. If the request is not authorized you will get an error like this:

    Code Block
    languagejson
    {
      "message": "Unauthorized"
    }

  7. To list all Datasources:

    Code Block
    languagebash
    curl --location \
      --header 'Content-Type: application/json' \
      --header 'Accept: application/json' \
      "http://${grafana_username}:${grafana_password}@${grafana_host}:${grafana_port}/api/datasources"

...