Store and Visualize IoT Device Metrics Using Telegraf and MetricFire

Store and Visualize IoT Device Metrics Using Telegraf and MetricFire

Table of Contents

Introduction 

Monitoring your IoT devices provides insights into their usage patterns, environmental conditions, and user behaviors. This information can be leveraged to optimize device performance, improve user experiences, and develop better-targeted services or products. By analyzing patterns and trends in device data, it's possible to implement predictive maintenance strategies, scheduling repairs or replacements before a device completely fails, which will reduce downtime and maintenance costs. 

In this article, we'll detail how to publish metrics to an MQTT broker, and configure the Telegraf agent to listen for and forward these metrics to a datasource.

Run An MQTT Broker

You may already be using your own broker but for our testing purposes we'll use Mosquitto, which is an open-source MQTT broker developed by the Eclipse Foundation. It's lightweight, easy to set up, and widely used due to its simplicity and reliability. Once installed and running, you will be able to publish some test metrics to Mosquitto.

Install Mosquitto (macOS)

See the Mosquitto installation docs that pertain to your OS, and for macOS it's just: 

brew install mosquitto

brew services restart mosquitto

Another good broker option is HiveMQ, which can be configured to output to databases, cloud services, other MQTT brokers, and more. They also have an integration with MetricFire's Hosted Graphite, making it a good option for production level IoT device monitoring.

Getting Started with the Telegraf Agent

Telegraf is a plugin-driven server agent built on InfluxDB, and is used for collecting and sending metrics/events from databases, systems, processes, devices, and applications. Telegraf is written in Go and compiles into a single binary with no external dependencies, and requires a very minimal memory footprint. It is compatible with many operating systems, and has many useful output plugins and input plugins for collecting and forwarding a wide variety of system performance metrics. 

Install Telegraf (macOS)

brew install telegraf

See the telegraf docs for up-to-date versions and installation commands for other operating systems (Ubuntu/Debian, RedHat/CentOS, Windows, Linux binaries). 

For MacOS, packages and files are generally installed in the /usr/local/etc/ directory.

Configure an Output

You can configure telegraf to output to a variety of sources, like Graphite, Kafka, InfluxDB, Prometheus, SQL, NoSQL, and more. 

In this example we will configure telegraf with a Graphite output. If you're not currently hosting your own datasource, you can start a free trial with Hosted Graphite by MetricFire in order to follow along with these next steps.

A Hosted Graphite account will provide the datasource, offers alerting features, and includes Hosted Grafana as a visualization tool.

To configure the Graphite output, you need to locate the downloaded telegraf configuration file at ~/usr/local/etc/telegraf/telegraf.conf and open it in your preferred text editor. Then you will need to make the following changes to the file:

Locate and comment out the line:

# [[outputs.influxdb]]

Then, uncomment the line:

[[outputs.graphite]]

Next, uncomment and edit the servers line to:

servers = ["carbon.hostedgraphite.com:2003"]

Finally, uncomment and edit the prefix line to:

prefix = "<YOUR_API_KEY>.telegraf"
If you don't already have a Hosted Graphite account, sign up for a free trial here to obtain a Hosted Graphite API key.
Otherwise, you can configure a different telegraf output to forward metrics to another datasource.

Configure Telegraf's MQTT Consumer Plugin:

Telegraf has many input plugins that can collect a wide range of data from many popular technologies and 3rd party sources. In this example, we'll demonstrate how to configure Telegraf to listen for topics being published to Mosquitto.

All you need to do is search for the inputs.mqtt_consumer section in your telegraf.conf file, uncomment the [[inputs.mqtt_consumer]] line, and uncomment the servers line (default is localhost at port 1883): 

[[inputs.mqtt_consumer]]
 servers = ["tcp://127.0.0.1:1883"]

Then define which topics to listen for from the MQTT broker, and define the data_format as Graphite:

topics = [
      "topic/state",
      "topic/sensor",
      "topic/uptime",
"unique-device-metric"
]

data_format = "graphite"

Now you can save the file and run telegraf to listen for Mosquitto (or another MQTT broker). The following command runs telegraf like a server and will show you if there are any configuration errors in the output:

telegraf --config telegraf.conf

Now you can just open another terminal window and publish some metrics to mosquitto (these metrics are in the Graphite format of metric.name <numeric-value>):

mosquitto_pub -t topic/sensor -m "mosquitto.sensor 2.2"

mosquitto_pub -t topic/state -m "mosquitto.state 4.4"

