zabbix-agent如何监控mysql

发布时间 2023-08-18 16:48:56作者: 厚礼蝎

如今,我们普遍是使用zabbix-agent来监控mysql,因为配置简单

只需要添加zabbix-agent2的模板,然后配置mysql的socket路径,然后,配置用户名密码就可以了

然而,咱们也还有相当一部分服务是跑在系统无法安装zabbix-agent2的系统上,这个时候就只能使用zabbix-agent来监控了

客户端配置

描述

在zabbix-agent模板中,有这么一段描述

Requirements for template operation:
1.Install Zabbix agent and MySQL client.
2.Copy template_db_mysql.conf into folder with Zabbix agent configuration (/etc/zabbix/zabbix_agentd.d/ by default). Don't forget to restart zabbix-agent.
3.Create MySQL user for monitoring. For example:
CREATE USER 'zbx_monitor'@'%' IDENTIFIED BY '<password>';
GRANT REPLICATION CLIENT,PROCESS,SHOW DATABASES,SHOW VIEW ON *.* TO 'zbx_monitor'@'%';
For more information read the MySQL documentation https://dev.mysql.com/doc/refman/8.0/en/grant.html , please. 
4.Create .my.cnf in home directory of Zabbix agent for Linux (/var/lib/zabbix by default) or my.cnf in c:\ for Windows. For example:
[client]
user='zbx_monitor'
password='<password>'


You can discuss this template or leave feedback on our forum https://www.zabbix.com/forum/zabbix-suggestions-and-feedback/384189-discussion-thread-for-official-zabbix-template-db-mysql

Template tooling version used: 0.42

大概意思就是说,需要先安装zabbix-agent和mysql客户端

然后将 配置文件 template_db_mysql.conf 存放到 /etc/zabbix/zabbix_agentd.d/ 目录下,然后重启zabbix-agent

然后在mysql中创建监控用户

CREATE USER 'zbx_monitor'@'%' IDENTIFIED BY '<password>';
GRANT REPLICATION CLIENT,PROCESS,SHOW DATABASES,SHOW VIEW ON *.* TO 'zbx_monitor'@'%';

最后再在zabbix-agent的家目录中放一个 .my.cnf 文件

内容是用户名和密码

[client]
user='zbx_monitor'
password='<password>'

这段描述就已经写的很清楚了

执行

找到配置文件 template_db_mysql.conf

https://git.zabbix.com/projects/ZBX/repos/zabbix/browse/templates/db/mysql_agent

在这里找到模板

然后当到 /etc/zabbix/zabbix_agentd.d/ 目录下

不过需要做一些修改

#template_db_mysql.conf created by Zabbix for "Template DB MySQL" and Zabbix 4.2
#For OS Linux: You need create .my.cnf in zabbix-agent home directory (/var/lib/zabbix by default) 
#For OS Windows: You need add PATH to mysql and mysqladmin and create my.cnf in %WINDIR%\my.cnf,C:\my.cnf,BASEDIR\my.cnf https://dev.mysql.com/doc/refman/5.7/en/option-files.html
#The file must have three strings:
#[client]
#user='zbx_monitor'
#password='<password>'
#
UserParameter=mysql.ping[*], HOME=/etc/zabbix  mysqladmin -h"$1" -P"$2" ping
UserParameter=mysql.get_status_variables[*], HOME=/etc/zabbix  mysql -h"$1" -P"$2" -sNX -e "show global status"
UserParameter=mysql.version[*], HOME=/etc/zabbix  mysqladmin -s -h"$1" -P"$2" version
UserParameter=mysql.db.discovery[*],HOME=/etc/zabbix  mysql -h"$1" -P"$2" -sN -e "show databases"
UserParameter=mysql.dbsize[*],HOME=/etc/zabbix  mysql -h"$1" -P"$2" -sN -e "SELECT COALESCE(SUM(DATA_LENGTH + INDEX_LENGTH),0) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='$3'"
UserParameter=mysql.replication.discovery[*],HOME=/etc/zabbix  mysql -h"$1" -P"$2" -sNX -e "show slave status"
UserParameter=mysql.slave_status[*],HOME=/etc/zabbix  mysql -h"$1" -P"$2" -sNX -e "show slave status"

需要在每条命令前添加一个参数 HOME=/etc/zabbix

明确指定 命令执行时的家目录

因为按照描述的说法,需要把用户名和密码文件存放到zabbix运行的家目录,但是 所谓的 /var/lib/zabbix 目录其实没有,为了方式无法找到的问题,我们直接明确指定家目录,然后把 .my.cnf 文件放在配置文件目录中

配置监控用户

CREATE USER 'zbx_monitor'@'%' IDENTIFIED BY 'abc111111';
GRANT REPLICATION CLIENT,PROCESS,SHOW DATABASES,SHOW VIEW ON *.* TO 'zbx_monitor'@'%';

然后写配置文件

vim /etc/zabbix/.my.cnf

[client]
user='zbx_monitor'
password='abc111111'

最后重启 zabbix-agent服务

systemctl restart zabbix-agent.service

服务端配置

只需要添加 MySQL by Zabbix agent 模板就好了,然后等待数据的到来