Monitoramento Linux com Prometheus e Grafana: Do Zero ao Dashboard
Prometheus + Grafana é a stack de monitoramento padrão do mercado DevOps. O Prometheus coleta e armazena métricas em série temporal; o Grafana transforma essas métricas em dashboards e alertas visuais. Juntos, eles substituem ferramentas mais antigas como Nagios e Zabbix em ambientes modernos.
Arquitetura 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 o Node Exporter (nos servidores a monitorar)
# 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 o 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 o 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 o 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)
Criando o Primeiro Dashboard
- No Grafana: Connections → Add data source → Prometheus
URL:http://localhost:9090→ Save & Test - Dashboards → Import → ID 1860 (Node Exporter Full — dashboard público com +30 painéis prontos)
- Selecione o data source Prometheus e clique Import
O dashboard 1860 já mostra CPU, RAM, disco, rede, carga do sistema e uptime de todos os seus servidores em tempo real, sem escrever nenhuma 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 que Prometheus + Grafana?
É a stack padrão de observabilidade em ambientes Kubernetes e cloud. Saber configurar métricas, dashboards e alertas é hoje um requisito básico para qualquer posição de SRE, DevOps ou sysadmin sênior — e diferencia muito o currículo de candidatos a vagas remotas internacionais.