How to install and configure Prometheus Node Exporter (node_exporter) as a service on CentOS 7 & CentOS 6

Arif
4 min readDec 8, 2020

--

Once you setup the Prometheus server, you need to configure it to scrape metrics from the remote servers. The default exporter for collecting System level metrics is node_exporter.

The node_exporter collects all system level metrics and expose on /metrics endpoint.

The default port number of node_exporter is 9100. Once you setup and start the node_exporter on your system, you can start collecting Metrics from your IP:9100/metrics.

You can initiate a curl call to fetch the metrics and expose it. In the similar way, Prometheus will initiate a connection to this end point and scrape metrics from the server.

And now you have data in your Prometheus server, now you can start visualize it from Prometheus or from the Grafana dashboard which has the data source as your Prometheus server. In this blog post, I will explain how we can install and configure node_exporter on CentOS6 / CentOS7 servers along with the initd/systemd script for managing node_exporter process.

Prerequisites

  • CentOS 6 or CentOS 7 server.
  • root / sudo access.
  • Connectivity should be enabled from Prometheus server to the end server on port number 9100 to scrape the metrics.

Install node_exporter

It’s not a big deal. Just download the tar, extract and run that’s it! Please do follow the steps below to install node_exporter for collecting system metrics:

Step 1: We need a user. Create a system user for the installation.

sudo useradd -rs /bin/false node_exporter

Step 2: Log into node_exporter user and download the latest node_exporter package.

Update: 8 Feb 2023

wget https://github.com/prometheus/node_exporter/releases/download/v1.5.0/node_exporter-1.5.0.linux-arm64.tar.gz

Step 3: Extract & move the node_exporter package and rename the directory to ‘node_exporter’ for your convenience.

sudo tar -xzvf node_exporter-1.5.0.linux-amd64.tar.gzsudo mv node_exporter-1.5.0.linux-amd64/node_exporter /usr/local/bin

Step 4: Enable 9100 for your Prometheus server.

You have to open port 9100 for your Prometheus servers, so that the Prometheus can scrape the data from node_exporter.

If you are using CentOS 7 server:

sudo firewall-cmd --add-port=9100/tcp
sudo firewall-cmd --reload

If you are using CentOS 6 server:

sudo iptables -A INPUT -i eth0 -p tcp -s x.x.x.x --dport 9100 -m state --state NEW -j ACCEPT

x.x.x.x --> Is the Prometheus server IP address

Step 5: Create a service file

Now we just startup the process in foreground. We have run the process in background as a daemon. So we need a service file to manage the process.

CentOS 7 service file for node_exporter

Create a file /etc/systemd/system/node_exporter.service with following content.

[Unit]
Description=Node Exporter
[Service]
User=node_exporter
EnvironmentFile=/etc/sysconfig/node_exporter
ExecStart=/usr/local/bin/node_exporter $OPTIONS
[Install]
WantedBy=multi-user.target

Since we have created a new unit file, we must reload the systemd daemon, set the service to always run at boot and start it :

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

CentOS 6 init file for node_exporter

If you are using CentOS 6 init based server, you can use the below script for managing node_exporter as a service.

File: /etc/init.d/node_exporter
Command: sudo service node_exporter status|stop|start

#!/bin/bash# chkconfig: 345 20 80
# description: my service
345 - 3,4,5 runlevels
20 - start priority
80- stop prioroty
OPTIONS=`cat /etc/sysconfig/node_exporter`
RETVAL=0
PROG="node_exporter"
EXEC="/usr/local/bin/node_exporter"
LOCKFILE="/var/lock/subsys/$PROG"
LOGFILE=/var/log/node_exporter.log
ErrLOGFILE=/var/log/node_exporter_error.log
# Source function library.
if [ -f /etc/rc.d/init.d/functions ]; then
. /etc/rc.d/init.d/functions
else
echo "/etc/rc.d/init.d/functions is not exists"
exit 0
fi
start() {
if [ -f $LOCKFILE ]
then
echo "$PROG is already running!"
else
echo -n "Starting $PROG: "
nohup $EXEC $OPTIONS > $LOGFILE 2> $ErrLOGFILE &
RETVAL=$?
[ $RETVAL -eq 0 ] && touch $LOCKFILE && success || failure
echo
return $RETVAL
fi
}
stop() {
echo -n "Stopping $PROG: "
killproc $EXEC
RETVAL=$?
[ $RETVAL -eq 0 ] && rm -r $LOCKFILE && success || failure
echo
}
restart ()
{
stop
sleep 1
start
}
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status $PROG
;;
restart)
restart
;;
*)
echo "Usage: $0 {start|stop|restart|status}"
exit 1
esac
exit $RETVAL

Make both files executable:

sudo chmod +x /etc/init.d/node_exporter

Test the script:

sudo /etc/init.d/node_exporter start
sudo /etc/init.d/node_exporter stop

Enable at startup with chkconfig

sudo chkconfig --add node_exporter

Here also we are passing the node_exporter options through this file “/etc/sysconfig/node_exporter”

That’s it. Now the process is up. We are good with node_exporter side.

Step 6: Configure Prometheus to scrape metrics

Open the Prometheus configuration file (on Prometheus Server) and add your server IP/hostname with port, then restart the Prometheus process.

sudo vim /etc/prometheus/prometheus.yml

Under the ‘scrape_config’ line, add new job_name node_exporter by copy-pasting the configuration below.

- job_name: 'node_exporter'
static_configs:
...
- targets: ['CLIEN-SERVER-IP:9100']
...

Save and exit. Then restart Prometheus. Step 7 will vary based on you Prometheus server configuration. This is the default basic configuration.

Now you can start visualizing your system metrics.

--

--