在 Linux 上“监控 Swagger”通常有两种理解,我分别说明,你可以按需要选择。
(最常见需求:监控 Swagger 页面或接口是否在线)
Swagger UI 通常是:
http://<ip>:<port>/swagger-ui.html
http://<ip>:<port>/swagger-ui/index.html
curl 监控curl -s -o /dev/null -w "%{http_code}" http://localhost:8080/swagger-ui.html
200:正常200:异常可写成脚本:
#!/bin/bash
STATUS=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:8080/swagger-ui.html)
if [ "$STATUS" != "200" ]; then
echo "Swagger UI is down"
fi
Swagger 实际的接口文档通常是:
/v2/api-docs (Swagger 2)
/v3/api-docs (OpenAPI 3)
curl http://localhost:8080/v3/api-docs | jq .info.title
✅ 能返回 JSON 说明服务正常
(偏生产环境)
如果 Swagger 只是接口文档,真正要监控的是 接口本身。
查看接口访问日志:
tail -f /var/log/your-app/app.log | grep "/api/"
常见监控指标:
如果服务暴露了 metrics(Spring Boot 为例):
management:
endpoints:
web:
exposure:
include: "*"
访问:
http://localhost:8080/actuator/prometheus
Prometheus 可监控:
(防止接口文档泄露)
curl -I http://<公网IP>/swagger-ui.html
location /swagger-ui.html {
allow 127.0.0.1;
deny all;
}
| 工具 | 用途 |
|---|---|
| cron + curl | 定时检查 |
| Prometheus | 指标监控 |
| Grafana | 可视化 |
| Zabbix | 传统监控 |
| ELK | 日志分析 |
示例 cron:
*/5 * * * * curl -f http://localhost:8080/swagger-ui.html || echo "Swagger down"
你可以告诉我:
我可以直接给你一套 可复制的监控方案。