mosquitto_pub -t topic/uptime -m "mosquitto.uptime 6.6"

mosquitto_pub -t unique-device-metric -m "mosquitto.unique 8.8"

Telegraf will listen for, and forward the above 4 metrics to your datasource. This is what they will look like in the Graphite format:

telegraf.<host-name>.topic-sensor.mosquitto.sensor
telegraf.<host-name>.topic-state.mosquitto.state
telegraf.<host-name>.topic-uptime.mosquitto.uptime
telegraf.<host-name>.unique-device-metric.mosquitto.unique

See the official GitHub repository for additional configuration options for the mqtt_consumer plugin.

Use Hosted Graphite by MetricFire to Create Custom Dashboards and Alerts

MetricFire is a monitoring platform that enables you to gather, visualize, and analyze metrics and data from sources such as servers, databases, networks, devices, and applications. By utilizing MetricFire, you can effortlessly identify problems and optimize resources from within your infrastructure. Hosted Graphite by MetricFire takes away the burden of self-hosting your own monitoring solution, allowing you more time and freedom to work on your most important tasks.

Once you have signed up for a Hosted Graphite account and used the above steps to configure your server with the Telegraf Agent, metrics will be forwarded, timestamped, and aggregated into the Hosted Graphite backend.

  1. Metrics will be sent and stored in the Graphite format of: metric.name.path <numeric-value> <unix-timestamp>
  2. The dot notation format provides a tree like data structure and makes them efficient to query
  3. Metrics are stored in your Hosted Graphite account for 2 years, and you can use them to create custom Alerts and Grafana dashboards

Build Dashboards in Hosted Graphite's Hosted Grafana

In the Hosted Graphite UI, navigate to Dashboards => Primary Dashboards and select the + button to create a new panel:

Store and Visualize IoT Device Metrics Using Telegraf and MetricFire - 1

Then you can use the query UI to select a graphite metric path (the default datasource will be the hosted graphite backend if you are accessing Grafana through your Hosted Graphite account).

The Hosted Graphite datasource also supports wildcard (*) searching to grab all metrics that match a specified path. Additionally, you can apply Graphite functions to the metric queries, as well as some styling from within the Grafana UI:

Store and Visualize IoT Device Metrics Using Telegraf and MetricFire - 2

Grafana has many additional options to apply different visualizations, modify the display, set units of measurement, and some more advanced features like configuring dashboard variables and event annotations.

See the Hosted Graphite dashboard docs for more details.

Creating Graphite Alerts

In the Hosted Graphite UI, navigate to Alerts => Graphite Alerts to create a new alert. Name the alert, add a query to the alerting metric field, and add a description of what this alert is:

Store and Visualize IoT Device Metrics Using Telegraf and MetricFire - 3

Then select the Alert Criteria tab to set a threshold, and select a notification channel. The default notification channel is the email you used to signup for the Hosted Graphite account, but you can easily configure channels for Slack, PagerDuty, Microsoft Teams, OpsGenie, custom webhooks and more. See the Hosted Graphite docs for more details on notification channels:

Store and Visualize IoT Device Metrics Using Telegraf and MetricFire - 4

Conclusion

Monitoring IoT devices not only ensures their optimal performance and security but also enables businesses to make informed decisions, improve operational efficiency, and enhance the overall reliability and longevity of their IoT ecosystem.

Monitoring different layers of your infrastructure provides valuable data, and using tools like dashboards and alerts will complement this data by providing real-time visualization, proactive identification of issues, historical trend analysis, and facilitating informed decision-making, all of which are essential for maintaining a robust and efficient infrastructure. 

Sign up for a free trial, and experiment with monitoring your IoT devices today! You can also book a demo and talk to the MetricFire team directly about your monitoring needs.

You might also like other posts...
metricfire Jan 24, 2024 · 6 min read

Monitor Heroku Add-Ons Using Hosted Graphite

Monitoring your Heroku stack helps you understand the performance of your application and infrastructure.... Continue Reading

metricfire Jan 23, 2024 · 8 min read

Monitor Redis Using Telegraf and MetricFire

Monitoring Redis instances is essential for maintaining performance, reliability, and security. It allows you... Continue Reading

metricfire Jan 19, 2024 · 8 min read

Monitor Your Mailchimp Campaigns Using Telegraf

Monitoring your email campaigns helps you track key performance indicators (KPIs) such as open... Continue Reading

header image

We strive for
99.999% uptime

Because our system is your system.

14-day trial 14-day trial
No Credit Card Required No Credit Card Required