Anasayfa / Software / Mastering Server Health Monitoring with Prometheus and Grafana: An Advanced Guide

Mastering Server Health Monitoring with Prometheus and Grafana: An Advanced Guide

server monitoring

Keeping your servers humming along is a non‑negotiable part of modern infrastructure management. While basic uptime checks are useful, they barely scratch the surface of what true observability can offer. In this guide, we’ll walk through an advanced, end‑to‑end setup for monitoring server health using Prometheus and Grafana. You’ll get hands‑on with real commands, configuration snippets, and best‑practice tips that will elevate your monitoring game from rudimentary alerts to actionable insights.

What You’ll Need

  • A Linux server (Ubuntu 22.04 LTS recommended) with sudo privileges.
  • Internet access to download binaries and Docker images.
  • Basic familiarity with systemd services and YAML.
  • Port 9090 (Prometheus) and 3000 (Grafana) open on your firewall.
  • Optional: Docker Engine if you prefer containerized installations.

Step 1: Install Prometheus

First, download the latest stable Prometheus release. As of this writing, version 2.53.0 is current. Execute the following commands:

wget https://github.com/prometheus/prometheus/releases/download/v2.53.0/prometheus-2.53.0.linux-amd64.tar.gz
 tar xvf prometheus-2.53.0.linux-amd64.tar.gz
 sudo mv prometheus-2.53.0.linux-amd64 /opt/prometheus
 sudo useradd --no-create-home --shell /usr/sbin/nologin prometheus
 sudo chown -R prometheus:prometheus /opt/prometheus

Create a dedicated systemd unit file 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=/opt/prometheus/prometheus.yml 
  --storage.tsdb.path=/opt/prometheus/data 
  --web.console.templates=/opt/prometheus/consoles 
  --web.console.libraries=/opt/prometheus/console_libraries

[Install]
WantedBy=multi-user.target
EOF

Now, craft a minimal prometheus.yml that scrapes the node exporter (which we’ll install next). Place it at /opt/prometheus/prometheus.yml:

global:
  scrape_interval: 15s
  evaluation_interval: 15s

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

Reload systemd and start Prometheus:

sudo systemctl daemon-reload
sudo systemctl enable prometheus
sudo systemctl start prometheus

Verify it’s running by visiting http://localhost:9090 in your browser.

Step 2: Deploy Node Exporter

Node Exporter is the gold‑standard exporter for exposing host‑level metrics (CPU, memory, disk, network, etc.). Install it as follows:

wget https://github.com/prometheus/node_exporter/releases/download/v1.8.0/node_exporter-1.8.0.linux-amd64.tar.gz
 tar xvf node_exporter-1.8.0.linux-amd64.tar.gz
 sudo mv node_exporter-1.8.0.linux-amd64/node_exporter /usr/local/bin/
 sudo useradd --no-create-home --shell /usr/sbin/nologin nodeusr
 sudo chown nodeusr:nodeusr /usr/local/bin/node_exporter

Create a systemd service for it:

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

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

[Install]
WantedBy=default.target
EOF

Start and enable the service:

sudo systemctl daemon-reload
sudo systemctl enable node_exporter
sudo systemctl start node_exporter

Confirm metrics are exposed by curling http://localhost:9100/metrics. You should see a long list of # HELP lines.

Step 3: Install Grafana

Grafana provides the visual layer. You can install it via the official APT repository for the cleanest experience:

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

Enable and start Grafana:

sudo systemctl enable grafana-server
sudo systemctl start grafana-server

Open http://localhost:3000. The default credentials are admin / admin. You’ll be prompted to change the password on first login.

Step 4: Connect Grafana to Prometheus

Within Grafana, navigate to Configuration → Data Sources → Add data source. Choose “Prometheus” and fill in:

  • URL: http://localhost:9090
  • Access: Server (default)

Click “Save & Test”. A green message confirms the connection.

Step 5: Import a Ready‑Made Dashboard

Grafana’s community offers a plethora of dashboards. For server health, dashboard ID 1860 (Node Exporter Full) is a solid starting point. To import:

  1. Click the “+” icon → “Import”.
  2. Enter “1860” in the “Grafana.com Dashboard” field.
  3. Select your Prometheus data source.
  4. Click “Import”.

The dashboard will instantly populate with CPU, memory, filesystem, and network panels.

Step 6: Set Up Alerting

Prometheus alerting rules live in a separate file, referenced from prometheus.yml. Create /opt/prometheus/alerts.yml with a couple of critical rules:

