标签归档:MariaDB数据库

Zabbix 3.4 监控 MariaDB(MySQL) 数据库性能

先决条件:

首先你需要安装 Zabbix Servers 及 Zabbix-agentd 端:

Zabbix Servers 3.4 源代码编译安装
Centos 7 Zabbix Agent 客户端源码编译安装配置

1.配置 userparameter_mysql.conf

Zabbix源码中包含了 userparameter_mysql.conf 配置文件,只需要将其拷贝到zabbix_agentd.conf.d目录下:

$ find / -name userparameter_mysql.conf
$ cd /tmp/zabbix-3.4.2/conf/zabbix_agentd/
$ cp userparameter_mysql.conf /usr/local/zabbix/etc/zabbix_agentd.conf.d/

2.修改 zabbix_agentd.conf

因为我们将userparameter_mysql.conf文件放在了zabbix_agentd.conf.d目录,所以需要取消下面注释,这样Zabbix Agent才能够加载此文件

$ vim /usr/local/zabbix/etc/zabbix_agentd.conf
...
Include=/usr/local/zabbix/etc/zabbix_agentd.conf.d/*.conf
...

注意:若 systemctl Zabbix-Agent.service 单元文件中已经加载此配置文件/路径,此步骤可以忽略。

3.创建监控 MariaDB(MySQL) 用户信息

MariaDB [(none)]> GRANT SELECT ON *.* TO 'zabbix'@'localhost' IDENTIFIED BY 'Renwolecom';
MariaDB [(none)]> FLUSH PRIVILEGES;
MariaDB [(none)]> exit
Bye

注意:MySQL用户权限可根据需求设置,可参考《Linux MariaDB(MySQL)数据库更改用户权限》。

4.创建 .my.cnf 文件,添加 MariaDB(MySQL) 用户信息

$ vim /usr/local/zabbix/etc/.my.cnf

# Zabbix Agent
[mysql]
host=localhost
user=zabbix
password="Renwolecom"
socket=/tmp/mysql.sock
[mysqladmin]
host=localhost
user=zabbix
password="Renwolecom"
socket=/tmp/mysql.sock

5.修改 userparameter_mysql.conf

修改后的内容如下:

$ cd /usr/local/zabbix/etc/zabbix_agentd.conf.d/
$ egrep -v "(^#|^$)" userparameter_mysql.conf

UserParameter=mysql.status[*],echo "show global status where Variable_name='$1';" | HOME=/usr/local/zabbix/etc/ /usr/local/mysql/bin/mysql -N | awk '{print $$2}'
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=/usr/local/zabbix/etc mysql -N'
UserParameter=mysql.ping,HOME=/usr/local/zabbix/etc/ /usr/local/mysql/bin/mysqladmin ping | grep -c alive
UserParameter=mysql.version,/usr/local/mysql/bin/mysql -V
UserParameter=mysql.vars[*],echo "show variables where Variable_name='$1';" | HOME=/usr/local/zabbix/etc/ /usr/local/mysql/bin/mysql -N | awk '{print $$2}'

注:以上内容请注意相关绝对路径。

6.重启 Zabbix-Agent 服务

$ systemctl restart zabbix-agent

最后在 Zabbix UI界面中添加MySQL监控模板即可。