Blackbox Exporter: monitorar URLs e serviços externos
Node Exporter monitora o servidor, Blackbox Exporter monitora o que o servidor alcança. Ele executa probes HTTP, TCP, DNS e ICMP a partir da VPS para verificar se URLs externas respondem, certificados SSL são válidos, APIs retornam 200 — e expõe tudo como métricas Prometheus para alertas no Alertmanager.
Instalar Blackbox Exporter
Subir o Blackbox Exporter com Docker Compose:
yaml
# docker-compose.yml:
services:
blackbox:
image: prom/blackbox-exporter:latest
restart: always
ports:
- "127.0.0.1:9115:9115"
volumes:
- ./blackbox.yml:/etc/blackbox_exporter/config.yml:ro
# blackbox.yml — definir módulos de probe:
modules:
# Probe HTTP que espera 200:
http_2xx:
prober: http
timeout: 10s
http:
valid_http_versions: ["HTTP/1.1", "HTTP/2.0"]
valid_status_codes: [200]
method: GET
follow_redirects: true
preferred_ip_protocol: ip4
# Probe HTTP com autenticação Bearer:
http_api_check:
prober: http
timeout: 10s
http:
valid_status_codes: [200]
headers:
Authorization: "Bearer ${API_TOKEN}"
body_size_limit: 64KB
# Probe TCP (verificar se porta está aberta):
tcp_connect:
prober: tcp
timeout: 5s
# Probe ICMP (ping):
icmp:
prober: icmp
timeout: 5sConfigurar scrape no Prometheus
Adicionar targets de monitoramento no prometheus.yml:
yaml
# prometheus.yml — scrape via Blackbox Exporter:
scrape_configs:
- job_name: 'blackbox_http'
metrics_path: /probe
params:
module: [http_2xx]
static_configs:
- targets:
- https://runstack.com.br
- https://api.runstack.com.br/health
- https://app.runstack.com.br
- https://seu-site.com.br
relabel_configs:
# Passar a URL como parâmetro do probe:
- source_labels: [__address__]
target_label: __param_target
# Label 'instance' = URL monitorada:
- source_labels: [__param_target]
target_label: instance
# Redirecionar scrape para o Blackbox:
- target_label: __address__
replacement: localhost:9115
# Monitorar portas TCP:
- job_name: 'blackbox_tcp'
metrics_path: /probe
params:
module: [tcp_connect]
static_configs:
- targets:
- banco-externo.com:5432
- redis-externo.com:6379
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- source_labels: [__param_target]
target_label: instance
- target_label: __address__
replacement: localhost:9115Alertas para downtime e SSL expirado
Regras do Alertmanager baseadas em métricas do Blackbox:
yaml
# prometheus/rules/blackbox-alerts.yml:
groups:
- name: blackbox
rules:
# Site fora do ar (probe falhou):
- alert: SiteForaDoAr
expr: probe_success == 0
for: 2m
labels:
severity: critical
annotations:
summary: "{{ $labels.instance }} está fora do ar"
description: "O probe HTTP falhou por mais de 2 minutos"
# Tempo de resposta alto:
- alert: AltaLatenciaHTTP
expr: probe_duration_seconds > 3
for: 5m
labels:
severity: warning
annotations:
summary: "{{ $labels.instance }} com latência alta"
description: "Tempo de resposta: {{ $value | humanizeDuration }}"
# Certificado SSL expirando em breve:
- alert: SSLExpirandoEmBreve
expr: probe_ssl_earliest_cert_expiry - time() < 86400 * 14
for: 1h
labels:
severity: warning
annotations:
summary: "Certificado SSL expirando: {{ $labels.instance }}"
description: "Expira em {{ $value | humanizeDuration }}"
# Certificado SSL já expirado:
- alert: SSLExpirado
expr: probe_ssl_earliest_cert_expiry - time() < 0
labels:
severity: critical
annotations:
summary: "Certificado SSL EXPIRADO: {{ $labels.instance }}"Dashboard no Grafana para Blackbox
Queries PromQL para visualizar disponibilidade e SSL:
bash
# Queries PromQL para painéis no Grafana:
# Status geral de todos os endpoints (1=up, 0=down):
probe_success
# Taxa de disponibilidade nos últimos 30 dias:
avg_over_time(probe_success[30d]) * 100
# Latência de resposta por endpoint:
probe_duration_seconds
# Dias até o certificado SSL expirar:
(probe_ssl_earliest_cert_expiry - time()) / 86400
# Status code HTTP retornado:
probe_http_status_code
# Versão do TLS/SSL:
probe_tls_version_info
# Importar dashboard pronto do Grafana:
# grafana.com/grafana/dashboards/7587
# (Blackbox Exporter Full — muito completo)
# Painel de "status atual" com stat panel:
# Threshold: 0 = vermelho (down), 1 = verde (up)
# Coloração automática: down=red, up=greenVerificar conteúdo da resposta HTTP
Probes avançados que validam o corpo da resposta:
yaml
# blackbox.yml — módulos avançados:
modules:
# Verificar que a resposta contém texto específico:
http_check_body:
prober: http
timeout: 10s
http:
valid_status_codes: [200]
fail_if_body_not_matches_regexp:
- '"status":"ok"' # health check deve ter "status":"ok"
fail_if_body_matches_regexp:
- '"status":"error"'
# Verificar que página não mostra erro 500:
http_no_error:
prober: http
timeout: 15s
http:
valid_status_codes: [] # qualquer status é aceito...
fail_if_body_matches_regexp:
- 'Internal Server Error'
- '500 - Application Error'
# POST para testar endpoint de API:
http_post_api:
prober: http
timeout: 10s
http:
method: POST
valid_status_codes: [200, 201]
headers:
Content-Type: application/json
body: '{"ping": true}'
fail_if_body_not_matches_regexp:
- '"pong"'
# Testar manualmente um probe:
curl "http://localhost:9115/probe?target=https://runstack.com.br&module=http_2xx&debug=true"$ runstack deploy --plan starter
Não quer configurar manualmente?
Não quer configurar manualmente? Implante o VPS em menos de 3 minutos com a Runstack. Infraestrutura da OPEN DATACENTER, com servidores no Brasil.