DEV Community

Cover image for How to set up a system monitoring for linux server using node exporter, prometheus & grafana
coder7475
coder7475

Posted on • Edited on

3 1 1 1 1

How to set up a system monitoring for linux server using node exporter, prometheus & grafana

Monitoring your Linux VPS is essential to track system performance and prevent potential issues. In this guide, you'll learn how to set up monitoring using Node Exporter, Prometheus, and Grafana Cloud.


1. Install and Configure Node Exporter

Node Exporter collects system metrics from the Linux VPS.

Step 1: Download Node Exporter

wget https://github.com/prometheus/node_exporter/releases/download/v1.9.0/node_exporter-1.9.0.linux-amd64.tar.gz
Enter fullscreen mode Exit fullscreen mode

Step 2: Extract and Move the Binary

tar -xvf node_exporter-1.9.0.linux-amd64.tar.gz
sudo mv node_exporter-1.9.0.linux-amd64/node_exporter /usr/local/bin/
Enter fullscreen mode Exit fullscreen mode

Step 3: Create a System User

sudo useradd --system --no-create-home --shell /bin/false node_exporter
Enter fullscreen mode Exit fullscreen mode

Step 4: Set Up Node Exporter as a Service

Create the service file:

sudo vim /etc/systemd/system/node_exporter.service
Enter fullscreen mode Exit fullscreen mode

Add the following content:

[Unit]
Description=Node Exporter
After=network.target
Wants=network-online.target

[Service]
User=node_exporter
Group=node_exporter
Type=simple

ExecStart=/usr/local/bin/node_exporter

# Securtiy Settings
ProtectHome=true
NoNewPrivileges=true
ProtectSystem=strict
PrivateTmp=true
PrivateDevices=true
ProtectKernelTunables=true
ProtectKernelModules=true
ProtectControlGroups=true
RestrictNamespaces=true
RestrictRealtime=true
RestrictSUIDSGID=true

# Resources
LimitNPROC=8192
LimitNOFILE=65535
LimitCORE=infinity
# Restart
Restart=always
RestartSec=5

[Install]
WantedBy=multi-user.target
Enter fullscreen mode Exit fullscreen mode

Step 5: Start and Enable Node Exporter

sudo systemctl daemon-reload
sudo systemctl start node_exporter
sudo systemctl enable node_exporter
Enter fullscreen mode Exit fullscreen mode

Step 6: Verify Node Exporter

ss -aplnt | grep 9100
curl http://localhost:9100/metrics
Enter fullscreen mode Exit fullscreen mode

2. Install and Configure Prometheus

Prometheus scrapes metrics from Node Exporter.

Step 1: Download Prometheus

wget https://github.com/prometheus/prometheus/releases/download/v3.2.0/prometheus-3.2.0.linux-amd64.tar.gz
Enter fullscreen mode Exit fullscreen mode

Step 2: Extract and Move Files

tar -xvf prometheus-3.2.0.linux-amd64.tar.gz
sudo mv prometheus-3.2.0.linux-amd64/prometheus /usr/local/bin/
sudo mv prometheus-3.2.0.linux-amd64/promtool /usr/local/bin/
sudo mkdir -p /etc/prometheus /var/lib/prometheus/data /var/lib/prometheus/query_log
sudo mv prometheus-3.2.0.linux-amd64/prometheus.yml /etc/prometheus/
Enter fullscreen mode Exit fullscreen mode

Step 3: Edit the Configuration File

sudo vim /etc/prometheus/prometheus.yml
Enter fullscreen mode Exit fullscreen mode

Add:

scrape_configs:
  - job_name: 'node'
    static_configs:
      - targets: ['localhost:9100', 'localhost:9090']
    scrape_interval: 15s
    scrape_timeout: 5s
Enter fullscreen mode Exit fullscreen mode

Verify the config:

promtool check config /etc/prometheus/prometheus.yml
Enter fullscreen mode Exit fullscreen mode