groups:
- name: server-health
  rules:
  - alert: HighCPUUsage
    expr: 100 - (avg by(instance) (irate(node_cpu_seconds_total{mode="idle"}[5m])) * 100) > 85
    for: 2m
    labels:
      severity: critical
    annotations:
      summary: "CPU usage over 85% on {{ $labels.instance }}"
      description: "CPU has been above 85% for more than 2 minutes."

  - alert: LowDiskSpace
    expr: node_filesystem_avail_bytes{mountpoint="/"} / node_filesystem_size_bytes{mountpoint="/"} < 0.10
    for: 5m
    labels:
      severity: warning
    annotations:
      summary: "Low disk space on {{ $labels.instance }}"
      description: "Less than 10% free space on root partition."

Reference this file in prometheus.yml:

rule_files:
  - "alerts.yml"

Reload Prometheus without downtime:

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

Grafana can now surface these alerts via its “Alerting” UI or forward them to external receivers (Slack, PagerDuty, etc.) using the Alerting → Notification channels page.

Step 7: Harden and Scale

For production environments, consider these enhancements:

  • TLS Encryption: Terminate TLS at a reverse proxy (NGINX) for both Prometheus and Grafana. Example NGINX snippet:
server {
    listen 443 ssl;
    server_name prometheus.example.com;
    ssl_certificate /etc/ssl/certs/fullchain.pem;
    ssl_certificate_key /etc/ssl/private/privkey.pem;
    location / {
        proxy_pass http://localhost:9090;
        proxy_set_header Host $host;
    }
}
  • Authentication: Enable basic auth on the reverse proxy or use OAuth with Grafana.
  • High Availability: Run multiple Prometheus instances with federation or use Thanos/ Cortex for long‑term storage.
  • Retention: Adjust --storage.tsdb.retention.time=30d flag to keep data for a month.

Common Mistakes to Avoid

Even seasoned engineers stumble over a few recurring pitfalls:

  • Scrape interval too low: Setting scrape_interval to 1s can overwhelm both Prometheus and target services. Stick to 15s‑30s for most server metrics.
  • Forgetting firewall rules: If ports 9090 or 3000 are blocked, the UI will be unreachable and alerts won’t fire.
  • Mismatched target addresses: Using localhost in prometheus.yml works only when Prometheus and exporters share the same host. For remote nodes, list their IPs or DNS names.
  • Neglecting service restarts: After editing prometheus.yml, always reload or restart the service; otherwise changes are ignored.
  • Over‑reliance on default dashboards: Community dashboards are great, but they may include panels you never use, adding noise. Trim them to focus on your KPIs.

Tips and Tricks

Here are a few pro‑level shortcuts:

  • Use recording rules: Pre‑aggregate expensive queries (e.g., average CPU per host) to reduce query latency.
  • Leverage Grafana templating: Create a variable $instance to switch between servers on the same dashboard.
  • Export alerts to multiple channels: Configure a “Contact point” in Grafana’s unified alerting to fan‑out to Slack, Email, and Opsgenie simultaneously.
  • Enable Prometheus remote write: Push metrics to a cloud‑hosted TSDB (e.g., Cortex) for global visibility across clusters.
  • Monitor Prometheus itself: Add the prometheus job to scrape its own /metrics endpoint and watch for scrape failures.

Frequently Asked Questions

Can I run Prometheus and Grafana on the same server?

Yes, for small‑to‑medium workloads. Just ensure the host has enough CPU and RAM (2‑4 GB RAM is a good baseline). For larger fleets, separate them to avoid resource contention.

Do I need to install an exporter for every service?

Not necessarily. The Node Exporter covers OS‑level metrics. For application‑specific metrics, instrument your code with Prometheus client libraries or use ready‑made exporters (e.g., mysqld_exporter, blackbox_exporter).

How do I back up my Grafana dashboards?

Grafana stores dashboards in a SQLite database by default (/var/lib/grafana/grafana.db). You can back up this file or use the “Export” button on each dashboard to save JSON definitions.

Conclusion

By now you should have a fully functional monitoring stack that captures low‑level server health, visualizes it in rich Grafana dashboards, and alerts you before issues become outages. While the steps above cover a single‑node setup, the same principles scale to multi‑node clusters, Kubernetes environments, and hybrid cloud architectures. Remember to iterate on your dashboards, refine alert thresholds, and keep security front‑and‑center. With Prometheus and Grafana in your toolbox, you’ll move from reactive firefighting to proactive, data‑driven operations.

Photo by Kevin Ache on Unsplash

Etiketlendi: