MySQL shell(ETPASS): M/S status, Sendmail

本文介绍了一种用于监控MySQL连接数并发送邮件通知的Shell脚本方案。当连接数超过预设阈值时,该脚本将通过邮件形式通知管理员,并记录详细信息到日志文件中。
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
#!/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

$ more slave_mysql_monitor.sh
#!/bin/sh
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
2. T he sendmail function
--------------------
$ more /root/xxd/monitor/include/functions
#!/bin/sh
function send_mail() {
send=$1
case "$send" in
"m_mysql")
/usr/sbin/sendmail -t
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值