
Server Monitoring with Prometheus and Grafana
- AuthorBen H.
- Date
- Time19 minutes
Architecture Overview
Node Exporter runs on monitored servers. Prometheus scrapes these metrics. Grafana visualizes and alerts on top of Prometheus data.
Step 1: Install Node Exporter on Target Server
bash
cd /tmp
curl -LO https://github.com/prometheus/node_exporter/releases/download/v1.8.2/node_exporter-1.8.2.linux-amd64.tar.gz
tar xvf node_exporter-1.8.2.linux-amd64.tar.gz
sudo cp node_exporter-1.8.2.linux-amd64/node_exporter /usr/local/bin/
sudo useradd --no-create-home --shell /bin/false node_exporter || trueStep 2: Create systemd Service for Node Exporter
bash
sudo tee /etc/systemd/system/node_exporter.service >/dev/null <<'EOF'
[Unit]
Description=Node Exporter
After=network.target
[Service]
User=node_exporter
Group=node_exporter
Type=simple
ExecStart=/usr/local/bin/node_exporter
[Install]
WantedBy=multi-user.target
EOF
sudo systemctl daemon-reload
sudo systemctl enable --now node_exporter
sudo systemctl status node_exporter --no-pagerStep 3: Install Prometheus and Grafana via Docker Compose
bash
mkdir -p ~/monitoring/{prometheus,grafana}
cd ~/monitoringyaml
cat > prometheus/prometheus.yml <<'EOF'
global:
scrape_interval: 15s
scrape_configs:
- job_name: prometheus
static_configs:
- targets: ['localhost:9090']
- job_name: node_exporter
static_configs:
- targets: ['TARGET_SERVER_IP:9100']
EOFyaml
cat > docker-compose.yml <<'EOF'
services:
prometheus:
image: prom/prometheus:v2.54.1
container_name: prometheus
command:
- '--config.file=/etc/prometheus/prometheus.yml'
volumes:
- ./prometheus/prometheus.yml:/etc/prometheus/prometheus.yml:ro
ports:
- '9090:9090'
restart: unless-stopped
grafana:
image: grafana/grafana:11.1.4
container_name: grafana
ports:
- '3000:3000'
volumes:
- grafana_data:/var/lib/grafana
restart: unless-stopped
volumes:
grafana_data:
EOF
docker compose up -dStep 4: Add Prometheus Data Source in Grafana
Open Grafana at http://SERVER_IP:3000, log in with admin/admin, then add Prometheus at http://prometheus:9090.
Step 5: Import Dashboard
Import Node Exporter Full dashboard (ID 1860) and map it to your Prometheus data source.
Step 6: Create Basic Prometheus Alerts
yaml
cat > prometheus/alerts.yml <<'EOF'
groups:
- name: node_alerts
rules:
- alert: HostHighCPU
expr: 100 - (avg by(instance) (rate(node_cpu_seconds_total{mode="idle"}[5m])) * 100) > 85
for: 10m
labels:
severity: warning
annotations:
summary: "High CPU on {{ $labels.instance }}"
- alert: HostLowDisk
expr: (node_filesystem_avail_bytes{fstype!="tmpfs"} / node_filesystem_size_bytes{fstype!="tmpfs"}) * 100 < 15
for: 15m
labels:
severity: critical
annotations:
summary: "Low disk space on {{ $labels.instance }}"
EOFyaml
# Add to prometheus.yml
rule_files:
- /etc/prometheus/alerts.ymlbash
# Mount alerts file in docker-compose and restart
# - ./prometheus/alerts.yml:/etc/prometheus/alerts.yml:ro
docker compose restart prometheus"Monitoring is only useful if alerts are actionable and linked to an operational response."



