本文通过Zabbix Graphs实时查看的SQL语句操作情况和mysql发送接收的字节数,Zabbix还可以监控mysql slow queries,mysql version,uptime,alive等。


在之前的博文里面写过如何通过Zabbix监控mysql主从同步是否OK,mysql从库是否有延时(Seconds_Behind_Master)主库,当mysql主从有异常时通过Email或者SMS通知DBA和系统人员。除此之外,Zabbix还可以监控mysql slow queries,mysql version,uptime,alive等。下面通过Zabbix Graphs实时查看的SQL语句操作情况和mysql发送接收的字节数。

1.Zabbix官方提供的监控mysql的模板Template App MySQL,可以看到相关的Items和key。

wKioL1MphNaTYVdQAAS5uuZeSQg754.jpg

2.把该模板Template App MySQL Link到相关的主机上面,发现Item的Status是不可用的,因为key的值是通过Mysql用户查看"show global status"信息或者用mysqladmin命令查看status或extended-status的信息而取的值。

mysql> show global status; mysql> show status;

3.结合官方提供的key编写Shell脚本,从数据库中取出Items的key的值。

[root@monitor scripts]# cat checkmysqlperformance.sh 
#!/bin/sh
#Create by Lance 2014.10.31
MYSQL_SOCK="/var/lib/mysql/mysql.sock"
HOME=/usr/local/zabbix/mysql
ARGS=1 
if [ $# -ne "$ARGS" ];then     
    echo "Please input one arguement:" 
fi
case $1 in
  Ping)
        result=`mysqladmin ping | grep -c alive `
        echo $result
  ;;
  Version)
        result=`mysql -V | cut -d" " -f6 |awk -F "," '{print "V" $1}'`
        echo $result
  ;;
  Uptime)
        result=`mysqladmin -S $MYSQL_SOCK status | cut -d" " -f2 `
        echo $result
  ;;
  Com_update)
        result=`mysqladmin -S $MYSQL_SOCK extended-status | grep -w  Com_update | cut -d "|" -f3`
        echo $result
  ;;
  Com_begin)
        result=`mysqladmin -S $MYSQL_SOCK extended-status | grep -w Com_begin | cut -d "|" -f3`
        echo $result
  ;;
  Bytes_received)
        result=`mysqladmin -S $MYSQL_SOCK extended-status | grep -w Bytes_received | cut -d "|" -f3`
        echo $result
  ;;
  Bytes_sent)
        result=`mysqladmin -S $MYSQL_SOCK extended-status | grep -w Bytes_sent | cut -d "|" -f3`
        echo $result
  ;;
  Com_commit)
        result=`mysqladmin -S $MYSQL_SOCK extended-status | grep -w Com_commit | cut -d "|" -f3`
        echo $result
  ;;
  Com_delete)
        result=`mysqladmin -S $MYSQL_SOCK extended-status | grep -w Com_delete | cut -d "|" -f3`
        echo $result
  ;;
  Com_insert)
        result=`mysqladmin -S $MYSQL_SOCK extended-status | grep -w Com_insert | cut -d "|" -f3`
        echo $result
  ;;
  Questions)
        result=`mysqladmin -S $MYSQL_SOCK extended-status | grep -w Questions | cut -d "|" -f3`
        echo $result
  ;;
  Com_rollback)
        result=`mysqladmin -S $MYSQL_SOCK extended-status | grep -w Com_rollback | cut -d "|" -f3`
        echo $result
  ;;
  Com_select)
        result=`mysqladmin -S $MYSQL_SOCK extended-status | grep -w Com_select | cut -d "|" -f3`
        result=`mysqladmin -S $MYSQL_SOCK extended-status | grep -w Com_select | cut -d "|" -f3`
        echo $result
  ;;
  Slow_queries)
        result=`mysqladmin -S $MYSQL_SOCK extended-status | grep -w Slow_queries | cut -d "|" -f3`
        echo $result
  ;;
esac

4.在Zabbix_agentd.conf里面添加UserParameter,格式如下,对于Zabbix来说,脚本其实就是一个插件。

UserParameter=mysql.version,mysql -V
UserParameter=mysql.ping,mysqladmin -uroot -p123456 -S /data/mysql/3306/mysql.sock ping | grep -c alive
UserParameter=mysql.status[*],/etc/zabbix/scripts/checkmysqlperformance.sh $1 $2

5.重启agentd服务器,然后在zabbix server用zabbix_get就可以取到key的值。

wKioL1MqRpGxzzs-AAG-6BleztI159.jpg

6.在zabbix前端可以实时查看SQL语句每秒钟的操作次数。

wKioL1MphhPR5bFZAAUaEiVnqWk146.jpg

7.在zabbix前端可以实时查看mysql发送接收的字节数。其中bytes received表示从所有客户端接收到的字节数,bytes sent表示发送给所有客户端的字节数。

wKiom1Mphnug3b9nAAPDKjy9BLM501.jpg

总结

把该脚本放到要监控的服务器上面(Modify mysql user and password),修改UserParameter的参数并重启agentd,Link官方提供的Template App MySQL模板即可。

我这里是测试环境用root账号,线上服务器安全期间可以给mysql用户授权readonly权限。

根据实际的需求,除了监控上述监控项之外,还可以监控mysql processlist,Innodb等。


原文:http://os.51cto.com/art/201404/435139.htm