A Flask-based centralized heartbeat monitoring service with configurable alerts through multiple channels (email, Slack, Discord, SMS).
- Heartbeat Monitoring: Applications send periodic heartbeats to indicate they're alive
- Application Management: Register and manage applications via web interface or API
- Missed Heartbeat Detection: Automatic detection when applications fail to send heartbeats
- Grace Periods: Configurable grace periods before triggering alerts
- Pluggable Alerts: Email, Slack, Discord, and SMS notifications
- Web Dashboard: Manage applications and view heartbeat status
- REST API: Full API for programmatic management
- Heartbeat History: Track when applications last sent heartbeats
- Background Monitoring: Continuous monitoring service for missed heartbeats
-
Install dependencies:
pip install -r requirements.txt
-
Set up environment:
cp .env.example .env # Edit .env with your configuration -
Run the application:
python app.py
-
Access the dashboard: Open http://localhost:5000 in your browser
-
Register an application:
- Use the web interface to add a new application
- Note the generated UUID for your application
-
Send heartbeats:
curl -X POST http://localhost:5000/heartbeat/YOUR-APP-UUID
Register your applications with Heartbeat Central, specifying:
- Name: Human-readable application name
- Expected Interval: How often the application should send heartbeats (in seconds)
- Grace Period: Additional time to wait before considering the application down
Applications send periodic POST requests to /heartbeat/{uuid} to indicate they're alive.
The background service continuously monitors for applications that haven't sent heartbeats within their expected intervals plus grace periods, and triggers alerts when applications are overdue.
Copy .env.example to .env and configure:
SECRET_KEY: Flask secret key for sessionsDATABASE_URL: Database connection string (defaults to SQLite)HEARTBEAT_CHECK_INTERVAL: How often to check for missed heartbeats (default: 30 seconds)SMTP_*: Email server configurationTWILIO_*: SMS configuration via Twilio
{
"to_email": "admin@example.com",
"smtp_server": "smtp.gmail.com",
"smtp_port": 587,
"username": "your-email@gmail.com",
"password": "your-app-password"
}{
"webhook_url": "https://hooks.slack.com/services/...",
"channel": "#alerts",
"username": "HeartbeatBot"
}{
"webhook_url": "https://discord.com/api/webhooks/...",
"username": "HeartbeatBot"
}{
"to_number": "+1234567890",
"account_sid": "your-twilio-sid",
"auth_token": "your-twilio-token",
"from_number": "+1987654321"
}POST /heartbeat/{uuid}- Receive heartbeat from application
GET /api/applications- List all applicationsPOST /api/applications- Create new applicationGET /api/applications/{id}- Get specific applicationPUT /api/applications/{id}- Update applicationDELETE /api/applications/{id}- Delete applicationGET /api/applications/{id}/heartbeats- Get heartbeat history
GET /health- Health check endpoint for load balancers
import requests
import time
import os
HEARTBEAT_URL = "http://heartbeat-central:5000/heartbeat/YOUR-APP-UUID"
INTERVAL = 60 # Send heartbeat every 60 seconds
def send_heartbeat():
try:
response = requests.post(HEARTBEAT_URL, timeout=5)
if response.status_code == 200:
print("Heartbeat sent successfully")
else:
print(f"Heartbeat failed: {response.status_code}")
except Exception as e:
print(f"Heartbeat error: {e}")
# Send heartbeat in your application loop
while True:
send_heartbeat()
time.sleep(INTERVAL)HEALTHCHECK --interval=60s --timeout=5s --start-period=30s --retries=3 \
CMD curl -f http://heartbeat-central:5000/heartbeat/YOUR-APP-UUID || exit 1# Send heartbeat every 5 minutes
*/5 * * * * curl -X POST http://heartbeat-central:5000/heartbeat/YOUR-APP-UUIDpip install -r requirements-dev.txt
pytestblack .
flake8 .FROM python:3.11-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
EXPOSE 5000
CMD ["python", "app.py"]- Set
FLASK_ENV=production - Ensure proper
SECRET_KEYis set - Configure
HEARTBEAT_CHECK_INTERVALbased on your needs - Use a production database (PostgreSQL recommended)
- Flask App: Web server and API
- SQLAlchemy: Database ORM with SQLite default
- APScheduler: Background job scheduling for missed heartbeat detection
- Alert Plugins: Modular notification system
- Bootstrap: Responsive web interface
If you're migrating from traditional healthcheck monitoring:
- Replace active endpoint polling with heartbeat sending in your applications
- Register each application in Heartbeat Central
- Update your applications to send periodic heartbeats
- Configure alerts for each application as needed
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests
- Submit a pull request