Linux System Monitoring using Telegraf and InfluxDB

Do you want to know what’s going on inside that black hole without having to manually check every single log file? Well, bro, I have some good news for you. In this guide, we’re gonna talk about how to set up Telegraf and InfluxDB for system monitoring in Linux.

To start, let’s install these bad boys on our machine. If you’re using Ubuntu or any other Debian-based distro, just run this command:


# Update and upgrade the system
sudo apt update && sudo apt upgrade -y

# Install Telegraf and InfluxDB for system monitoring
sudo apt install telegraf influxdb

# Explanation:
# The first line updates the list of available packages and their versions, while the second line upgrades any outdated packages.
# The third line installs Telegraf and InfluxDB on the system.

If you’re using RedHat or CentOS, use these commands instead:


# This script updates and upgrades the system, installs the EPEL repository, and installs Telegraf and InfluxDB.
# It is intended for use on RedHat or CentOS systems.

# Update and upgrade the system using yum package manager.
sudo yum update && sudo yum upgrade -y

# Install the EPEL repository, which contains additional packages for RedHat and CentOS.
sudo yum install epel-release

# Install Telegraf and InfluxDB using yum package manager.
sudo yum install telegraf influxdb

Now that we have Telegraf and InfluxDB installed, let’s configure them. First, open the Telegraf configuration file:

// Use sudo to run the following command with root privileges
sudo nano /etc/telegraf/telegraf.conf
// Use the nano text editor to open the Telegraf configuration file

Add these lines to it:

# This code segment creates an output to an InfluxDB database
[[outputs.influxdb]]
  # This line specifies the URLs of the InfluxDB server to send data to
  urls = ["http://localhost:8086"]
  # This line specifies the name of the database to send data to
  database = "my_database"
  # This line specifies the username for authentication to the database
  username = "my_username"
  # This line specifies the password for authentication to the database
  password = "my_password"

Replace `my_database`, `my_username`, and `my_password` with your own values. This will tell Telegraf to send the data it collects to InfluxDB at localhost on port 8086, using a specific database, username, and password. Save and close the file.

Next, let’s configure InfluxDB by running this command:


# Open the InfluxDB configuration file using the nano text editor with superuser privileges
sudo nano /etc/influxdb/influxdb.conf

# Configure Telegraf to send data to InfluxDB at localhost on port 8086
# Replace "my_database" with the name of your desired database
# Replace "my_username" with your desired username
# Replace "my_password" with your desired password
# Save and close the file


# Next, run the following command to configure InfluxDB


# End of script.

Add these lines to it:

# This code script is used to enable authentication for a website using the Hypertext Transfer Protocol (HTTP).

# The first line specifies the use of HTTP.
[http]

# The second line enables authentication for the website.
auth-enabled = true

# The following lines specify the type of authentication and the users who are allowed to access the website.
[auth]
type = basic
users = [
  "my_username:my_password" # This line specifies the username and password for a user who is allowed to access the website. The format is "username:password".
]

Replace `my_username` and `my_password` with your own values. This will enable authentication for InfluxDB, which is a good security practice. Save and close the file.

Restart both services to apply these changes:

# Restart Telegraf and InfluxDB services
sudo systemctl restart telegraf influxdb
# The above command will restart both services to apply changes made to the configuration file

# Replace `my_username` and `my_password` with your own values
# This will enable authentication for InfluxDB, which is a good security practice
# Save and close the file before running the restart command
# This ensures that the changes made to the configuration file are applied before restarting the services

Now that we have everything set up, let’s test it out by running this command in our terminal:


# This script runs the Telegraf agent with root privileges and specifies the configuration file to use
sudo telegraf agent -config /etc/telegraf/telegraf.conf

This will start the Telegraf service and send data to InfluxDB. You can check if everything is working correctly by opening your web browser and going to http://localhost:8086. This should bring you to the InfluxDB dashboard, where you’ll see a list of databases and measurements (i.e., metrics).

That’s it! Now you can monitor various system metrics using Telegraf and visualize them in InfluxDB. You can also set up alerts based on specific thresholds to get notified when something goes wrong.

SICORPS