主从监控插件编写:

#!/bin/bashMYSQL="/usr/local/mysql/bin/mysql -uslave -pslave12345 -h227.0.0.1 -P"VAR_LOG=/tmp/check_mysql_status.log.$2 && >$VAR_LOGfunction usage(){ cat <<EOF`basename $0` need at least value: `basename $0` -[s|m] port第一个参数是 -s 那么以是检查从模式, -m就是检查主模式 不能省略第二个参数是数据库端口,如3308 3309,不能省略mysql权限,需执行如下SQL:grant replication client,super on *.* to 'slave'@'127.0.0.1' identified by 'slave12345';EOFexit 1}function check_mysql_status(){ #echo $MYSQL $@ echo "show status;" | $MYSQL$@ | perl -p -e 's/\s+(\w+)/="$1"/g' >> $VAR_LOG #echo "$mysql_status"}function check_mysql_slave_status(){ #echo $MYSQL $@ echo "show slave status\G" | $MYSQL$@ | perl -p -e 's/:\s(.*)$/="$1"/g; s/^\s+//g; s/^\*.*\*//;' >> $VAR_LOG}function check_slave_results(){ if [ "$Slave_IO_Running" == "Yes" ] && [ "$Slave_SQL_Running" == "Yes" ];then if [ $tmp_code -ne 0 ] && [ $Seconds_Behind_Master -gt 100 ] ;then code=2 elif [ $Seconds_Behind_Master -lt 50 ] ;then code=0 else code=1 fi else code=2 fi }if [ $# -le 0 ] || [ "$1" == "-h" ];then usagefiif [ "$1" == "-s" ];then #echo "检查从模式" check_mysql_slave_status $2ficode=0check_mysql_status $2. $VAR_LOGPER_SECOND_QUESTION=`echo "$Uptime $Queries" | awk '{printf ("%4.3f") ,$2/$1}END{if($2/$1<0.05) exit 0 ;else exit 1;}'`tmp_code=$?if [ -z $Uptime ];then echo "$MYSQL $@ 连接失败,请检查服务" && exit 2fiif [ "$1" == "-s" ];then ##输出不同的信息check_slave_results printf "主库:$Master_Host:$Master_Port Uptime:$Uptime Threads:$Threads_running Questions:$Questions Slow_queries:$Slow_queries Open_tables:$Open_tables Queries per second avg:%s Slave_IO_Running:$Slave_IO_Running Slave_SQL_Running:$Slave_SQL_Running Seconds_Behind_Master:$Seconds_Behind_Master|Threads=$Threads_running Questions=$Questions Slowqueries=$Slow_queries Opens=$Opened_tables Opentables=$Open_tables Queriespersecondavg=%s BehindMaster=$Seconds_Behind_Master" $PER_SECOND_QUESTION $PER_SECOND_QUESTIONelse ##输出不同的状态信息 printf "Uptime:$Uptime Threads:$Threads_running Questions:$Questions Slow_queries:$Slow_queries Open_tables:$Open_tables Queries per second avg:%s|Threads=$Threads_running Questions=$Questions Slowqueries=$Slow_queries Opens=$Opened_tables Opentables=$Open_tables Queriespersecondavg=%s" $PER_SECOND_QUESTION $PER_SECOND_QUESTIONfiexit $code


mysql连接数监控:(监控mysql的最大连接数(最大连接数小于1000报警),和当前连接数 ,可用连接数少于50报警)

#!/bin/bash#MYSQL="/usr/local/mysql/bin/mysql -unagios -pmonitor_slave#status -h$3 -P"MYSQL="/usr/local/mysql/bin/mysql -unagios -pmonitor_slave#status -h227.0.0.1 -P"VAR_LOG=/tmp/check_mysql_status_con_$2.log && >$VAR_LOG#Threads_con_log=/tmp/check_mysql_tread_conn.log.$2 && >$Threads_con_log#Max_conn_log=/tmp/check_mysql_max_con.log.$2 && >$Max_conn_logfunction usage(){ cat <<EOF`basename $0` need at least value: `basename $0` -[s|m] portfirst arg -s : monitor slave mode -m : moitor master modesecond arg port: mysqld prot 3306 or 3307 or 3308 ...mysql privilege :you neeed exec :grant replication client,super on *.* to 'slave'@'127.0.0.1' identified by 'slave12345';EOFexit 1}function check_mysql_status(){ #echo $MYSQL $@ echo "show status;" | $MYSQL$@ | perl -p -e 's/\s+(\w+)/="$1"/g' >> $VAR_LOG #echo "$mysql_status"}function check_mysql_con_status(){ #echo $MYSQL $@ echo "show status like 'Threads_connected';" | $MYSQL$@ | perl -p -e 's/\s+(\w+)/="$1"/g' >> $VAR_LOG echo "show variables like 'max_connections';" |$MYSQL$@ | perl -p -e 's/\s+(\w+)/="$1"/g' >> $VAR_LOG #echo "$mysql_status"}function check_mysql_con_results(){ #echo "$max_connections,$Threads_connected" Residue_conn=$(($max_connections-$Threads_connected)) #echo $Residue_conn if [ $max_connections -lt 1000 ];then if [ $Residue_conn -lt 50 ] ;then code=2 else code=1 fi else code=0 fi }if [ $# -le 0 ] || [ "$1" == "-h" ];then usageficode=0check_mysql_status $2check_mysql_con_status $2. $VAR_LOGResidue_conn=$max_connectionscheck_mysql_con_results $2PER_SECOND_QUESTION=`echo "$Uptime $Queries" | awk '{printf ("%4.3f") ,$2/$1}END{if($2/$1<0.05) exit 0 ;else exit 1;}'`tmp_code=$?#if [ -z $Uptime ];then echo "$MYSQL $@ Connect Failed,Please check!" && exit 2fi# printf "Uptime:$Uptime Threads_connected:$Threads_connected Max_connections:$max_connections Residue_connectios:$Residue_conn Open_tables:$Open_tables Queries per second avg:%s|Threads=$Threads_running Questions=$Questions Slowqueries=$Slow_queries Opens=$Opened_tables Opentables=$Open_tables Queriespersecondavg=%s" $PER_SECOND_QUESTION $PER_SECOND_QUESTION#exit $code