blog.zhuohua.store's Archiver

admin 发表于 2020-7-18 17:15

Zabbix创建模板监控Linux下的MariaDB

笺注:这是在 [url=http://blog.zhuohua.store/viewthread.php?tid=256&extra=page%3D1]CentOS8编译安装Zabbix4.4.5[/url] 的基础上进行的,监控本机的MariaDB


配置本机的MariaDB数据库:( 使用数据库管理员root@localhost )
[root@centos8 ~]# mysql -u"[color=Blue]root[/color]" -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)]> [color=Blue]Select user();[/color]
+----------------+
| user()         |
+----------------+
| [color=Purple]root@localhost[/color] |
+----------------+
1 row in set (0.000 sec)

MariaDB [(none)]>



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

MariaDB [(none)]> grant all on *.* to zbx_monitor@localhost;
Query OK, 0 rows affected (0.001 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' IDENTIFIED BY PASSWORD '*242E46A1E8D30FE06F7CE37B55BFC25BA981D70C' |
+-----------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.001 sec)

MariaDB [(none)]>



显示所有库:
[root@centos8 ~]# mysql -u"zbx_monitor" -p"168" -e "show databases;"
[color=Purple]+--------------------+
| Database           |
+--------------------+
| db1                |
| information_schema |
| mysql              |
| performance_schema |
| zabbix             |
+--------------------+[/color]
[root@centos8 ~]#


[root@centos8 ~]# mysql -u"zbx_monitor" -p"168" [color=DarkRed]-sN[/color] -e "show databases;"
[color=Purple]db1
information_schema
mysql
performance_schema
zabbix[/color]
[root@centos8 ~]#



显示所有库的总大小:(以字节为单位)
[root@centos8 ~]# mysql -u"zbx_monitor" -p"168" -e "select concat(sum(DATA_LENGTH)) as data from information_schema.TABLES;"
[color=Purple]+----------+
| data     |
+----------+
| 10971378 |
+----------+[/color]
[root@centos8 ~]#


[root@centos8 ~]# mysql -u"zbx_monitor" -p"168" -e "select concat(sum(DATA_LENGTH)) as data from information_schema.TABLES;" |awk '{print $1}'|tail -1
[color=Purple]10971378[/color]
[root@centos8 ~]#


[root@centos8 ~]# mysql -u"zbx_monitor" -p"168" [color=DarkRed]-sN[/color] -e "select concat(sum(DATA_LENGTH)) as data from information_schema.TABLES;"
[color=Purple]10971378[/color]
[root@centos8 ~]#



显示库zabbix的大小:(以字节为单位)
[root@centos8 ~]# mysql -u"zbx_monitor" -p"168" -e "select concat(sum(DATA_LENGTH)) as data from information_schema.TABLES where table_schema = '[color=Blue]zabbix[/color]';"
[color=Purple]+---------+
| data    |
+---------+
| 9650176 |
+---------+[/color]
[root@centos8 ~]#


[root@centos8 ~]# mysql -u"zbx_monitor" -p"168" -e "select concat(sum(DATA_LENGTH)) as data from information_schema.TABLES where table_schema = '[color=Blue]zabbix[/color]';" |awk '{print $1}'|tail -1
[color=Purple]9650176[/color]
[root@centos8 ~]#


[root@centos8 ~]# mysql -u"zbx_monitor" -p"168" [color=DarkRed]-sN[/color] -e "select concat(sum(DATA_LENGTH)) as data from information_schema.TABLES where table_schema = '[color=Blue]zabbix[/color]';"
[color=Purple]9650176[/color]
[root@centos8 ~]#



显示库zabbix的表users的大小:(以字节为单位)
[root@centos8 ~]# mysql -u"zbx_monitor" -p"168" -e "select concat(sum(DATA_LENGTH))as data from information_schema.TABLES where table_schema='[color=Blue]zabbix[/color]' and table_name='[color=DarkRed]users[/color]';"
[color=Purple]+-------+
| data  |
+-------+
| 16384 |
+-------+[/color]
[root@centos8 ~]#


[root@centos8 ~]# mysql -u"zbx_monitor" -p"168" -e "select concat(sum(DATA_LENGTH))as data from information_schema.TABLES where table_schema='[color=Blue]zabbix[/color]' and table_name='[color=DarkRed]users[/color]';" |awk '{print $1}'|tail -1
[color=Purple]16384[/color]
[root@centos8 ~]#


[root@centos8 ~]# mysql -u"zbx_monitor" -p"168" [color=DarkRed]-sN[/color] -e "select concat(sum(DATA_LENGTH))as data from information_schema.TABLES where table_schema='[color=Blue]zabbix[/color]' and table_name='[color=DarkRed]users[/color]';"
[color=Purple]16384[/color]
[root@centos8 ~]#





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

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

# UnsafeUserParameters=0
修改为:(启用该功能)
UnsafeUserParameters=[color=Blue]1[/color]

接着插入:
[code]UserParameter=command_0,mysql -u"zbx_monitor" -p"168" -sN -e "show databases;"

UserParameter=command_1,mysql -u"zbx_monitor" -p"168" -sN -e "select concat(sum(DATA_LENGTH)) as data from information_schema.TABLES;"

UserParameter=command_2,mysql -u"zbx_monitor" -p"168" -sN -e "select concat(sum(DATA_LENGTH)) as data from information_schema.TABLES where table_schema = 'zabbix';"

UserParameter=command_3[*],mysql -u"zbx_monitor" -p"168" -sN -e "select concat(sum(DATA_LENGTH)) as data from information_schema.TABLES where table_schema = '$1';"

UserParameter=command_4[*],mysql -u"zbx_monitor" -p"168" -sN -e "select concat(sum(DATA_LENGTH))as data from information_schema.TABLES where table_schema='$1' and table_name='$2';"[/code]

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



######

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

显示所有库:
[root@centos8 ~]# /usr/local/zabbix/bin/zabbix_get -s 127.0.0.1 -k [color=DarkRed]command_0[/color]
[color=Purple]db1
information_schema
mysql
performance_schema
zabbix[/color]
[root@centos8 ~]#


显示所有库的总大小:(以字节为单位)
[root@centos8 ~]# /usr/local/zabbix/bin/zabbix_get -s 127.0.0.1 -k [color=DarkRed]command_1[/color]
[color=Purple]10971378[/color]
[root@centos8 ~]#


显示库zabbix的大小:(以字节为单位)
[root@centos8 ~]# /usr/local/zabbix/bin/zabbix_get -s 127.0.0.1 -k [color=DarkRed]command_2[/color]
[color=Purple]9650176[/color]
[root@centos8 ~]#


显示库zabbix的大小:(以字节为单位)
[root@centos8 ~]# /usr/local/zabbix/bin/zabbix_get -s 127.0.0.1 -k [color=DarkRed]command_3["zabbix"][/color]
[color=Purple]9650176[/color]
[root@centos8 ~]#


显示库db1的大小:(以字节为单位)
[root@centos8 ~]# /usr/local/zabbix/bin/zabbix_get -s 127.0.0.1 -k [color=DarkRed]command_3["db1"][/color]
[color=Purple]374779[/color]
[root@centos8 ~]#


显示库zabbix的表users的大小:(以字节为单位)
[root@centos8 ~]# /usr/local/zabbix/bin/zabbix_get -s 127.0.0.1 -k [color=DarkRed]command_4["zabbix","users"][/color]
[color=Purple]16384[/color]
[root@centos8 ~]#





##################Zabbix创建模板:

配置》模板》创建模板:(群组可以随便选)
[attach]20576[/attach]



在模板Template DB MariaDB_2中创建监控项:
监控项名称: MariaDB command_1
键值: [color=DarkRed]command_1[/color]
信息类型: 数字(无正负)
[attach]20577[/attach]
备注:其他地方保持默认,点击底下的“添加”按键。



在模板Template DB MariaDB_2中创建监控项:
监控项名称: MariaDB command_2
键值: [color=DarkRed]command_2[/color]
信息类型: 数字(无正负)
[attach]20578[/attach]
备注:
单位留空时,结果在单位换算时会每超过1000就除以1000,保留两位小数,是四舍五入;
在监控项对应的图形中可以看到结果;



在模板Template DB MariaDB_2中创建监控项:
监控项名称: MariaDB command_3["zabbix"]
键值: [color=DarkRed]command_3["zabbix"][/color]
信息类型: 数字(无正负)
单位: B
[attach]20579[/attach]
备注:
使用单位“B”时,结果在单位换算时会每超过1024就除以1024,保留两位小数,是四舍五入;



在模板Template DB MariaDB_2中创建监控项:
监控项名称: MariaDB command_3["db1"]
键值: [color=DarkRed]command_3["db1"][/color]
信息类型: 数字(无正负)
单位: Bytes
[attach]20580[/attach]
备注:
使用单位“Bytes”时,结果在单位换算时会每超过1000就除以1000,保留两位小数,是四舍五入;



在模板Template DB MariaDB_2中创建监控项:
监控项名称: MariaDB command_4["zabbix","users"]
键值: [color=DarkRed]command_4["zabbix","users"][/color]
信息类型: 数字(无正负)
[attach]20581[/attach]
本页拖下去:
[attach]20582[/attach]


至此,模板Template DB MariaDB_2有了5个监控项:
[attach]20583[/attach]





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

在模板Template DB MariaDB_2中,给监控项“MariaDB command_1”创建图形:
[attach]20584[/attach]
监控项:
[attach]20585[/attach]



在模板Template DB MariaDB_2中,给监控项“MariaDB command_2”创建图形:
[attach]20586[/attach]
监控项:
[attach]20587[/attach]



在模板Template DB MariaDB_2中,给监控项“MariaDB command_3["zabbix"]”创建图形:
[attach]20588[/attach]
监控项:
[attach]20589[/attach]



在模板Template DB MariaDB_2中,给监控项“MariaDB command_3["db1"]”创建图形:
[attach]20590[/attach]
监控项:
[attach]20591[/attach]



在模板Template DB MariaDB_2中,
给监控项“MariaDB command_4["zabbix","users"]”创建图形:
[attach]20592[/attach]
监控项:
[attach]20593[/attach]


至此,模板Template DB MariaDB_2有了5个图形:
[attach]20594[/attach]





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

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


主机MariaDB_2添加成功:
[attach]20596[/attach]


给主机MariaDB_2添加模板:( Template DB MariaDB_2  )
[attach]20597[/attach]


模板Template DB MariaDB_2添加成功:
[attach]20598[/attach]


[size=4]笺注:主机会继承所使用的模板的监控项、触发器、图形等等。[/size]



######

监测》图形:

查看主机MariaDB_2的图形“MariaDB command_1 image”:
[attach]20599[/attach]

[attach]20600[/attach]

[attach]20601[/attach]



查看主机MariaDB_2的图形“MariaDB command_2 image”:
[attach]20602[/attach]

[attach]20603[/attach]

[attach]20604[/attach]



查看主机MariaDB_2的图形“MariaDB command_3["zabbix"] image”:
[attach]20605[/attach]

[attach]20606[/attach]

[attach]20607[/attach]



查看主机MariaDB_2的图形“MariaDB command_3["db1"] image”:
[attach]20608[/attach]

[attach]20609[/attach]

[attach]20610[/attach]



查看主机MariaDB_2的图形“MariaDB command_4["zabbix","users"] image”:
[attach]20611[/attach]

[attach]20612[/attach]

[attach]20613[/attach]





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

在模板Template DB MariaDB_2中,
给监控项“MariaDB command_3["zabbix"]”创建一个触发器:(返回值大于9.5MB就告警)

触发器名称: [color=Blue]Database zabbix is bigger than 9.5MB[/color]
严重性: 警告
[attach]20614[/attach]

插入表达式:( 监控项 [color=Blue]Template DB MariaDB_2: MariaDB command_3["zabbix"][/color] )
[attach]20615[/attach]

自动生成的表达式:(触发器的表达式要用到监控项中的键值)
{Template DB MariaDB_2:[color=DarkRed]command_3["zabbix"][/color].last()}>9.5M
[attach]20616[/attach]

本页拖下去:
[attach]20617[/attach]


至此,模板Template DB MariaDB_2有了1个触发器:
[attach]20618[/attach]



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

使用了模板Template DB MariaDB_2的主机会立即继承新的触发器:
[attach]20619[/attach]

[attach]20620[/attach]


主机MariaDB_2的图形“MariaDB command_3["zabbix"] image”会自动发生改变:(多了一条告警线,多了一个触发器)
[attach]20621[/attach]

[attach]20622[/attach]



主机MariaDB_2的监控项“MariaDB command_3["zabbix"]”的返回值大于9.5MB
时,仪表板会如下图显示:(显示对应触发器的名称)
[color=Blue]Database zabbix is bigger than 9.5MB[/color]
[attach]20623[/attach]

[attach]20624[/attach]



######

在主机MariaDB_2中禁用触发器:
[attach]20625[/attach]

笺注:触发器被禁用后,仪表板的告警会自动消失,在主机MariaDB_2的图形“MariaDB command_3["zabbix"] image”中也看不到这个触发器了。





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

在模板Template DB MariaDB_2的监控项中更改“单位”:
监控项名称: MariaDB command_2
键值: command_2
信息类型: 数字(无正负)
单位: [color=Blue]uptime[/color]
[attach]20626[/attach]
备注:
现在假设返回值是时间“秒”;
uptime - 转换成 [color=DarkRed]hh:mm:ss [/color]
或 [color=DarkRed]天,hh:mm:ss[/color]

再次,查看主机MariaDB_2的图形“MariaDB command_2 image”:
[attach]20627[/attach]

[attach]20628[/attach]

[attach]20629[/attach]





相关文章:
[url=http://blog.zhuohua.store/viewthread.php?tid=231&page=1&extra=#pid234]使用Navicat远程管理MySQL[/url]
[url=http://blog.zhuohua.store/viewthread.php?tid=336&page=1&extra=#pid411]Zabbix创建模板监控Windows下的MariaDB[/url]

页: [1]

Powered by Discuz! Archiver 7.2  © 2001-2009 Comsenz Inc.