通过华为云RDS实例列表接口获取RDS实例,用于资源的自动发现
通过华为云CES获取监控数据准
用于访问华为云的AK/SK
python3环境
用于调用华为云api的相关模块依赖
#!/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自动扫描模板后,点自动发现规则,可以正常获取到数据
验证监控数据是否获取正常
点击发现出来实例,测试监控项可以正常获取到数据
错误信息