########################################
### function: install logstash, this version of es has authentication
### author: T_T
### create date: 2019-11
### modify date: 2019-11
########################################

## Useage ##
if [ -z "$1" ];then
    echo "Useage : sh $0 clustername {KAFKA_SERVERS ES_SERVERS ES_PASSWORD}"
    echo "Default: sh $0 clustername 127.0.0.1:9092 127.0.0.1:9200 Kingdee@2020"
    exit 1
fi

## ENV ##
source /etc/profile
pd=$PWD
CLUSTER_NAME=$1
KAFKA_SERVERS=$2
ES_SERVERS=$3
ES_USER=$4
ES_PASSWORD="`echo ${5}|sed -e 's/&/\\\\\&/g' -e 's/\//\\\\\//g'`"

logstash_version="logstash-6.8.18"
export COSMIC_CLUSTER_NAME=${COSMIC_CLUSTER_NAME:-ierp}
export MC_CLUSTER_NAME=${MC_CLUSTER_NAME:-mc}
export KAFKA_SERVERS=${KAFKA_SERVERS:-127.0.0.1:9092}
export ES_SERVERS=${ES_SERVERS:-127.0.0.1:9200}
export ES_USER=${ES_USER:-elastic}
export ES_PASSWORD=${ES_PASSWORD:-Kingdee@2020}

export CLUSTER_NAME=${CLUSTER_NAME:-${COSMIC_CLUSTER_NAME}}
export COSMIC_KAFKA_GROUP_ID=${COSMIC_KAFKA_GROUP_ID:-$CLUSTER_NAME}
export MC_KAFKA_GROUP_ID=${MC_KAFKA_GROUP_ID:-$MC_CLUSTER_NAME}
export COSMIC_KAFKA_TOPIC=${COSMIC_KAFKA_TOPIC:-"${CLUSTER_NAME}-log"}
export MC_KAFKA_TOPIC=${MC_KAFKA_TOPIC:-"${MC_CLUSTER_NAME}-log"}
export COSMIC_ES_INDEX=${COSMIC_ES_INDEX:-"${CLUSTER_NAME}-log-%{+yyyy-MM-dd}"}
export MC_ES_INDEX=${MC_ES_INDEX:-"${MC_CLUSTER_NAME}-log-%{+yyyy-MM-dd}"}


## install logstash ##
if [ -z "$JAVA_HOME" ];then
    echo "jdk not be installed,please install jdk first!"
    exit 2
fi 
tar -xzf $pd/$logstash_version.tar.gz
## Modify config file ## 

sed -i "s/bootstrap_servers .*/bootstrap_servers => \"${KAFKA_SERVERS}\"/g" $pd/$logstash_version/config/javalog.conf
sed -i "3,13 {s/group_id .*/group_id => \"${COSMIC_KAFKA_GROUP_ID}\"/g}" $pd/$logstash_version/config/javalog.conf
sed -i "15,25 {s/group_id .*/group_id => \"${MC_KAFKA_GROUP_ID}\"/g}" $pd/$logstash_version/config/javalog.conf
sed -i "3,13 {s/topics .*/topics => \"${COSMIC_KAFKA_TOPIC}\"/g}" $pd/$logstash_version/config/javalog.conf
sed -i "15,25 {s/topics .*/topics => \"${MC_KAFKA_TOPIC}\"/g}" $pd/$logstash_version/config/javalog.conf
sed -i "s/hosts .*/hosts  => \"${ES_SERVERS}\"/g" $pd/$logstash_version/config/javalog.conf
sed -i "29,37 {s/index .*/index  => \"${COSMIC_ES_INDEX}\"/g}" $pd/$logstash_version/config/javalog.conf
sed -i "39,47 {s/index .*/index  => \"${MC_ES_INDEX}\"/g}" $pd/$logstash_version/config/javalog.conf
sed -i "s/user .*/user => \"${ES_USER}\"/g" $pd/$logstash_version/config/javalog.conf
sed -i "s/password .*/password => \"${ES_PASSWORD}\"/g" $pd/$logstash_version/config/javalog.conf

## register systemd ##
systemctl stop logstash
cat > /usr/lib/systemd/system/logstash.service << EOF
[Unit]
Description=logstash server (broker)
After=syslog.target network.target 

[Service]
Type=simple
Environment="PATH=$JAVA_HOME/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/root/bin"
ExecStart=$pd/$logstash_version/bin/logstash "--path.config" "$pd/$logstash_version/config/javalog.conf"

[Install]
WantedBy=multi-user.target
EOF

## server options ##
systemctl daemon-reload
systemctl start logstash
systemctl enable logstash
