zabbix监控mysql

1.创建用来连接mysql的数据库用户

> mysql -uroot -p -e"GRANT USAGE ON *.* TO 'zabbix'@'localhost' IDENTIFIED BY 'password'";
> mysql -uroot -p -e"flush privileges";

2.自定义key,创建mysql监控配置

# cd /usr/local/zabbix/etc/zabbix_agentd.conf.d
# vi userparameter_mysql.conf

这个文件可以从zabbix_server服务端安装时解压目录/usr/local/src/zabbix3.x.x/conf/zabbix_agentd/userparameter_mysql.conf拷贝过来,添加以下内容:(mysql的参数和zabbix需要使用的命令)

# For all the following commands HOME should be set to the directory that has .my.cnf file with password information.

# Flexible parameter to grab global variables. On the frontend side, use keys like mysql.status[Com_insert].
# Key syntax is mysql.status[variable].
UserParameter=mysql.status[*],echo "show global status where Variable_name='$1';" | HOME=/etc/zabbix /usr/local/mysql/bin/mysql -N | awk '{print $$2}' # My line

# Flexible parameter to determine database or table size. On the frontend side, use keys like mysql.size[zabbix,history,data].
# Key syntax is mysql.size[<database>,<table>,<type>].
# Database may be a database name or "all". Default is "all".
# Table may be a table name or "all". Default is "all".
# Type may be "data", "index", "free" or "both". Both is a sum of data and index. Default is "both".
# Database is mandatory if a table is specified. Type may be specified always.
# Returns value in bytes.
# 'sum' on data_length or index_length alone needed when we are getting this information for whole database instead of a single table
UserParameter=mysql.size[*],bash -c 'echo "select sum($(case "$3" in both|"") echo "data_length+index_length";; data|index) echo "$3_length";; free) echo "data_free";; esac)) from information_schema.tables$([[ "$1" = "all" || ! "$1" ]] || echo " where table_schema="$1"")$([[ "$2" = "all" || ! "$2" ]] || echo "and table_name="$2"");" | HOME=/etc/zabbix /usr/local/mysql/bin/mysql -N'

#Default below
UserParameter=mysql.ping,HOME=/etc/zabbix /usr/local/mysql/bin/mysqladmin ping | grep -c alive #My line
UserParameter=mysql.uptime,HOME=/etc/zabbix /usr/local/mysql/bin/mysqladmin status | cut -f2 -d ":" | cut -f1 -d "T" | tr -d " "
UserParameter=mysql.threads,HOME=/etc/zabbix /usr/local/mysql/bin/mysqladmin status | cut -f3 -d ":" | cut -f1 -d "Q" | tr -d " "
UserParameter=mysql.questions,HOME=/etc/zabbix /usr/local/mysql/bin/mysqladmin status | cut -f4 -d ":"|cut -f1 -d "S" | tr -d " "
UserParameter=mysql.slowqueries,HOME=/etc/zabbix /usr/local/mysql/bin/mysqladmin status | cut -f5 -d ":" | cut -f1 -d "O" | tr -d " "
UserParameter=mysql.qps,HOME=/etc/zabbix /usr/local/mysql/bin/mysqladmin status | cut -f9 -d ":" | tr -d " "
UserParameter=mysql.version,/usr/local/mysql/bin/mysql -V

这里HOME是.my.cnf文件所在的目录
另外,要注意userparameter_mysql.conf文件里的mysql命令路径(可以提前做好mysql的系统环境变量,以防mysql命令不被系统识别)

3.编辑zabbix agentd客户端的配置

# vim /etc/zabbix/etc/zabbix_agentd.conf
Include=/etc/zabbix/etc/zabbix_agentd.conf.d/ 		 #扩展配置目录
UnsafeUserParameters=1   			         #允许自定义key

4.配置zabbix连接mysql用户信息配置 

# cd /etc/zabbix
# vim .my.cnf
[mysql]
host=localhost
user=zabbix
password=password
socket=/var/run/mysqld/mysqld.sock
[mysqladmin]
host=localhost
user=zabbix
password=password
socket=/var/run/mysqld/mysqld.sock

如果在数据库grant授权时,针对的是localhost,这个.my.cnf里面就不用加host参数了。
但如果grant授权时针对的是本机的ip(如xxx.xxx.xxx.xxx),那么在.my.cnf文件里就要加上host参数进行指定,即在.my.cnf文件就要加上:
host=xxx.xxx.xxx.xxx
socket=/var/run/mysqld/mysqld.sock #这一行可以不用加上,默认会找到的

5.重启zabbix client

# service zabbix_agentd restart
或者
# /etc/zabbix/sbin/zabbix_agentd 

6.在zabbix server上通过zabbix_get测试是否能获取zabbix agentd的mysql status

# zabbix_get -s 192.168.1.25 -p 10050 -k "mysql.status[Uptime]"
578

如果出现类似这一串key的数字,就说明配置ok,服务端能监控到客户端的mysql数据了!

最后在监控界面增加主机对应的MySQL模板。

原文地址:https://www.cnblogs.com/abclife/p/6733023.html