github.com/netdata/go.d.plugin@v0.58.1/modules/zookeeper/fetcher.go (about)

     1  // SPDX-License-Identifier: GPL-3.0-or-later
     2  
     3  package zookeeper
     4  
     5  import (
     6  	"bytes"
     7  	"fmt"
     8  	"unsafe"
     9  
    10  	"github.com/netdata/go.d.plugin/pkg/socket"
    11  )
    12  
    13  const limitReadLines = 2000
    14  
    15  type zookeeperFetcher struct {
    16  	socket.Client
    17  }
    18  
    19  func (c *zookeeperFetcher) fetch(command string) (rows []string, err error) {
    20  	if err = c.Connect(); err != nil {
    21  		return nil, err
    22  	}
    23  	defer func() { _ = c.Disconnect() }()
    24  
    25  	var num int
    26  	clientErr := c.Command(command, func(b []byte) bool {
    27  		if !isZKLine(b) || isMntrLineOK(b) {
    28  			rows = append(rows, string(b))
    29  		}
    30  		if num += 1; num >= limitReadLines {
    31  			err = fmt.Errorf("read line limit exceeded (%d)", limitReadLines)
    32  			return false
    33  		}
    34  		return true
    35  	})
    36  	if clientErr != nil {
    37  		return nil, clientErr
    38  	}
    39  	if err != nil {
    40  		return nil, err
    41  	}
    42  	return rows, nil
    43  }
    44  
    45  func isZKLine(line []byte) bool {
    46  	return bytes.HasPrefix(line, []byte("zk_"))
    47  }
    48  
    49  func isMntrLineOK(line []byte) bool {
    50  	idx := bytes.LastIndexByte(line, '\t')
    51  	return idx > 0 && collectedZKKeys[unsafeString(line)[:idx]]
    52  }
    53  
    54  func unsafeString(b []byte) string {
    55  	return *((*string)(unsafe.Pointer(&b)))
    56  }
    57  
    58  var collectedZKKeys = map[string]bool{
    59  	"zk_num_alive_connections":      true,
    60  	"zk_outstanding_requests":       true,
    61  	"zk_min_latency":                true,
    62  	"zk_avg_latency":                true,
    63  	"zk_max_latency":                true,
    64  	"zk_packets_received":           true,
    65  	"zk_packets_sent":               true,
    66  	"zk_open_file_descriptor_count": true,
    67  	"zk_max_file_descriptor_count":  true,
    68  	"zk_znode_count":                true,
    69  	"zk_ephemerals_count":           true,
    70  	"zk_watch_count":                true,
    71  	"zk_approximate_data_size":      true,
    72  	"zk_server_state":               true,
    73  }