Monitoreo Linux con Prometheus y Grafana: del cero al dashboard
Prometheus + Grafana es la stack de monitoreo estándar del mercado DevOps. Prometheus recolecta y almacena métricas en serie temporal; Grafana transforma esas métricas en dashboards y alertas visuales. Juntos, sustituyen herramientas más antiguas como Nagios y Zabbix en ambientes modernos.
Arquitectura Básica
Servidor Linux
└── Node Exporter (:9100) ← expõe métricas do SO em /metrics
Servidor de Monitoramento
├── Prometheus (:9090) ← faz scrape das métricas a cada 15s
│ └── AlertManager ← dispara alertas (email, Slack, PagerDuty)
└── Grafana (:3000) ← dashboard que lê do Prometheus
Instalando Node Exporter (en los servidores a monitorear)
# Baixar e instalar
VERSION="1.8.2"
wget https://github.com/prometheus/node_exporter/releases/download/v${VERSION}/node_exporter-${VERSION}.linux-amd64.tar.gz
tar xzf node_exporter-*.tar.gz
sudo mv node_exporter-*/node_exporter /usr/local/bin/
sudo chmod +x /usr/local/bin/node_exporter
# Criar service systemd
sudo tee /etc/systemd/system/node_exporter.service <<'EOF'
[Unit]
Description=Node Exporter
After=network.target
[Service]
Type=simple
ExecStart=/usr/local/bin/node_exporter
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
sudo systemctl daemon-reload
sudo systemctl enable --now node_exporter
# Verificar métricas expostas
curl http://localhost:9100/metrics | head -30
Instalando Prometheus
VERSION="2.53.0"
wget https://github.com/prometheus/prometheus/releases/download/v${VERSION}/prometheus-${VERSION}.linux-amd64.tar.gz
tar xzf prometheus-*.tar.gz
sudo mv prometheus-*/prometheus /usr/local/bin/
sudo mv prometheus-*/promtool /usr/local/bin/
# Diretórios
sudo mkdir -p /etc/prometheus /var/lib/prometheus
sudo mv prometheus-*/prometheus.yml /etc/prometheus/
# Service systemd
sudo tee /etc/systemd/system/prometheus.service <<'EOF'
[Unit]
Description=Prometheus
After=network.target
[Service]
Type=simple
ExecStart=/usr/local/bin/prometheus --config.file=/etc/prometheus/prometheus.yml --storage.tsdb.path=/var/lib/prometheus --storage.tsdb.retention.time=30d
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
sudo systemctl daemon-reload
sudo systemctl enable --now prometheus
Configurando prometheus.yml
# /etc/prometheus/prometheus.yml
global:
scrape_interval: 15s # coleta métricas a cada 15 segundos
evaluation_interval: 15s
scrape_configs:
- job_name: "prometheus"
static_configs:
- targets: ["localhost:9090"]
- job_name: "servidores_linux"
static_configs:
- targets:
- "192.168.1.10:9100" # web01
- "192.168.1.11:9100" # web02
- "192.168.1.20:9100" # db01
labels:
ambiente: "producao"
# Validar e recarregar configuração
promtool check config /etc/prometheus/prometheus.yml
sudo systemctl reload prometheus
Instalando Grafana
# Ubuntu/Debian
sudo apt install -y apt-transport-https software-properties-common wget
wget -q -O - https://apt.grafana.com/gpg.key | sudo gpg --dearmor -o /usr/share/keyrings/grafana.gpg
echo "deb [signed-by=/usr/share/keyrings/grafana.gpg] https://apt.grafana.com stable main" | sudo tee /etc/apt/sources.list.d/grafana.list
sudo apt update && sudo apt install grafana -y
sudo systemctl enable --now grafana-server
# Acesse: http://SEU_IP:3000 | admin / admin (troque na primeira vez)
Creando el Primer Dashboard
- En Grafana: Connections → Add data source → Prometheus
URL:http://localhost:9090→ Save & Test - Dashboards → Import → ID 1860 (Node Exporter Full — dashboard público con +30 paneles listos)
- Seleccione el data source Prometheus y haga clic en Import
El dashboard 1860 ya muestra CPU, RAM, disco, red, carga del sistema y uptime de todos sus servidores en tiempo real, sin escribir ninguna query.
Queries PromQL Básicas
# % de CPU em uso (total - idle)
100 - (avg by(instance)(rate(node_cpu_seconds_total{mode="idle"}[5m])) * 100)
# % de memória disponível
(node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes) * 100
# % de disco usado
100 - ((node_filesystem_avail_bytes{mountpoint="/"} /
node_filesystem_size_bytes{mountpoint="/"}) * 100)
# Bytes recebidos por segundo na eth0
rate(node_network_receive_bytes_total{device="eth0"}[5m])
Alertas
# /etc/prometheus/rules/alertas.yml
groups:
- name: infraestrutura
rules:
- alert: DiscoQuaseCheio
expr: |
100 - ((node_filesystem_avail_bytes{mountpoint="/"} /
node_filesystem_size_bytes{mountpoint="/"}) * 100) > 85
for: 5m
labels:
severity: warning
annotations:
summary: "Disco acima de 85% em {{ $labels.instance }}"
- alert: MemoriaInsuficiente
expr: (node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes) * 100 < 10
for: 2m
labels:
severity: critical
annotations:
summary: "Menos de 10% de RAM disponível em {{ $labels.instance }}"
¿Por qué Prometheus + Grafana?
Es la stack estándar de observabilidad en ambientes Kubernetes y cloud. Saber configurar métricas, dashboards y alertas es hoy un requisito básico para cualquier posición de SRE, DevOps o sysadmin sénior — y diferencia mucho el currículum de candidatos a puestos remotos internacionales.
Eso fue solo una pieza. El curso enseña el camino completo — desde tu primer comando hasta el día del examen.