在 Linux 环境下,**Swagger(通常指 OpenAPI)本身的“监控”**一般不是直接监控 Swagger UI,而是围绕 API 可用性、接口调用情况、文档一致性和性能指标 来做。下面按常见场景给你一个完整思路。
在 Linux 服务器上,通常关注以下几类:
使用 curl + systemd / cron
curl -sf http://localhost:8080/swagger-ui.html > /dev/null
配合脚本:
#!/bin/bash
URL="http://localhost:8080/swagger-ui.html"
if ! curl -sf "$URL" > /dev/null; then
echo "Swagger UI is down"
# 发告警
fi
modules:
http_2xx:
prober: http
http:
preferred_ip_protocol: ip4
scrape_configs:
- job_name: swagger
static_configs:
- targets:
- http://localhost:8080/swagger-ui.html
✅ 可监控:
@RestController
public class OrderController {
@GetMapping("/orders")
public List<Order> getOrders() {
// 自动被 Swagger 扫描
}
}
配合:
监控指标:
openapi-generator-cli generate \
-i swagger.json \
-g python \
-o client
然后定时调用接口。
schemathesis(强烈推荐)pip install schemathesis
schemathesis run http://localhost:8080/v3/api-docs
✅ 可以:
常见指标:
http_server_requests_secondsapi_call_totalapi_error_totalGrafana 面板可显示:
如果你通过 Nginx 暴露 Swagger:
location /swagger/ {
proxy_pass http://backend;
}
监控:
工具:
diff api-docs.json previous-api-docs.json
或用:
openapi-diffswagger-diffCI 中阻断破坏性变更。
/v3/api-docs示例(Nginx 日志分析):
awk '{print $7}' access.log | grep swagger | sort | uniq -c
curl + cronLinux 下 Swagger 的监控,本质是:用 Swagger 作为接口契约,对 API 可用性、性能和一致性进行监控。
如果你愿意,可以告诉我:
我可以给你一套可直接落地的配置示例。