跳过正文
  1. Posts/

ELK安装部署脚本及MySQL主从同步监控脚本

·430 字·3 分钟·
Kubehan
作者
Kubehan
云原生知识栈:深度解析容器技术、Kubernetes、Istio、DevOps 实践、Prometheus 监控、Envoy 代理、Golang 开发及云原生架构与微服务趋势的专业博客

ELK安装部署脚本及MySQL主从同步监控脚本

第一个ELK的安装部署脚本

<span class="hljs-meta" style="color: #75715e; line-height: 26px;">#!/bin/bash</span><br><span class="hljs-comment" style="color: #75715e; line-height: 26px;">#mail:xuel@anchnet.com</span><br><span class="hljs-comment" style="color: #75715e; line-height: 26px;">#data:2017/9/7</span><br><span class="hljs-comment" style="color: #75715e; line-height: 26px;">#AutoInstall ELK scripts</span><br><span class="hljs-comment" style="color: #75715e; line-height: 26px;">#Software:elasticsearch-5.4.1/logstash-5.4.1/filebeat-5.4.1/kibana-5.4.1</span><br>clear<br><span class="hljs-built_in" style="color: #a6e22e; line-height: 26px;">echo</span> <span class="hljs-string" style="color: #a6e22e; line-height: 26px;">"##########################################"</span><br><span class="hljs-built_in" style="color: #a6e22e; line-height: 26px;">echo</span> <span class="hljs-string" style="color: #a6e22e; line-height: 26px;">"#       Auto Install ELK.               ##"</span><br><span class="hljs-built_in" style="color: #a6e22e; line-height: 26px;">echo</span> <span class="hljs-string" style="color: #a6e22e; line-height: 26px;">"#       Press Ctrl + C to cancel        ##"</span><br><span class="hljs-built_in" style="color: #a6e22e; line-height: 26px;">echo</span> <span class="hljs-string" style="color: #a6e22e; line-height: 26px;">"#       Any key to continue             ##"</span><br><span class="hljs-built_in" style="color: #a6e22e; line-height: 26px;">echo</span> <span class="hljs-string" style="color: #a6e22e; line-height: 26px;">"##########################################"</span><br><span class="hljs-built_in" style="color: #a6e22e; line-height: 26px;">read</span> -p <br>software_dir=<span class="hljs-string" style="color: #a6e22e; line-height: 26px;">"/usr/local/software"</span><br>elasticsearch_url=<span class="hljs-string" style="color: #a6e22e; line-height: 26px;">"https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.4.1.tar.gz"</span><br>kibana_url=<span class="hljs-string" style="color: #a6e22e; line-height: 26px;">"https://artifacts.elastic.co/downloads/kibana/kibana-5.4.1-linux-x86_64.tar.gz"</span><br>logstash_url=<span class="hljs-string" style="color: #a6e22e; line-height: 26px;">"https://artifacts.elastic.co/downloads/logstash/logstash-5.4.1.tar.gz"</span><br>filebeat_url=<span class="hljs-string" style="color: #a6e22e; line-height: 26px;">"https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-5.4.1-linux-x86_64.tar.gz"</span><br>sys_version=<code>cat /etc/redhat-release |awk <span class="hljs-string" style="color: #a6e22e; line-height: 26px;">'{print $4}'</span>|cut -d. -f1
IP=ip addr|grep <span class="hljs-string" style="color: #a6e22e; line-height: 26px;">"inet "</span>|grep -v 127.0.0.1|awk <span class="hljs-string" style="color: #a6e22e; line-height: 26px;">'{print $2}'</span>|cut -d/ -f1
jvm_conf="/usr/local/elasticsearch/config/jvm.options"
sys_mem=free -m|grep Mem:|awk <span class="hljs-string" style="color: #a6e22e; line-height: 26px;">'{print $2}'</span>|awk <span class="hljs-string" style="color: #a6e22e; line-height: 26px;">'{sum+=$1} END {print sum/1024}'</span>|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
clear
}
#initial system:install java wget;set hostname;disable firewalld
init_sys() {
[ -f /etc/init.d/functions ] && . /etc/init.d/functions
"${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
sed -i "s/SELINUX=enforcing/SELINUX=disabled/"  /etc/selinux/config
setenforce 0
yum install -y java-1.8.0-openjdk wget net-tools
hostnamectl set-hostname elk-server          
systemctl stop firewalld
cat >>/etc/security/limits.conf<<EOF
* soft nofile 65536 
* hard nofile 65536 
* soft nGproc 65536 
* hard nproc 65536
EOF
}

#install elasticsearch
install_elasticsearch() {
cd $software_dir
tar zxf elasticsearch-5.4.1.tar.gz
mv elasticsearch-5.4.1 /usr/local/elasticsearch
mkdir -p /usr/local/elasticsearch/data /usr/local/elasticsearch/logs
useradd elasticsearch
chown -R elasticsearch:elasticsearch /usr/local/elasticsearch
echo "vm.max_map_count = 655360" >>/etc/sysctl.conf && sysctl -p
if [ ${sys_mem} -eq 0 ];then
 sed -i "s#grep "</span>^-Xmx<span class="hljs-string" style="color: #a6e22e; line-height: 26px;">" <span class="hljs-variable" style="color: #a6e22e; line-height: 26px;">${jvm_conf}</span>#"-Xmx512m"#g" ${jvm_conf}
 sed -i "s#grep "</span>^-Xms<span class="hljs-string" style="color: #a6e22e; line-height: 26px;">" <span class="hljs-variable" style="color: #a6e22e; line-height: 26px;">${jvm_conf}</span>#"-Xms512m"#g" ${jvm_conf}
else
 sed -i "s#grep "</span>^-Xmx<span class="hljs-string" style="color: #a6e22e; line-height: 26px;">" <span class="hljs-variable" style="color: #a6e22e; line-height: 26px;">${jvm_conf}</span>#"-Xmx${sys_mem}g"#g" ${jvm_conf}
 sed -i "s#grep "</span>^-Xms<span class="hljs-string" style="color: #a6e22e; line-height: 26px;">" <span class="hljs-variable" style="color: #a6e22e; line-height: 26px;">${jvm_conf}</span>#"-Xms${sys_mem}g"#g" ${jvm_conf}
fi
cat >>/usr/local/elasticsearch/config/elasticsearch.yml<<EOF
cluster.name: my-application
node.name: elk-server
path.data: /usr/local/elasticsearch/data
path.logs: /usr/local/elasticsearch/logs
network.host: 127.0.0.1
http.port: 9200
discovery.zen.ping.unicast.hosts: ["elk-server"]
EOF
su - elasticsearch -c "nohup /usr/local/elasticsearch/bin/elasticsearch &"
}

#install logstash
install_logstash() {
cd $software_dir
tar -zxf logstash-5.4.1.tar.gz
mv logstash-5.4.1 /usr/local/logstash
cat>/usr/local/logstash/config/01-syslog.conf<<EOF
input {
 beats {
  port => "5044"
  }
 }
output {
 elasticsearch {
  hosts => "127.0.0.1:9200"
 }
 stdout { codec => rubydebug }
}
EOF
nohup /usr/local/logstash/bin/logstash -f /usr/local/logstash/config/01-syslog.conf & >/dev/null
}

