Anasayfa / Software / How to Monitor Server Metrics with Prometheus and Grafana – A Beginner’s Guide

How to Monitor Server Metrics with Prometheus and Grafana – A Beginner’s Guide

Prometheus Grafana

Keeping an eye on the health of your servers is essential, but you don’t need a PhD in observability to get started. In this guide we’ll walk you through installing Prometheus, a powerful open‑source metrics collector, and Grafana, a flexible visualization platform. By the end you’ll have a live dashboard that shows CPU usage, memory pressure, disk I/O, and more—all updated in real time. The steps are written for beginners, with clear commands, configuration snippets, and tips to avoid common pitfalls.

What You'll Need

  • A Linux server (Ubuntu 20.04+ or CentOS 8+ recommended)
  • Root or sudo access on the server
  • Basic familiarity with the command line
  • An internet connection to download packages
  • A web browser to view Grafana dashboards

Step 1: Install Prometheus

First, download the latest stable release of Prometheus from the official website. On Ubuntu you can run:

sudo apt-get update && sudo apt-get install -y wget tar
wget https://github.com/prometheus/prometheus/releases/download/v2.53.0/prometheus-2.53.0.linux-amd64.tar.gz
sudo tar -xvf prometheus-2.53.0.linux-amd64.tar.gz -C /opt/
sudo mv /opt/prometheus-2.53.0.linux-amd64 /opt/prometheus
sudo useradd --no-create-home --shell /bin/false prometheus
sudo chown -R prometheus:prometheus /opt/prometheus

Create a basic configuration file (/etc/prometheus/prometheus.yml) that tells Prometheus where to scrape metrics from. A minimal example looks like this:

global:
  scrape_interval: 15s
  evaluation_interval: 15s

scrape_configs:
  - job_name: 'node_exporter'
    static_configs:
      - targets: ['localhost:9100']

Finally, set up a systemd service so Prometheus starts on boot:

sudo tee /etc/systemd/system/prometheus.service > /dev/null <<EOF
[Unit]
Description=Prometheus Monitoring
Wants=network-online.target
After=network-online.target

[Service]
User=prometheus
Group=prometheus
Type=simple
ExecStart=/opt/prometheus/prometheus 
  --config.file=/etc/prometheus/prometheus.yml 
  --storage.tsdb.path=/var/lib/prometheus 
  --web.console.templates=/opt/prometheus/consoles 
  --web.console.libraries=/opt/prometheus/console_libraries

[Install]
WantedBy=multi-user.target
EOF
sudo systemctl daemon-reload
sudo systemctl enable prometheus
sudo systemctl start prometheus

Visit http://your_server_ip:9090 to verify the UI is reachable.

Step 2: Configure Prometheus to Scrape Metrics

Prometheus can collect metrics from any HTTP endpoint that exposes data in the Prometheus text format. The most common source for server‑level metrics is the Node Exporter (covered in Step 3). Add additional scrape_configs for any services you want to monitor, such as MySQL, Nginx, or custom applications.

For example, to monitor a Docker daemon, add:

- job_name: 'docker'
  static_configs:
    - targets: ['localhost:9323']

After editing prometheus.yml, reload the configuration without restarting the service:

curl -X POST http://localhost:9090/-/reload

If the reload fails, check the Prometheus logs (journalctl -u prometheus -f) for syntax errors.

Step 3: Install Node Exporter on the Server

Node Exporter is a lightweight daemon that exposes hardware and OS metrics. Install it with the following commands:

wget https://github.com/prometheus/node_exporter/releases/download/v1.8.0/node_exporter-1.8.0.linux-amd64.tar.gz
sudo tar -xvf node_exporter-1.8.0.linux-amd64.tar.gz -C /opt/
sudo mv /opt/node_exporter-1.8.0.linux-amd64 /opt/node_exporter
sudo useradd --no-create-home --shell /bin/false nodeusr
sudo chown -R nodeusr:nodeusr /opt/node_exporter

Create a systemd unit:

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

[Service]
User=nodeusr
Group=nodeusr
Type=simple
ExecStart=/opt/node_exporter/node_exporter

[Install]
WantedBy=default.target
EOF
sudo systemctl daemon-reload
sudo systemctl enable node_exporter
sudo systemctl start node_exporter

