zabbix-6.4 monitor MySQL

Table of contents

1. rpm install zabbix_agentd service

2. Write the zabbix_agentd.conf file

3. Write template files

4. Create a mysql user and grant permissions

5. Create .my.cnf file

6. Add rules to SELinux policy

Notice:

If the template cannot read the .my.cnf information, resulting in monitoring errors, you can try to modify the template configuration file:


1. rpm install zabbix_agentd service


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

2. Write the zabbix_agentd.conf file

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

3. Write template files


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='<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"

4. Create a mysql user and grant permissions

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

5. Create .my.cnf file

mkdir -p /var/lib/zabbix
vim /var/lib/zabbix/.my.cnf


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

6. Add rules to SELinux policy

systemctl edit zabbix-agent.service

# cat <<EOF > 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

Notice:

If the template cannot read the .my.cnf information, resulting in monitoring errors, you can try to modify the template configuration file:

#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 --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"

Guess you like

Origin blog.csdn.net/m0_57126939/article/details/132274552