Inhaltsverzeichnis
< Alle Themen

PulseSensor – Telegraf configuration

Telegraf

As already discussed in previous chapters, we use Telegraf to process data between our MQTT Broker (Mosquitto) running on or Natrix Gateway (source) and the Grafana Live API (sink). The Grafana Live API has been introduced with Grafana 8 and -according to the ‘store & forward’ principle it receives data, plots them on a dashboard and forgets afterwards.

Telegraf Agent configuration options

For the Telegraf Agent we only have to adjust two parameters which are exclusively targeted to have a good performance and data accuracy for the live measurement of data:

  • The data collection interval for all input sources (500ms)
  • The flush interval for all outputs (500ms)
# Configuration for telegraf agent
[agent]
  ## Default data collection interval for all inputs
  interval = "500ms"
  round_interval = true

  metric_batch_size = 1000

  metric_buffer_limit = 10000

  collection_jitter = "0s"

  ## Default flushing interval for all outputs. Maximum flush_interval will be
  ## flush_interval + flush_jitter
  flush_interval = "500ms"

  flush_jitter = "0s"

  precision = ""

  ## Override default hostname, if empty use os.Hostname()
  hostname = "localhost"
  ## If set to true, do no set the "host" tag in the telegraf agent.
  omit_hostname = false
Configuring Telegraf data source

Configurations for Telegraf all happen in the respective ‘telegraf.conf’ file. We find this file in the Telegraf installation directory which we created here (Telegraf configuration file).   For the data source, which is again or Mosquitto MQTT Broker, we need to configure the [[inputs.mqtt_consumer]] plugin.

What needs to be configured is:

  • Server IP of the MQTT Broker
  • Topic to subscribe for in order to receive the PulseSensor metrics
  • A topic tag to better identify our metrics data in Grafana (optional)
  • A client-id which identifies the Telegraf plug-in as connected to the broker (optional)
  • The format of the data expected to be received from the MQTT Broker (json)
# # Read metrics from MQTT topic(s)
 [[inputs.mqtt_consumer]]
#   ## Broker URLs for the MQTT server or cluster.  To connect to multiple
   servers = ["tcp://192.168.188.31:1883"]
#
#   ## Topics that will be subscribed to.
   topics = [
     "topic/pulsesensor/metrics",
   ]
#   ## The message topic will be stored in a tag specified by this value.  If set
#   ## to the empty string no topic tag will be created.
topic_tag = "PulseSensor-Metrics"
#   ## If unset, a random client ID will be generated.
 client_id = "Telegraf_Subscriber"
#
#   ## Data format to consume.
#   ## Each data format has its own unique set of configuration options, read
#   ## more about them here:
#   ## https://github.com/influxdata/telegraf/blob/master/docs/DATA_FORMATS_INPUT.md
   data_format = "json"
Configuring Telegraf data sink

Our data sink is the Grafana Live API. This is basically a Restful API endpoint where Telegraf pushes the data it receives from the MQTT broker due to its subscription. We expect around 120msg/minute arriving from the PulseSensor project via the Mosquitto MQTT Broker. These messages need to be processed by the ‘inputs.mqtt.consumer’ plugin and being push to the Grafana Live API endpoint. Since version 1.19 Telegraf supports high-frequency updates by using a WebSocket output plugin.

For authentication purposes we need to generate an access token (API key) within Grafana first.

On the panel on the left-hand side, go to [Configuration]->[API Keys]. With the [Add API Key] button you enter the API key creation menu. Give the API key a name and an expiration date (time to live], e.g. 1y (=1 year). With [ADD] you request Grafana to issue a new API Key. Copy the key as we require it in the Telegraf  configuration for the ‘outputs.websocket’ plugin.

This is what we configure as ‘outputs.websocket’ plugin.

  • WebSocket endpoint of the Grafana Live API
  • Data format of the data to be pushed to the WebSocket (influx)
  • Authorization token
# # Generic WebSocket output writer.
 [[outputs.websocket]]
#   ## URL is the address to send metrics to. Make sure ws or wss scheme is used.
  url = "ws://192.168.188.52:3000/api/live/push/pulsesensor"
#
#   ## Timeouts (make sure read_timeout is larger than server ping interval or set to zero).
#   # connect_timeout = "30s"
#   # write_timeout = "30s"
#   # read_timeout = "30s"
#
#   ## Optionally turn on using text data frames (binary by default).
#   # use_text_frames = false
#
#   ## Optional TLS Config
#   # tls_ca = "/etc/telegraf/ca.pem"
#   # tls_cert = "/etc/telegraf/cert.pem"
#   # tls_key = "/etc/telegraf/key.pem"
#   ## Use TLS but skip chain & host verification
#   # insecure_skip_verify = false
#
#   ## Data format to output.
#   ## Each data format has it's own unique set of configuration options, read
#   ## more about them here:
#   ## https://github.com/influxdata/telegraf/blob/master/docs/DATA_FORMATS_OUTPUT.md
 data_format = "influx"
#
#   ## Additional HTTP Upgrade headers
 [outputs.websocket.headers]
#   #   Authorization = "Bearer <TOKEN>"
     Authorization = "Bearer eyJrIjoiWE5vcWYzSkhPTEtwdEgzcTB4a290OGtOeTg5QVBrY2QiLCJuIjoiUHVsc2VTZW5zb3IiLCJpZCI6MX0="

Inside Grafana Influx data is converted to Grafana data frames and then frames are published to Grafana Live channels. In this case, the channel where our PulseSensor data will be published is stream/pulsesensor/mqtt_consumer. The stream scope is constant, the custom_stream_id namespace is the last part of API URL set in Telegraf configuration (http://:3000/api/live/push/pulsesensor) and the path is mqtt_consumer – the name of a measurement. (Grafana(1), 2021)