返回列表 发帖

Zabbix使用模板监控Linux下的MariaDB

笺注:这是在 CentOS8编译安装Zabbix4.4.5 的基础上进行的,监控本机的MariaDB


配置本机的MariaDB数据库:( 使用数据库管理员root@localhost )
[root@centos8 ~]# mysql -u"root" -h"localhost"
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 100
Server version: 10.3.17-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]>

注释:
此时,数据库管理员root@localhost没有设置密码;
-h"localhost" 是可以省略的;



查看当前登录的数据库用户:
MariaDB [(none)]> Select user();
+----------------+
| user()         |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.001 sec)

MariaDB [(none)]>



再授权数据库用户zbx_monitor@localhost(数据库用户可以不要密码的),只能本地登录,对所有的库有完全控制的权限:
MariaDB [(none)]> create user zbx_monitor@localhost;
Query OK, 0 rows affected (0.001 sec)

MariaDB [(none)]> grant all on *.* to zbx_monitor@localhost;
Query OK, 0 rows affected (0.000 sec)

MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.001 sec)

MariaDB [(none)]>


查看本地数据库用户zbx_monitor@localhost的权限:
MariaDB [(none)]> show grants for zbx_monitor@localhost;
+----------------------------------------------------------+
| Grants for zbx_monitor@localhost                         |
+----------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'zbx_monitor'@'localhost' |
+----------------------------------------------------------+
1 row in set (0.001 sec)

MariaDB [(none)]>





#############
#############

修改Zabbix服务器的Agent配置文件:
[root@centos8 ~]# find / -name zabbix_agentd.conf
/root/zabbix-4.4.5/conf/zabbix_agentd.conf
/usr/local/zabbix/etc/zabbix_agentd.conf
[root@centos8 ~]#


[root@centos8 ~]# vi /usr/local/zabbix/etc/zabbix_agentd.conf

# UnsafeUserParameters=0
修改为:(启用该功能)
UnsafeUserParameters=1

接着插入:
  1. UserParameter=mysql.ping[*], mysqladmin -u"zbx_monitor" -h"$1" -P"$2" ping
  2. UserParameter=mysql.get_status_variables[*], mysql -u"zbx_monitor" -h"$1" -P"$2" -sNX -e "show global status"
  3. UserParameter=mysql.version[*], mysqladmin -u"zbx_monitor" -s -h"$1" -P"$2" version
  4. UserParameter=mysql.db.discovery[*], mysql -u"zbx_monitor" -h"$1" -P"$2" -sN -e "show databases"
  5. UserParameter=mysql.dbsize[*], mysql -u"zbx_monitor" -h"$1" -P"$2" -sN -e "SELECT SUM(DATA_LENGTH + INDEX_LENGTH) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='$3'"
  6. UserParameter=mysql.replication.discovery[*], mysql -u"zbx_monitor" -h"$1" -P"$2" -sNX -e "show slave status"
  7. UserParameter=mysql.slave_status[*], mysql -u"zbx_monitor" -h"$1" -P"$2" -sNX -e "show slave status"
复制代码

如下图:
图片1.png


保存好配置文件后,重启一下Zabbix本机的客户端和服务端:
pkill -9 -U zabbix
/usr/local/zabbix/sbin/zabbix_agentd
/usr/local/zabbix/sbin/zabbix_server





本地测试:

使用命令mysqladmin检测MariaDB服务有没有在运行中:(没有运行时)
[root@centos8 ~]# systemctl stop mysql
[root@centos8 ~]#
[root@centos8 ~]# mysqladmin -u"zbx_monitor" ping
mysqladmin: connect to server at 'localhost' failed
error: 'Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)'
Check that mysqld is running and that the socket: '/var/lib/mysql/mysql.sock' exists!

[root@centos8 ~]#


使用命令mysqladmin检测MariaDB服务有没有在运行中:(在运行时)
[root@centos8 ~]# systemctl start mysql
[root@centos8 ~]#
[root@centos8 ~]# mysqladmin -u"zbx_monitor" ping
mysqld is alive
[root@centos8 ~]#



