A lightweight uptime monitoring system using Kafka for real-time website health checks with Discord notifications and data archiving.
- Monitor multiple websites concurrently
- Real-time status checks with latency tracking
- Discord alerts for failures (4xx, 5xx, connection errors)
- Kafka-based event streaming architecture
- Core: Python 3.9+
- Streaming: Apache Kafka (Confluent Image)
- Database: PostgreSQL 15, SQLAlchemy (ORM)
- Infrastructure: Docker, Docker Compose
- Testing: Pytest, Unittest.mock
- Utilities: Discord Webhooks, Requests, Dotenv
The system follows a Fan-Out Pattern using Kafka Consumer Groups:
graph LR
A[Producer: Site Checker] -->|JSON Events| B{Kafka Topic: website-monitor}
subgraph Consumers
C[Watchdog Consumer]
D[Archiver Consumer]
end
B --> C
B --> D
C -->|Alert 4xx/5xx| E((Discord Webhook))
D -->|SQLAlchemy| F[(PostgreSQL DB)]
style B fill:#f96,stroke:#333,stroke-width:2px
style F fill:#339,stroke:#fff,color:#fff
uptime_monitor/
├── consumer/
│ ├── main.py # Watchdog: Sends Discord alerts
│ └── consumer_db.py # Archiver: Saves data to Postgres
├── producer/
│ └── main.py # Checker: Pings sites & sends to Kafka
├── tests/
│ └── test_monitor.py # Unit tests for business logic
├── config.py # Centralized config loader
├── database.py # SQLAlchemy DB setup & models
├── utils.py # Shared Kafka & Parsing utilities
├── Dockerfile # Application container image
├── docker-compose.yml # Full stack orchestration
├── .env.example # Environment variables template
└── requirements.txt # Python dependencies
# Clone the repository
git clone <your-repo-url>
cd uptime_monitor
# Create environment file
cp .env.example .envEdit .env and add your Discord webhook URL:
DISCORD_WEBHOOK_URL=https://discord.com/api/webhooks/your_webhook_url_hereGet a webhook URL from Discord: Server Settings → Integrations → Webhooks → New Webhook
# Build and start all containers
docker-compose up -d --build
# View logs (all services)
docker-compose logs -f
# View specific service logs
docker-compose logs -f producer
docker-compose logs -f consumer-alerts
docker-compose logs -f consumer-db# Check container status
docker-compose ps
# Check database for saved records
docker exec -it postgres_db psql -U admin -d uptime_db -c "SELECT * FROM website_stats ORDER BY created_at DESC LIMIT 10;"
# View Kafka topics
docker exec -it kafka kafka-topics --bootstrap-server localhost:29092 --list# Stop all containers
docker-compose down
# Stop and remove volumes (clean slate)
docker-compose down -v- Install dependencies:
pip install -r requirements.txt- Start Kafka Broker and PostgreSQL container in the background:
docker-compose up -d kafka postgres- Configure environment variables in
.env:
# Kafka Config
KAFKA_BOOTSTRAP_SERVERS=localhost:9092
KAFKA_TOPIC_NAME=website-monitor
SITES_TO_MONITOR=https://google.com,https://github.com,https://httpbin.org/status/404,https://httpbin.org/status/503
SLEEP_TIME=60
DISCORD_WEBHOOK_URL=your_webhook_url
# Database Config
DB_HOST=localhost:5432
DB_NAME=uptime_db
DB_USER=admin
DB_PASS=password123Run the producer (monitors websites):
python producer/main.pyRun the alert consumer (sends alerts to Discord):
python consumer/main.pyRun the DB consumer (saves events to the database):
python consumer/consumer_db.pyEdit docker-compose.yml to customize monitored sites:
producer:
environment:
SITES_TO_MONITOR: https://google.com,https://github.com,https://yoursite.com
SLEEP_TIME: 60 # Check interval in secondsTwo independent consumer groups receive ALL messages:
alert_consumer_group- Sends Discord notificationsdb_consumer_group- Saves to PostgreSQL
# Run all tests
pytest
# Run with verbose output
pytest -v
# Run specific test file
pytest tests/test_monitor.py