下载安卓APP箭头
箭头给我发消息

客服QQ:3315713922

Zabbix监控Mysql数据库性能

作者:课课家教育     来源: http://www.kokojia.com点击数:992发布时间: 2019-04-15 14:37:44

标签: 通过Zabbix监控mysql主从同步是否OKmysql从库是否有延时主库有异常时是否会通知系统人员

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

  wKioL1MphNaTYVdQAAS5uuZeSQg754.jpg

  2.把该模板TemplateAppMySQLLink到相关的主机上面,发现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.20

  MYSQL_SOCK="/data/mysql/3306/mysql.sock"

  MYSQL_PWD=`cat/data/mysql/3306/.mysqlpassword`

  ARGS=1

  if[$#-ne"$ARGS"];then

  echo"Pleaseinputonearguement:"

  fi

  case$1in

  Uptime)

  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)"

  ;;

  esac

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

  UserParameter=mysql.version,mysql-V

  UserParameter=mysql.ping,mysqladmin-uroot-p123456-S/data/mysql/3306/mysql.sockping|grep-calive

  UserParameter=mysql.status[*],/etc/zabbix/scripts/checkmysqlperformance.sh$1$2

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

  wKioL1MqRpGxzzs-AAG-6BleztI159.jpg

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

  wKioL1MphhPR5bFZAAUaEiVnqWk146.jpg

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

  wKiom1Mphnug3b9nAAPDKjy9BLM501.jpg

  总结

  把该脚本放到要监控的服务器上面(Modifymysqluserandpassword),修改UserParameter的参数并重启agentd,Link官方提供的TemplateAppMySQL模板即可。

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

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

找oracle视频教程学oracle,上课课家教育IT培训专家。

  

赞(0)
踩(0)
分享到:
华为认证网络工程师 HCIE直播课视频教程