使用命令mysqladmin查看MariaDB的版本信息等等:
[root@centos8 ~]# mysqladmin -u"zbx_monitor" -s version
mysqladmin  Ver 9.1 Distrib 10.3.17-MariaDB, for Linux on x86_64
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Server version          10.3.17-MariaDB
Protocol version        10
Connection              Localhost via UNIX socket
UNIX socket             /var/lib/mysql/mysql.sock
Uptime:                 170 days 22 hours 10 min 2 sec

Threads: 33  Questions: 58590  Slow queries: 0  Opens: 140  Flush tables: 1  Open tables: 134  Queries per second avg: 0.003
[root@centos8 ~]#

注释:这里可以看到目前MariaDB服务运行了170天22小时10分2秒;



使用命令mysql显示MariaDB里的全部库:
[root@centos8 ~]# mysql -u"zbx_monitor" -sN -e "show databases"
db1
information_schema
mysql
performance_schema
zabbix

[root@centos8 ~]#



使用命令mysql显示库zabbix的大小:(返回值以字节为单位;不保留小数;使用模板的格式)
[root@centos8 ~]# mysql -u"zbx_monitor" -h"localhost" -P"3306" -sN -e "SELECT SUM(DATA_LENGTH + INDEX_LENGTH) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='zabbix'"
15548416
[root@centos8 ~]#


使用命令mysql显示库zabbix的大小:(返回值以字节为单位;保留两位小数,是四舍五入)
[root@centos8 ~]# mysql -u"zbx_monitor" -h"localhost" -P"3306" -sN -e "select concat(round(sum(DATA_LENGTH),2)) as data from information_schema.TABLES where table_schema = 'zabbix';"
9764864.00
[root@centos8 ~]#


[root@centos8 ~]# mysql -u"zbx_monitor" -h"localhost" -P"3306" -sN -e "select concat(round(sum(DATA_LENGTH + INDEX_LENGTH),2)) as data from information_schema.TABLES where table_schema = 'zabbix';"
15548416.00
[root@centos8 ~]#



使用命令mysql显示库zabbix的大小:(返回值以MB为单位;保留两位小数,是四舍五入)
[root@centos8 ~]# mysql -u"zbx_monitor" -h"localhost" -P"3306" -sN -e "select concat(round(sum(DATA_LENGTH/1024/1024),2)) as data from information_schema.TABLES where table_schema = 'zabbix';"
9.31
[root@centos8 ~]#


[root@centos8 ~]# mysql -u"zbx_monitor" -h"localhost" -P"3306" -sN -e "select concat(round(sum(DATA_LENGTH/1024/1024 + INDEX_LENGTH/1024/1024),2)) as data from information_schema.TABLES where table_schema = 'zabbix';"
14.83
[root@centos8 ~]#

注释:
data_length + index_length 等于库的表的总大小:
data_length: 数据的实际大小
index_length: 表索引的大小





##############
##############

在Zabbix服务器测试,验证能否获取本机(127.0.0.1)的键值:

检测本机MariaDB服务有没有在运行中:(没有运行时)
[root@centos8 ~]# /usr/local/zabbix/bin/zabbix_get -s 127.0.0.1 -k mysql.ping["localhost","3306"]
mysqladmin: connect to server at 'localhost' failed
error: 'Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)'
Check that mysqld is running and that the socket: '/var/lib/mysql/mysql.sock' exists!

[root@centos8 ~]#


检测本机MariaDB服务有没有在运行中:(在运行时)
[root@centos8 ~]# /usr/local/zabbix/bin/zabbix_get -s 127.0.0.1 -k mysql.ping["localhost","3306"]
mysqld is alive
[root@centos8 ~]#


输出本机MariaDB里的全部库:
[root@centos8 ~]# /usr/local/zabbix/bin/zabbix_get -s 127.0.0.1 -k mysql.db.discovery["localhost","3306"]
db1
information_schema
mysql
performance_schema
zabbix

[root@centos8 ~]#





##################Zabbix添加被监控主机:

配置》主机》创建主机(主机名称无需与被监控主机的真实主机名一致;群组可以随便选)
图片2.png
注释:
使用agent代理程序的接口。
IP地址: 127.0.0.1 (同一台主机可以被重复添加,只要主机名称不一样即可)
使用 TCP 10050 端口。