Now Prometheus will start pulling metrics from http://localhost:9100/metrics. You can test it directly with curl http://localhost:9100/metrics.

Step 4: Install Grafana

Grafana provides beautiful graphs and alerting capabilities. On Ubuntu, the quickest way is to use the official APT repository:

sudo apt-get install -y apt-transport-https software-properties-common wget
wget -q -O - https://apt.grafana.com/gpg.key | sudo apt-key add -
sudo add-apt-repository "deb https://apt.grafana.com stable main"
sudo apt-get update
sudo apt-get install -y grafana
sudo systemctl enable grafana-server
sudo systemctl start grafana-server

Grafana listens on port 3000 by default. Open http://your_server_ip:3000 in a browser. The default login is admin / admin; you’ll be prompted to change the password on first login.

Step 5: Add Prometheus as a Data Source in Grafana

Once logged in, click the gear icon → Data SourcesAdd data source. Choose “Prometheus” from the list and fill in the details:

  • URL: http://localhost:9090
  • Access: Server (default)
  • Leave the rest as defaults and click “Save & Test”.

If Grafana reports “Data source is working”, you’re ready to build dashboards.

Step 6: Create Your First Dashboard and Set Up Alerts

Click the “+” icon → DashboardAdd new panel. In the query editor, select the Prometheus data source and type a simple query, for example:

node_cpu_seconds_total{mode="system"}

Grafana will render a time‑series graph. Use the panel settings to change the visualization type, add legends, and set a meaningful title like “System CPU Usage”.

To add an alert, switch to the “Alert” tab, click “Create Alert”, and define a rule such as “When average CPU usage over 5 minutes is above 80%”. Choose a notification channel (email, Slack, etc.) that you have configured under “Alerting → Notification channels”.

Repeat the process for other key metrics: memory (node_memory_MemAvailable_bytes), disk I/O (node_disk_io_time_seconds_total), and network traffic (node_network_receive_bytes_total). Save the dashboard and share the URL with your team.

Common Mistakes to Avoid

1 Forgetting to open firewall ports: Prometheus (9090), Node Exporter (9100), and Grafana (3000) must be reachable from your monitoring workstation. Use ufw allow 9090/tcp or the equivalent on your distro.

2 Mismatched scrape intervals: Setting a very short scrape_interval (e.g., 1s) can overload the server and increase storage usage. Stick with the default 15s unless you have a specific need.

3 Running services as root: Always create dedicated, non‑privileged users for Prometheus, Node Exporter, and Grafana. This limits the impact of a potential compromise.

4 Ignoring retention settings: By default Prometheus stores data for 15 days. Adjust --storage.tsdb.retention.time if you need longer history, but be aware of disk consumption.

5 Incorrect alert routing: Test each notification channel before relying on it. Grafana will silently drop alerts if the channel is misconfigured.

Tips and Tricks

• Use promtool check config /etc/prometheus/prometheus.yml to validate your config before reloading.

• Enable --web.enable-admin-api in Prometheus to allow remote reloads via API calls.

• Export Grafana dashboards as JSON (Dashboard → Settings → JSON Model) for version control.

• Combine multiple exporters (cAdvisor for containers, blackbox_exporter for endpoint health) to get a fuller picture.

Frequently Asked Questions

Do I need to install Prometheus on every server I want to monitor?

No. Prometheus follows a pull model, so you only need one central Prometheus instance. Each target server runs an exporter (like Node Exporter) that Prometheus scrapes over HTTP.

Can I monitor Windows servers with this setup?

Yes. Use the windows_exporter (formerly wmi_exporter) on Windows machines, then add its endpoint to prometheus.yml just like the Linux Node Exporter.

How much storage will Prometheus need for a month of data?

It depends on the number of metrics and scrape interval. As a rule of thumb, a single node exporter with default settings consumes roughly 2‑3 GB per month. Adjust retention or enable compression if storage becomes an issue.

Conclusion

Monitoring server health doesn’t have to be daunting. By installing Prometheus, exposing metrics with Node Exporter, and visualizing everything in Grafana, you gain real‑time insight into CPU, memory, disk, and network performance. Follow the steps above, watch out for the common mistakes, and you’ll have a robust observability stack that scales as your infrastructure grows. Happy monitoring!

Photo by Birmingham Museums Trust on Unsplash

Etiketlendi: