Zabbix 监控Mysql
Posted 项思凯
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Zabbix 监控Mysql相关的知识,希望对你有一定的参考价值。
简介
通过 zabbix 默认的mysql监控模板,来监控Mysql数据库,但是直接使用是无法引用模板监控的,需要进行如下配置方可使用。
环境
- 服务:Zabbix 3.0.28、zabbix-agent 2.4.7
- 系统:Linux Centos7/Redhat7
部署
操作zabbix-agent客户端
1、在被监控数据库内创建监控账号
mysql -uroot -p
GRANT PROCESS,SUPER,REPLICATION CLIENT ON *.* TO mysqlcheck@\'192.168.0.8\' IDENTIFIED BY \'mysqlcheck\';
2、创建mysql监控脚本
vim /etc/zabbix/chk_mysql.sh
#!/bin/sh MYSQL_SOCK="/var/lib/mysql/mysql.sock" MYSQL_USER=\'mysqlcheck\' MYSQL_PWD=\'mysqlcheck\' MYSQL_HOST=\'192.168.0.8\' MYSQL_PORT=\'3307\' ARGS=1 if [ $# -ne "$ARGS" ];then echo "Please input one arguement:" fi case $1 in Uptime) result=`/usr/bin/mysqladmin -u$MYSQL_USER -h$MYSQL_HOST -p${MYSQL_PWD} -P${MYSQL_PORT} -S $MYSQL_SOCK status 2>/dev/null|cut -f2 -d":"|cut -f1 -d"T"` echo $result ;; Com_update) result=`/usr/bin/mysqladmin -u$MYSQL_USER -h$MYSQL_HOST -p${MYSQL_PWD} -P${MYSQL_PORT} -S $MYSQL_SOCK extended-status 2>/dev/null|grep -w "Com_update"|cut -d"|" -f3` echo $result ;; Slow_queries) result=`/usr/bin/mysqladmin -u$MYSQL_USER -h$MYSQL_HOST -p${MYSQL_PWD} -P${MYSQL_PORT} -S $MYSQL_SOCK status 2>/dev/null |cut -f5 -d":"|cut -f1 -d"O"` echo $result ;; Com_select) result=`/usr/bin/mysqladmin -u$MYSQL_USER -h$MYSQL_HOST -p${MYSQL_PWD} -P${MYSQL_PORT} -S $MYSQL_SOCK extended-status 2>/dev/null |grep -w "Com_select"|cut -d"|" -f3` echo $result ;; Com_rollback) result=`/usr/bin/mysqladmin -u$MYSQL_USER -h$MYSQL_HOST -p${MYSQL_PWD} -P${MYSQL_PORT} -S $MYSQL_SOCK extended-status 2>/dev/null |grep -w "Com_rollback"|cut -d"|" -f3` echo $result ;; Questions) result=`/usr/bin/mysqladmin -u$MYSQL_USER -h$MYSQL_HOST -p${MYSQL_PWD} -P${MYSQL_PORT} -S $MYSQL_SOCK status 2>/dev/null|cut -f4 -d":"|cut -f1 -d"S"` echo $result ;; Com_insert) result=`/usr/bin/mysqladmin -u$MYSQL_USER -h$MYSQL_HOST -p${MYSQL_PWD} -P${MYSQL_PORT} -S $MYSQL_SOCK extended-status 2>/dev/null |grep -w "Com_insert"|cut -d"|" -f3` echo $result ;; Com_delete) result=`/usr/bin/mysqladmin -u$MYSQL_USER -h$MYSQL_HOST -p${MYSQL_PWD} -P${MYSQL_PORT} -S $MYSQL_SOCK extended-status 2>/dev/null |grep -w "Com_delete"|cut -d"|" -f3` echo $result ;; Com_commit) result=`/usr/bin/mysqladmin -u$MYSQL_USER -h$MYSQL_HOST -p${MYSQL_PWD} -P${MYSQL_PORT} -S $MYSQL_SOCK extended-status 2>/dev/null |grep -w "Com_commit"|cut -d"|" -f3` echo $result ;; Bytes_sent) result=`/usr/bin/mysqladmin -u$MYSQL_USER -h$MYSQL_HOST -p${MYSQL_PWD} -P${MYSQL_PORT} -S $MYSQL_SOCK extended-status 2>/dev/null |grep -w "Bytes_sent" |cut -d"|" -f3` echo $result ;; Bytes_received) result=`/usr/bin/mysqladmin -u$MYSQL_USER -h$MYSQL_HOST -p${MYSQL_PWD} -P${MYSQL_PORT} -S $MYSQL_SOCK extended-status 2>/dev/null|grep -w "Bytes_received" |cut -d"|" -f3` echo $result ;; Com_begin) result=`/usr/bin/mysqladmin -u$MYSQL_USER -h$MYSQL_HOST -p${MYSQL_PWD} -P${MYSQL_PORT} -S $MYSQL_SOCK extended-status 2>/dev/null|grep -w "Com_begin"|cut -d"|" -f3` echo $result ;; *) echo "Usage:$0(Uptime|Com_update|Slow_queries|Com_select|Com_rollback|Questions)" ;; esac
3、添加权限
chmod 755 chk_mysql.sh
4、修改添加zabbix-agent配置文件
UnsafeUserParameters=1 UserParameter=mysql.version,mysql -V UserParameter=mysql.status[*],/etc/zabbix/chk_mysql.sh $1 UserParameter=mysql.ping,netstat -ntpl |grep 3307 |grep mysql |wc |awk \'{print $1}\'
# 通过端口号判断mysql存活 UserParameter=mysql.ping,netstat -ntpl |grep 3317 |grep mysql |wc|awk \'{print $1}\'
5、重启zabbix-agent
systemctl restart zabbix-agentd
# 服务端测试 zabbix_get -s 10.x.x.x -p10050 -k "system.cpu.load[all,avg15]";
6、添加zabbix管理页面添加服务
以上是关于Zabbix 监控Mysql的主要内容,如果未能解决你的问题,请参考以下文章