Skip to main content
Version: Next

Telegraf to InfluxDB through the gateway

This guide follows a Telegraf agent on a plant PC to the gateway and on to InfluxDB. The agent's CPU and memory become tags, so they can have an alarm, a screen and history; an event stream then writes their changes to an InfluxDB bucket. It is for the engineer who runs Telegraf and the administrator who owns the InfluxDB. Telegraf 1.34 and InfluxDB 2.7 are the versions this has been tried with; nothing else has.

Prerequisites​

  • A gateway with the Telegraf module (io.scadex.driver.telegraf) running; see Modules and Telegraf.

  • The engineer role for the connection and the tags, and the admin role for the API key, the database connection and the event stream.

  • Telegraf installed on the plant PC, started with a configuration file you can edit.

  • An InfluxDB 2 the gateway can reach, with an organization, a bucket and an API token that can write to the bucket. A throwaway one for trying things out; the variables are the image's own initial setup settings, and the values in angle brackets are yours to choose:

    docker run -d --name influxdb -p 8086:8086 \
    -e DOCKER_INFLUXDB_INIT_MODE=setup \
    -e DOCKER_INFLUXDB_INIT_USERNAME=<user> \
    -e DOCKER_INFLUXDB_INIT_PASSWORD=<password> \
    -e DOCKER_INFLUXDB_INIT_ORG=<org> \
    -e DOCKER_INFLUXDB_INIT_BUCKET=<bucket> \
    -e DOCKER_INFLUXDB_INIT_ADMIN_TOKEN=<token> \
    influxdb:2.7

Steps​

1. Make a key for the agent​

  1. Open Gateway, then Security, then API keys, and click New key.
  2. Give it a Name such as Plant PC telegraf, choose the Operator role, and click Create key.
  3. Copy the secret now with Copy key; it is shown once. It starts with scx_.

An operator key can write values and do nothing else, and revoking it stops the agent at once. See API keys.

2. Add the agent as a connection​

  1. Open Connections, then Devices, then Telegraf, and choose Add agent.
  2. Give it a Name, for example Plant_PC, and leave Enabled on.
  3. Leave Stale after (seconds) at 60. With Telegraf sending every 10 seconds, six missed sends turn the tags bad.
  4. Choose Add agent.

Select the connection and choose Test. Until an agent has sent something it says Nothing has arrived from an agent yet. Set up the agent to send to this connection, then test again.

3. Point Telegraf at the gateway​

Open the connection's Set up the agent tab and copy the configuration. It has the gateway's address and the connection's id filled in. Put your key in token, and add the inputs and the interval:

[agent]
interval = "10s"
flush_interval = "10s"

[[inputs.cpu]]
percpu = false
totalcpu = true

[[inputs.mem]]

[[outputs.influxdb_v2]]
urls = ["https://app.scadex.io/api/v1/modules/io.scadex.driver.telegraf/ingest/<the connection's id>"]
token = "<the API key>"
organization = "scadex"
bucket = "scadex"

The gateway ignores organization and bucket, but Telegraf's output needs both set. Restart Telegraf. It sends gzip line protocol with Authorization: Token <the API key>, which the gateway accepts.

After a few seconds Test says Data arrived 4 s ago. with the number of series seen.

4. Turn series into tags​

  1. Open the connection's Browse tab. It lists the measurements the agent has sent: cpu and mem.
  2. Open cpu, then Open the tag set below it, a folder named by its tags, and tick Select all to tick every field. Or tick only usage_idle.
  3. In Tag path to create them under, type Plant.PC1, and choose the button that reads Create N tags.
  4. Go Up and do the same for mem.

The tags are named after their series: Plant.PC1.cpu.cpu-total_<host>.usage_idle and Plant.PC1.mem.<host>.used_percent, where <host> is the host name Telegraf reports, with anything but letters, digits, _ and - replaced by _. Each takes the last value the agent sent at once. The cpu fields are floats, so those tags are Double; the byte counts in mem are integers, so those tags are Int64.

5. Use them like any other tag​

Open Tags, then Tag browser, and select Plant.PC1.mem.<host>.used_percent. It has a value, a quality and a timestamp, and it can be used everywhere a tag can:

  • Add an alarm in the tag's panel, for example a high alarm at 90; see Alarms and From an alarm to a webhook.
  • Show it on a screen or in a dashboard.
  • While the historian module runs, it records the tag's values, so the tag has a trend.

If the agent stops, the tags read bad after the stale limit, with the message No data from the agent for 60 s, and keep their last value. They go good again when it sends.

6. Add the InfluxDB connection​

  1. Open Connections, then Databases, and click New connection.
  2. Choose InfluxDB as the Engine. Set Scheme to http unless your InfluxDB has a certificate the gateway trusts, then Host and Port (8086).
  3. Enter the Organization, the Default bucket and the Token (<org>, <bucket> and <token> from the prerequisites).
  4. Under Test the connection, choose Test connection. It says that the connection works, with InfluxDB's version (for example InfluxDB v2.7.12) and the time it took, or which setting is wrong; InfluxDB connections lists the messages. No point is written by the test.
  5. Click Add connection.

7. Write the tags' changes to InfluxDB​

  1. Open Automation, then Event streams, and add a stream.
  2. Under Stream, give it a Name, Plant PC to InfluxDB, and leave Sending on.
  3. Under Destination, set Send to to InfluxDB, choose the Connection you just added, and leave Measurement at scadex.
  4. Under Batches, leave Send after at 5 seconds and Or once at 100 changes.
  5. Under Tags, enter the Tag paths: Plant.PC1.*.
  6. Save, then choose Send a test batch. The answer says it wrote a test point to your bucket.

Within a few seconds Recent batches lists a delivery for each batch with the result sent and the destination influxdb: <connection>/<bucket>.

8. Read it back​

Query the bucket with the influx command line tool, here from inside the container of the throwaway InfluxDB:

docker exec influxdb influx query \
'from(bucket: "<bucket>") |> range(start: -10m) |> filter(fn: (r) => r._measurement == "scadex")' \
--org <org> --token <token>

Each change of each tag is one point. What reaches InfluxDB is not what Telegraf sent:

Telegraf sentInfluxDB holds
The measurement, memThe stream's measurement, scadex
The tags, host=<host>A tag path with the tag's whole path, [default]Plant.PC1.mem.<host>.used_percent, and a tag quality
Fields such as used_percent and activeOne field per point: value for every number (always a float), bool, or text
Its own timestampThe tag's timestamp, in nanoseconds

The tag's path already carries the measurement, the host and the field, so a query filters on path. See Event streams for the line protocol.

If it does not work​

What you seeLook at
Browse says Nothing has arrived yetTelegraf's log, the address, and that it was restarted. Test on the connection says what the gateway has seen
Telegraf's log shows 401The key is wrong, expired or revoked
Telegraf's log shows 403The key has the viewer role; make one with the Operator role
Telegraf's log shows 404The connection's id in urls is wrong, or the connection is switched off
The tags read bad with No data from the agent for 60 sThe agent stopped sending, or sends less often than the stale limit
A delivery says failedThe detail says which setting of the InfluxDB connection to look at; the batch is kept and sent again every 10 seconds
A delivery says droppedInfluxDB refused the batch as wrong, with its reason in the detail, and it was not sent again

What to do next​

  • Telegraf describes the endpoint, the data types, the time rules and the limits.
  • Event streams covers batching, retries and sending the same points through a Telegraf listener to other outputs.
  • Databases and storage covers the connection and its health tags.