#/bin/bash
# 文件名： elk_install.sh
# 功能：　ELK　一键安装脚本[单机版]
# 作者：　shuhui@2016
# 附语: 暂时未启用 TLS

### 定义变量

SERVER_NODE=server.vqiu.cn                                                  # ELK 服务器域名[制作证书时用]
USER=admin                                                                  # Kibana 登陆名
PASS=admin                                                                  # Kibana 登陆密码

LOGSTASH_SERVER=127.0.0.1                                                   # logstash 服务器地址

PKG_URL=(
	https://down.vqiu.cn/package/tarball/elk/elasticsearch-2.3.2.rpm
	https://down.vqiu.cn/package/tarball/elk/kibana-4.5.0-1.x86_64.rpm
	https://down.vqiu.cn/package/tarball/elk/logstash-2.3.2-1.noarch.rpm
	https://down.vqiu.cn/package/tarball/elk/filebeat-1.2.2-x86_64.rpm
)


function download_pkg() {
	for pkg in ${PKG_URL[@]}
	do
		[[ -e ${pkg##*/} ]] || curl -L -O $pkg
	done
}

function jdk_install() {
	yum -y install java-1.8.0-openjdk
}

function elasticsearch_install() {
	yum -y localinstall elasticsearch-2.3.2.rpm

	# 更改 elasticsearch 数据存放路径
	mkdir -pv /data/elasticsearch/{data,logs}
	chown -R elasticsearch:elasticsearch /data/elasticsearch
	sed -i "s@# path.data: /path/to/data@path.data: /data/elasticsearch/data@" /etc/elasticsearch/elasticsearch.yml

	systemctl enable elasticsearch
	systemctl restart elasticsearch
}

function kibana_install() {
	yum -y localinstall kibana-4.5.0-1.x86_64.rpm

	# 只监听本地回环接口
	sed -i 's/.*server.host:.*/server.host: localhost/' /opt/kibana/config/kibana.yml

	systemctl start kibana
	chkconfig kibana on

	yum -y install nginx httpd-tools
	htpasswd -cb /etc/nginx/htpasswd.users ${USER} ${PASS}
	cat > /etc/nginx/conf.d/kibana.conf <<EOF
server {
    listen 80;

    server_name ${SERVER_NODE};

    auth_basic "Restricted Access";
    auth_basic_user_file /etc/nginx/htpasswd.users;

    location / {
        proxy_pass http://localhost:5601;
        proxy_http_version 1.1;
        proxy_set_header Upgrade \$http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host \$host;
        proxy_cache_bypass \$http_upgrade;        
    }
}
EOF
	nginx -t >/dev/null 2>&1
	if [[ $? -eq 0 ]]
	then
		systemctl enable nginx.service
		systemctl restart nginx.service
	fi

	cd ~
	curl -L -O https://down.vqiu.cn/package/tarball/elk/beats-dashboards-1.1.0.zip
	unzip beats-dashboards-1.1.0.zip
	cd beats-dashboards-1.1.0
	./load.sh

	cd ~
	curl -O https://down.vqiu.cn/package/tarball/elk/filebeat-index-template.json
	curl -XPUT 'http://localhost:9200/_template/filebeat?pretty' -d@filebeat-index-template.json

}

function logstash_install() {
	yum -y localinstall logstash-2.3.2-1.noarch.rpm
	
	# 生成证书
	#cd /etc/pki/tls
	#openssl req -subj "/CN=${SERVER_NODE}/" -x509 -days 3650 -batch -nodes -newkey rsa:2048 -keyout private/filebeat.key -out certs/filebeat.crt

	curl -L -s https://down.vqiu.cn/conf/Linux_conf/elk/01-beats-input.conf -o /etc/logstash/conf.d/01-beats-input.conf 
	curl -L -s https://down.vqiu.cn/conf/Linux_conf/elk/10-syslog-filter.conf -o /etc/logstash/conf.d/10-syslog-filter.conf
	curl -L -s https://down.vqiu.cn/conf/Linux_conf/elk/30-elasticsearch-output.conf -o /etc/logstash/conf.d/30-elasticsearch-output.conf

    #/opt/logstash/bin/plugin update logstash-input-beats

	service logstash configtest
	systemctl restart logstash
	sudo chkconfig logstash on
	
}

function filebeat_install() {

	rpm -ivh filebeat-1.2.2-x86_64.rpm
	cp /etc/filebeat/filebeat.yml /etc/filebeat/filebeat.yml.origin
	curl -L -s https://down.vqiu.cn/conf/Linux_conf/elk/filebeat.yml-centos.yml -o /etc/filebeat/filebeat.yml
	sed -i "s/hosts:.*/hosts: [\"${LOGSTASH_SERVER}:5044\"]/"  /etc/filebeat/filebeat.yml
	
	systemctl enable filebeat
	systemctl start filebeat
}

function nginx_config() {

	mkdir -p /opt/logstash/patterns
	curl -L -s https://down.vqiu.cn/conf/Linux_conf/elk/nginx -o /opt/logstash/patterns/nginx
	chown -R logstash:logstash  /opt/logstash/patterns

	curl -L -s https://down.vqiu.cn/package/tarball/GeoIP/GeoLiteCity.dat -o /etc/logstash/GeoLiteCity.dat
	chown logstash /etc/logstash/GeoLiteCity.dat

	curl -L -s https://down.vqiu.cn/conf/Linux_conf/elk/11-nginx-filter.conf -o /etc/logstash/conf.d/11-nginx-fileter.conf

	service logstash configtest && chkconfig logstash on && service logstash restart

}

download_pkg
jdk_install
elasticsearch_install
kibana_install
logstash_install
filebeat_install
nginx_config
