Skip to content

Commit ae18eab

Browse files
0x0034kongfei605Copilot
authored
feat: 添加对 zxid 使用的采集 (#1411)
* feat: 添加对 zxid 使用的采集 Signed-off-by: ruochen <wanxialianwei@gmail.com> * chore(zookeeper): parse zxid string Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * chore(zookeeper): update inputs/zookeeper/zookeeper.go Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * chore(zookeeper): handle command not executed case in gatherSrvrResult --------- Signed-off-by: ruochen <wanxialianwei@gmail.com> Co-authored-by: kongfei605 <kongfei605@gmail.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent ff363e8 commit ae18eab

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

inputs/zookeeper/zookeeper.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,18 @@ func (ins *Instance) gatherOneHost(wg *sync.WaitGroup, slist *types.SampleList,
157157

158158
ins.gatherRuokResult(ruokConn, slist, tags)
159159

160+
srvrConn, err := ins.ZkConnect(zkHost)
161+
if err != nil {
162+
slist.PushFront(types.NewSample("", "zk_zxid", 0, tags))
163+
log.Println("E! failed to connect zookeeper:", zkHost, "error:", err)
164+
return
165+
}
166+
167+
defer srvrConn.Close()
168+
169+
srvrConn.SetDeadline(time.Now().Add(time.Duration(ins.Timeout) * time.Second))
170+
ins.gatherSrvrResult(srvrConn, slist, tags)
171+
160172
}
161173

162174
func (ins *Instance) gatherMntrResult(conn net.Conn, slist *types.SampleList, globalTags map[string]string) {
@@ -239,6 +251,30 @@ func (ins *Instance) gatherRuokResult(conn net.Conn, slist *types.SampleList, gl
239251
}
240252
}
241253

254+
func (ins *Instance) gatherSrvrResult(conn net.Conn, slist *types.SampleList, globalTags map[string]string) {
255+
res := sendZookeeperCmd(conn, "srvr")
256+
if strings.Contains(res, cmdNotExecutedSffx) {
257+
log.Printf(commandNotAllowedTmpl, "srvr", conn.RemoteAddr().String())
258+
slist.PushFront(types.NewSample("", "zk_zxid", 0, globalTags))
259+
return
260+
}
261+
lines := strings.Split(res, "\n")
262+
263+
for _, l := range lines {
264+
if !strings.HasPrefix(l, "Zxid:") {
265+
continue
266+
}
267+
zxidStr := strings.TrimSpace(strings.Split(l, ":")[1])
268+
zxid, err := strconv.ParseUint(zxidStr, 0, 64)
269+
if err != nil {
270+
log.Printf("E! failed to parse zxid: %s", err)
271+
return
272+
}
273+
low4Bytes := zxid & 0xFFFFFFFF
274+
slist.PushFront(types.NewSample("", "zk_zxid_used", low4Bytes, globalTags))
275+
}
276+
}
277+
242278
func sendZookeeperCmd(conn net.Conn, cmd string) string {
243279
_, err := conn.Write([]byte(cmd))
244280
if err != nil {

0 commit comments

Comments
 (0)