This script is actually another slow log detector, but it will also send mail instead of writing logs only.
1. monitor MySQL, if the connection >= 30 then call the sendmail function
---------------------------------------------------------
$ more master_mysql_monitor.sh
$ more slave_mysql_monitor.sh---------------------------------------------------------
$ more master_mysql_monitor.sh
#!/bin/sh
flag=30 //more than 30 connections
path=/root/xxd/shell/mysql/monitor
user=username
pass=password
host=IP
logd=`date +%Y%m%d`
logf=$path/logs/mm_run_$logd.log
errf=$path/logs/mm_err_$logd.log
comm="set names utf8;show full processlist;"
date=`date`
shel=`echo $comm | mysql -u$user -p$pass -h$host | awk '{print $0"$"}'`
line=`echo $shel | awk '{zcou=split($0,mcom,"$");for (leij=0;leij
;}' | wc -l`
line=$[line-3]
. $path/include/functions //the sendmail function see 2
if [ $line -gt $flag ];
then
echo "--------------------------------" >> $errf
echo "$date ---- not NULL:$line" >> $errf
echo $shel | awk '{zcou=split($0,mcom,"$");for (leij=0;leij> $errf
echo "--------------------------------" >> $errf
send_mail "m_mysql"
fi
echo "----------------------------------------" >> $logf
echo "$date ---- not NULL:$line" >> $logf
echo $shel | awk '{zcou=split($0,mcom,"$");for (leij=0;leij> $logf
echo "----------------------------------------" >> $logf
#!/bin/sh2. T he sendmail function
flag="NULL"
path=/root/xxd/shell/mysql/monitor
user=username
pass=password
host=IP
logd=`date +%Y%m%d`
logf=$path/logs/sm_log_$logd.log
errf=$path/logs/sm_err_$logd.log
comm="set names utf8;show slave status\G;"
date=`date`
line=`echo $comm | mysql -u$user -p$pass -h$host | awk '{if ($1=="Seconds_Behind
_Master:") print $2;}'`
. $path/include/functions
if [[ $line == $flag ]];
then
echo "-----------------------------" >> $errf
echo "$date" >> $errf
echo $comm | mysql -u$user -p$pass -h$host >> $errf
echo "-----------------------------" >> $errf
send_mail "s_mysql"
fi
echo $date >> $logf
--------------------
$ more /root/xxd/monitor/include/functions
$ more /root/xxd/monitor/include/functions
#!/bin/sh
function send_mail() {
send=$1
case "$send" in
"m_mysql")
/usr/sbin/sendmail -t