elk安装脚本
#!/bin/bash
. /etc/rc.d/init.d/functions
clear
echo "#############################################################################"
echo "# Auto Install ELK. ##"
echo "# Press Ctrl+C to cancel ##"
echo "# Any key to continue ##"
echo "# Softwae:elasticsearch-7.10.0/logstash-7.10.0/filebeat-7.10.0-/kibana-7.10.0 ##"
echo "# 本次安装没有安装elasticsearch-head,请使用谷歌插件 ##"
echo "#谷歌插件地址:https://chrome.google.com/webstore/detail/elasticsearch-head/ffmkiejjmecolpfloofpjologoblkegm"
echo "#############################################################################"
read -n 1
software_dir="/opt/elk"
elasticsearch_url="https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.10.0-linux-x86_64.tar.gz"
kibana_url="https://artifacts.elastic.co/downloads/kibana/kibana-7.10.0-linux-x86_64.tar.gz"
logstash_url="https://artifacts.elastic.co/downloads/logstash/logstash-7.10.0-linux-x86_64.tar.gz"
filebeat_url="https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-7.10.0-linux-x86_64.tar.gz"
sys_version=`cat /etc/redhat-release |awk '{print $4}'|cut -d. -f1`
#IP=`ip addr|grep "inet "|grep -v 127.0.0.1|awk '{print $2}'|cut -d/ -f1`
jvm_conf="/opt/elk/es/config/jvm.options"
sys_mem=`free -m|grep Mem:|awk '{print $2}'|awk '{sum+=$1} END {print sum/1024}'|cut -d. -f1`
#wget software
wget_fun() {
if [ ! -d ${software_dir} ];then
mkdir -p ${software_dir} && cd ${software_dir}
else
cd ${software_dir}
fi
for software in $elasticsearch_url $kibana_url $logstash_url $filebeat_url
do
wget -c $software
done
sleep 3
clear
}
init_sys() {
[ ${sys_version} != 7 ] && echo "Error:This Scripts Support Centos7.xx" && exit 1
[ $(id -u) != "0" ] && echo "Error: You must be root to run this script" && exit 1
setenforce 0
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source ipset="ssh_whitelist" port protocol="tcp" port="9200" accept" --permanent
#firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source ipset="ssh_whitelist" port protocol="tcp" port="9300" accept" --permanent
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source ipset="ssh_whitelist" port protocol="tcp" port="5601" accept" --permanent
firewall-cmd --reload
java -version &>/dev/null
if [ $? -eq 0];then
echo -e "\033[1;5;31m 已安装jdk,注意更改jdk所有者和组\033[0m"
else
echo "安装centos7jdk"
mkdir -p /local/usr/java && cd /usr
###jdk not install
tar -zxf jdk-15.0.1_linux-aarch64_bin.tar.gz -C /usr/java
chown swadmin:swadmin java
cat >>/etc/profile<<EOF
JAVA_HOME=/usr/local/java/jdk-15.0.1
export PATH=$PATH:$JAVA_HOME/bin
EOF
source /etc/profile
fi
cat >>/etc/security/limits.conf <<EOF
* soft nofile 65536
* hard nofile 131072
* soft nproc 2048
* hard nproc 4096
EOF
echo "vm.max_map_count=655360" >> /etc/sysctl.conf && sysctl -p
egrep '* soft nproc 1024' /etc/security/limits.d/20-nproc.conf &>/dev/null
if [ $? -eq 0 ];then
sed -i 's/ * soft nproc 1024/* soft nproc 4096/g' /etc/security/limits.d/20-nproc.conf
if [ $? -eq 0 ];then
echo "es系统参数设定成功"
else
echo "es系统参数设定失败"
fi
fi
}
install_elasticsearch() {
netstat -antup | grep 9200
if [ $? -ne 0 ];then
cd $software_dir
tar zxf elasticsearch-7.10.0-linux-x86_64.tar.gz
mv elasticsearch-7.10.0 es
if [ ${sys_mem} -eq 0 ];then
sed -i "s#`grep "^-Xmx" ${jvm_conf}`#"-Xmx512m"#g" ${jvm_conf}
sed -i "s#`grep "^-Xms" ${jvm_conf}`#"-Xms512m"#g" ${jvm_conf}
else
sed -i "s#`grep "^-Xmx" ${jvm_conf}`#"-Xmx${sys_mem}g/2"#g" ${jvm_conf}
sed -i "s#`grep "^-Xms" ${jvm_conf}`#"-Xms${sys_mem}g/2"#g" ${jvm_conf}
fi
cat >>/opt/elk/es/config/elasticsearch.yml<<EOF
node.name: elk-server
network.host: 0.0.0.0
cluster.initial_master_nodes: ["elk-server"]
EOF
else
echo "9200"&&exit 1
fi
}
#install logstash
install_logstash() {
cd $software_dir
tar -zxf logstash-7.10.0-linux-x86_64.tar.gz
mv logstash-7.10.0 logstash
#cat /dev/null>/opt/elk/logstash/config/logstash-sample.conf
cat>/opt/elk/logstash/config/logstash-sample.conf<<EOF
####################redis###################
input {
redis {
host => "127.0.0.1"
port => "17693"
password => "9tN6GFGK60Jk8BNkBJM611GwA66uDFeG"
data_type => "list"
key => "filebeat"
type => "redis-input"
}
}
filter {
}
output {
}
EOF
}
#install filebeat
install_filebeat() {
cd $software_dir
tar -zxf filebeat-7.10.0-linux-x86_64.tar.gz
mv filebeat-7.10.0-linux-x86_64 filebeat
sed -i 's/filebeat.inputs/#filebeat.inputs/g' /opt/elk/filebeat/filebeat.yml >/dev/null
sed -i 's/- type:/#- type:/g' /opt/elk/filebeat/filebeat.yml >/dev/null
sed -i 's/enabled: false/#enabled: false/g' /opt/elk/filebeat/filebeat.yml >/dev/null
sed -i 's@ - /var/log/\*.log@# - /var/log/*.log@g' /opt/elk/filebeat/filebeat.yml >/dev/null
sed -i 's/ paths:/# paths:/g' /opt/elk/filebeat/filebeat.yml >/dev/null
sed -i 's/output.elasticsearch:/#output.elasticsearch:/g' /opt/elk/filebeat/filebeat.yml >/dev/null
sed -i 's/ hosts: \["localhost:9200"\]/# hosts ["localhost:9200"]/g' /opt/elk/filebeat/filebeat.yml >/dev/null
cat >>/opt/elk/filebeat/filebeat.yml<<EOF
# ============================== Filebeat inputs ===============================
filebeat.inputs:
- type: log
enabled: True
paths:
- /opt/data/data_17693/redis.log
tags: ["redis17693"]
# ================================== Outputs ===================================
#-------------------------- reids output ------------------------------
output.redis:
hosts: ["127.0.0.1:17693"]
password: "9tN6GFGK60Jk8BNkBJM611GwA66uDFeG"
key: "filebeat"
timeout: 5
max_retries: 3
######
EOF
}
#install kibana
install_kibana() {
netstat -antup | grep 5601
if [ $? -ne 0 ];then
cd $software_dir
tar -zxf kibana-7.10.0-linux-x86_64.tar.gz
mv kibana-7.10.0-linux-x86_64 kibana
cat >>/opt/elk/kibana/config/kibana.yml<<EOF
server.port: 5601
server.host: "0.0.0.0"
elasticsearch.hosts: ["http://127.0.0.1:9200"]
i18n.locale: "zh-CN"
EOF
fi
}
start_up() {
cd /opt
chown -R swadmin:swadmin elk
su swadmin -c "
cd $software_dir/es
nohup ./bin/elasticsearch & &>/dev/null"
echo "es start"
su swadmin -c "
cd $software_dir/filebeat
nohup ./filebeat -e -c filebeat.yml & &>/dev/null"
echo "filebeat start"
su swadmin -c "
cd $software_dir/logstash
nohup ./bin/logstash -f config/logstash-sample.conf & &>/dev/null"
echo "logstash start"
su swadmin -c "
cd $software_dir/kibana
nohup ./bin/kibana & &>/dev/null"
echo "kibana start"
}
check() {
port=$1
program=$2
check_port=`netstat -lntup|grep ${port}|wc -l`
check_program=`ps -ef|grep ${program}|grep -v grep|wc -l`
if [ $check_port -gt 0 ] && [ $check_program -gt 0 ];then
action "${program} run is ok!" /bin/true
else
action "${program} run is error!" /bin/false
fi
}
main() {
wget_fun
init_sys
install_elasticsearch
install_filebeat
install_logstash
install_kibana
start_up
echo -e "\033[32m Checking Elasticsearch...\033[0m"
sleep 2
check 9200 elasticsearch
echo -e "\033[32m Checking Logstash...\033[0m"
sleep 2
check 9200 logstash
echo -e "\033[32m Checking Kibana...\033[0m"
sleep 2
check 5601 kibana
action "ELK install is success!" /bin/true
#echo "url:http://$IP:5601"
}
main