1. 关联mysql模版
步骤:配置 > 主机 > 点击主机的主机 > 模版
点击那个模版进去看看模版的描述
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 restart zabbix-agent.
3.Create MySQL user for monitoring. For example:
CREATE USER 'zbx_monitor'@'%' IDENTIFIED BY '<password>';
GRANT USAGE,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>
2. 设置模版mysql端
因为机器上(Zabbix server)找不到Template_DB_MySQL.conf,所以查找zabbix自带的模板userparameter_mysql.conf
1)查看zabbix中的mysql监控模板,find / -name userparameter_mysql.conf
[root@ ~]# find / -name userparameter_mysql.conf /usr/share/doc/zabbix-agent-5.0.4/userparameter_mysql.conf
打开看到这个文件的第一句话
#template_db_mysql.conf created by Zabbix for "Template DB MySQL" and Zabbix 4.2
2)拷贝到zabbix_agentd.conf.d/目录下
cp /usr/share/doc/zabbix-agent-5.0.4/userparameter_mysql.conf /etc/zabbix/zabbix_agentd.d/
3)测试模版是否有效
# zabbix_agentd -t mysql.ping -c /etc/zabbix/zabbix_agentd.d/userparameter_mysql.conf mysql.ping [t|mysqladmin: connect to server at '-P' failed error: 'Unknown MySQL server host '-P' (2)'
4) 模版修改
# vi /etc/zabbix/zabbix_agentd.d/userparameter_mysql.conf 把原来的
UserParameter=mysql.ping[*], mysqladmin -h"$1" -P"$2" ping
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"
改为
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 SUM(DATA_LENGTH + INDEX_LENGTH) 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 (要相信原生的模版写法)
5)mysql下新增一个监控用户zabbix
CREATE USER 'zabbix'@'%' IDENTIFIED BY '123456';
GRANT USAGE,REPLICATION CLIENT,PROCESS,SHOW DATABASES,SHOW VIEW ON *.* TO 'zabbix'@'%';
6)/etc/zabbix下新增一个文件 .my.cnf (隐藏文件查看需要 ls -al)
[client] user=zabbix password=123456 host=192.168.217.43
7)重启动zabbix_agentd
# systemctl restart zabbix-agent
这一步结束后,要等,等5分钟,10分钟的,不要着急。
8)看 监测 > 最新数据
点右边的“图形”可以看图表
我这里使用的是zabbix 自带模板 Template App MySQL
创建主机 192.168.2.6
关联模板
创建监控项
创建图形
查看监控图像
设置报警,创建触发器,动作(当STATUS=0时,报警)
进行测试
停掉数据库(测试环境)
linux:~ # /etc/init.d/mysql stop