TIG Stack: Using Telegraf, InfluxDB and Grafana on Aiven
Tags: grafana, telegraf, influxdb, monitoring
Published: 2021-06-16 07:21:43

---
title: TIG Stack: Using Telegraf, InfluxDB and Grafana on Aiven
published: true
date: 2021-06-15 00:00:00 UTC
tags: grafana, telegraf, influxdb, monitoring
canonical_url: https://aiven.io/blog/tig-stack-using-telegraf-influxdb-and-grafana-on-aiven
---

![Sing the song of CPU](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bneujdq74bcqhx7n9aq5.png)

Performance monitoring is an essential aspect of every project, in both the digital and traditional world. Read on to find out how to use the TIG stack (Telegraf, InfluxDB, Grafana) to visualise the health status of your laptop.

Here's the overall process:
1. [Create time series storage and reporting platform with InfluxDB and Grafana](#1-create-time-series-storage-and-reporting-platform)
2. [Set up Telegraf to collect and distribute metrics](#2-set-up-telegraf-to-collect-and-distribute-metrics)
3. [Visualise your metrics with Grafana](#3-visualise-your-metrics)


We'll create the whole monitoring pipeline in minutes with few commands in our terminal to report CPU metrics from a Mac. The stack we're using is this:
- [Telegraf](https://github.com/influxdata/telegraf) for metrics collection and distribution
- [InfluxDB](https://aiven.io/influxdb?utm_source=devto&utm_medium=organic&utm_campaign=blog_art&utm_content=post) for data storage
- [Grafana](https://aiven.io/grafana?utm_source=devto&utm_medium=organic&utm_campaign=blog_art&utm_content=post) for the analysis and visualisation.

To create some test load on your system, please open your favourite random applications, or open 10+ browser tabs playing the greatest hits of the amazing [Albano Carrisi](https://www.youtube.com/watch?v=tNlz5W9N24k) (you'll thank me for this later ;))!

## 1. Create Time Series Storage and Reporting Platform

InfluxDB will be our metrics storage. Create an instance with the following [Aiven's CLI](https://help.aiven.io/en/articles/3074410-aiven-command-line-interface-tips-and-tricks?utm_source=devto&utm_medium=organic&utm_campaign=blog_art&utm_content=post) command in our terminal:

```bash
avn service create --plan startup-4 \
  --service-type influxdb \
  --cloud google-europe-west3 \
  demo-influx
```
The above creates an InfluxDB instance (`-t influxdb`) named `demo-influx` on Google's cloud region `europe-west3` with a `startup-4` plan. To review InfluxDB plans and associated cost, you can check the [pricing page](https://aiven.io/pricing?utm_source=devto&utm_medium=organic&utm_campaign=blog_art&utm_content=post).

With a similar command, create a Grafana instance, changing only the type of instance (`-t grafana`) and the instance name (`demo-grafana`):

```bash
avn service create --plan startup-4 \
  --service-type grafana \
  --cloud google-europe-west3 \
  demo-grafana
```
The last bit of setup needed on Aiven's side is the integration between Grafana and InfluxDB. Set it up with the following command:

```bash
avn service integration-create  \
  -t datasource                 \
  -s demo-grafana               \
  -d demo-influx
```
You already created a datasource in our `demo-grafana` instance pointing to `demo-influx`. You're now ready to receive the metrics.

## 2. Set up Telegraf to collect and distribute metrics

[Telegraf](https://github.com/influxdata/telegraf) is an open source tool enabling easy metrics collection and distribution. To install it, you can follow [these instructions](https://docs.influxdata.com/telegraf/v1.18/introduction/installation/). If you are on a Mac like me, use for example [Homebrew](https://brew.sh/) with the following command in the terminal:

```bash
brew install telegraf
```

Once Telegraf is installed, configure it to collect some metrics and push them to the `demo-influx` instance. Get InfluxDB's service URI with the following command:

```bash
avn service get demo-influx --format '{service_uri}'
```

The service URI is in the form `https+influxdb://avnadmin:<PASSWORD>@<HOSTNAME>:<PORT>/defaultdb`. Note down the `<HOSTNAME>`, `<PORT>` and `<PASSWORD>` parameters.

Next, configure Telegraf by creating a file named `telegraf.conf` with the following content:

```bash
[global_tags]
 [agent]
  interval = "10s"
  hostname = "Francesco.Mac"

 [inputs.cpu]
  totalcpu = true

 [outputs.influxdb]
  url = "https://<HOST>:<PORT>"
  database = "defaultdb"
  username = "avnadmin"
  password = "<PASSWORD>"
  precision = "10s"
  skip_database_creation = true
```

Here you are creating a Telegraf agent that will report metrics over a 10 seconds interval, using the [CPU Input Plugin](https://github.com/influxdata/telegraf/blob/master/plugins/inputs/cpu/README.md) to report the `totalcpu` stats.
The `[outputs.influxdb]` plugin sends the collected metrics to the `influxDB` endpoint defined in the `url` parameter.

Finally, you're setting `skip_database_creation = true`, since the `defaultdb` database already exists in the instance.

It's time to start Telegraf with the following command in the terminal:

```bash
telegraf -config telegraf.conf
```

If no errors arise, you're now sending your CPU metrics to InfluxDB.

## 3. Visualise your metrics

The data is now getting stored in the `demo-influx` instance. We could query it via InfluxQL, but especially for time-series metrics, a line graph is usually much better for showing the current status and recent trend. Creating such visualisation in Grafana is achievable in few steps, with the first one being... understanding how to connect to Grafana itself.

Retrieve the connection parameters with the following command in another terminal window:

```bash
avn service get demo-grafana --format '{service_uri_params}'
```

The output is similar to the one shown below. It includes the Grafana hostname, port and the randomly generated password for the default `avnadmin` user.

```json
{
  "host": "<HOSTNAME>",
  "password": "<PASSWORD>",
  "port": "<PORT>",
  "user": "avnadmin"
}
```

Now open your browser to `https://<HOSTNAME>:<PORT>` and use the `avnadmin` user and related `<PASSWORD>` to log in.

Click **Explore** ![explore_icon](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/igg73cv917rx3sznto4z.png) and select the pre-created `aiven-influxdb-demo-influx` datasource that points to the previously created InfluxDB instance.

Let's monitor the CPU usage by selecting the `cpu` metric group in the `FROM` area. The metric we want to plot is the `usage_user` which we can select in the `field()` section. The settings should look like the below

![CPU metrics settings](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uw597ckicwiuxbfyikk7.png)

Now click the **Run Query** button to visualise the `usage_user` graph. The result should be similar to the following image, which is showing CPU usage consistently staying between 10% and 30% on my Mac.

![CPU graph](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mu3xc6qzws8otumfbb96.png)

It's now time to change some settings:
* Click on the time range icon ![time range](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v0a4d1r53yl5ulmrhv4q.png) and select to display only the last 5 minutes of data.
* Click on the arrow next to the **Run Query** button, and select `10s` as refresh interval.

Now your graph should refresh every 10 seconds showing you the last 5 minutes dataset, like in the image below (speeded up).

![cpu graph moving](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mu30ske3w95fmsnj62sz.gif)

## What's next?

Congrats, you just created your first monitoring pipeline!

This first visualisation uses only the basics of Grafana; should the next challenge be to create something more advanced or setup an alert? Up to you to define, in the meantime here are some resources you might find useful:

* [Telegraf](https://github.com/influxdata/telegraf) an open source server agent for collecting and sending metrics
* [InfluxDB](https://aiven.io/influxdb?utm_source=devto&utm_medium=organic&utm_campaign=blog_art&utm_content=post) used for metrics storage
* [Grafana](https://aiven.io/grafana?utm_source=devto&utm_medium=organic&utm_campaign=blog_art&utm_content=post) used to create stunning visualisations

P.s. I hope you enjoyed Albano Carrisi!