mirror of
https://gitlab.com/meowww_dev/heartbeat-go.git
synced 2026-11-28 22:34:50 +00:00
No description
- Go 99%
- Dockerfile 0.5%
- Makefile 0.5%
| .claude/commands | ||
| .opencode/commands | ||
| checks | ||
| config | ||
| logging | ||
| webhook | ||
| .dockerignore | ||
| .gitignore | ||
| .gitlab-ci.yml | ||
| AGENTS.md | ||
| CHANGELOG.md | ||
| Dockerfile | ||
| go.mod | ||
| go.sum | ||
| main.go | ||
| main_unix.go | ||
| main_windows.go | ||
| Makefile | ||
| README.md | ||
Heartbeat Go
A lightweight, cross-platform health monitoring tool for TCP and Ping checks.
Features
- TCP checks - Monitor TCP port availability and latency
- Ping checks - ICMP ping with configurable count
- Multiple input methods - Config file, CLI arguments, or environment variables
- Flexible logging - File output or stdout
- Webhook notifications - Send status changes to HTTP endpoints
- Docker ready - Designed for containerized deployments
Quick Start
Linux / macOS
# Download from GitLab Releases or build from source
./heartbeat-linux-amd64 --targets '[{"name":"web","host":"example.com","port":443,"type":"tcp"}]'
Windows
# Download from GitLab Releases or build from source
heartbeat-windows-amd64.exe --targets "[{""name"":""web"",""host"":""example.com"",""port"":443,""type"":""tcp""}]"
Installation
Pre-built Binaries
Download the latest release from GitLab Releases:
| Platform | Binary | Architecture |
|---|---|---|
| Linux | heartbeat-linux-amd64 |
x86_64 |
| Windows | heartbeat-windows-amd64.exe |
x86_64 |
Build from Source
# Linux
GOOS=linux GOARCH=amd64 go build -o heartbeat-linux-amd64 .
# Windows
GOOS=windows GOARCH=amd64 go build -o heartbeat-windows-amd64.exe .
Docker
# Pull from GitLab Container Registry
docker pull registry.gitlab.com/meowww_dev/heartbeat-go:latest
# Run with inline targets
docker run -e HEARTBEAT_TARGETS='[{"name":"web","host":"example.com","port":443,"type":"tcp"}]' registry.gitlab.com/meowww_dev/heartbeat-go:latest
# Run with config file
docker run -v $(pwd)/config.json:/config.json:ro -e HEARTBEAT_CONFIG_FILE=/config.json registry.gitlab.com/meowww_dev/heartbeat-go:latest
Configuration
Heartbeat Go supports three configuration methods. They are applied in this order:
Priority: Config File → CLI → ENV → Defaults
⚠️ Config file settings have the highest priority and will override CLI arguments and environment variables.
1. Config File
{
"settings": {
"interval_seconds": 30,
"timeout_ms": 3000,
"max_log_size_mb": 10,
"max_log_files": 5,
"log_file": "heartbeat.log",
"run_log_file": "run.log",
"stdout": true,
"run_stdout": true,
"rotate_on_start": true,
"log_on_change": true,
"start_delay": 3,
"webhook": {
"url": "https://example.com/webhook",
"method": "POST"
},
"run_webhook": {
"url": "https://example.com/run-webhook",
"method": "GET"
},
"webhook_max_retries": 3,
"webhook_retry_interval_sec": 30,
"run_webhook_max_retries": 3,
"run_webhook_retry_interval_sec": 30,
"http": {
"enable": true,
"port": 8000
}
},
"targets": [
{
"name": "web",
"host": "example.com",
"port": 443,
"type": "tcp"
},
{
"name": "gateway",
"host": "192.168.1.1",
"type": "ping",
"count": 3
},
{
"name": "api",
"url": "https://api.example.com/health",
"type": "http",
"method": "GET",
"accepted_codes": [200, 204]
}
]
}
2. CLI Arguments
Linux / macOS
./heartbeat-linux-amd64 \
--config-file config.json \
--interval-seconds 60 \
--timeout-ms 5000 \
--verbose
Windows
heartbeat-windows-amd64.exe --config-file config.json --interval-seconds 60 --timeout-ms 5000 --verbose
All CLI Options
| Option | Default | Description |
|---|---|---|
--config-file |
- | Path to config file |
--targets |
- | Targets as JSON array |
--interval-seconds |
30 | Check interval in seconds |
--timeout-ms |
3000 | TCP connection timeout in milliseconds |
--start-delay |
3 | Delay before first check in seconds |
--max-log-size-mb |
10 | Max log file size in MB before rotation |
--max-log-files |
5 | Number of old log files to keep |
--log-file |
- | Status log file path |
--run-log-file |
- | Run event log file path |
--stdout |
false | Enable stdout for status log |
--run-stdout |
false | Enable stdout for run log |
--rotate-on-start |
true | Rotate log files on startup |
--log-on-change |
true | Only log on status change |
--verbose |
false | Enable verbose console output |
--webhook-url |
- | Webhook URL for status changes |
--webhook-method |
POST | Webhook method (GET or POST) |
--webhook-max-retries |
3 | Max retries for failed status webhooks |
--webhook-retry-interval |
30 | Retry interval in seconds for status webhooks |
--run-webhook-url |
- | Webhook URL for run events |
--run-webhook-method |
POST | Webhook method for run events (GET or POST) |
--run-webhook-max-retries |
3 | Max retries for failed run webhooks |
--run-webhook-retry-interval |
30 | Retry interval in seconds for run webhooks |
--http-enable |
false | Enable HTTP status endpoint |
--http-port |
8000 | HTTP status endpoint port |
3. Environment Variables
export HEARTBEAT_CONFIG_FILE=config.json
export HEARTBEAT_INTERVAL_SECONDS=60
export HEARTBEAT_TIMEOUT_MS=5000
export HEARTBEAT_LOG_FILE=/var/log/heartbeat.log
export HEARTBEAT_STDOUT=true
export HEARTBEAT_LOG_ON_CHANGE=true
export HEARTBEAT_TARGETS='[{"name":"web","host":"example.com","port":443,"type":"tcp"}]'
export HEARTBEAT_VERBOSE=true
export HEARTBEAT_HTTP_ENABLE=true
export HEARTBEAT_HTTP_PORT=8000
All Environment Variables
| Variable | Default | Description |
|---|---|---|
HEARTBEAT_CONFIG_FILE |
- | Path to config file |
HEARTBEAT_TARGETS |
- | Targets as JSON array |
HEARTBEAT_INTERVAL_SECONDS |
30 | Check interval in seconds |
HEARTBEAT_TIMEOUT_MS |
3000 | TCP connection timeout in ms |
HEARTBEAT_START_DELAY |
3 | Delay before first check in seconds |
HEARTBEAT_MAX_LOG_SIZE_MB |
10 | Max log file size in MB |
HEARTBEAT_MAX_LOG_FILES |
5 | Number of old log files to keep |
HEARTBEAT_LOG_FILE |
- | Status log file path |
HEARTBEAT_RUN_LOG_FILE |
- | Run event log file path |
HEARTBEAT_STDOUT |
false | Enable stdout for status log |
HEARTBEAT_RUN_STDOUT |
false | Enable stdout for run log |
HEARTBEAT_ROTATE_ON_START |
true | Rotate log files on startup |
HEARTBEAT_LOG_ON_CHANGE |
true | Only log on status change |
HEARTBEAT_VERBOSE |
false | Enable verbose output |
HEARTBEAT_WEBHOOK_URL |
- | Webhook URL for status changes |
HEARTBEAT_WEBHOOK_METHOD |
POST | Webhook method (GET or POST) |
HEARTBEAT_WEBHOOK_MAX_RETRIES |
3 | Max retries for failed status webhooks |
HEARTBEAT_WEBHOOK_RETRY_INTERVAL |
30 | Retry interval in seconds for status webhooks |
HEARTBEAT_RUN_WEBHOOK_URL |
- | Webhook URL for run events |
HEARTBEAT_RUN_WEBHOOK_METHOD |
POST | Webhook method for run events (GET or POST) |
HEARTBEAT_RUN_WEBHOOK_MAX_RETRIES |
3 | Max retries for failed run webhooks |
HEARTBEAT_RUN_WEBHOOK_RETRY_INTERVAL |
30 | Retry interval in seconds for run webhooks |
HEARTBEAT_HTTP_ENABLE |
false | Enable HTTP status endpoint |
HEARTBEAT_HTTP_PORT |
8000 | HTTP status endpoint port |
Target Configuration
Each target requires specific fields:
| Field | Type | Required | Description |
|---|---|---|---|
name |
string | yes | Unique identifier for the target |
type |
string | yes | tcp, ping, or http |
host |
string | for tcp/ping | IP address or hostname |
port |
integer | for tcp | Port number (1-65535) |
count |
integer | for ping/tcp | Number of attempts (default: 3 for ping, 1 for tcp). Acts as confirmation count: a single probe is issued first, and the full count only runs when the probe's result differs from the last known status. |
interval |
integer | for tcp | Milliseconds between confirmation attempts when count > 1 (default: 0) |
url |
string | for http | Full URL (e.g., https://example.com/health) |
method |
string | for http | HTTP method (GET, POST, HEAD) - default: GET |
accepted_codes |
array | for http | Array of accepted HTTP status codes |
Examples
{
"name": "web-server",
"host": "192.168.1.100",
"port": 443,
"type": "tcp"
}
{
"name": "gateway",
"host": "192.168.1.1",
"type": "ping",
"count": 5
}
{
"name": "db",
"host": "db.internal",
"port": 5432,
"type": "tcp",
"count": 3,
"interval": 100
}
{
"name": "api-health",
"url": "https://api.example.com/health",
"type": "http",
"method": "GET",
"accepted_codes": [200, 204]
}
Output Format
Logs are written in JSON Lines format:
{"timestamp":"2026-03-26T12:00:00Z","name":"web","target":"example.com","port":443,"protocol":"tcp","status":"up","latencyMs":42.5}
{"timestamp":"2026-03-26T12:00:00Z","event":"heartbeat_start","targets":["web"]}
{"timestamp":"2026-03-26T12:00:30Z","name":"web","target":"example.com","port":443,"protocol":"tcp","status":"down","error":"connection refused"}
Status Values
up- Target is reachabledown- Target is not reachabletimeout- Connection timed outdegraded- Partial failure across multiple attempts (ping/tcp withcount > 1)
HTTP Status Endpoint
Enable a simple HTTP endpoint to query current target statuses:
./heartbeat-go --http-enable --http-port 8000 --targets '[...]'
Query the endpoint:
curl http://localhost:8000/
Response:
{
"timestamp": "2026-03-30T12:00:00Z",
"targets": [
{"name": "web", "status": "up"},
{"name": "gateway", "status": "down"}
]
}
Notes
- Status changes are logged only when the status changes (not every check cycle). Use
--log-on-change false/HEARTBEAT_LOG_ON_CHANGE=false/"log_on_change": falseto log every check cycle instead. - Use
--stdout/--run-stdoutorstdout: true/run_stdout: truein config to enable stdout - In Docker, stdout is enabled by default via ENV
- Uses unprivileged ICMP (works without root on Linux)
- Targets from CLI arguments are combined with targets from config file
- Failed webhooks are automatically retried. If the target status changes while a retry is pending, the stale webhook is skipped.
- Dead letters are logged to the run log when max retries are reached
Testing
Run all tests:
go test ./...
Run with verbose output:
go test -v ./...
Run specific package:
go test -v ./config
go test -v ./logging
go test -v ./checks
Test Coverage
| Package | Tests | Description |
|---|---|---|
| config | 17 | Config loading, validation, JSON parsing, settings |
| logging | 23 | File writing, webhooks, log rotation |
| checks/tcp | 5 | TCP connection tests |
| checks/ping | 6 | Ping tests |
Tests use real network connections where appropriate and HTTP test server for webhook mocking.