1.agent端配置
前提条件:
关闭防火墙
关闭selinux
安装zabbix-agent
- 拷贝模板到指定目录
cp /usr/share/doc/zabbix-agent-5.0.30/userparameter_mysql.conf /etc/zabbix/zabbix_agentd.d/
vim /etc/zabbix/zabbix_agentd.d/userparameter_mysql.conf
#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[*], mysqladmin -h"127.0.0.1" -P"3306" ping |grep -c alive
UserParameter=mysql.ping[*], mysqladmin ping |grep -c alive
UserParameter=mysql.get_status_variables[*], mysql -h"$1" -P"$2" -sNX -e "show global status"
UserParameter=mysql.version[*], mysqladmin -s -h"$1" -P"$2" version
UserParameter=mysql.db.discovery[*], mysql -h"$1" -P"$2" -sN -e "show databases"
UserParameter=mysql.dbsize[*], mysql -h"$1" -P"$2" -sN -e "SELECT SUM(DATA_LENGTH + INDEX_LENGTH) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='$3'"
UserParameter=mysql.replication.discovery[*], mysql -h"$1" -P"$2" -sNX -e "show slave status"
UserParameter=mysql.slave_status[*], mysql -h"$1" -P"$2" -sNX -e "show slave status"
- 创建用于监控mysql的用户
CREATE USER 'zbx_monitor'@'%' IDENTIFIED BY '<password>';
GRANT REPLICATION CLIENT,PROCESS,SHOW DATABASES,SHOW VIEW ON *.* TO 'zbx_monitor'@'%';
- 使用命令测试获取值
mysqladmin -uzbx_monitor -pZabbix@123 ping |grep -c alive
my.cnf文件追加以下内容
vim /etc/my.cnf
[client]
user='zbx_monitor'
password='<password>'
[mysqladmin]
user='zbx_monitor'
password='<password>'
systemctl restart mysqld
修改完my.cnf使用以下命令测试
HOME=/var/lib/zabbix/ mysqladmin ping |grep -c alive
2.server端配置
添加模板
使用 zabbix_get 获取键值测试
zabbix_get -s 192.168.4.124 -k mysql.ping["{$MYSQL.HOST}","{$MYSQL.PORT}"]