How to monitor your server's hardware with node_exporter?

How to monitor your server's hardware with node_exporter?
Photo by Daniel Korpai / Unsplash

Hello good people, today we are going to talk about how to monitor your VPS/VDS/Dedicated servers using node_exporter. Node_exporter is basically a service which is constantly running on your server and it collecting plenty of information about server's hardware.

Once node_exporter is up and running, our Prometheus will collect this information and Grafana, in turn, will collect it from Prometheus to create nice graphs and charts. Let's begin.

This article is based on great work of https://github.com/ExploreNYM/self-hosted-monitor

First we will enter a few variables. One will be node_exporter version, please check the latest version here (as of today, the latest version is 1.7.0):

node_exporter_version="1.8.2"

We only want our own Prometheus to collect stats from this server, so we are going to limit connections later, for now let's set up Prometheus IP as a variable:

read -p "Enter the Prometheus server IP (e.g., 103.127.133.15): " prometheus_ip

It is a good practice to run apps from a dedicated user's account, without root access. Let's create a user without a home directory:

useradd --no-create-home --shell /bin/false node_exporter

Let's download node_exporter to a temp location:

wget "https://github.com/prometheus/node_exporter/releases/download/v$node_exporter_version/node_exporter-$node_exporter_version.linux-amd64.tar.gz" -O /tmp/node_exporter-$node_exporter_version.linux-amd64.tar.gz

Unarchive it:

tar xvfz /tmp/node_exporter-$node_exporter_version.linux-amd64.tar.gz -C /tmp

Move the binary to the usual location

mv /tmp/node_exporter-$node_exporter_version.linux-amd64/node_exporter /usr/local/bin/node_exporter

Change the ownership of the binary

chown node_exporter:node_exporter /usr/local/bin/node_exporter

chmod 0755 /usr/local/bin/node_exporter

Almost there! Let's now create a service file to run node_exporter as a daemon:

cat <<EOF > /etc/systemd/system/node_exporter.service
[Unit]
Description=Node Exporter
Wants=network-online.target
After=network-online.target

[Service]
User=node_exporter
Group=node_exporter
Type=simple
ExecStart=/usr/local/bin/node_exporter

[Install]
WantedBy=multi-user.target
EOF

Now we need to activate the service and start it:

systemctl daemon-reload
systemctl enable node_exporter.service
systemctl start node_exporter.service

Just in case let's check the status of our new service:

systemctl status node_exporter

If you see green "running" you are good to go. If there is anything else, you need to check logs to understand why the service failed to load.

Remember I mentioned about the security? Let's now limit connection to node_exporter to our Prometheus machine only:

ufw allow from $prometheus_ip to any port 9100 comment 'Allow Prometheus server to access node exporter'

And remove the files we do not need:

rm -rf /tmp/node_exporter-${node_exporter_version}.linux-amd64.tar.gz
rm -rf /tmp/node_exporter-${node_exporter_version}.linux-amd64