目录
2.1.2 复制template_db_mysql.conf
现在zabbix5.0不但添加了agent2,修改了前端的展示样式,还增加了不少新的模板,如监控mysql、reids等。
也有不少out-of-the-box(开箱即用)的模板,今天我就使用现成的模板监控mysql,学会了mysql监控,那么其它开箱即用的模板相信你也懂使用了。
一、关于开箱即用模板
1.1 开箱即用模板介绍
Zabbix致力于提供越来越多有用的开箱即用 templates.列表。 开箱即用的模板已预先配置,因此是加速监视作业部署的有用方法。
可以看官网zabbix开箱即用相关文档
2.2 mysql开箱即用模板
我这里使用 Zabbix agent template operation 中的mysql模板,使用介绍如下
Template name | Mandatory macros | Additional steps/comments |
---|---|---|
Template DB MySQL | {$MYSQL.HOST} - the hostname or IP address of MySQL host or container (default: localhost) {$MYSQL.PORT} - the database service port (default: 3306) | 1. If necessary, add the path to the mysql and mysqladmin utilities to the global environment variable PATH. 2. Copy the template_db_mysql.conf file from templates directory of Zabbix into folder with Zabbix agent configuration (/etc/zabbix/zabbix_agentd.d/ by default) and restart Zabbix agent.3. Create MySQL user zbx_monitor . To grant required priveleges to the user, run:GRANT USAGE,REPLICATION CLIENT,PROCESS,SHOW DATABASES,SHOW VIEW ON *.* TO '<username>'@'%'; (see MYSQL documentation for details). 4. Create .my.cnf in the 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>' |
具体用法,点上面的 Template DB MySQL 链接,则会打zabbix官网的git仓库,并告诉你详解的步骤
比如支持的版本
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
PS:上面有一个步骤,需要复制 template_db_mysql.conf 文件,是在打开的说明的上一级目录下载的,如下图所示:
二、例子:使用模板监控mysql8
我这里就直接按 Zabbix agent template operation 中的mysql模板 Template DB MySQL教程来安装。点击 Template DB MySQL 会进入此模板的使用说明,
2.1 zabbix5 mysql模板使用前准备工作
按照Template DB MySQL 中的步骤:
- Install Zabbix agent and MySQL client. If necessary, add the path to the
mysql
andmysqladmin
utilities to the global environment variable PATH.- 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.- Create MySQL user for monitoring (
<password>
at your discretion):- Create
.my.cnf
in home directory of Zabbix agent for Linux (/var/lib/zabbix
by default ) ormy.cnf
in c:\ for Windows. The file must have three strings:
2.1.1 mysql8安装(dnf)
我在其中一个agent安装了一个mysql8,这里我选择了vm821,mysql8安装方法见《centos8搭建zabbix5.0》的 “2.2 安装mysql8.0 (dnf)”,这里我就不多讲了。
dnf安装已经配置好了环境变量。
2.1.2 复制template_db_mysql.conf
template_db_mysql.conf在Template DB MySQL的上一级目录中,如下图所示:
#在安装了mysql的agent上操作,我这里是vm821
cd /etc/zabbix/zabbix_agentd.d/
#我这里直接把 template_db_mysql.conf 内容写进去,你也可以把整个包下载下来把文件复制过过来
#不能直接用wget下载,因为没有提供下载,wget下载的话只能是下载错误的包含有html代码的文件
cat>template_db_mysql.conf<<EOF
#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"
EOF
cat template_db_mysql.conf
ll
cd ~
PS:用cat>>文件名<<EOF 或 cat>文件名<<EOF 写入文件时,内容中包含有$符号时,,加个转义符\,即写成 \$,要么它就直接变成取值了
#效果:
[root@vm821 zabbix_agentd.d]# cat 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"
PS:从上面的内容可以看出,mysql监控也是使用自定义的key来操作的,如果没有现成的模板,其实自己也可以按照这个思想去弄。
2.1.3 创建监控账号
因mysql8默认加密方式是 caching_sha2_password
,zabbix5.0不支持这种,还是设置回旧的 mysql_native_password 加密方式,所以在创建密码的时候指定一下。操作如下:
#使用root账号登录mysql8输入如下语句
CREATE USER 'zbx_monitor'@'%'IDENTIFIED BY 'hua123';
GRANT USAGE,REPLICATION CLIENT,PROCESS,SHOW DATABASES,SHOW VIEW ON *.* TO 'zbx_monitor'@'%';
ALTER USER 'zbx_monitor'@'%' IDENTIFIED WITH mysql_native_password BY 'hua123';
flush privileges;
PS:注意上 面的语句不能写成,如下的,这种写法是有问题的,会提示语法错误
CREATE USER 'zbx_monitor'@'%' WITH mysql_native_password IDENTIFIED BY '123456';
GRANT USAGE,REPLICATION CLIENT,PROCESS,SHOW DATABASES,SHOW VIEW ON *.* TO 'zbx_monitor'@'%';
#效果
[root@vm821 ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 8.0.21 MySQL Community Server - GPL
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> CREATE USER 'zbx_monitor'@'%'IDENTIFIED BY 'hua123';
Query OK, 0 rows affected (1.25 sec)
mysql> GRANT USAGE,REPLICATION CLIENT,PROCESS,SHOW DATABASES,SHOW VIEW ON *.* TO 'zbx_monitor'@'%';
Query OK, 0 rows affected (0.27 sec)
mysql> ALTER USER 'zbx_monitor'@'%' IDENTIFIED WITH mysql_native_password BY 'hua123';
Query OK, 0 rows affected (0.15 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.30 sec)
mysql> \q
Bye
[root@vm821 ~]#
[root@vm821 ~]# mysql -uzbx_monitor -p -e 'show databases;'
Enter password:
+--------------------+
| Database |
+--------------------+
| hua |
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
[root@vm821 ~]#
2.1.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>'
按上面提示我这里在创建一个 .my.cnf
文件
#建立相关目录
mkdir -p /var/lib/zabbix
#写入文件,用户名和密码
cat>/var/lib/zabbix/.my.cnf<<EOF
[client]
user='zbx_monitor'
password='hua123'
EOF
cat /var/lib/zabbix/.my.cnf
#重启
systemctl restart zabbix-agent
systemctl status zabbix-agent
2.2 添加 Template DB MySQL 监控模板
上面的准备工作做好之后,在被监控的机子添加模板就行了,如果添加我这里就不具体操作了,不懂的可以回看我的《zabbix 配置流程讲解(重要)》
为了方便实验我这里只添加一个模板,如下图所示:
过大概1分钟看一下是否有数据生成,如下图所示:
这样一个mysql监控就弄好了