How to list all available Kafka brokers in a cluster?

后端 未结 9 1143
甜味超标
甜味超标 2020-12-22 23:50

I am writing a shell script to monitor kafka brokers.

I have gone through some links and found that if ZooKeeper contains a list of brokers, and if, in this list, th

9条回答
  •  囚心锁ツ
    2020-12-23 00:10

    Here are a couple of quick functions I use when bash scripting Kafka Data Load into Demo Environments. In this example I use HDP with no security, but it is easily modified to other environments and intended to be quick and functional rather than particularly robust.

    The first retrieves the address of the first ZooKeeper node from the config:

    ZKS1=$(cat /usr/hdp/current/zookeeper-client/conf/zoo.cfg | grep server.1)
    [[ ${ZKS1} =~ server.1=(.*?):[0-9]*:[0-9]* ]]
    export ZKADDR=${BASH_REMATCH[1]}:2181
    echo "using ZooKeeper Server $ZKADDR"
    

    The second retrieves the Broker IDs from ZooKeeper:

    echo "Fetching list of Kafka Brokers"
    export BROKERIDS=$(/usr/hdp/current/kafka-broker/bin/zookeeper-shell.sh ${ZKADDR} <<< 'ls /brokers/ids' | tail -1)
    export BROKERIDS=${BROKERIDS//[!0-9 ]/}
    echo "Found Kafka Broker IDS: $BROKERIDS"
    

    The third parses ZooKeeper again to retrieve the list of Kafka Brokers Host:port ready for use in the command-line client:

    unset BROKERS
    for i in $BROKERIDS
    do
    DETAIL=$(/usr/hdp/current/kafka-broker/bin/zookeeper-shell.sh ${ZKADDR} <<< "get /brokers/ids/$i")
    [[ $DETAIL =~ PLAINTEXT:\/\/(.*?)\"\] ]]
    if [ -z ${BROKERS+x} ]; then BROKERS=${BASH_REMATCH[1]}; else 
    BROKERS="${BROKERS},${BASH_REMATCH[1]}"; fi
    done
    echo "Found Brokerlist: $BROKERS"
    

提交回复
热议问题