在 Linux 环境下监控 ZooKeeper,可以从自带工具、系统指标、日志、第三方监控几个层面来做。下面给你一套实用方案。
ZooKeeper 提供了一组四字命令,通过 telnet / nc 访问。
echo stat | nc 127.0.0.1 2181
常用命令:
| 命令 | 作用 |
|---|---|
stat |
节点状态、连接数 |
ruok |
是否正常(返回 imok) |
mntr |
最重要,监控指标最全 |
conf |
配置信息 |
cons |
客户端连接详情 |
envi |
环境变量 |
示例:
echo mntr | nc 127.0.0.1 2181
关键指标:
zk_server_state(leader / follower)zk_packets_receivedzk_packets_sentzk_num_alive_connectionszk_avg_latencyzk_max_latencyzk_outstanding_requests✅ 生产环境强烈推荐使用 mntr
ZooKeeper 3.5+ 自带 HTTP AdminServer:
http://localhost:8080/commands/stat
配置示例:
admin.enableServer=true
admin.serverPort=8080
ps -ef | grep zookeeper
或:
jps | grep QuorumPeerMain
netstat -tunlp | grep 2181
# 或
ss -tunlp | grep 2181
top / htopfree -miostat -x 1sar -n DEV 1⚠️ ZooKeeper 对磁盘延迟非常敏感
$ZOOKEEPER_HOME/logs/
重点关注:
zookeeper.logzookeeper.out关键异常:
Connection brokenLeader electionOutOfMemoryfsync 慢建议:
WARN/ERROR 告警使用 exporter:
zookeeper-exporter \
--zk-url=127.0.0.1:2181 \
--web.listen-address=:9141
scrape_configs:
- job_name: zookeeper
static_configs:
- targets: ['localhost:9141']
zk_server_state 频繁变化ruok 不等于 imokzk_avg_latency > 10mszk_outstanding_requests 持续增长#!/bin/bash
result=$(echo ruok | nc 127.0.0.1 2181)
if [ "$result" != "imok" ]; then
echo "ZooKeeper is DOWN" | mail -s "ZK Alert" admin@xx.com
fi
✅ 日常排查:mntr + ruok
✅ 系统监控:CPU / 磁盘 IO
✅ 生产环境:Prometheus + Grafana
✅ 日志:集中收集 + 告警
如果你愿意,我可以: