欢迎点评,大家一起来优化
计划思路: 只有在mysql slave java 进程状态都正常的情况下才允许nginx 运行, 否则就干掉它, 负载用的是阿里的SLB
#bin/bash
#邮件函数 我怕看不见就定义了发送十条
mysql_kill_ngx(){
ps -ef|grep nginx|grep -v grep |awk '{print $2}'|xargs -i{} kill -9 {}
pkill nginx
for i in {1..10};
do
char="MySQL slave is not ok"
echo "$char"
echo "ERROR:`hostname` $char"|mail -s "$char" ***@.com>/dev/null 2>&1
#sleep 60
done
}
java_kill_ngx(){
ps -ef|grep nginx|grep -v grep |awk '{print $2}'|xargs -i{} kill -9 {}
pkill nginx
for i in {1..10};
do
echo "ERROR:`hostname` Java is not ok "|mail -s "Java is not ok" ***@.com >/dev/null 2>&1
#sleep 60
done
}
#这里是一个循环保证脚本一直监控
while true
do
HOME=/home /data/opt/mysql/bin/mysql -e "show slave statusG;"| egrep "_Running|Behind_Master" >slave.log #查询slave状态 这个命令方法解释参见(https://www.cnblogs.com/musen/p/11512122.html)
array=($(egrep "_Running|Behind_Master" slave.log|awk '{print $NF}'))
#判断Mysql slave状态
if [ "${array[0]}" == "Yes" -a "${array[1]}" == "Yes" -a "${array[2]}" == "0" ]
then
echo "MySQL is slave is ok"
java_status=`ps -ef|grep java|grep 'jar$'|grep app`
nginx_status=`ps -ef|grep nginx|grep -v grep`
#判断java进程状态
if [ -z "${java_status}" ];
then echo "java not ok"
java_kill_ngx
else
echo "Java is ok"
#判断nginx状态
if [ -z "${nginx_status}" ];
then echo "nginx not ok"
else
echo "nginx is ok"
/opt/ng/sbin/nginx
fi
fi
else
mysql_kill_ngx
#break
fi
sleep 30
done