shell 监控mysql 锁表并报警

1、monitor_dbLocked

#!/bin/sh
mark=`date +%Y%m%d%H%M`
time_out=20
status=”ok”
check_status=”Locked”
send_mail=”off”
mysql_default_password=”xxxxxxxx”
host_name=`hostname`

declare -a mysqllist
declare -a mysql_exec_path
declare -a mysql_password
declare -a mysql_locked_port
declare -a mysql_show_port

function usage(){
cat << HELP

$0 — USAGE: sh $0 mysql_exec_path mysql_password mysql_socket

Example: sh $0 /opt/xxxx/mysql/bin/mysql xxxxxxx /tmp/socket
HELP
}
function script_arg_init(){
if [ “x$1” != “x0″ ] ; then
mysql_exec_path=$1
else
mysql_exec_path=”/opt/xxxx/mysql/bin/mysql”
fi

if [ “x$2” != “x0″ ];then
mysql_password=$2
else
mysql_password=”xxxxxx”
fi

if [ “x$3” != “x0″ ];then
mysql_socket=$3
else
mysql_socket=”/tmy/mysql.sock”
fi

if [ “x$4” != “x” ];then
mysql_locked_port=$4
else
mysql_locked_port=3306
fi
}

for i in $mysqllist;do
for j in `echo $i|awk -F ‘:’ ‘{ k=1;while(k<=NF){print $k;k++}}’`;do

if [ -n “${j}” ] && ([ `echo $j|grep ‘datadir=’` ] || [ `echo $j|grep ‘socket=’` ] || [ `echo $j|grep ‘port=’` ]);then
#mysql_datastr=(`echo $j|awk -F “=” ‘{print $2}’`)
if [ `echo $j|grep ‘datadir=’` ];then
mysql_datadir=(`echo $j|awk -F “=” ‘{print $2}’`)
elif [ `echo $j|grep ‘socket=’` ];then
mysql_socket=(`echo $j|awk -F “=” ‘{print $2}’`)
elif [ `echo $j|grep ‘port=’` ];then
mysql_port=(`echo $j|awk -F “=” ‘{print $2}’`)
fi
fi
done
if [ “$mysql_port” == “3312” ];then
main 0 “xxxxxxxxx” $mysql_socket $mysql_port
else
main 0 $mysql_default_password $mysql_socket $mysql_port
fi
done

if [ “$send_mail” == “on” ] ;then
ip=`/bin/sh get_ip.sh|/usr/bin/head -n 1`
email_content=`cat log_mail/log_mail_$mark.txt`
sendEmail -s smtp.163.com -f [email protected] -t [email protected] -a log_mail/log_mail_$mark.txt -u “ALERT:$host_name[$ip] Locked Table $mysql_show_port ” -m “$email_content” -xu xxxx -xp xxxxxxx
echo “$mark send email”
fi

2、get_ip

#!/bin/sh
OS=`uname`
IO=””
case $OS in
Linux) IP=`/sbin/ifconfig  | grep ‘inet addr:’| grep -v -E ‘127.0.0.1|192.168.’ | cut -d: -f2 | awk ‘{ print $1}’`;;
FreeBSD|OpenBSD) IP=`ifconfig  | grep -E ‘inet.[0-9]’ | grep -v -E ‘127.0.0.1|192.168.’ | awk ‘{ print $2}’` ;;
SunOS) IP=`ifconfig -a | grep inet | grep -v -E ‘127.0.0.1|192.168.’ | awk ‘{ print $2} ‘` ;;
*) IP=”Unknown”;;
esac
echo “$IP”

3、sendEmail

sendEmail -f ADDRESS [options]

Required:
-f ADDRESS                from (sender) email address
* At least one recipient required via -t, -cc, or -bcc
* Message body required via -m, STDIN, or -o message-file=FILE

Common:
-t ADDRESS [ADDR …]     to email address(es)
-u SUBJECT                message subject
-m MESSAGE                message body
-s SERVER[:PORT]          smtp mail relay, default is localhost:25

Optional:
-a   FILE [FILE …]      file attachment(s)
-cc  ADDRESS [ADDR …]   cc  email address(es)
-bcc ADDRESS [ADDR …]   bcc email address(es)
-xu  USERNAME             username for SMTP authentication
-xp  PASSWORD             password for SMTP authentication
Paranormal:
-b BINDADDR[:PORT]        local host bind address
-l LOGFILE                log to the specified file
-v                        verbosity, use multiple times for greater effect
-q                        be quiet (i.e. no STDOUT output)
-o NAME=VALUE             advanced options, for details try: –help misc
-o message-content-type=<auto|text|html>
-o message-file=FILE         -o message-format=raw
-o message-header=HEADER     -o message-charset=CHARSET
-o reply-to=ADDRESS          -o timeout=SECONDS
-o username=USERNAME         -o password=PASSWORD
-o tls=<auto|yes|no>         -o fqdn=FQDN

Help:
–help                    the helpful overview you’re reading now
–help addressing         explain addressing and related options
–help message            explain message body input and related options
–help networking         explain -s, -b, etc
–help output             explain logging and other output options
–help misc               explain -o options, TLS, SMTP auth, and more

4、脚本下载

monitor_dblocked_v1.tar

此条目发表在技术生涯分类目录,贴了, 标签。将固定链接加入收藏夹。

发表评论

邮箱地址不会被公开。 必填项已用*标注