目录
1、rpm安装zabbix_agentd服务
2、编写zabbix_agentd.conf文件
3、编写模板文件
4、创建mysql用户并赋权限
5、创建.my.cnf文件
6、将规则添加到SELinux策略中
注意:
若模板无法读取.my.cnf 信息,从而导致监控报错,可以尝试修改模板配置配置文件:
rpm -Uvh https://repo.zabbix.com/zabbix/6.4/rhel/7/x86_64/zabbix-release-6.4-1.el7.noarch.rpm
yum clean all
# b. Install Zabbix agent
yum install zabbix-agent
# c. Start Zabbix agent process
# Start Zabbix agent process and make it start at system boot.
sudo systemctl stop zabbix-agent
systemctl restart zabbix-agent
systemctl enable zabbix-agent
cd /etc/zabbix
vim zabbix_agentd.conf
PidFile=/var/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd_1.log
LogFileSize=0
Server=192.168.3.246
ServerActive=192.168.3.246
Hostname=192.168.3.244
Include=/etc/zabbix/zabbix_agentd.d/*.conf
UnsafeUserParameters=1
vim /etc/zabbix/zabbix_agentd.d/template_db_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=''
#
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 COALESCE(SUM(DATA_LENGTH + INDEX_LENGTH),0) 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"
CREATE USER 'zbx_monitor'@'%' IDENTIFIED BY '123456';
GRANT REPLICATION CLIENT,PROCESS,SHOW DATABASES,SHOW VIEW ON *.* TO 'zbx_monitor'@'%';
mkdir -p /var/lib/zabbix
vim /var/lib/zabbix/.my.cnf
[client]
user='zbx_monitor'
password='123456'
systemctl edit zabbix-agent.service
# cat < zabbix_home.te
module zabbix_home 1.0;
require {
type zabbix_agent_t;
type zabbix_var_lib_t;
type mysqld_etc_t;
type mysqld_port_t;
type mysqld_var_run_t;
class file { open read };
class tcp_socket name_connect;
class sock_file write;
}
#============= zabbix_agent_t ==============
allow zabbix_agent_t zabbix_var_lib_t:file read;
allow zabbix_agent_t zabbix_var_lib_t:file open;
allow zabbix_agent_t mysqld_etc_t:file read;
allow zabbix_agent_t mysqld_port_t:tcp_socket name_connect;
allow zabbix_agent_t mysqld_var_run_t:sock_file write;
EOF
# checkmodule -M -m -o zabbix_home.mod zabbix_home.te
# semodule_package -o zabbix_home.pp -m zabbix_home.mod
# semodule -i zabbix_home.pp
# restorecon -R /var/lib/zabbix
#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
#The file must have three strings:
#[client]
#user='zbx_monitor'
#password=''
#
UserParameter=mysql.ping[*], mysqladmin --defaults-extra-file='/var/lib/zabbix/.my.cnf' -h"$1" -P"$2" ping
UserParameter=mysql.get_status_variables[*], mysql --defaults-extra-file='/var/lib/zabbix/.my.cnf' -h"$1" -P"$2" -sNX -e "show global status"
UserParameter=mysql.version[*], mysqladmin --defaults-extra-file='/var/lib/zabbix/.my.cnf' -h"$1" -P"$2" version
UserParameter=mysql.db.discovery[*], mysql --defaults-extra-file='/var/lib/zabbix/.my.cnf' -h"$1" -P"$2" -sN -e "show databases"
UserParameter=mysql.dbsize[*], mysql --defaults-extra-file='/var/lib/zabbix/.my.cnf' -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[*], mysql --defaults-extra-file='/var/lib/zabbix/.my.cnf' -h"$1" -P"$2" -sNX -e "show slave status"
UserParameter=mysql.slave_status[*], mysql --defaults-extra-file='/var/lib/zabbix/.my.cnf' -h"$1" -P"$2" -sNX -e "show slave status"