您好,欢迎访问一九零五行业门户网

自己编写Nagios 监控MySQL插件

主从监控插件编写:#!/bin/bash mysql=/usr/local/mysql/bin/mysql -uslave -pslave12345 -h127.0.0.1 -pvar_log=/tmp/check_m
主从监控插件编写:
#!/bin/bash
mysql=/usr/local/mysql/bin/mysql -uslave -pslave12345 -h127.0.0.1 -p
var_log=/tmp/check_mysql_status.log.$2 && >$var_log
function usage(){
    cat `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';
eof
exit 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
    usage
fi
if [ $1 == -s ];then
    #echo 检查从模式
    check_mysql_slave_status $2
fi
code=0
check_mysql_status $2
. $var_log
per_second_question=`echo $uptime $queries | awk '{printf (%4.3f) ,$2/$1}end{if($2/$1tmp_code=$?
if [ -z $uptime ];then
    echo $mysql $@ 连接失败,请检查服务 && exit 2
fi
if [ $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_question
else    ##输出不同的状态信息
    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_question
fi
exit $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 -h127.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_log
function usage(){
        cat `basename $0` need at least value: `basename $0` -[s|m] port
first arg  -s : monitor slave mode
            -m : moitor master mode
second 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';
eof
exit 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
        usage
fi
code=0
check_mysql_status $2
check_mysql_con_status $2
. $var_log
residue_conn=$max_connections
check_mysql_con_results $2
per_second_question=`echo $uptime $queries | awk '{printf (%4.3f) ,$2/$1}end{if($2/$1tmp_code=$?
#
if [ -z $uptime ];then
        echo $mysql $@ connect failed,please check! && exit 2
fi
#
 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
nagios 的详细介绍:请点这里
nagios 的下载地址:请点这里
相关阅读:
网络监控器nagios全攻略
nagios搭建与配置详解
nginx环境下构建nagios监控平台
在rhel5.3上配置基本的nagios系统(使用nagios-3.1.2)
centos 5.5+nginx+nagios监控端和被控端安装配置指南
ubuntu 13.10 server 安装 nagios core 网络监控运用
其它类似信息

推荐信息