N3XT3R1337/url-health-monitor
GitHub: N3XT3R1337/url-health-monitor
一款基于FastAPI和Celery的自托管网站可用性监控服务,支持Cron定时检查、多渠道告警、响应时间追踪和事件管理。
Stars: 0 | Forks: 0
```
██╗ ██╗██████╗ ██╗ ██╗ ██╗███████╗ █████╗ ██╗ ████████╗██╗ ██╗
██║ ██║██╔══██╗██║ ██║ ██║██╔════╝██╔══██╗██║ ╚══██╔══╝██║ ██║
██║ ██║██████╔╝██║ ███████║█████╗ ███████║██║ ██║ ███████║
██║ ██║██╔══██╗██║ ██╔══██║██╔══╝ ██╔══██║██║ ██║ ██╔══██║
╚██████╔╝██║ ██║███████╗ ██║ ██║███████╗██║ ██║███████╗██║ ██║ ██║
╚═════╝ ╚═╝ ╚═╝╚══════╝ ╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝ ╚═╝
███╗ ███╗ ██████╗ ███╗ ██╗██╗████████╗ ██████╗ ██████╗
████╗ ████║██╔═══██╗████╗ ██║██║╚══██╔══╝██╔═══██╗██╔══██╗
██╔████╔██║██║ ██║██╔██╗ ██║██║ ██║ ██║ ██║██████╔╝
██║╚██╔╝██║██║ ██║██║╚██╗██║██║ ██║ ██║ ██║██╔══██╗
██║ ╚═╝ ██║╚██████╔╝██║ ╚████║██║ ██║ ╚██████╔╝██║ ██║
╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═══╝╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝
```





网站在线时间监控服务,支持基于 Cron 的健康检查、Webhook 通知(Slack、Discord、电子邮件)、状态仪表盘 API、响应时间追踪以及事件历史记录。
## 功能特性
- **自动健康检查** — 基于 Cron 的监控,支持为每个 URL 配置检查间隔
- **多渠道通知** — 事件发生时通过 Slack、Discord 和电子邮件发送警报
- **响应时间追踪** — 详细的延迟指标,包括 P95、最小值、最大值和平均值
- **事件管理** — 自动检测、严重性升级和解决状态追踪
- **状态仪表盘 API** — 实时概览、正常运行时间报告以及每个监控器的详细统计
- **批量操作** — 批量创建监控器和解决事件
- **Celery 任务队列** — 用于非阻塞健康检查的后台 Worker
- **小时聚合** — 每小时自动汇总响应时间统计数据
- **自动清理** — 30 天后自动清除旧的健康检查记录
## 技术栈
| 组件 | 技术 |
|----------------|-------------------------|
| API 框架 | FastAPI |
| 任务队列 | Celery |
| 消息代理 | Redis |
| ORM | SQLAlchemy 2.0 |
| HTTP 客户端 | httpx |
| 数据验证 | Pydantic v2 |
| 数据库 | SQLite (默认) / PostgreSQL |
| 容器化 | Docker & Docker Compose |
## 安装说明
### 前置条件
- Python 3.12+
- Redis(用于 Celery 任务队列)
- Docker & Docker Compose(可选)
### 本地设置
```
git clone https://github.com/N3XT3R1337/url-health-monitor.git
cd url-health-monitor
python -m venv venv
source venv/bin/activate # Windows: venv\Scripts\activate
pip install -r requirements.txt
cp .env.example .env
```
启动 API 服务器:
```
uvicorn app.main:app --reload --port 8000
```
启动 Celery Worker(在单独的终端中):
```
celery -A app.tasks.celery_tasks worker --loglevel=info
```
启动 Celery Beat 调度器(在单独的终端中):
```
celery -A app.tasks.celery_tasks beat --loglevel=info
```
### Docker 设置
```
docker compose up -d
```
这将启动 API、Celery Worker、Celery Beat 以及 Redis。
## 使用指南
### 创建监控器
```
curl -X POST http://localhost:8000/api/v1/monitors \
-H "Content-Type: application/json" \
-d '{
"name": "GitHub",
"url": "https://github.com",
"method": "GET",
"expected_status_code": 200,
"check_interval": 120,
"notification_channels": [
{
"channel_type": "slack",
"webhook_url": "https://hooks.slack.com/services/YOUR/WEBHOOK/URL"
}
]
}'
```
### 触发手动检查
```
curl -X POST http://localhost:8000/api/v1/monitors/1/check
```
### 获取仪表盘统计数据
```
curl http://localhost:8000/api/v1/dashboard/stats
```
```
{
"total_monitors": 5,
"monitors_up": 4,
"monitors_down": 1,
"monitors_degraded": 0,
"overall_uptime_percentage": 99.72,
"active_incidents": 1,
"avg_response_time_ms": 245.8
}
```
### 查看响应时间历史
```
curl http://localhost:8000/api/v1/monitors/1/response-times?hours=48
```
### 列出活跃事件
```
curl http://localhost:8000/api/v1/incidents/active/all
```
### 解决事件
```
curl -X POST http://localhost:8000/api/v1/incidents/1/resolve
```
### 批量创建监控器
```
curl -X POST http://localhost:8000/api/v1/monitors/bulk \
-H "Content-Type: application/json" \
-d '{
"monitors": [
{"name": "Google", "url": "https://google.com"},
{"name": "GitHub", "url": "https://github.com"},
{"name": "Reddit", "url": "https://reddit.com"}
]
}'
```
### 获取正常运行时间报告
```
curl http://localhost:8000/api/v1/dashboard/uptime-report?hours=168
```
## API 文档
运行后,可在以下地址访问交互式文档:
- **Swagger UI**: [http://localhost:8000/docs](http://localhost:8000/docs)
- **ReDoc**: [http://localhost:8000/redoc](http://localhost:8000/redoc)
## 运行测试
```
pytest tests/ -v
```
## 环境变量
| 变量 | 默认值 | 描述 |
|----------------------------|------------------------------------|------------------------------------|
| `DATABASE_URL` | `sqlite:///./health_monitor.db` | 数据库连接字符串 |
| `REDIS_URL` | `redis://localhost:6379/0` | Redis 连接 URL |
| `CELERY_BROKER_URL` | `redis://localhost:6379/1` | Celery Broker URL |
| `SLACK_WEBHOOK_URL` | — | 默认 Slack Webhook |
| `DISCORD_WEBHOOK_URL` | — | 默认 Discord Webhook |
| `SMTP_HOST` | — | SMTP 服务器地址 |
| `SMTP_PORT` | `587` | SMTP 服务器端口 |
| `INCIDENT_THRESHOLD` | `3` | 触发警报前的连续失败次数 |
| `RESPONSE_TIME_WARNING_MS` | `2000` | 响应时间降级阈值 |
## 许可证
本项目基于 MIT 许可证授权 —— 详情请参阅 [LICENSE](LICENSE) 文件。
标签:API, Cron, Discord通知, Slack通知, Webhook, 事件历史, 健康检查, 力导向图, 可用性监控, 告警系统, 响应时间追踪, 搜索引擎查询, 服务健康, 正常运行时间, 状态仪表盘, 状态页面, 网站状态, 网站监控, 网络调试, 自动化, 请求拦截, 运维工具, 运行时操纵, 运行时间监控, 逆向工具, 邮件通知