Step 4: Set Up Prometheus as a Service

sudo useradd -rs /bin/false prometheus
Enter fullscreen mode Exit fullscreen mode
sudo chown -R prometheus:prometheus /etc/prometheus /var/lib/prometheus /usr/local/bin/prometheus /usr/local/bin/promtool
sudo chmod -R 755 /var/lib/prometheus
Enter fullscreen mode Exit fullscreen mode

Create the service file:

sudo vim /etc/systemd/system/prometheus.service
Enter fullscreen mode Exit fullscreen mode

Add:

[Unit]
Description=Prometheus Monitoring System
Documentation=https://prometheus.io/docs/prometheus/latest/installation/
After=network-online.target
Wants=network-online.target

[Service]
Type=simple
User=prometheus
Group=prometheus
WorkingDirectory=/var/lib/prometheus

ExecStart=/usr/local/bin/prometheus \
    --config.file=/etc/prometheus/prometheus.yml \
    --storage.tsdb.path=/var/lib/prometheus/data \
    --web.console.templates=/etc/prometheus/consoles \
    --web.console.libraries=/etc/prometheus/console_libraries \
    --query.lookback-delta=5m \
    --enable-feature=memory-snapshot-on-shutdown

# Security Settings
ProtectHome=true
NoNewPrivileges=true
ProtectSystem=false
PrivateTmp=true
PrivateDevices=true
ProtectKernelTunables=true
ProtectKernelModules=true
ProtectControlGroups=true
RestrictNamespaces=true
RestrictRealtime=true
RestrictSUIDSGID=true

# Resource Limits
LimitNOFILE=65535
LimitNPROC=4096
LimitCORE=infinity

# Restart Configuration
Restart=always
RestartSec=5

[Install]
WantedBy=multi-user.target
Enter fullscreen mode Exit fullscreen mode

Step 5: Start and Enable Prometheus

sudo systemctl daemon-reload
sudo systemctl start prometheus
sudo systemctl enable prometheus
Enter fullscreen mode Exit fullscreen mode

Step 6: Verify Prometheus

ss -aplnt | grep 9090
curl localhost:9090/metrics
Enter fullscreen mode Exit fullscreen mode

3. Set Up Grafana Cloud Dashboard

Grafana visualizes metrics collected by Prometheus.

Step 1: Sign Up for Grafana Cloud

  • Create an account on Grafana Cloud.
  • Obtain your Prometheus remote write URL and API key.

Step 2: Configure Prometheus for Remote Write

Edit /etc/prometheus/prometheus.yml:

remote_write:
  - url: "https://prometheus-blocks-prod-us-central1.grafana.net/api/v1/write"
    basic_auth:
      username: "<your-grafana-cloud-instance-id>"
      password: "<your-api-key>"
Enter fullscreen mode Exit fullscreen mode

Step 3: Restart Prometheus

sudo systemctl restart prometheus
Enter fullscreen mode Exit fullscreen mode

Step 4: Import Dashboards in Grafana Cloud


4. Verify Setup

  • Access Grafana Cloud at https://<your-grafana-instance>.grafana.net.
  • View metrics such as CPU usage, memory consumption, and disk I/O in real-time.

References

  1. Installing Prometheus and Node Exporter
  2. Step-by-step guide for Prometheus and Grafana
  3. Configuring Prometheus Remote Write
  4. How to Monitor Linux Servers with Prometheus
  5. Official Prometheus Documentation

Now, your Linux VPS is fully monitored with Node Exporter, Prometheus, and Grafana Cloud. 🎉 Let me know in the comments if you have any questions! 🚀

Image of Datadog

Create and maintain end-to-end frontend tests

Learn best practices on creating frontend tests, testing on-premise apps, integrating tests into your CI/CD pipeline, and using Datadog’s testing tunnel.

Download The Guide

Top comments (0)

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay