grafana / mqtt-datasource

MQTT Datasource for Grafana allows streaming data from any MQTT broker running either locally or remotely.
Apache License 2.0
168 stars 49 forks source link

add datasource with terraform #70

Open M4vish opened 1 year ago

M4vish commented 1 year ago

Hi all,

I'm trying to add the grafana mqtt data source with terraform in a k3s cluster but unfortunately it does not work. The "grafana-mqtt-datasource" is installed successfully and I'm able to add a new datasource with the webfrontend, but not with the terraform scripts. I tried different settings for the "datasource type" e.g. MQTT, mqtt, Mqtt but without any success. Attached some snippets.

template "grafana-values.tpl":

# https://github.com/grafana/helm-charts/blob/main/charts/grafana/values.yaml
admin:
  existingSecret: "${GRAFANA_SECRET}"
  userKey: GRAFANA_USERNAME
  passwordKey: GRAFANA_PASSWORD

envFromSecret: "${GRAFANA_SECRET}"

plugins:
  - grafana-mqtt-datasource

datasources:
  datasources.yaml:
    apiVersion: 1
    datasources:
    - name: ${VO_DATASOURCE_NAME}
      type: mqtt
      uri: ws://${MQTT_SERVICE_NAME}.${BROKER_NAMESPACE}:${MQTT_PORT}
      Username: ${MQTT_USERNAME}
      Password: ${MQTT_PASSWORD}
      isDefault: true
      editable: true

main.tf

resource "kubernetes_secret" "grafana-secret" {
  metadata {
    name      = var.secret
    namespace = var.namespace
  }
  data = {
    GRAFANA_USERNAME    = var.username
    GRAFANA_PASSWORD    = var.password
  }
  type = "Opaque"
}

# https://github.com/grafana/helm-charts/tree/main/charts/grafana
resource "helm_release" "grafana" {
  name        = var.name
  repository  = var.repository
  chart       = var.chart
  version     = var.chartversion
  namespace   = var.namespace

  values = [templatefile("${path.module}/grafana-values.tpl", {
    GRAFANA_PORT_NAME   = var.port_name
    GRAFANA_SECRET      = var.secret
    VO_DATASOURCE_NAME  = var.datasource_name
    MQTT_SERVICE_NAME   = var.mqtt_service_name
    BROKER_NAMESPACE    = var.broker_namespace
    MQTT_PORT           = var.mqtt_websocket_port
    MQTT_USERNAME       = var.mqtt_username
    MQTT_PASSWORD       = var.mqtt_password
  })]
}

The Script is working, the datasources.yml is created successfully with the correct YAML syntax and in the webfrontend you'll see the mqtt Datasource. But when you click on it, it says "Plugin not found, no installed plugin with that id". But again, if you add the mqtt datasource by hand, it works as it should. Thx in advance.

deviantony commented 1 year ago

@M4vish I managed to make Grafana use a datasource with this plugin at boot time, you can use the following configuration as a reference:

apiVersion: 1

datasources:
  - name: MQ-data
    type: grafana-mqtt-datasource
    version: 1
    editable: true
    isDefault: false
    jsonData:
      uri: tcp://broker:1883
      username: ${MQTT_USERNAME} 
    secureJsonData:
      password: ${MQTT_PASSWORD}