zabbix监控mysql数据库性能

在之前的博文里面写过如何通过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。

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 sfzhang 2014.02.20MYSQL_SOCK=”/data/mysql/3306/mysql.sock”MYSQL_PWD=`cat /data/mysql/3306/.mysqlpassword`ARGS=1if [ $# -ne “$ARGS” ];thenecho “Please input one arguement:”ficase $1 inUptime)result=`mysqladmin -uroot -p${MYSQL_PWD} -S $MYSQL_SOCK status|cut -f2 -d”:”|cut -f1 -d”T”`echo $result;;Com_update)result=`mysqladmin -uroot -p${MYSQL_PWD} -S $MYSQL_SOCK extended-status |grep -w “Com_update”|cut -d”|” -f3`echo $result;;Slow_queries)result=`mysqladmin -uroot -p${MYSQL_PWD} -S $MYSQL_SOCK status |cut -f5 -d”:”|cut -f1 -d”O”`echo $result;;Com_select)result=`mysqladmin -uroot -p${MYSQL_PWD} -S $MYSQL_SOCK extended-status |grep -w “Com_select”|cut -d”|” -f3`echo $result;;Com_rollback)result=`mysqladmin -uroot -p${MYSQL_PWD} -S $MYSQL_SOCK extended-status |grep -w “Com_rollback”|cut -d”|” -f3`echo $result;;Questions)result=`mysqladmin -uroot -p${MYSQL_PWD} -S $MYSQL_SOCK status|cut -f4 -d”:”|cut -f1 -d”S”`echo $result;;Com_insert)result=`mysqladmin -uroot -p${MYSQL_PWD} -S $MYSQL_SOCK extended-status |grep -w “Com_insert”|cut -d”|” -f3`echo $result;;Com_delete)result=`mysqladmin -uroot -p${MYSQL_PWD} -S $MYSQL_SOCK extended-status |grep -w “Com_delete”|cut -d”|” -f3`echo $result;;Com_commit)result=`mysqladmin -uroot -p${MYSQL_PWD} -S $MYSQL_SOCK extended-status |grep -w “Com_commit”|cut -d”|” -f3`echo $result;;Bytes_sent)result=`mysqladmin -uroot -p${MYSQL_PWD} -S $MYSQL_SOCK extended-status |grep -w “Bytes_sent” |cut -d”|” -f3`echo $result;;Bytes_received)result=`mysqladmin -uroot -p${MYSQL_PWD} -S $MYSQL_SOCK extended-status |grep -w “Bytes_received” |cut -d”|” -f3`echo $result;;Com_begin)result=`mysqladmin -uroot -p${MYSQL_PWD} -S $MYSQL_SOCK extended-status |grep -w “Com_begin”|cut -d”|” -f3`echo $result;;*)echo “Usage:$0(Uptime|Com_update|Slow_queries|Com_select|Com_rollback|Questions)”;;esac

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

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

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

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

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

总结: 1)把该脚本放到要监控的服务器上面(Modify mysql user and password),修改UserParameter的参数并重启agentd,Link官方提供的Template App MySQL模板即可。 2)我这里是测试环境用root账号,线上服务器安全期间可以给mysql用户授权readonly权限。

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

天才是百分之一的灵感加上百分之久十久的努力

zabbix监控mysql数据库性能

相关文章:

你感兴趣的文章:

标签云: