Python 自动运维工具,提供 REST API 接口,支持服务器监控告警、自动化部署、日志采集分析和任务调度。
- 服务器监控告警 - 定时采集 CPU/内存/磁盘/网络指标,可配置告警规则,支持 Email/Webhook/Slack 通知
- 自动化部署 - 执行部署脚本,支持回滚,实时日志流
- 日志采集分析 - 支持文件/API 日志采集,JSON/正则解析,全文查询,实时流
- 任务调度 - Cron/间隔/一次性任务,立即执行,执行历史追踪
- GitHub Webhook - 接收 GitHub push 事件,自动 git pull + 可选部署脚本,HMAC-SHA256 签名验证
pip install -e .python -m app.main
# 或
uvicorn app.main:app --reload服务启动后访问:
- Swagger UI: http://localhost:8000/api/docs
- ReDoc: http://localhost:8000/api/redoc
- 健康检查: http://localhost:8000/api/v1/health
编辑 config/config.yaml 修改以下配置:
app:
port: 8000 # 监听端口
security:
api_key: "your-key" # API 认证密钥(留空禁用)
monitor:
collection_interval_seconds: 30 # 指标采集间隔
thresholds:
cpu_percent: 85.0 # CPU 告警阈值
notifications:
channels:
webhook:
enabled: true
url: "https://your-webhook.example.com"| Method | Path | 说明 |
|---|---|---|
| GET | /api/v1/health |
存活探针 |
| GET | /api/v1/health/detailed |
就绪探针(含各子系统状态) |
| Method | Path | 说明 |
|---|---|---|
| GET | /api/v1/monitor/current |
当前系统指标 |
| GET | /api/v1/monitor/history |
历史指标(支持时间范围过滤) |
| POST | /api/v1/monitor/collect |
立即触发指标采集 |
| GET | /api/v1/monitor/alerts/rules |
列出告警规则 |
| POST | /api/v1/monitor/alerts/rules |
创建告警规则 |
| PUT | /api/v1/monitor/alerts/rules/{id} |
更新告警规则 |
| DELETE | /api/v1/monitor/alerts/rules/{id} |
删除告警规则 |
| GET | /api/v1/monitor/alerts/events |
告警事件列表 |
| POST | /api/v1/monitor/alerts/events/{id}/resolve |
标记告警已解决 |
POST /api/v1/monitor/alerts/rules
{
"name": "high-cpu",
"metric": "cpu_percent",
"operator": ">",
"threshold": 85.0,
"severity": "warning",
"notification_channels": ["webhook"],
"cooldown_seconds": 300
}可用指标: cpu_percent, memory_percent, disk_percent, net_bytes_sent_rate, net_bytes_recv_rate
| Method | Path | 说明 |
|---|---|---|
| GET | /api/v1/deployments |
部署列表 |
| POST | /api/v1/deployments |
创建并触发部署 |
| GET | /api/v1/deployments/{id} |
部署详情 |
| DELETE | /api/v1/deployments/{id} |
取消部署 |
| POST | /api/v1/deployments/{id}/rollback |
回滚 |
| GET | /api/v1/deployments/{id}/logs |
实时日志流(SSE) |
POST /api/v1/deployments
{
"name": "api-service",
"version": "v2.3.1",
"environment": "prod",
"script_path": "scripts/example_deploy.sh",
"rollback_script_path": "scripts/example_rollback.sh",
"env_vars": {"IMAGE_TAG": "v2.3.1"},
"timeout_seconds": 300
}| Method | Path | 说明 |
|---|---|---|
| GET | /api/v1/logs/sources |
日志源列表 |
| POST | /api/v1/logs/sources |
注册日志源 |
| POST | /api/v1/logs/sources/{id}/collect |
立即采集 |
| GET | /api/v1/logs/entries |
查询日志(支持时间/级别/服务/关键字过滤) |
| POST | /api/v1/logs/entries |
批量写入日志 |
| GET | /api/v1/logs/entries/stream |
实时日志流(SSE) |
| GET | /api/v1/logs/stats |
日志统计 |
POST /api/v1/logs/entries
{
"entries": [
{
"level": "ERROR",
"message": "Database connection failed",
"service": "api-service",
"host": "server-01"
}
]
}| Method | Path | 说明 |
|---|---|---|
| GET | /api/v1/tasks |
任务列表 |
| POST | /api/v1/tasks |
创建任务 |
| PUT | /api/v1/tasks/{id} |
更新任务 |
| DELETE | /api/v1/tasks/{id} |
删除任务 |
| POST | /api/v1/tasks/{id}/enable |
启用任务 |
| POST | /api/v1/tasks/{id}/disable |
禁用任务 |
| POST | /api/v1/tasks/{id}/run |
立即执行 |
| GET | /api/v1/tasks/{id}/executions |
执行历史 |
| GET | /api/v1/tasks/executions |
全部执行历史 |
POST /api/v1/tasks
{
"name": "daily-backup",
"task_type": "shell",
"cron_expression": "0 2 * * *",
"command_or_url": "scripts/backup.sh",
"timeout_seconds": 3600,
"description": "Daily backup at 2:00 AM"
}POST /api/v1/tasks
{
"name": "health-ping",
"task_type": "http",
"interval_seconds": 60,
"command_or_url": "http://localhost:8000/api/v1/health",
"extra_args": {"method": "GET"}
}| Method | Path | 说明 |
|---|---|---|
| GET | /api/v1/webhook/github/projects |
项目配置列表 |
| POST | /api/v1/webhook/github/projects |
注册新项目 |
| PUT | /api/v1/webhook/github/projects/{id} |
更新项目配置 |
| DELETE | /api/v1/webhook/github/projects/{id} |
删除项目 |
| POST | /api/v1/webhook/github/projects/{id}/pull |
手动触发 git pull |
| POST | /api/v1/webhook/github/events |
GitHub Webhook 接收端点 |
| GET | /api/v1/webhook/github/events |
事件历史查询 |
| GET | /api/v1/webhook/github/events/{id} |
事件详情 |
第一步:注册项目
POST /api/v1/webhook/github/projects
{
"name": "my-app",
"repo_full_name": "your-org/my-app",
"local_path": "/srv/apps/my-app",
"branch": "main",
"webhook_secret": "your-strong-secret",
"deploy_script": "scripts/deploy.sh",
"deploy_timeout_seconds": 300
}第二步:在 GitHub 配置 Webhook
进入仓库 Settings → Webhooks → Add webhook:
| 字段 | 值 |
|---|---|
| Payload URL | https://your-server.com/api/v1/webhook/github/events |
| Content type | application/json |
| Secret | 与项目中 webhook_secret 相同 |
| Events | Just the push event |
工作流程:
GitHub push → HMAC 签名验证 → 匹配项目+分支 → git fetch && git reset --hard origin/<branch> → 执行 deploy_script(可选)
事件状态流转:
received → pulling → pull_ok / pull_failed
→ deploying → deploy_ok / deploy_failed
手动触发 pull(无需 GitHub 事件):
POST /api/v1/webhook/github/projects/1/pull
auto_operation/
├── app/
│ ├── main.py # FastAPI 应用入口
│ ├── config.py # 配置管理
│ ├── database.py # 数据库连接
│ ├── dependencies.py # 依赖注入
│ ├── exceptions.py # 异常处理
│ ├── core/ # 核心模块 (scheduler, logger)
│ ├── models/ # SQLAlchemy 数据模型
│ ├── schemas/ # Pydantic 请求/响应模型
│ ├── services/ # 业务逻辑层
│ ├── api/v1/ # API 路由层
│ └── notifications/ # 通知渠道
├── config/
│ └── config.yaml # 运行时配置
├── data/ # SQLite 数据库
├── logs/ # 应用日志
├── workspace/ # 部署脚本工作目录
└── scripts/ # 示例脚本
在 config.yaml 中设置 security.api_key 后,所有请求需携带请求头:
X-API-Key: your-api-key
留空则禁用认证(开发环境适用)。