Prometheus and Grafana are today’s standards in the world of monitoring solutions. So, if you need an ideal system monitoring solution for the Linux environment, you should definitely consider installing these two with exporters best suited for your environment.
Today, we are going to use one of the best-known exporters called Node Exporter. It is used for scraping hardware and OS metrics exposed by *NIX kernels, written in Go with pluggable metric collectors. Node Exporter gives us general metrics like CPU, RAM, disk usage, and so on. For the purpose of this task, I will be using CentOS 7.6 OpenVZ VPS. So, let’s cut right to the chase and open some terminals.
System info:
[root@nikola ~]# cat /etc/*release
CentOS Linux release 7.6.1810 (Core)
NAME="CentOS Linux"
VERSION="7 (Core)"
Now, let’s upgrade.
[root@nikola ~]# yum upgrade -y
On this occasion, we are going to do everything from the terminal.
Prometheus
Our first task is to install Prometheus.
Firstly, you need to download the Prometheus source code:
[root@nikola ~]# wget --progress=dot https://github.com/$(wget https://github.com/prometheus/prometheus/releases/ -O - | egrep '/.*/.*/*.linux-amd64.tar.gz' -o)
Secondly, you need to create the necessary directories:
[root@nikola ~]# mkdir -p /etc/prometheus
[root@nikola ~]# mkdir -p /var/lib/prometheus
Then, we are going to add a dedicated user without shell access and home directory:
[root@nikola ~]# useradd --no-create-home --shell /bin/false prometheus
Change the ownership of the previously created directories:
[root@nikola ~]# chown -R prometheus:prometheus /etc/prometheus
[root@nikola ~]# chown -R prometheus:prometheus /var/lib/prometheus
Unpack the Prometheus source code:
[root@nikola ~]# tar -xvzf prometheus-*.tar.gz
Copy the files to their dedicated paths:
[root@nikola ~]# cp -r prometheus-*/prometheus /usr/local/bin/
[root@nikola ~]# cp -r prometheus-*/promtool /usr/local/bin/
[root@nikola ~]# cp -r prometheus-*/consoles /etc/prometheus/
[root@nikola ~]# cp -r prometheus-*/console_libraries/ /etc/prometheus/
Change ownership:
[root@nikola ~]# chown prometheus:prometheus /usr/local/bin/prometheus
[root@nikola ~]# chown prometheus:prometheus /usr/local/bin/promtool
[root@nikola ~]# chown -R prometheus:prometheus /etc/prometheus/consoles
[root@nikola ~]# chown -R prometheus:prometheus /etc/prometheus/console_libraries
Copy the main Prometheus config file and changing its owner:
[root@nikola ~]# cp -r prometheus-*/prometheus.yml /etc/prometheus/
[root@nikola ~]# chown prometheus:prometheus /etc/prometheus/prometheus.yml
Now for the fun part.
Add custom systemd service:
[root@nikola ~]# cat <<EOF | sudo tee /etc/systemd/system/prometheus.service
[Unit]
Description=Prometheus
Wants=network-online.target
After=network-online.target
[Service]
User=prometheus
Group=prometheus
Type=simple
ExecStart=/usr/local/bin/prometheus \
--config.file /etc/prometheus/prometheus.yml \
--storage.tsdb.path /var/lib/prometheus/ \
--web.console.templates=/etc/prometheus/consoles \
--web.console.libraries=/etc/prometheus/console_libraries
[Install]
WantedBy=multi-user.target
EOF
Systemd daemon needs to be restarted since we have created a new service in the previous step:
[root@nikola ~]# systemctl daemon-reload
Then, it is time to start the Prometheus service and enable it to start automatically at boot:
[root@nikola ~]# systemctl enable --now prometheus && systemctl status prometheus
● prometheus.service - Prometheus
Loaded: loaded (/etc/systemd/system/prometheus.service; disabled; vendor preset: disabled)
Active: active (running) since Sun 2019-07-21 08:19:48 MDT; 10ms ago
Main PID: 1848 (prometheus)
CGroup: /system.slice/prometheus.service
└─1848 /usr/local/bin/prometheus --config.file /etc/prometheus/prometheus.yml --storage.tsdb.path /var/lib/prometheus/ --web.console.templates=/etc/prometheus/consoles --web.console.libraries=/etc/prometheus/console_libraries...
Provided that you have done all the steps correctly, you should have a fully functional Prometheus by now.
Node Exporter
Secondly, we need to install Node Exporter:
[root@nikola ~]# wget --progress=dot https://github.com/$(wget https://github.com/prometheus/node_exporter/releases/ -O - | egrep '/.*/.*/*.linux-amd64.tar.gz' -o)
Adding a dedicated user:
[root@nikola ~]# useradd --no-create-home --shell /bin/false nodeusr
Unpacking:
[root@nikola ~]# tar -xvzf node_exporter-*.tar.gz
Copying:
[root@nikola ~]# cp -r node_exporter-*/node_exporter /usr/local/bin/
Ownership:
[root@nikola ~]# chown nodeusr:nodeusr /usr/local/bin/node_exporter
Adding systemd service:
[root@nikola ~]# cat <<EOF | sudo tee /etc/systemd/system/node_exporter.service
[Unit]
Description=Node Exporter
After=network.target
[Service]
User=nodeusr
Group=nodeusr
Type=simple
ExecStart=/usr/local/bin/node_exporter
[Install]
WantedBy=multi-user.target
EOF
Systemd daemon reload:
[root@nikola ~]# systemctl daemon-reload
Starting the Node Exporter service and enabling it at boot sequence:
[root@nikola ~]# systemctl enable --now node_exporter && systemctl status node_exporter
● node_exporter.service - Node Exporter
Loaded: loaded (/etc/systemd/system/node_exporter.service; disabled; vendor preset: disabled)
Active: active (running) since Sun 2019-07-21 08:22:59 MDT; 7ms ago
Main PID: 1901 (node_exporter)
CGroup: /system.slice/node_exporter.service
└─1901 /usr/local/bin/node_exporter
Jul 21 08:22:59 nikola systemd[1]: Started Node Exporter.
Grafana
The last step is installing Grafana. We will be using the official Grafana repo as our installation source.
Firstly, add repo:
[root@nikola ~]# cat <<EOF | sudo tee /etc/yum.repos.d/grafana.repo
[grafana]
name=grafana
baseurl=https://packages.grafana.com/oss/rpm
repo_gpgcheck=1
enabled=1
gpgcheck=1
gpgkey=https://packages.grafana.com/gpg.key
sslverify=1
sslcacert=/etc/pki/tls/certs/ca-bundle.crt
EOF
Then, install Grafana:
[root@nikola ~]# yum install grafana -y
Finally, start Grafana and check its status:
[root@nikola ~]# systemctl enable --now grafana-server && systemctl status grafana-server
● grafana-server.service - Grafana instance
Loaded: loaded (/usr/lib/systemd/system/grafana-server.service; disabled; vendor preset: disabled)
Active: active (running) since Sun 2019-07-21 08:25:10 MDT; 6ms ago
Docs: http://docs.grafana.org
Main PID: 2018 (grafana-server)
CGroup: /system.slice/grafana-server.service
└─2018 /usr/sbin/grafana-server --config=/etc/grafana/grafana.ini --pidfile=/var/run/grafana/grafana-server.pid --packaging=rpm cfg:default.paths.logs=/var/log/grafana cfg:default.paths.data=/var/lib/grafana cfg:default.path...
If you have done everything right, you will be able to access your new shining monitoring at:
http://FQDN or IP:3000
Default user: admin
Default password: admin
In case you are experiencing any issues while opening the URL, please check your firewall settings and open ports.
Wrap Up
Well done! Your ideal system monitoring solution is set and ready to use! When we meet again we will create custom graphs, set up email and other channels of notifications, and much more. Until then, make sure to read our Blog and follow us on Social Networks to keep up with our latest innovations. Of course, you are welcome to contact us at [email protected] in case you have any questions regarding any aspect of our expertise.
#Until next time.
#Kind regards
#Nikola Ilic
#Senior Linux System Administrator / DevOps Rookie