Adiciona arquivos do serviço heartbeat e documentação

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Ronaldo Dias 2026-06-15 16:03:51 -03:00
parent b665f72df5
commit 84ef1e44a0
4 changed files with 103 additions and 1 deletions

View file

@ -1 +1,59 @@
# heartbeat
Serviço systemd que coleta métricas do sistema a cada 5 minutos e envia um ping para o [Healthchecks.io](https://healthchecks.io), permitindo monitorar se a máquina está viva e saudável.
## Arquivos
| Arquivo | Destino | Descrição |
|---|---|---|
| `heartbeat.sh` | `/usr/local/bin/heartbeat.sh` | Script que coleta métricas e envia o ping |
| `heartbeat.service` | `/etc/systemd/system/heartbeat.service` | Unit systemd que executa o script |
| `heartbeat.timer` | `/etc/systemd/system/heartbeat.timer` | Timer que dispara o serviço a cada 5 minutos |
## Métricas enviadas
O script coleta e envia as seguintes informações em JSON:
- `timestamp` — data e hora da execução (ISO 8601)
- `uptime` — tempo de atividade da máquina
- `load` — carga média do sistema (1, 5 e 15 minutos)
- `disk_free` — espaço livre em disco na raiz (`/`)
- `mem_free` — memória disponível
## Instalação
```bash
# Copiar o script
sudo cp heartbeat.sh /usr/local/bin/heartbeat.sh
sudo chmod +x /usr/local/bin/heartbeat.sh
# Copiar os units systemd
sudo cp heartbeat.service /etc/systemd/system/
sudo cp heartbeat.timer /etc/systemd/system/
# Recarregar e habilitar
sudo systemctl daemon-reload
sudo systemctl enable --now heartbeat.timer
```
## Configuração
Antes de instalar, edite `heartbeat.sh` e substitua a URL do ping pela URL do seu check no Healthchecks.io:
```bash
# Linha a editar no heartbeat.sh
https://hc-ping.com/SEU-UUID-AQUI
```
## Verificar status
```bash
# Ver se o timer está ativo
systemctl status heartbeat.timer
# Ver próxima execução
systemctl list-timers heartbeat.timer
# Ver log da última execução
journalctl -u heartbeat.service -n 20
```

7
heartbeat.service Normal file
View file

@ -0,0 +1,7 @@
[Unit]
Description=Heartbeat para Healthchecks.io
[Service]
Type=oneshot
ExecStart=/usr/local/bin/heartbeat.sh

28
heartbeat.sh Executable file
View file

@ -0,0 +1,28 @@
#!/bin/bash
# Coleta informações em tempo real
UPTIME=$(uptime -p)
LOAD=$(cut -d' ' -f1-3 /proc/loadavg)
DISK_FREE=$(df -h / --output=avail | tail -1 | xargs)
MEM_FREE=$(free -h | awk '/^Mem:/ {print $7}')
TIMESTAMP=$(date -Iseconds)
# Monta o JSON
JSON=$(cat <<EOF
{
"timestamp": "$TIMESTAMP",
"uptime": "$UPTIME",
"load": "$LOAD",
"disk_free": "$DISK_FREE",
"mem_free": "$MEM_FREE"
}
EOF
)
# Envia para o Healthchecks
curl -fsS -m 10 --retry 3 \
-X POST \
-H "Content-Type: application/json" \
-d "$JSON" \
https://hc-ping.com/05bcb14f-acc2-4e10-af33-6d0782350b94

9
heartbeat.timer Normal file
View file

@ -0,0 +1,9 @@
[Unit]
Description=Heartbeat para Healthchecks.io
[Timer]
OnCalendar=*-*-* *:0/5:00
AccuracySec=1s
[Install]
WantedBy=timers.target