本文本章转载地址:https://www.cnblogs.com/magicaltravel/p/6182384.html
#!/bin/bash
#定义变量v_username='mysql'
v_datetime=`date +"%Y-%m-%d %H:%M:%S"`
v_hostname=`hostname`
v_ip=`/sbin/ifconfig bond2|grep 'inet addr'|awk '{print $2}'|cut -d: -f2`
MY_user="root"
MY_pwd=***
MY_base=`ps -ef|grep mysqld|grep 3306|awk -F '--' '{print $2}'|awk -F '=' '{print $2}'|sed 's/[ \t]*$//g'`
MY_cm="$MY_base/bin/mysql"
MY_admin="$MY_base/bin/mysqladmin"
MY_errlog=`ps -ef|grep mysqld|grep 3306|awk -F '--' '{print $5}'|awk -F '=' '{print $2}'|sed 's/[ \t]*$//g'`
#发送人
v_sendmail=`hostname`@139.com
#接收人
v_receivemail='zhaoying@richinfo.cn zhouying@richinfo.cn'
#邮件发送程序
MAIL_BIN="/home/crond/bsmtp"
#设置LC_ALL
export LC_ALL=en_US.UTF-8
#监控目录
v_path="/home/mysql/scripts/monitorlog"
if [ ! -d "$v_path" ];then
mkdir -p "$v_path"
fi
#数据库存活监控
rm -f /home/mysql/scripts/monitorlog/mysql_process.log
ps -elf|grep mysqld|grep -v grep > /home/mysql/scripts/monitorlog/mysql_process.log
if [ -s /home/mysql/scripts/monitorlog/mysql_process.log ];then
echo "mysql process is ok" >> $v_path/monitor_mysql.log
else
echo "############################" > $v_path/monitor_mysql.log
echo "USER: ${v_username}" >> $v_path/monitor_mysql.log
echo "HOSTNAME: ${v_hostname}" >> $v_path/monitor_mysql.log
echo "IP: ${v_ip}" >> $v_path/monitor_mysql.log
echo "############################" >> $v_path/monitor_mysql.log
echo "MYSQL IS DOWN" >> $v_path/monitor_mysql.log
$MAIL_BIN -f ${v_sendmail} -h smtp.api.localdomain -s "${v_hostname} MYSQL PROCESS WARNING!!! - ${v_datetime}" ${v_receivemail} < $v_path/monitor_mysql.log
fi
#活跃会话告警
active_connect=70
sum_active_connect=`$MY_admin -u$MY_user -p$MY_pwd pr|grep -iv sleep|wc -l`
monitor_active_connec=`expr $active_connect / 2 + $active_connect`
if [ "$sum_active_connect" -ge "$monitor_active_connec" ];then
echo "############################" > $v_path/my_active_connect.log
echo "USER: ${v_username}" >> $v_path/my_active_connect.log
echo "HOSTNAME: ${v_hostname}" >> $v_path/my_active_connect.log
echo "IP: ${v_ip}" >> $v_path/my_active_connect.log
echo "############################" >> $v_path/my_active_connect.log
echo "now active connect warning,Usual active connec is $active_connect, now active connect is $sum_active_connect" >> $v_path/my_active_connect.log
$MAIL_BIN -f ${v_sendmail} -h smtp.api.localdomain -s "${v_hostname} ACTIVE CONNECT WARNING!!! - ${v_datetime}" ${v_receivemail} < $v_path/my_active_connect.log
fi
#慢日志告警
if [ -s "$v_path/my_slow_dir.txt" ];then
echo "slow_dir_ok"
else
$MY_cm -u$MY_user -p$MY_pwd -e "show global variables like 'slow_query_log_file'" > $v_path/my_slow_dir.txt
fi
MY_slow=`grep slow $v_path/my_slow_dir.txt|awk '{print $2}'`
count_slow=`wc -l $MY_slow|awk '{print $1}'`
#echo "$count_slow"
sleep 5
count_slow_now=`wc -l $MY_slow|awk '{print $1}'`
if [ "$count_slow" -ne "$count_slow_now" ];then
echo "############################" > $v_path/my_slow.log
echo "USER: ${v_username}" >> $v_path/my_slow.log
echo "HOSTNAME: ${v_hostname}" >> $v_path/my_slow.log
echo "IP: ${v_ip}" >> $v_path/my_slow.log
echo "############################" >> $v_path/my_slow.log
/home/mysql/scripts/pt-query-digest --since=2h $MY_slow >> $v_path/my_slow.log
$MAIL_BIN -f ${v_sendmail} -h smtp.api.localdomain -s "${v_hostname} SLOW LOG WARNING!!! - ${v_datetime}" ${v_receivemail} < $v_path/my_slow.log
fi
#锁等待告警
monitor_lock=3
$MY_cm -u$MY_user -p$MY_pwd -e "show global status like 'Innodb_row_lock_current_waits'" > $v_path/lock.log
count_lock=`grep "[0-9]" $v_path/lock.log|awk '{print $2}'`
if [ "$count_lock" -ge "$monitor_lock" ];then
echo "############################" >> $v_path/lock.log
echo "USER: ${v_username}" >> $v_path/lock.log
echo "HOSTNAME: ${v_hostname}" >> $v_path/lock.log
echo "IP: ${v_ip}" >> $v_path/lock.log
echo "############################" >> $v_path/lock.log
$MY_cm -u$MY_user -p$MY_pwd -e "show engine innodb status\G" >> $v_path/lock.log
$MAIL_BIN -f ${v_sendmail} -h smtp.api.localdomain -s "${v_hostname} SLOW LOG WARNING!!! - ${v_datetime}" ${v_receivemail} < $v_path/lock.log
fi
#mysql错误日志告警
if [ -s "$v_path/mysql_error.log" ];then
echo "error_ok"
else
grep -i error $MY_errlog > $v_path/mysql_error.log
fi
count_error=`grep -i error $v_path/mysql_error.log|wc -l|awk '{print $1}'`
monitor_error=`grep -i error $MY_errlog |wc -l`
#echo "$monitor_error monitor_error"
if [ "$monitor_error" -ne "$count_error" ];then
echo "############################" > $v_path/my_error.log
echo "USER: ${v_username}" >> $v_path/my_error.log
echo "HOSTNAME: ${v_hostname}" >> $v_path/my_error.log
echo "IP: ${v_ip}" >> $v_path/my_error.log
echo "############################" >> $v_path/my_error.log
grep -i error $MY_errlog|tail -n 20 >> $v_path/my_error.log
$MAIL_BIN -f ${v_sendmail} -h smtp.api.localdomain -s "${v_hostname} ERROR LOG WARNING!!! - ${v_datetime}" ${v_receivemail} < $v_path/my_error.log
if [ $? == 0 ];then
rm -f $v_path/mysql_error.log
fi
fi
#连接总数告警
connect=30
sum_connect=`$MY_admin -u$MY_user -p$MY_pwd pr|wc -l`
monitor_connect=`expr $connect / 10`
if [ "$sum_connect" -le "$monitor_connect" ];then
echo "############################" > $v_path/my_connect.log
echo "USER: ${v_username}" >> $v_path/my_connect.log
echo "HOSTNAME: ${v_hostname}" >> $v_path/my_connect.log
echo "IP: ${v_ip}" >> $v_path/my_connect.log
echo "############################" >> $v_path/my_connect.log
echo "now connect warning,Usual connect is $connect, now connect is $sum_connect" >>$v_path/my_connect.log
$MAIL_BIN -f ${v_sendmail} -h smtp.api.localdomain -s "${v_hostname} SUM CONNECT WARNING!!! - ${v_datetime}" ${v_receivemail} < $v_path/my_connect.log
fi