|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +#data:2017/9/7 |
| 4 | +#AutoInstall ELK scripts |
| 5 | +#Software:elasticsearch-5.4.1/logstash-5.4.1/filebeat-5.4.1/kibana-5.4.1 |
| 6 | +clear |
| 7 | +echo "##########################################" |
| 8 | +echo "# Auto Install ELK. ##" |
| 9 | +echo "# Press Ctrl + C to cancel ##" |
| 10 | +echo "# Any key to continue ##" |
| 11 | +echo "##########################################" |
| 12 | +read -p |
| 13 | +software_dir="/usr/local/software" |
| 14 | +elasticsearch_url="https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.4.1.tar.gz" |
| 15 | +kibana_url="https://artifacts.elastic.co/downloads/kibana/kibana-5.4.1-linux-x86_64.tar.gz" |
| 16 | +logstash_url="https://artifacts.elastic.co/downloads/logstash/logstash-5.4.1.tar.gz" |
| 17 | +filebeat_url="https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-5.4.1-linux-x86_64.tar.gz" |
| 18 | +sys_version=`cat /etc/redhat-release |awk '{print $4}'|cut -d. -f1` |
| 19 | +IP=`ip addr|grep "inet "|grep -v 127.0.0.1|awk '{print $2}'|cut -d/ -f1` |
| 20 | +jvm_conf="/usr/local/elasticsearch/config/jvm.options" |
| 21 | +sys_mem=`free -m|grep Mem:|awk '{print $2}'|awk '{sum+=$1} END {print sum/1024}'|cut -d. -f1` |
| 22 | + |
| 23 | +#wget software |
| 24 | +wget_fun() { |
| 25 | +if [ ! -d ${software_dir} ];then |
| 26 | + mkdir -p ${software_dir} && cd ${software_dir} |
| 27 | +else |
| 28 | + cd ${software_dir} |
| 29 | +fi |
| 30 | +for software in $elasticsearch_url $kibana_url $logstash_url $filebeat_url |
| 31 | +do |
| 32 | + wget -c $software |
| 33 | +done |
| 34 | +clear |
| 35 | +} |
| 36 | +#initial system:install java wget;set hostname;disable firewalld |
| 37 | +init_sys() { |
| 38 | +[ -f /etc/init.d/functions ] && . /etc/init.d/functions |
| 39 | +[ "${sys_version}" != "7" ] && echo "Error:This Scripts Support Centos7.xx" && exit 1 |
| 40 | +[ $(id -u) != "0" ] && echo "Error: You must be root to run this script" && exit 1 |
| 41 | +sed -i "s/SELINUX=enforcing/SELINUX=disabled/" /etc/selinux/config |
| 42 | +setenforce 0 |
| 43 | +yum install -y java-1.8.0-openjdk wget net-tools |
| 44 | +hostnamectl set-hostname elk-server |
| 45 | +systemctl stop firewalld |
| 46 | +cat >>/etc/security/limits.conf<<EOF |
| 47 | +* soft nofile 65536 |
| 48 | +* hard nofile 65536 |
| 49 | +* soft nGproc 65536 |
| 50 | +* hard nproc 65536 |
| 51 | +EOF |
| 52 | +} |
| 53 | + |
| 54 | +#install elasticsearch |
| 55 | +install_elasticsearch() { |
| 56 | +cd $software_dir |
| 57 | +tar zxf elasticsearch-5.4.1.tar.gz |
| 58 | +mv elasticsearch-5.4.1 /usr/local/elasticsearch |
| 59 | +mkdir -p /usr/local/elasticsearch/data /usr/local/elasticsearch/logs |
| 60 | +useradd elasticsearch |
| 61 | +chown -R elasticsearch:elasticsearch /usr/local/elasticsearch |
| 62 | +echo "vm.max_map_count = 655360" >>/etc/sysctl.conf && sysctl -p |
| 63 | +if [ ${sys_mem} -eq 0 ];then |
| 64 | + sed -i "s#`grep "^-Xmx" ${jvm_conf}`#"-Xmx512m"#g" ${jvm_conf} |
| 65 | + sed -i "s#`grep "^-Xms" ${jvm_conf}`#"-Xms512m"#g" ${jvm_conf} |
| 66 | +else |
| 67 | + sed -i "s#`grep "^-Xmx" ${jvm_conf}`#"-Xmx${sys_mem}g"#g" ${jvm_conf} |
| 68 | + sed -i "s#`grep "^-Xms" ${jvm_conf}`#"-Xms${sys_mem}g"#g" ${jvm_conf} |
| 69 | +fi |
| 70 | +cat >>/usr/local/elasticsearch/config/elasticsearch.yml<<EOF |
| 71 | +cluster.name: my-application |
| 72 | +node.name: elk-server |
| 73 | +path.data: /usr/local/elasticsearch/data |
| 74 | +path.logs: /usr/local/elasticsearch/logs |
| 75 | +network.host: 127.0.0.1 |
| 76 | +http.port: 9200 |
| 77 | +discovery.zen.ping.unicast.hosts: ["elk-server"] |
| 78 | +EOF |
| 79 | +su - elasticsearch -c "nohup /usr/local/elasticsearch/bin/elasticsearch &" |
| 80 | +} |
| 81 | + |
| 82 | +#install logstash |
| 83 | +install_logstash() { |
| 84 | +cd $software_dir |
| 85 | +tar -zxf logstash-5.4.1.tar.gz |
| 86 | +mv logstash-5.4.1 /usr/local/logstash |
| 87 | +cat>/usr/local/logstash/config/01-syslog.conf<<EOF |
| 88 | +input { |
| 89 | + beats { |
| 90 | + port => "5044" |
| 91 | + } |
| 92 | + } |
| 93 | +output { |
| 94 | + elasticsearch { |
| 95 | + hosts => "127.0.0.1:9200" |
| 96 | + } |
| 97 | + stdout { codec => rubydebug } |
| 98 | +} |
| 99 | +EOF |
| 100 | +nohup /usr/local/logstash/bin/logstash -f /usr/local/logstash/config/01-syslog.conf & >/dev/null |
| 101 | +} |
| 102 | + |
| 103 | +#install filebeat |
| 104 | +install_filebeat() { |
| 105 | +cd $software_dir |
| 106 | +tar -zxf filebeat-5.4.1-linux-x86_64.tar.gz |
| 107 | +mv filebeat-5.4.1-linux-x86_64 /usr/local/filebeat |
| 108 | +cat >/usr/local/filebeat/filebeat.yml<<EOF |
| 109 | +filebeat.prospectors: |
| 110 | +- input_type: log |
| 111 | + paths: |
| 112 | + - /var/log/*.log |
| 113 | +output.logstash: |
| 114 | + hosts: ["127.0.0.1:5044"] |
| 115 | +EOF |
| 116 | +cd /usr/local/filebeat/ |
| 117 | +nohup /usr/local/filebeat/filebeat & >/dev/null |
| 118 | +} |
| 119 | + |
| 120 | +#install kibana |
| 121 | +install_kibana() { |
| 122 | +cd $software_dir |
| 123 | +tar -zxf kibana-5.4.1-linux-x86_64.tar.gz |
| 124 | +mv kibana-5.4.1-linux-x86_64 /usr/local/kibana |
| 125 | +cat >> /usr/local/kibana/config/kibana.yml <<EOF |
| 126 | +server.port: 5601 |
| 127 | +server.host: "0.0.0.0" |
| 128 | +elasticsearch.url: "http://127.0.0.1:9200" |
| 129 | +EOF |
| 130 | +nohup /usr/local/kibana/bin/kibana & >/dev/null |
| 131 | +} |
| 132 | + |
| 133 | +check() { |
| 134 | +port=$1 |
| 135 | +program=$2 |
| 136 | +check_port=`netstat -lntup|grep ${port}|wc -l` |
| 137 | +check_program=`ps -ef|grep ${program}|grep -v grep|wc -l` |
| 138 | +if [ $check_port -gt 0 ] && [ $check_program -gt 0 ];then |
| 139 | + action "${program} run is ok!" /bin/true |
| 140 | +else |
| 141 | + action "${program} run is error!" /bin/false |
| 142 | +fi |
| 143 | +} |
| 144 | + |
| 145 | +main() { |
| 146 | +init_sys |
| 147 | +wget_fun |
| 148 | +install_elasticsearch |
| 149 | +install_filebeat |
| 150 | +install_logstash |
| 151 | +install_kibana |
| 152 | +echo -e "\033[32m Checking Elasticsearch...\033[0m" |
| 153 | +sleep 20 |
| 154 | +check :9200 "elasticsearch" |
| 155 | +echo -e "\033[32m Checking Logstash...\033[0m" |
| 156 | +sleep 2 |
| 157 | +check ":9600" "logstash" |
| 158 | +echo -e "\033[32m Checking Kibana...\033[0m" |
| 159 | +sleep 2 |
| 160 | +check ":5601" "kibana" |
| 161 | +action "ELK install is success!" /bin/true |
| 162 | +echo "url:http://$IP:5601" |
| 163 | +} |
| 164 | +main |
0 commit comments