Kafka Mirror

Bash script for starting Kafka mirror

9

Votes

#!/bin/bash
#
# /etc/init.d/kafka_mirror -- startup script for kafka mirror
# chkconfig: - 98 02
# Description: Start kafka mirror
### END INIT INFO

# EDIT THESE VARS BEFORE EXECUTION : 
export KAFKA_USER=[YOUR_KAFKA_USER]
export KAFKA_GROUP=[KAFKA_USER_GROUP]
export KAFKA_HOME=[KAFKA_DIR_PATH]
export KAFKA_CONFIG=[CONFIG_FOLDER_PATH]
export LOG=/[CHOOSE_YOUR_PATH]/kafka_mirror.out

# read in consumer & producer config files from /opt/etl/kafka/config
export KAFKA_MIRROR_CONSUMER_CONFIGS=$(ls $KAFKA_CONFIG/mirrorConsumer*)
export KAFKA_MIRROR_PRODUCER_CONFIG=$(ls $KAFKA_CONFIG/mirrorProducer*)

export KAFKA_MIRROR_NUM_STREAMS=2
export KAFKA_MIRROR_NUM_PRODUCERS=1
export KAFKA_MIRROR_QUEUE_SIZE=10000
export KAFKA_MIRROR_WHITELIST='.*'

export JAVA_HOME="/usr/java/latest/"

# kafka should run with user 'etl'
export KAFKA_USER=etl

# 'KAFKA_MIRROR_USAGE' is the message if this script is called without any options
KAFKA_MIRROR_USAGE="Usage: $0 {\e[00;32mstart\e[00m|\e[00;31mstop\e[00m|\e[00;33mstatus\e[00m|\e[00;31mrestart\e[00m}"

kafka_mirror_pid() {
    echo `ps -ef |grep MirrorMaker |grep -v "kafka-run-class.sh" |grep -v grep |awk '{print $2}'`
}

start() {
    pid=$(kafka_mirror_pid)
    if [ -n "$pid" ]; then
        echo -e "\e[00;31mKafka Mirror Maker is already running (pid: $pid)\e[00m"
    else
        # Start kafka Mirror
        echo -e "\e[00;32mStarting Kafka Mirror Maker \e[00m"
        if [ `user_exists $KAFKA_USER` = "0" ]; then
            su $KAFKA_USER -c '$KAFKA_HOME/bin/kafka-run-class.sh kafka.tools.MirrorMaker --consumer.config $KAFKA_MIRROR_CONSUMER_CONFIGS --num.streams $KAFKA_MIRROR_NUM_STREAMS --producer.config $KAFKA_MIRROR_PRODUCER_CONFIG --whitelist=$KAFKA_MIRROR_WHITELIST >> $LOG &'
        else
            sh $KAFKA_HOME/bin/kafka-run-class.sh kafka.tools.MirrorMaker --consumer.config $KAFKA_MIRROR_CONSUMER_CONFIGS --num.streams $KAFKA_MIRROR_NUM_STREAMS --producer.config $KAFKA_MIRROR_PRODUCER_CONFIG --whitelist=$KAFKA_MIRROR_WHITELIST >> $LOG &
        fi
        sleep 2
        status
    fi
    return 0
}

status(){
    pid=$(kafka_mirror_pid)
    if [ -n "$pid" ]; then echo -e "\e[00;33mKafka Mirror Maker is running with pid: $pid\e[00m"
        else echo -e "\e[00;31mKafka Mirror Maker is not running\e[00m"
    fi
}

stop() {
    echo -e "\e[00;31mStopping Kafka Mirror Process\e[00m"
    pid=$(kafka_mirror_pid)
    status
    if [ -n "$pid" ]; then
        /bin/kill -9 $pid >/dev/null 2>&1
        status
    fi
    return 0
}

user_exists(){
    if [ "$(id |cut -d '(' -f 2 |cut -d ')' -f 1)" == "etl" ] >/dev/null 2>&1; then
        echo "1"
    else
        echo "0"
    fi
}

case $1 in

        start)
          start
        ;;

        stop)
          stop
        ;;

        restart)
          stop
          start
        ;;

        status)
                status

        ;;

        *)
                echo -e $KAFKA_MIRROR_USAGE
        ;;
esac

Vote Here

You must earn at least 1 vote on your snippets to be allowed to vote

Terms Of Use

Privacy Policy

Featured snippets are MIT license

Gears & Masters

Advertise

DevOpsnipp.com © 2020

medium.png