在 CentOS 上监控 ZooKeeper 的运行状态,常见方式有以下几种,从简单命令到专业监控体系都可以用。
ps -ef | grep zookeeper
或
jps | grep QuorumPeerMain
ZooKeeper 默认端口:
netstat -lntp | grep java
# 或
ss -lntp | grep 2181
echo ruok | nc localhost 2181
返回 imok 表示正常。
常用四字命令:
echo stat | nc localhost 2181 # 状态
echo mntr | nc localhost 2181 # 监控指标(最重要)
echo conf | nc localhost 2181 # 配置
echo cons | nc localhost 2181 # 客户端连接
⚠️ 若提示 nc: command not found:
yum install -y nc
若使用 mntr,需确认 zoo.cfg 中开启:
4lw.commands.whitelist=*
#!/bin/bash
result=$(echo ruok | nc localhost 2181)
if [ "$result" != "imok" ]; then
echo "ZooKeeper is down!" | mail -s "ZK Alert" admin@xxx.com
systemctl restart zookeeper
fi
加入 crontab:
crontab -e
*/5 * * * * /opt/check_zk.sh
systemctl status zookeeper
若 ZooKeeper 以服务方式安装:
systemctl enable zookeeper
systemctl restart zookeeper
ZooKeeper 日志位置(视安装方式):
/var/log/zookeeper/
# 或
$ZOOKEEPER_HOME/logs/
实时查看:
tail -f zookeeper.log
关注关键字:
docker run -d -p 9141:9141 \
-e ZOOKEEPER_URL=localhost:2181 \
carlpett/zookeeper-exporter
Prometheus 抓取 :9141/metrics
✅ 可监控:
zabbix_agent 执行 mntrzk_avg_latency > 100)结合 Prometheus + Grafana,展示:
echo mntr | nc localhost 2181
关注字段:
zk_server_state leader / follower
zk_outstanding_requests
zk_avg_latency
zk_max_latency
zk_packets_received
zk_packets_sent
✅ 开发/测试
ruok + mntr✅ 生产环境
如果你需要:
可以直接告诉我你的 CentOS 版本和 ZooKeeper 部署方式(单机 / 集群)。