#install filebeat
install_filebeat() {
cd $software_dir
tar -zxf filebeat-5.4.1-linux-x86_64.tar.gz
mv filebeat-5.4.1-linux-x86_64 /usr/local/filebeat
cat >/usr/local/filebeat/filebeat.yml<<EOF
filebeat.prospectors:
- input_type: log
  paths:
    - /var/log/*.log
output.logstash:
  hosts: ["127.0.0.1:5044"]
EOF
cd /usr/local/filebeat/
nohup /usr/local/filebeat/filebeat & >/dev/null
}

#install kibana
install_kibana() {
cd $software_dir
tar -zxf kibana-5.4.1-linux-x86_64.tar.gz
mv kibana-5.4.1-linux-x86_64 /usr/local/kibana
cat >> /usr/local/kibana/config/kibana.yml <<EOF
server.port: 5601
server.host: "0.0.0.0"
elasticsearch.url: "http://127.0.0.1:9200"
EOF
nohup /usr/local/kibana/bin/kibana & >/dev/null
}

check() {
port=$1
program=$2
check_port=netstat -lntup|grep <span class="hljs-variable" style="color: #a6e22e; line-height: 26px;">${port}</span>|wc -l
check_program=ps -ef|grep <span class="hljs-variable" style="color: #a6e22e; line-height: 26px;">${program}</span>|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() {
init_sys
wget_fun
install_elasticsearch
install_filebeat
install_logstash
install_kibana
echo -e "\033[32m Checking Elasticsearch...\033[0m"
sleep 20
check :9200 "elasticsearch"
echo -e "\033[32m Checking Logstash...\033[0m"
sleep 2
check ":9600" "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
</code>

第二个MySQL同步状态检查脚本

<span class="hljs-meta" style="color: #75715e; line-height: 26px;">#!/bin/bash</span><br>mysql_user=<span class="hljs-string" style="color: #a6e22e; line-height: 26px;">'root'</span><br>mysql_pass=<span class="hljs-string" style="color: #a6e22e; line-height: 26px;">"bhVd!564qazWSX78U#7"</span><br>data=$(/bin/date +%Y-%m-%d-%H:%M)<br>/bin/netstat -lntup|egrep <span class="hljs-string" style="color: #a6e22e; line-height: 26px;">":3306"</span>|grep -v grep>/dev/null0<br><span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">if</span> [ $? -eq 0 ];<span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">then</span><br> Slave_IO=<code>/data/mysql/bin/mysql -u<span class="hljs-variable" style="color: #a6e22e; line-height: 26px;">${mysql_user}</span> -p<span class="hljs-variable" style="color: #a6e22e; line-height: 26px;">${mysql_pass}</span> -e <span class="hljs-string" style="color: #a6e22e; line-height: 26px;">"show slave status\G"</span>|grep <span class="hljs-string" style="color: #a6e22e; line-height: 26px;">"Slave_IO_Running:"</span>|awk -F<span class="hljs-string" style="color: #a6e22e; line-height: 26px;">": "</span> <span class="hljs-string" style="color: #a6e22e; line-height: 26px;">'{print $2}'</span>
 Slave_SQL=/data/mysql/bin/mysql -u<span class="hljs-variable" style="color: #a6e22e; line-height: 26px;">${mysql_user}</span> -p<span class="hljs-variable" style="color: #a6e22e; line-height: 26px;">${mysql_pass}</span> -e <span class="hljs-string" style="color: #a6e22e; line-height: 26px;">"show slave status\G"</span>|grep <span class="hljs-string" style="color: #a6e22e; line-height: 26px;">"Slave_SQL_Running:"</span>|awk -F<span class="hljs-string" style="color: #a6e22e; line-height: 26px;">": "</span> <span class="hljs-string" style="color: #a6e22e; line-height: 26px;">'{print $2}'</span>
  if [ "$Slave_IO" == "Yes" ] && [ "$Slave_SQL" == "Yes" ];then
   STAT=1 && echo "$data mysql-status is ok">>/var/log/mysql-status.log
  else
   STAT=0 && echo "$data mysql-status is error">>/var/log/mysql-status.log
  fi
else
 STAT=0 && echo "$data mysql-status is error">>/var/log/mysql-status.log
fi
/usr/bin/zabbix_sender -z 101.227.67.205 -s "DaoDaEr-mysql-status" -k mysql -o $STAT
</code>