Edit

Collect custom metrics for a Linux virtual machine with the InfluxData Telegraf agent

This article explains how to deploy and configure the InfluxData Telegraf agent on a Linux virtual machine to send metrics to Azure Monitor.

Note

InfluxData Telegraf is an open source agent and not officially supported by Azure Monitor. For issues with the Telegraf connector, refer to the Telegraf GitHub page here: InfluxData

InfluxData Telegraf agent

Telegraf is a plug-in-driven agent that collects metrics from more than 150 sources. Depending on what workloads run on your Linux virtual machine (VM), you can configure the agent to use specialized input plug-ins to collect metrics. Examples of these workloads are MySQL, NGINX, and Apache. By using output plug-ins, the agent can then write to destinations that you choose. The Telegraf agent integrates directly with the Azure Monitor custom metrics REST API. It supports an Azure Monitor output plug-in. When you use this plug-in, the agent can collect workload-specific metrics on your Linux VM and submit them as custom metrics to Azure Monitor.

A diagram showing the Telegraph agent overview.

Connect to the VM

Create an SSH connection to the VM where you want to install Telegraf. Select the Connect button on the overview page for your VM.

A screenshot of the Virtual machine overview page with the connect button highlighted.

In Connect to virtual machine, keep the default options to connect by domain name system (DNS) name over port 22. In Login using VM local account, a connection command is shown. To copy the command, select the button. The following example shows the SSH connection command:

ssh azureuser@XXXX.XX.XXX

Paste the SSH connection command into a shell, such as Azure Cloud Shell or Bash on Ubuntu on Windows, or use an SSH client of your choice to create the connection.

Install and configure Telegraf

To install the Telegraf Debian package onto the VM, run the following commands from your SSH session:

Add the repository:

# download the package to the VM
curl -s https://repos.influxdata.com/influxdb.key | sudo apt-key add -
source /etc/lsb-release
sudo echo "deb https://repos.influxdata.com/${DISTRIB_ID,,} ${DISTRIB_CODENAME} stable" | sudo tee /etc/apt/sources.list.d/influxdb.list
sudo curl -fsSL https://repos.influxdata.com/influxdata-archive_compat.key | sudo apt-key --keyring /etc/apt/trusted.gpg.d/influxdata-archive_compat.gpg add

Install the package:

sudo apt-get update
sudo apt-get install telegraf

Telegraf's configuration file defines Telegraf's operations. By default, an example configuration file is installed at the path /etc/telegraf/telegraf.conf. The example configuration file lists all possible input and output plug-ins. However, you create a custom configuration file and have the agent use it by running the following commands:

# generate the new Telegraf config file in the current directory
telegraf --input-filter cpu:mem --output-filter azure_monitor config > azm-telegraf.conf

# replace the example config with the new generated config
sudo cp azm-telegraf.conf /etc/telegraf/telegraf.conf

Note

The preceding code enables only two input plug-ins: cpu and mem. You can add more input plug-ins, depending on the workload that runs on your machine. Examples are Docker, MySQL, and NGINX. For a full list of input plug-ins, see the Other configurations section.

Finally, to have the agent start using the new configuration, we force the agent to stop and start by running the following commands:

# stop the telegraf agent on the VM
sudo systemctl stop telegraf
# start and enable the telegraf agent on the VM to ensure it picks up the latest configuration
sudo systemctl enable --now telegraf

Now the agent collects metrics from each of the input plug-ins specified and emits them to Azure Monitor.

Plot your Telegraf metrics in the Azure portal

  1. Open the Azure portal.

  2. Navigate to the new Monitor tab. Then select Metrics.

  3. Select your VM in the resource selector.

  4. Select the Telegraf/CPU namespace, and select the usage_system metric. You can choose to filter by the dimensions on this metric or split on them.

    A screenshot showing a metric chart with telegraph metrics selected.

Other configurations

The preceding walkthrough shows how to configure the Telegraf agent to collect metrics from a few basic input plug-ins. The Telegraf agent supports over 150 input plug-ins, with some supporting other configuration options. InfluxData published a list of supported plugins and instructions on how to configure them.

Also, in this walkthrough, you used the Telegraf agent to emit metrics about the VM the agent is deployed on. The Telegraf agent can also collect and forward metrics for other resources. To learn how to configure the agent to emit metrics for other Azure resources, see Azure Monitor Custom Metric Output for Telegraf.

Clean up resources

When they're no longer needed, you can delete the resource group, VM, and all related resources. To do so, select the resource group for the VM and select Delete. Then confirm the name of the resource group to delete.

Next steps