#!/bin/bas
PackageDir=/usr/local/src

# 定义颜色函数
color(){
    case $1 in
        "red") echo -e "\033[31m$2\033[0m" ;;     # 红色
        "green") echo -e "\033[32m$2\033[0m" ;;   # 绿色
        "yellow") echo -e "\033[33m$2\033[0m" ;;  # 黄色
        "blue") echo -e "\033[34m$2\033[0m" ;;    # 蓝色
        *) echo "$2" ;;
    esac
}

install_logstash(){
cd $PackageDir
if [ ! -f "logstash-7.6.2.rpm" ]; then
        wget http://54.179.201.103:8001/logstash-7.6.2.rpm
else
        color red  'package:logstash-7.6.2.rpm already exist!'
fi
        yum -y localinstall logstash-7.6.2.rpm
        sed -i s/Xms1g/Xms4g/g /etc/logstash/jvm.options
        sed -i s/Xmx1g/Xmx4g/g /etc/logstash/jvm.options
	if [ ! -f "/etc/systemd/system/logstash.service" ];then
	cat >>/etc/systemd/system/logstash.service<<EOF
		[Unit]
Description=logstash

[Service]
Type=simple
User=logstash
Group=logstash
# Load env vars from /etc/default/ and /etc/sysconfig/ if they exist.
# Prefixing the path with '-' makes it try to load, but if the file doesn't
# exist, it continues onward.
EnvironmentFile=-/etc/default/logstash
EnvironmentFile=-/etc/sysconfig/logstash
ExecStart=/usr/share/logstash/bin/logstash "--path.settings" "/etc/logstash"
Restart=always
WorkingDirectory=/
Nice=19
LimitNOFILE=16384

[Install]
WantedBy=multi-user.target
EOF
        fi

	systemctl enable logstash
	systemctl enable logstash

}

create_config(){
        #请输入Topic
        echo -n $(color "green" "请输入topic(例如：aws01,aws02,aws03...)：") 
        read topic
        echo $(color "blue" "你输入的topic为：$topic")
        echo -n $(color "green" "请输入topicgroup(例如：aws01group,aws02group.aws03group)：") 
        read topicgroup
        echo $(color "blue" "你输入的topicgroup为：$topicgroup")
        cat >>/etc/logstash/conf.d/logstash.conf<<EOF
input {
  kafka {
    bootstrap_servers => "192.168.10.170:9092,192.168.10.171:9092,192.168.10.172:9092"
    topics => ["$topic"]
    group_id => "$topicgroup"
    client_id => "logstash-1"
    codec => "json"
  }
}

filter {
    ###替换@timestamp时间为日志真实时间######
    grok {
        match => {  "message" => "(?<timestamp>%{TIMESTAMP_ISO8601})"  }
    }
    date {
        match => [ "timestamp", "ISO8601" ]
    }
    mutate {
         remove_field => [ "timestamp" ]
    }
}

output {
EOF



cd /etc/logstash/conf.d
for ip in 10 11 20 85 115
do
        services=`sudo ssh -i ${key_file}  ec2-user@192.168.10.$ip  "ls /home/ec2-user/application"`
        for service in $services
        do
		if [[ $service =~ stats ]] || [[ $service =~ elotcmd ]] || [[ $service == openfire_virtual_client ]] || [[ $service =~ bak$ ]] || [[ $service =~ not-use ]] || [[ $service =~ pargo ]];then
                        continue
                fi

		cat >>/etc/logstash/conf.d/logstash.conf<<EOF
  else if [type] == "web$ip-$service-info" {
    elasticsearch {
      hosts => ["http://192.168.10.170:9200", "http://192.168.10.171:9200", "http://192.168.10.172:9200"]
      user => "elastic"
      password => "Yang18659250927"
      index => "web$ip-$service-info-%{+YYYY.MM.dd}"
    }
  }
  else if [type] == "web$ip-$service-error" {
    elasticsearch {
      hosts => ["http://192.168.10.170:9200", "http://192.168.10.171:9200", "http://192.168.10.172:9200"]
      user => "elastic"
      password => "Yang18659250927"
      index => "web$ip-$service-error-%{+YYYY.MM.dd}"
    }
  }

EOF
        done
done
sed -i '25s/  else if /  if /'  /etc/logstash/conf.d/logstash.conf       
echo "}" >> /etc/logstash/conf.d/logstash.conf
}




echo -n $(color "green" "请选择对应环境的key文件(例如：第二套：AWS02，第三套:AWS03......)") 
read Key
echo $(color "blue" "你输入的key为：$Key")
[ ! -f "/home/ec2-user/$Key" ] && wget -P /home/ec2-user  http://54.179.201.103:8001/key/$Key".pem"
chmod 600 /home/ec2-user/$Key".pem"
key_file="/home/ec2-user/"$Key".pem"

install_logstash
create_config
