Template DB MySQL學習總結

Zabbix 5.0下如何應用Template DB MySQL來監控MySQL數據庫呢?下面簡單整理一下如何配置、應用Zabbix下自帶的模板Template DB MySQL。其實很是簡單。html

 

Template DB MySQL能夠在Zabbix 5.0的模板下面查看,也有簡單的安裝相關說明,以下所示:mysql

 

clip_image001

 

Requirements for template operation:git

1.Install Zabbix agent and MySQL client.sql

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>';app

GRANT USAGE,REPLICATION CLIENT,PROCESS,SHOW DATABASES,SHOW VIEW ON *.* TO 'zbx_monitor'@'%';socket

For more information read the MYSQL documentation https://dev.mysql.com/doc/refman/8.0/en/grant.html , please. tcp

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:ui

[client]

user=zbx_monitor

password=<password>

 

在GitHub上https://git.zabbix.com/projects/ZBX/repos/zabbix/browse/templates/db/mysql_agent上,你能夠看到更詳細的信息

 

 

Overview

 

For Zabbix version: 5.0

The template is developed for monitoring DBMS MySQL and its forks.

 

This template was tested on:

 

·         MySQL, version 5.7, 8.0

·         Percona, version 8.0

·         MariaDB, version 10.4

·         Zabbix, version 4.2.1

 

Setup

 

See Zabbix template operation for basic instructions.

 

1. Install Zabbix agent and MySQL client. If necessary, add the path to the mysql and mysqladmin utilities to the global environment variable PATH.

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 (<password> at your discretion):

 

CREATE USER 'zbx_monitor'@'%' IDENTIFIED BY '<password>';

GRANT USAGE,REPLICATION CLIENT,PROCESS,SHOW DATABASES,SHOW VIEW ON *.* TO 'zbx_monitor'@'%';

 

For more information please read the MYSQL documentation https://dev.mysql.com/doc/refman/8.0/en/grant.html

 

4. Create .my.cnf in home directory of Zabbix agent for Linux (/var/lib/zabbix by default ) or my.cnf in c:\ for Windows. The file must have three strings:

 

[client]

user='zbx_monitor'

password='<password>'

 

NOTE: Use systemd to start the Zabbix agent on Linux OS. For example in Centos use "systemctl edit zabbix-agent.service" to set the required user to start the Zabbix agent.

 

Add the rule to the SELinux policy (example for Centos):

 

# 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

 

 

下面用中文簡單總結歸納一下

 

 

1:在被監控的主機上安裝Zabbix Agent和 MySQL Client,若有必要,將mysql和mysqladmin應用程序的路徑添加到全局環境變量PATH中。

 

2:將Template_DB_MySQL.conf文件拷貝到Zabbix Agent的配置目錄(默認爲etc/zabbix/zabbix_agentd.d/),不要忘記重啓Zabbix Agent。

 

    注意,Template_DB_MySQL.conf文件在哪裏呢?相信有些人會懵逼。這個文件在https://git.zabbix.com/projects/ZBX/repos/zabbix/browse/templates/db/mysql_agent下

 

    我將zabbix源碼download下面,此文件位於zabbix-master@18cf3730036\templates\db\mysql_agent下。

 

3:在被監控的數據庫上建立數據庫帳號

 

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

 

我的觀點:出於安全考慮,帳號限定只能本機訪問,不要設置爲任意IP能夠訪問。

 

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

 

4:找不到這個目錄/var/lib/zabbix,那麼新建此目錄,而後建立.my.cnf

 

# rpm -ql zabbix-agent 
/etc/logrotate.d/zabbix-agent
/etc/zabbix/zabbix_agentd.conf
/etc/zabbix/zabbix_agentd.d
/usr/lib/systemd/system/zabbix-agent.service
/usr/lib/tmpfiles.d/zabbix-agent.conf
/usr/sbin/zabbix_agentd
/usr/share/doc/zabbix-agent-5.0.1
/usr/share/doc/zabbix-agent-5.0.1/AUTHORS
/usr/share/doc/zabbix-agent-5.0.1/COPYING
/usr/share/doc/zabbix-agent-5.0.1/ChangeLog
/usr/share/doc/zabbix-agent-5.0.1/NEWS
/usr/share/doc/zabbix-agent-5.0.1/README
/usr/share/doc/zabbix-agent-5.0.1/userparameter_mysql.conf
/usr/share/man/man8/zabbix_agentd.8.gz
/var/log/zabbix
/var/run/zabbix

 

新建目錄/var/lib/zabbix

# mkdir /var/lib/zabbix

 

添加訪問數據庫的帳號信息。

 

[client]

user='zbx_monitor'

password='<password>'

 

 

關於Template DB MySQL的Discovery rules、Items 、Triggers、Macros,下面參考資料中已有詳細介紹,在此略過。

 

 

 

參考資料:

 

https://www.zabbix.com/integrations/mysql

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

相關文章
相關標籤/搜索