监控目标与总体架构
主机与服务可用性监控
systemctl status jenkinssystemctl start|restart jenkinssystemctl enable jenkinsss -tulnp | grep 8080 或 netstat -tulnp | grep 8080firewall-cmd --permanent --add-port=8080/tcp && firewall-cmd --reloadtop/htop/atop 观察 CPU、内存、I/Ojournalctl -u jenkins -n 50 --no-pager 或 tail -n 50 /var/log/jenkins/jenkins.log#!/usr/bin/env bash
set -e
JENKINS_URL="http://127.0.0.1:8080"
JOB="your-job-name"
CRITICAL=1
# 1) 服务存活
if ! systemctl is-active --quiet jenkins; then
echo "CRITICAL: jenkins service is not running"
exit $CRITICAL
fi
# 2) 端口连通
if ! ss -tulnp | grep -q :8080; then
echo "CRITICAL: port 8080 not listening"
exit $CRITICAL
fi
# 3) 最近一次构建结果(匿名只读;如启用安全需使用API Token)
if command -v curl >/dev/null 2>&1; then
LAST_BUILD=$(curl -s -f -u "$JENKINS_USER:$JENKINS_TOKEN" \
"$JENKINS_URL/job/$JOB/lastBuild/api/json" 2>/dev/null || echo '{}')
RESULT=$(echo "$LAST_BUILD" | grep -o '"result":"[^"]*"' | head -n1 | cut -d'"' -f4)
if [[ "$RESULT" == "FAILURE" ]]; then
echo "CRITICAL: last build of $JOB failed (result=$RESULT)"
exit $CRITICAL
fi
fi
echo "OK: jenkins is running, port 8080 is open, last build status=$RESULT"
exit 0
以上命令与路径适用于 CentOS 上的常规部署与问题定位。Jenkins 应用内与 API 监控
curl -u username:token http://<IP>:8080/job/<job_name>/lastBuild/api/jsonhttp://<IP>:8080/queue/api/json、http://<IP>:8080/computer/api/jsonjenkins-cli -s http://<IP>:8080/ -auth username:token agent list(查看代理节点状态)。指标可视化与长期趋势
scrape_configs:
- job_name: 'jenkins'
metrics_path: '/metrics'
static_configs:
- targets: ['<JENKINS_HOST>:8080']
告警与故障排查建议
systemctl status jenkins、ss -tulnp | grep 8080;journalctl -u jenkins -n 50 --no-pager、tail -n 50 /var/log/jenkins/jenkins.log;