Zabbix-5.4监控阿里云RDS MySQL之Zabbix_API脚本获取数据
概述场景
本文完整介绍ZABBIX使用阿里官方API接口获取阿里云RDS MySQL性能指标数据,通过阿里云API自动发现模板可以全面监控到RDS MySQL近四十多项指标值,为运维工程师、数据分析师、系统架构师诊断优化提供依据。随着Zabbix agent2的发布,Zabbix-5.4监控阿里云RDS似乎变得简单了许多,但是并不是所有的数据库都支持装插件,所以API这种方式依然有用,但从另外一个角度来看,如果公司没有专线前往公有云,那么数据库需要开启对外访问,隐藏了一些额外的安全风险,所以监控之前可以斟酌再三是否有必要,很多的公有云的自有监控也做的不错。
资源环境
1、阿里云ECS 4H8G(其他公共云服务器适用)
2、阿里云RDS MySQL-8.0.18
3、ZABBIX-5.4.0正式版
4、Python 2.7(必备依赖)
5、aliyun-python-sdk-core 2.3.5、aliyun-python-sdk-rds 2.1.4
一、阿里云ECS服务器环境准备
1、云服务器获取渠道
各大云商几乎都有免费7天-12月不等的试用云产品,如阿里云、百度云、腾讯智能云、亚马逊云、谷歌云、西数云等等,以及针对新手入云给出相应优惠,非本文重点不多赘述,可移步本站首页,下图
二、阿里云RDS MySQL数据库配置
1、进入云商官网,或点击下图进入云RDS MySQL版免费体验试用即可
三、安装ZABBIX
1、ZABBIX-5.4.0详细安装及赠送Grafana安装部署教程,请移步《Zabbix5.4+Grafana7.5源码部署教程》
四、ZABBIX与MySQL监控依赖环境配置
1.1、安装Python 2.7、Pip 21.1.2
##这里教大家一个委婉的方法安装Python 2.7,执行pip2,提示bash: pip2: 未找到命令...,神奇的发现Python 2.7作为依赖给安装了
[root@localhost opt]# pip2
bash: pip2: 未找到命令...
安装软件包“python2-pip”以提供命令“pip2”? [N/y] y
* 正在队列中等待...
* 装入软件包列表...
下列软件包必须安装:
python2-2.7.17-2.module_el8.3.0+478+7570e00c.x86_64 An interpreted, interactive, object-oriented programming language
python2-libs-2.7.17-2.module_el8.3.0+478+7570e00c.x86_64 Runtime libraries for Python 2
python2-pip-9.0.3-18.module_el8.3.0+478+7570e00c.noarch A tool for installing and managing Python 2 packages
python2-pip-wheel-9.0.3-18.module_el8.3.0+478+7570e00c.noarch The pip wheel
python2-setuptools-39.0.1-12.module_el8.3.0+478+7570e00c.noarch Easily build and distribute Python packages
python2-setuptools-wheel-39.0.1-12.module_el8.3.0+478+7570e00c.noarch The setuptools wheel
继续更改? [N/y] y
* 正在队列中等待...
* 正在等待认证...
* 正在队列中等待...
* 正在下载软件包...
* 正在请求数据...
* 正在测试更改...
* 正在安装软件包...
Usage:
pip <command> [options]
Commands:
install Install packages.
download Download packages.
uninstall Uninstall packages.
freeze Output installed packages in requirements format.
list List installed packages.
show Show information about installed packages.
check Verify installed packages have compatible dependencies.
search Search PyPI for packages.
wheel Build wheels from your requirements.
hash Compute hashes of package archives.
completion A helper command used for command completion.
help Show help for commands.
General Options:
-h, --help Show help.
--isolated Run pip in an isolated mode, ignoring environment variables and user configuration
-v, --verbose Give more output. Option is additive, and can be used up to 3 times.
-V, --version Show version and exit.
-q, --quiet Give less output. Option is additive, and can be used up to 3 times (corresponding to WARNING, ERROR, and CRITICAL logging levels).
--log <path> Path to a verbose appending log.
--proxy <proxy> Specify a proxy in the form [user:passwd@]proxy.server:port.
--retries <retries> Maximum number of retries each connection should attempt (default 5 times).
--timeout <sec> Set the socket timeout (default 15 seconds).
--exists-action <action> Default action when a path already exists: (s)witch, (i)gnore, (w)ipe, (b)ackup, (a)bort.
--trusted-host <hostname> Mark this host as trusted, even though it does not have valid or any HTTPS.
--cert <path> Path to alternate CA bundle.
--client-cert <path> Path to SSL client certificate, a single file containing the private key and the certificate in PEM format.
--cache-dir <dir> Store the cache data in <dir>.
--no-cache-dir Disable the cache.
--disable-pip-version-check
Don't periodically check PyPI to determine whether a new version of pip is available for download. Implied with --no-index.
[root@localhost bin]# pip -V
pip 21.1.2 from /usr/local/lib/python3.6/site-packages/pip (python 3.6)
[root@localhost bin]# python2.7 -V
Python 2.7.17
1.2、安装Python 2.7.17、Pip 9.0.3(常规方式安装、备选)
[root@localhost bin]# yum -y install epel-release
[root@localhost bin]# yum install python2 -y
Repository epel is listed more than once in the configuration
Last metadata expiration check: 0:00:20 ago on Wed 02 Jun 2021 05:55:13 PM CST.
Dependencies resolved.
===================================================================================================================================================
Package Architecture Version Repository Size
===================================================================================================================================================
Installing:
python2 x86_64 2.7.17-2.module_el8.3.0+478+7570e00c AppStream 109 k
Installing dependencies:python2 libs x86_64 2.7.17-2.module_el8.3.0+478+7570e00c AppStream 6.0 M
python2-pip-wheel noarch 9.0.3-18.module_el8.3.0+478+7570e00c AppStream 1.0 M
python2-setuptools-wheel noarch 39.0.1-12.module_el8.3.0+478+7570e00c AppStream 287 k
Installing weak dependencies:
python2-pip noarch 9.0.3-18.module_el8.3.0+478+7570e00c AppStream 1.7 M
python2-setuptools noarch 39.0.1-12.module_el8.3.0+478+7570e00c AppStream 642 k
Enabling module streams:
python27 2.7
Transaction Summary
===================================================================================================================================================
Install 6 Packages
Total download size: 9.7 M
Installed size: 37 M
Is this ok [y/N]: y
Downloading Packages:
(1/6): python2-2.7.17-2.module_el8.3.0+478+7570e00c.x86_64.rpm 735 kB/s | 109 kB 00:00
(2/6): python2-pip-9.0.3-18.module_el8.3.0+478+7570e00c.noarch.rpm 6.7 MB/s | 1.7 MB 00:00
(3/6): python2-pip-wheel-9.0.3-18.module_el8.3.0+478+7570e00c.noarch.rpm 7.1 MB/s | 1.0 MB 00:00
(4/6): python2-setuptools-39.0.1-12.module_el8.3.0+478+7570e00c.noarch.rpm 12 MB/s | 642 kB 00:00
(5/6): python2-setuptools-wheel-39.0.1-12.module_el8.3.0+478+7570e00c.noarch.rpm 5.0 MB/s | 287 kB 00:00
(6/6): python2-libs-2.7.17-2.module_el8.3.0+478+7570e00c.x86_64.rpm 12 MB/s | 6.0 MB 00:00
---------------------------------------------------------------------------------------------------------------------------------------------------
Total 19 MB/s | 9.7 MB 00:00
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : python2-setuptools-wheel-39.0.1-12.module_el8.3.0+478+7570e00c.noarch 1/6
Installing : python2-pip-wheel-9.0.3-18.module_el8.3.0+478+7570e00c.noarch 2/6
Installing : python2-libs-2.7.17-2.module_el8.3.0+478+7570e00c.x86_64 3/6
Installing : python2-pip-9.0.3-18.module_el8.3.0+478+7570e00c.noarch 4/6
Installing : python2-setuptools-39.0.1-12.module_el8.3.0+478+7570e00c.noarch 5/6
Installing : python2-2.7.17-2.module_el8.3.0+478+7570e00c.x86_64 6/6
Running scriptlet: python2-2.7.17-2.module_el8.3.0+478+7570e00c.x86_64 6/6
Verifying : python2-2.7.17-2.module_el8.3.0+478+7570e00c.x86_64 1/6
Verifying : python2-libs-2.7.17-2.module_el8.3.0+478+7570e00c.x86_64 2/6
Verifying : python2-pip-9.0.3-18.module_el8.3.0+478+7570e00c.noarch 3/6
Verifying : python2-pip-wheel-9.0.3-18.module_el8.3.0+478+7570e00c.noarch 4/6
Verifying : python2-setuptools-39.0.1-12.module_el8.3.0+478+7570e00c.noarch 5/6
Verifying : python2-setuptools-wheel-39.0.1-12.module_el8.3.0+478+7570e00c.noarch 6/6
Installed:
python2-2.7.17-2.module_el8.3.0+478+7570e00c.x86_64 python2-pip-9.0.3-18.module_el8.3.0+478+7570e00c.noarch
python2-setuptools-39.0.1-12.module_el8.3.0+478+7570e00c.noarch python2-libs-2.7.17-2.module_el8.3.0+478+7570e00c.x86_64
python2-pip-wheel-9.0.3-18.module_el8.3.0+478+7570e00c.noarch python2-setuptools-wheel-39.0.1-12.module_el8.3.0+478+7570e00c.noarch
Complete!
2、aliyun-python-sdk-core 2.3.5与aliyun-python-sdk-rds 2.1.4模块安装
[root@localhost bin]# /usr/bin/env pip2.7 install aliyun-python-sdk-core==2.3.5 aliyun-python-sdk-rds==2.1.4 datetime
WARNING: Running pip install with root privileges is generally not a good idea. Try `pip2.7 install --user` instead.
Collecting aliyun-python-sdk-core==2.3.5
Downloading https://pypi.doubanio.com/packages/dd/5a/04e4aad6271a992bcd371a41295b8467433721deae463377b2245b2c6f73/aliyun-python-sdk-core-2.3.5.tar.gz
Collecting aliyun-python-sdk-rds==2.1.4
Downloading https://pypi.doubanio.com/packages/85/77/839e36f2add182a6855bca473ae28e52d3f8e20e7de603af8b452c62f9e5/aliyun-python-sdk-rds-2.1.4.tar.gz
Collecting datetime
Downloading https://pypi.doubanio.com/packages/73/22/a5297f3a1f92468cc737f8ce7ba6e5f245fcfafeae810ba37bd1039ea01c/DateTime-4.3-py2.py3-none-any.whl (60kB)
100% |████████████████████████████████| 61kB 442kB/s
Collecting zope.interface (from datetime)
Downloading https://pypi.doubanio.com/packages/47/55/20205df173d5c544a0002ee4b8f3aa862e3f1e2a0994a76ceed1587017f2/zope.interface-5.4.0-cp27-cp27mu-manylinux1_x86_64.whl (247kB)
100% |████████████████████████████████| 256kB 2.5MB/s
Collecting pytz (from datetime)
Downloading https://pypi.doubanio.com/packages/70/94/784178ca5dd892a98f113cdd923372024dc04b8d40abe77ca76b5fb90ca6/pytz-2021.1-py2.py3-none-any.whl (510kB)
100% |████████████████████████████████| 512kB 84.2MB/s
Requirement already satisfied: setuptools in /usr/lib/python2.7/site-packages (from zope.interface->datetime)
Installing collected packages: aliyun-python-sdk-core, aliyun-python-sdk-rds, zope.interface, pytz, datetime
Running setup.py install for aliyun-python-sdk-core ... done
Running setup.py install for aliyun-python-sdk-rds ... done
Successfully installed aliyun-python-sdk-core-2.3.5 aliyun-python-sdk-rds-2.1.4 datetime-4.3 pytz-2021.1 zope.interface-5.4.0
##
校验模块是都已安装成功
[root@localhost bin]# pip2 list
DEPRECATION: The default format will switch to columns in the future. You can use --format=(legacy|columns) (or define a format=(legacy|columns) in your pip.conf under the [list] section) to disable this warning.
aliyun-python-sdk-core (2.3.5) //已存在,ok
aliyun-python-sdk-rds (2.1.4) //已存在,ok
DateTime (4.3)
pip (9.0.3)
pytz (2021.1)
setuptools (39.0.1)
zope.interface (5.4.0)
五、ZABBIX与MySQL监控步骤
1、下载阿里云RDS MYSQL官方API文件
仓库拉取地址:git clone https://github.com/XWJR-Ops/zabbix-RDS-monitor
直接下载地址:https://codeload.github.com/XWJR-Ops/zabbix-RDS-monitor/zip/refs/heads/master
2、配置阿里云RDS API相关信息
从阿里云控制台获取 AccessKey ,并修改脚本(check_rds.py、discovery_rds.py)中的 ID 与 Secret,修改区域 RegionId(MySQL所在区域)
3、上传check_rds.py、discovery_rds.py至指定目录,并修改zabbix_agent.conf配置
##我这里使用的是zabbix_agent2,也适用zabbix_agentd,上传脚本至/etc/zabbix/zabbix_agent2.d/下并赋予可执行权限
[root@it java-core]# cd /etc/zabbix/zabbix_agent2.d/
[root@it zabbix_agent2.d]# chmod +x check_rds.py discovery_rds.py
[root@it zabbix_agent2.d]# chown -R zabbix:zabbix /etc/zabbix/zabbix_agent2.d/
##启用agent2的conf索引
[root@it zabbix_agent2.d]# vim /etc/zabbix/zabbix_agent2.conf
Include=/etc/zabbix/zabbix_agent2.d/*.conf //取消#,启用
4、创建阿里云RDS MySQL脚本索引文件,并上传至/etc/zabbix/zabbix_agent2.d/下
[root@it zabbix_agent2.d]# vim /etc/zabbix/zabbix_agent2.d/url.conf
##添加如下内容
#rds
UserParameter=rds.discovery,/usr/bin/env python2.7 /etc/zabbix/zabbix_agent2.d/discovery_rds.py
UserParameter=check.rds[*],/usr/bin/env python2.7 /etc/zabbix/zabbix_agent2.d/check_rds.py $1 $2 $3
5、重启ZABBIX的Agent服务
[root@it zabbix_agent2.d]# systemctl restart zabbix-agent2.service
6、ZABBIX前端被监控主机配置
将下载的阿里云API包zabbix-RDS-monitor目录下的zbx_AliyunRds_templates.xml模板导入到zabbix系统中
ZABBIX前端关联主机,并校验监控结果
六、ZABBIX监控RDS MySQL常见BUG报错解决
1、报错Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-aozns17o/cryptography/
##解决
root@it ~]# pip3 install --upgrade pip
WARNING: Running pip install with root privileges is generally not a good idea. Try `pip3 install --user` instead.
Collecting pip
Downloading https://pypi.doubanio.com/packages/cd/82/04e9aaf603fdbaecb4323b9e723f13c92c245f6ab2902195c53987848c78/pip-21.1.2-py3-none-any.whl (1.5MB)
100% |████████████████████████████████| 1.6MB 56.2MB/s
Installing collected packages: pip
Successfully installed pip-21.1.2