笺注:
被监控主机的Agent的安装可参考 Zabbix使用Agent监控Windows2008R2
MySQL的安装可参考 Windows2008R2_安装MySQL5.5
被监控主机配置MySQL数据库:( 使用数据库管理员root@localhost )
mysql -u"root" -p"123" -h"localhost"
注释:
-h"localhost" 是可以省略的;
查看当前登录的数据库用户:
mysql> Select user();
+----------------+
| user() |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)
mysql>
再授权数据库用户zbx_monitor@localhost(数据库用户可以不要密码的),只能本地登录,对所有的库有完全控制的权限:
mysql> grant all on *.* to zbx_monitor@localhost;
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql>
查看本地数据库用户zbx_monitor@localhost的权限:
mysql> show grants for zbx_monitor@localhost;
+----------------------------------------------------------+
| Grants for zbx_monitor@localhost |
+----------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'zbx_monitor'@'localhost' |
+----------------------------------------------------------+
1 row in set (0.00 sec)
mysql>
查看所有数据库用户及其主机信息:
mysql> select user,host from mysql.user;
+-------------+-----------+
| user | host |
+-------------+-----------+
| root | localhost |
| zbx_monitor | localhost |
+-------------+-----------+
2 rows in set (0.00 sec)
使用本地数据库用户zbx_monitor@localhost登录MySQL数据库:
C:\Users\Administrator>mysql -u"zbx_monitor"
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 109
Server version: 5.5.45 MySQL Community Server (GPL)
Copyright (c) 2000, 2015, 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>
查看当前登录的数据库用户:
mysql> Select user();
+-----------------------+
| user() |
+-----------------------+
| zbx_monitor@localhost |
+-----------------------+
1 row in set (0.02 sec)
mysql>
#######
#######
修改被监控主机的Agent配置文件:
C:\Program Files\Zabbix Agent\zabbix_agentd.conf
修改为:
UnsafeUserParameters=1
接着插入:
- UserParameter=mysql.ping[*], mysqladmin -u"zbx_monitor" -h"$1" -P"$2" ping
- UserParameter=mysql.get_status_variables[*], mysql -u"zbx_monitor" -h"$1" -P"$2" -sNX -e "show global status"
- UserParameter=mysql.version[*], mysqladmin -u"zbx_monitor" -s -h"$1" -P"$2" version
- UserParameter=mysql.db.discovery[*], mysql -u"zbx_monitor" -h"$1" -P"$2" -sN -e "show databases"
- 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'"
- UserParameter=mysql.replication.discovery[*], mysql -u"zbx_monitor" -h"$1" -P"$2" -sNX -e "show slave status"
- UserParameter=mysql.slave_status[*], mysql -u"zbx_monitor" -h"$1" -P"$2" -sNX -e "show slave status"
复制代码
如下图:
保存好配置文件后,重启Zabbix Agent服务:
#######
在被监控主机上使用CMD命令行进行本地测试:
使用命令mysqladmin检测MySQL服务有没有在运行中:(在运行时)
mysqladmin -u"zbx_monitor" ping
MySQL服务没有运行时:
使用命令mysqladmin查看MySQL的版本信息等等:
C:\Users\Administrator>mysqladmin -u"zbx_monitor" -s version
mysqladmin Ver 8.42 Distrib 5.5.45, for Win64 on x86
Copyright (c) 2000, 2015, 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.
Server version 5.5.45
Protocol version 10
Connection localhost via TCP/IP
TCP port 3306
Uptime: 19 min 17 sec
Threads: 2 Questions: 117 Slow queries: 0 Opens: 65 Flush tables: 1 Open tables: 58 Queries per second avg: 0.101
C:\Users\Administrator>
注释:这里可以看到目前MySQL服务运行了19分17秒;
使用命令mysql显示MySQL里的全部库:
C:\Users\Administrator>mysql -u"zbx_monitor" -sN -e "Show databases"
information_schema
discuz
mysql
performance_schema
test
C:\Users\Administrator>
数据库用户有权限的话,都可以使用这些命令:
C:\Users\Administrator>mysql -u"root" -p"123" -e "Show databases"
+--------------------+
| Database |
+--------------------+
| information_schema |
| discuz |
| mysql |
| performance_schema |
| test |
+--------------------+
C:\Users\Administrator>
备注:这相当于在被监控主机上进行本地监控,所以被监控主机无需打开防火墙的TCP 3306端口。
##############
##############
在Zabbix服务器测试,验证能否获取被监控主机(192.168.168.148)的键值:
[root@centos8 ~]# /usr/local/zabbix/bin/zabbix_get -s 192.168.168.148 -k mysql.ping["localhost","3306"]
ZBX_NOTSUPPORTED: Timeout while executing a shell script.
[root@centos8 ~]#
解决方法:
修改被监控主机的Agent配置文件:
C:\Program Files\Zabbix Agent\zabbix_agentd.conf
修改为 Timeout=30
保存好配置文件后,重启Zabbix Agent服务:
检测远程MySQL服务有没有在运行中:(在运行时)
[root@centos8 ~]# /usr/local/zabbix/bin/zabbix_get -s 192.168.168.148 -k mysql.ping["localhost","3306"]
mysqld is alive
[root@centos8 ~]#
检测远程MySQL服务有没有在运行中:(没有运行时)
[root@centos8 ~]# /usr/local/zabbix/bin/zabbix_get -s 192.168.168.148 -k mysql.ping["localhost","3306"]
mysqladmin: connect to server at 'localhost' failed
error: 'Can't connect to MySQL server on 'localhost' (10061)'
Check that mysqld is running on localhost and that the port is 3306.
You can check this by doing 'telnet localhost 3306'
[root@centos8 ~]#
输出远程MySQL里的全部库:
[root@centos8 ~]# /usr/local/zabbix/bin/zabbix_get -s 192.168.168.148 -k mysql.db.discovery["localhost","3306"]
information_schema
discuz
mysql
performance_schema
test
[root@centos8 ~]#
##################Zabbix添加被监控主机:
配置》主机》创建主机 (主机名称无需与被监控主机的真实主机名一致;群组可以随便选;IP地址那里要输入被监控主机的IP地址;端口为10050)
注释:
使用agent代理程序的接口。
使用 TCP 10050 端口。
主机添加成功:
给主机添加模板:( Template DB MySQL )
注释:模板Template DB MySQL 是Zabbix自带的。
模板添加成功:
查看主机MySQL_2继承模板的宏:
###
查看模板Template DB MySQL自带图形:(要等一段时间才会出现)
监测》图形:
查看主机MySQL_2的图形MySQL bandwidth:
注释:“还剩30分钟”是指从此刻开始算起,之前的30分钟。
######
模板Template DB MySQL自带的触发器:
名称:MySQL: Server has slow queries (over {$MYSQL.SLOW_QUERIES.MAX.WARN} for 5m)
表达式:{MySQL_2:mysql.slow_queries.rate.min(5m)}>{$MYSQL.SLOW_QUERIES.MAX.WARN}
注释: {$MYSQL.SLOW_QUERIES.MAX.WARN} 是宏,继承于模板Template DB MySQL,默认值为 3
######
模板Template DB MySQL自带的监控MySQL服务状态的监控项:
名称: Availability: MySQL status
键值: mysql.ping["{$MYSQL.HOST}","{$MYSQL.PORT}"]
本页拖下去:(默认已启用)
模板Template DB MySQL自带的监控MySQL服务状态的触发器:
名称: MySQL: Service is down
表达式:{MySQL_2:mysql.ping["{$MYSQL.HOST}","{$MYSQL.PORT}"].last()}=0
注释:触发器的表达式要用到监控项中的键值。
本页拖下去:(默认已启用)
假如被监控主机的MySQL服务没有在运行了,仪表板会如下图显示:(显示对应触发器的名称)
MySQL: Service is down
######
模板Template DB MySQL自带的监控MySQL服务运行时长的监控项:
名称: Info: Uptime
键值: mysql.uptime
模板Template DB MySQL自带的监控MySQL服务运行时长的触发器:
名称: MySQL: Service has been restarted (uptime < 10m)
表达式: {MySQL_2:mysql.uptime.last()}<10m
注释:触发器的表达式要用到监控项中的键值。
假如被监控主机的MySQL服务重启了,仪表板会如下图显示:(显示对应触发器的名称)
MySQL: Service has been restarted (uptime < 10m)
注释:这告警10分钟后自动消失。
######
模板Template DB MySQL有自动检查MySQL各个库的大小的监控项:
监控项名称: Info: Size of database mysql
键值: mysql.dbsize["{$MYSQL.HOST}","{$MYSQL.PORT}","mysql"]
在Zabbix服务器上可以使用命令行获取到库“mysql”的大小:(返回值以字节为单位)
[root@centos8 ~]# /usr/local/zabbix/bin/zabbix_get -s 192.168.168.148 -k mysql.dbsize["localhost","3306","mysql"]
701700
[root@centos8 ~]#
注释:
键值的格式要改变一下,不要使用变量;
这相当于在被监控主机上进行本地监控,所以应该使用"localhost"
######
监控项名称: Info: Size of database discuz
键值: mysql.dbsize["{$MYSQL.HOST}","{$MYSQL.PORT}","discuz"]
在Zabbix服务器上可以使用命令行获取到库“discuz”的大小:(返回值以字节为单位)
[root@centos8 ~]# /usr/local/zabbix/bin/zabbix_get -s 192.168.168.148 -k mysql.dbsize["localhost","3306","discuz"]
409499
[root@centos8 ~]#
笺注:根据监控项“Info: Size of database discuz”的键值格式,在创建触发器时会失败。
重新创建一个监控项:(键值中不要使用变量(宏))
自定义名称: Size of database discuz
自定义键值: mysql.dbsize["localhost","3306","discuz"]
信息类型: 数字(无正负)
备注:其他地方保持默认,点击底下的“添加”按键。
给监控项(Size of database discuz)创建图形:
监控项: MySQL_2: Size of database discuz
至此,查看图形“Size of database discuz image”就可以知道MySQL的库discuz有多大了:
######
给监控项(Size of database discuz)创建触发器:(返回值大于200KB就告警)
触发器名称: database discuz is bigger than 200KB
严重性: 严重
表达式: {MySQL_2:mysql.dbsize["localhost","3306","discuz"].last()}>200K
注释:触发器的表达式要用到监控项中的键值。
本页拖下去:
给监控项“Size of database discuz”创建触发器后,对应图形“Size of database discuz image”会自动发生改变:(多了一条告警线,多了一个触发器)
当监控项“Size of database discuz”的返回值大于200KB时,仪表板会如下图显示:(显示对应触发器的名称)
database discuz is bigger than 200KB
######
######
在Zabbix服务器上编写Shell脚本进行监控:
[root@centos8 ~]# mkdir -p /script
[root@centos8 ~]#
[root@centos8 ~]# cat /script/xx.sh
#!/bin/bash
Key_1=$(/usr/local/zabbix/bin/zabbix_get -s 192.168.168.148 -k mysql.dbsize["localhost","3306","discuz"])
echo $Key_1
Key_2=`expr $Key_1 / 1024`
echo "----------"
echo $Key_2 "KB"
脚本运行的结果:
[root@centos8 ~]# bash /script/xx.sh
409499
----------
399 KB
[root@centos8 ~]#
备注:相除后,整数部分是多少,就是多少,不是四舍五入!
相关文章:
Zabbix使用模板监控Linux下的MySQL
Zabbix使用模板监控Windows下的MariaDB |