在数字化大背景下,数据是重要的生产资料,这些数据存放在哪里,如何保障数据安全是所有企业都要考虑的事情。华为云RDS凭借安全可靠,可根据业务规模动态扩容的特性,受到越来越多中小企业的青睐,对华为云RDS监控的需求也随之增长。本文将介绍如何使用Zabbix监控华为云RDS。

一、思路
•通过华为云RDS实例列表接口获取RDS实例,用于资源的自动发现

•通过华为云CES获取监控数据准

二、准备工作
•用于访问华为云的AK/SK
在这里插入图片描述
•python3环境

•用于调用华为云api的相关模块依赖 https://github.com/huaweicloud/huaweicloud-sdk-python-v3

三、编写实例自动发现脚本
#!/bin/python3

coding: utf-8

from huaweicloudsdkcore.auth.credentials import BasicCredentials
from huaweicloudsdkrds.v3.region.rds_region import RdsRegion
from huaweicloudsdkcore.exceptions import exceptions
from huaweicloudsdkrds.v3 import *
import json
if name == “main”:
#这里填访问密钥
ak = “”
sk = “”

credentials = BasicCredentials(ak, sk) \

client = RdsClient.new_builder() \
    .with_credentials(credentials) \
    .with_region(RdsRegion.value_of("cn-north-4")) \
    .build()

try:
    request = ListInstancesRequest()
    response = client.list_instances(request)
    response = json.loads(str(response))
    #拼接成用于自动发现的json数据,id为RDS的实例ID,name为RDS的名称
    r = []
    for i in response['instances']:
          buf = {'{#INSTANCE}':i['id'],'{#INSTANCE_NANME}':i['name']}
          r.append(buf)
    print(json.dumps({"data":r}))

except exceptions.ClientRequestException as e:
    print(e.status_code)
    print(e.request_id)
    print(e.error_code)
    print(e.error_msg)

四、编写监控数据获取脚本
#!/bin/python3

coding: utf-8

from huaweicloudsdkcore.auth.credentials import BasicCredentials
from huaweicloudsdkces.v1.region.ces_region import CesRegion
from huaweicloudsdkcore.exceptions import exceptions
from huaweicloudsdkces.v1 import *
import json
import time
import sys
#传入的监控项
key = sys.argv[1]
#请求时间戳
from_time = int(round(time.time() * 1000)-300000)
to_time = int(round(time.time() * 1000))
if name == “main”:
#这里填访问密钥
ak = “”
sk = “”

credentials = BasicCredentials(ak, sk) \

client = CesClient.new_builder() \
    .with_credentials(credentials) \
    .with_region(CesRegion.value_of("cn-north-4")) \
    .build()

try:
    request = BatchListMetricDataRequest()
    listDimensionsMetrics = [
        MetricsDimension(
            name="rds_cluster_id",
            #这里是实例ID,可改为变量,模板宏即{HOST.HOST}
            value=""
        )
    ]
    listMetricsbody = [
        MetricInfo(
            namespace="SYS.RDS",
            metric_name=key,
            dimensions=listDimensionsMetrics
        )
    ]
    request.body = BatchListMetricDataRequestBody(
        to=to_time,
        _from=from_time,
        filter="max",
        period="1",
        metrics=listMetricsbody
    )
    response = client.batch_list_metric_data(request)
    res = json.loads(str(response))
    instance = json.dumps(res)
    instances = json.loads(instance)['metrics']
    for i in instances:
        #取最新的一个值
          print(i['datapoints'][0]['max'])
          #print(i)

except exceptions.ClientRequestException as e:
    print(0)
    #print(e.request_id)
    #print(e.error_code)
    #print(e.error_msg)

五、新增模板
•新增自动发现规则,用于实例自动发现
在这里插入图片描述
•自动发现规则,链接监控数据获取模板
在这里插入图片描述
在这里插入图片描述
六、验证
•验证是否可以正常发现实例

新增主机,链接华为云RDS自动扫描模板后,点自动发现规则,可以正常获取到数据
在这里插入图片描述
•验证监控数据是否获取正常

点击发现出来实例,测试监控项可以正常获取到数据
在这里插入图片描述
以上即为使用Zabbix监控华为云RDS的操作过程。

这一期的Zabbix的技术分享到这就结束了,更多Zabbix内容直接关注乐维社区(https://forum.lwops.cn/),专注运维技术分享,关注我,运维更无忧。

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