在 Ubuntu 上使用 Postman 进行接口监控
一 方案总览
二 方案一 Postman 桌面版 Monitor 监控
pm.test("Status is 200", () => pm.response.to.have.status(200));
pm.test("Response time < 2000 ms", () => pm.expect(pm.response.responseTime).to.be.below(2000));
三 方案二 Ubuntu 服务器无头监控 Newman
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt-get install -y nodejs
sudo npm install -g newman
#!/usr/bin/env bash
LOG="/var/log/postman-monitor.log"
TS=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
newman run my-collection.json \
--environment my-env.json \
--reporters cli,json \
--reporter-json-export "report-${TS}.json" \
--timeout-request 10000 \
--delay-request 500 \
>> "$LOG" 2>&1
# 简单失败告警示例(可替换为企业微信/钉钉/飞书 Webhook)
if grep -q '"failures":\[' "report-${TS}.json"; then
curl -X POST -H 'Content-Type: application/json' \
-d '{"text":"Postman 监控失败,请查看日志与报告"}' \
https://your-webhook-url
fi
*/5 * * * * /opt/monitor/run.sh),并配合 logrotate 做日志轮转。newman-reporter-html 等第三方报告器生成 HTML 报告并归档。四 方案三 Docker 化监控
docker run -d \
--name postman-monitor \
-v "$PWD/collection.json:/runner/collection.json:ro" \
-v "$PWD/env.json:/runner/env.json:ro" \
-e CRON="*/5 * * * *" \
-e REPORTER=json \
kevinniu666/postman-prometheus:1.0.0
五 关键实践与排错要点
pm.test("Response time < 2000 ms", () => pm.expect(pm.response.responseTime).to.be.below(2000));
--insecure 仅用于测试环境,生产请正确部署 CA/证书链。