温馨提示×

centos如何监控zookeeper运行

小樊
46
2026-10-03 11:11:44
栏目: 智能运维

在 CentOS 上监控 ZooKeeper 的运行状态,常见方式有以下几种,从简单命令到专业监控体系都可以用。


一、基础方式(命令行)

1. 查看进程是否运行

ps -ef | grep zookeeper

或

jps | grep QuorumPeerMain

2. 查看端口监听

ZooKeeper 默认端口:

  • 2181(客户端)
  • 2888 / 3888(集群)
netstat -lntp | grep java
# 或
ss -lntp | grep 2181

二、使用 ZooKeeper 自带命令(推荐)

1. 四字命令(非常实用)

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

四、系统服务级别监控(CentOS 7+)

1. 使用 systemd

systemctl status zookeeper

若 ZooKeeper 以服务方式安装:

systemctl enable zookeeper
systemctl restart zookeeper

五、日志监控(很重要)

ZooKeeper 日志位置(视安装方式):

/var/log/zookeeper/
# 或
$ZOOKEEPER_HOME/logs/

实时查看:

tail -f zookeeper.log

关注关键字:

  • ERROR
  • WARN
  • Connection refused
  • Leader election

六、专业监控方案(生产环境推荐)

1. Prometheus + ZooKeeper Exporter

docker run -d -p 9141:9141 \
  -e ZOOKEEPER_URL=localhost:2181 \
  carlpett/zookeeper-exporter

Prometheus 抓取 :9141/metrics

✅ 可监控:

  • 延迟
  • 连接数
  • znode 数量
  • 集群角色(leader/follower)

2. Zabbix 监控 ZooKeeper

  • 使用 zabbix_agent 执行 mntr
  • 自定义 item
  • 设置触发器(如 zk_avg_latency > 100)

3. Grafana 可视化

结合 Prometheus + Grafana,展示:

  • QPS
  • 延迟
  • 会话数
  • 集群状态

七、集群健康检查(重点)

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
  • 日志 tail

✅ 生产环境

  • systemd
  • Prometheus + Exporter
  • 日志 + 告警

如果你需要:

  • ZooKeeper 安装为 systemd 服务
  • 完整监控脚本
  • Prometheus + Grafana 配置示例

可以直接告诉我你的 CentOS 版本和 ZooKeeper 部署方式(单机 / 集群)。

0 踩