主机MariaDB_2添加成功:
图片3.png


给主机MariaDB_2添加模板:( Template DB MySQL  )
图片4.png


模板添加成功:
图片5.png


查看主机MariaDB_2继承模板的宏:
图片6.png



###

模板Template DB MySQL自带的图形:
图片7.png


图片8.png

图片9.png
注释:
一个图形可以有多个监控项;
也可以克隆后,重新编辑;



###

查看模板Template DB MySQL自带图形:(要等一段时间才会出现)

监测》图形:
图片10.png

图片11.png

图片12.png



图片13.png

图片14.png

图片15.png



图片16.png

图片17.png

图片18.png



图片19.png

图片20.png

图片21.png



图片22.png

图片23.png

图片24.png



图片25.png

图片26.png

图片27.png





######

模板Template DB MySQL有自动检查MariaDB各个库的大小的监控项:
监控项名称: Info: Size of database zabbix
键值: mysql.dbsize["{$MYSQL.HOST}","{$MYSQL.PORT}","zabbix"]
图片28.png


在Zabbix服务器上可以使用命令行获取到库“zabbix”的大小:(返回值以字节为单位)
[root@centos8 ~]# /usr/local/zabbix/bin/zabbix_get -s 127.0.01 -k mysql.dbsize["localhost","3306","zabbix"]
18644992
[root@centos8 ~]#

注释:
键值的格式要改变一下,不要使用变量;
这相当于在被监控主机上进行本地监控,所以应该使用"localhost"


笺注:根据监控项“Info: Size of database zabbix”的键值格式,在创建触发器时会失败。



重新创建一个监控项:(键值中不要使用变量(宏))
自定义名称: Size of database zabbix
自定义键值: mysql.dbsize["localhost","3306","zabbix"]
信息类型: 数字(无正负)
图片29.png
备注:其他地方保持默认,点击底下的“添加”按键。



给监控项“Size of database zabbix”创建图形:
图片30.png

监控项: MariaDB_2: Size of database zabbix
图片31.png
注释:可以自定义图形的绘画风格、颜色等等。



至此,查看图形“Size of database zabbix image”就可以知道MariaDB的库zabbix有多大了:
图片32.png

图片33.png

图片34.png



######

给监控项“Size of database zabbix”创建一个触发器:(返回值大于20MB就告警)

触发器名称: Database zabbix is bigger than 20MB
严重性: 一般严重
表达式: {MariaDB_2:mysql.dbsize["localhost","3306","zabbix"].last()}>20M
图片35.png
注释:触发器的表达式要用到监控项中的键值。

本页拖下去:
图片36.png



给监控项“Size of database zabbix”创建触发器“Database zabbix is bigger than 20MB”后,对应图形“Size of database zabbix image”会自动发生改变:(多了一条告警线,多了一个触发器)
图片37.png

图片38.png



当监控项“Size of database zabbix”的返回值大于20MB时,仪表板会如下图显示:(显示对应触发器的名称)
Database zabbix is bigger than 20MB
图片39.png

图片40.png





######

再给监控项“Size of database zabbix”创建一个触发器:(返回值大于0.02GB就告警)
触发器名称: Database zabbix is bigger than 0.02GB
严重性: 严重
表达式: {MariaDB_2:mysql.dbsize["localhost","3306","zabbix"].last()}>0.02G
图片41.png
注释:触发器的表达式要用到监控项中的键值。

本页拖下去:
图片42.png



给监控项“Size of database zabbix”创建触发器“Database zabbix is bigger than 0.02GB”后,对应图形“Size of database zabbix image”会自动发生改变:(又多了一条告警线,又多了一个触发器)
图片43.png

图片44.png



当监控项“Size of database zabbix”的返回值大于0.02GB时,仪表板会如下图显示:(显示对应触发器的名称)
Database zabbix is bigger than 0.02GB
图片45.png

图片46.png





相关文章:
使用Navicat远程管理MySQL
Zabbix调用Python3脚本监控Linux下的MariaDB

Zabbix使用模板监控Linux下的MySQL
Zabbix使用模板监控Windows下的MariaDB

返回列表