之前的博文写过如何通过Zabbix监控mysql主从同步是否OK,mysql从库是否有延迟(Seconds_Behind_Master)主库,mysql主从什么时候有异常,通过Email或SMS通知DBA和系统人员。此外,Zabbix还可以监控mysql慢查询、mysql版本、uptime、alive等,下面是通过ZabbixGraphs实时查看sql语句运行情况和mysql发送接收的字节数。1、Zabbix官方提供的监控mysql的TemplateAppMySQL,可以看到相关的Items和key。2.将模板TemplateAppMySQL链接到相关主机,发现Item的Status不可用,因为key的值是Mysql用户查看“showglobalstatus”信息或者使用mysqladmin命令查看的查看status或extended-status信息所取的值。mysql>showglobalstatus;mysql>showstatus;3.结合官方提供的key写一个Shell脚本从数据库中取出Items的key的值。[root@monitorscripts]#catcheckmysqlperformance.sh#!/bin/sh#Createbysfzhang2014.02.20MYSQL_SOCK="/data/mysql/3306/mysql.sock"MYSQL_PWD=`cat/data/mysql/3306/.mysqlpassword`ARGS=1if[$#-ne"$ARGS"];thenecho"Pleaseinputonearguement:"ficase$1inUptime)result=`mysqladmin-uroot-p${MYSQL_PWD}-S$MYSQL_SOCKstatus|cut-f2-d":"|cut-f1-d"T"`echo$result;;Com_update)result=`mysqladmin-uroot-p${MYSQL_PWD}-S$MYSQL_SOCKextended-status|grep-w"Com_update"|cut-d"|"-f3`echo$result;;Slow_queries)result=`mysqladmin-uroot-p${MYSQL_PWD}-S$MYSQL_SOCKstatus|cut-f5-d":"|cut-f1-d"O"`echo$result;;Com_select)result=`mysqladmin-uroot-p${MYSQL_PWD}-S$MYSQL_SOCKextended-status|grep-w"Com_select"|cut-d"|"-f3`echo$result;;Com_rollback)result=`mysqladmin-uroot-p${MYSQL_PWD}-S$MYSQL_SOCKextended-status|grep-w"Com_rollback"|cut-d"|"-f3`echo$result;;Questions)result=`mysqladmin-uroot-p${MYSQL_PWD}-S$MYSQL_SOCKstatus|cut-f4-d":"|cut-f1-d"S"`echo$result;;Com_insert)result=`mysqladmin-uroot-p${MYSQL_PWD}-S$MYSQL_SOCKextended-status|grep-w"Com_insert"|cut-d"|"-f3`echo$result;;Com_delete)result=`mysqladmin-uroot-p${MYSQL_PWD}-S$MYSQL_SOCKextended-status|grep-w"Com_delete"|cut-d"|"-f3`echo$result;;Com_commit)result=`mysqladmin-uroot-p${MYSQL_PWD}-S$MYSQL_SOCKextended-status|grep-w"Com_commit"|cut-d"|"-f3`echo$result;;Bytes_sent)result=`mysqladmin-uroot-p${MYSQL_PWD}-S$MYSQL_SOCKextended-status|grep-w"Bytes_sent"|cut-d"|"-f3`echo$result;;Bytes_received)result=`mysqladmin-uroot-p${MYSQL_PWD}-S$MYSQL_SOCKextended-status|grep-w"Bytes_received"|cut-d"|"-f3`echo$result;;com_begin)result=`mysqladmin-uroot-p${MYSQL_PWD}-S$MYSQL_SOCKextended-status|grep-w"Com_begin"|cut-d"|"-f3`echo$result;;*)echo"Usage:$0(Uptime|Com_update|Slow_queries|Com_select|Com_rollback|Questions)";;esac4。在Zabbix_agentd.conf中添加UserParameter,格式如下,对于Zabbix,脚本其实就是一个PluginUserParameter=mysql.version,mysql-VUserParameter=mysql.ping,mysqladmin-uroot-p123456-S/data/mysql/3306/mysql.sockping|grep-caliveUserParameter=mysql.status[*],/etc/zabbix/scripts/checkmysqlperformance.sh$1$25。重启agentdserver,然后在zabbixserver上使用zabbix_get获取key的值。6、在zabbix前端可以实时查看SQL语句每秒的操作数。7、在zabbix前端可以实时查看mysql发送和接收的字节数。其中bytesreceived表示从所有客户端接收到的字节数,bytessent表示发送给所有客户端的字节数。综上所述,把脚本放到要监控的服务器上(修改mysql用户和密码),修改UserParameter的参数,重启agentd。Link提供的TemplateAppMySQL模板就足够了。这里我测试环境使用root账号。在线服务器安全期间,mysql用户可以被授予readonly权限。根据实际需要,除了监控以上监控项外,还可以监控mysqlprocesslist、Innodb等。
