腾讯云SDK获取CDB相关信息范例【实操系列】
程序员文章站
2022-04-27 07:52:19
...
1. 获取CDB列表信息
from tencentcloud.common import credential
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
from tencentcloud.cdb.v20170320 import cdb_client, models
try:
cred = credential.Credential("secretId", "secretKey")
client = cdb_client.CdbClient(cred, "ap-shanghai")
req = models.DescribeDBInstancesResponse()
req.Limit=100 # 默认只会展示20条信息
resp = client.DescribeDBInstances(req)
print(resp.to_json_string())
except TencentCloudSDKException as err:
print(err)
当然输出的数据是一堆json,自然想到是jq
命令过滤出需要的信息
➜ ~ python getcdblist.py |jq '.Items |.[]|{InstanceId,InstanceName,Vip,Vport,EngineVersion,Memory,Cpu}'
{
"InstanceId": "xx-InstanceId",
"InstanceName": "xx-InstanceName",
"Vip": "0.0.0.0",
"Vport": 3306,
"EngineVersion": "5.7",
"Memory": 16000,
"Cpu": 4
}
{
"InstanceId": "yy-InstanceId",
"InstanceName": "yy-InstanceName",
"Vip": "0.0.0.0",
"Vport": 3306,
"EngineVersion": "5.7",
"Memory": 16000,
"Cpu": 4
}
{
"InstanceId": "zz-InstanceId",
"InstanceName": "zz-InstanceName",
"Vip": "0.0.0.0",
"Vport": 3306,
"EngineVersion": "5.7",
"Memory": 16000,
"Cpu": 4
}
2. 获取mysql慢查询日志
# -*- coding: utf-8 -*-
from tencentcloud.common import credential
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
from tencentcloud.cdb.v20170320 import cdb_client, models
try:
cred = credential.Credential("secretId", "secretKey")
client = cdb_client.CdbClient(cred, "ap-shanghai")
# 慢查询日志
req = models.DescribeSlowLogsRequest()
req.Limit=1
req.InstanceId="xx-InstanceId"
resp = client.DescribeSlowLogs(req)
resp = resp.to_json_string()
print(resp)
except TencentCloudSDKException as err:
print(err)
➜ ~ python getcdbslowlog.py | jq
{
"TotalCount": 8,
"RequestId": "xxxx-xxxx-xxxx-xxxx-xxxx",
"Items": [
{
"Name": "InstanceId_slowlog_20200215.log",
"IntranetUrl": "http://xxxxxxxx",
"Date": "2020-02-15",
"InternetUrl": "https://xxxxxxxx",
"Type": "slowlog",
"Size": 3482
}
]
}
3. 获取监控信息
from tencentcloud.common import credential
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
from tencentcloud.cdb.v20170320 import cdb_client, models
try:
cred = credential.Credential("secretId", "secretKey")
client = cdb_client.CdbClient(cred, "ap-shanghai")
req = models.DescribeDeviceMonitorInfoRequest()
req.InstanceId='xx-InstanceId'
req.Count=1
resp = client.DescribeDeviceMonitorInfo(req)
print(resp)
except TencentCloudSDKException as err:
print(err)
➜ ~ python getcdbmon.py
[TencentCloudSDKException] code:OperationDenied.ActionNotSupport message:该接口暂只支持最高配置实例查询 requestId:xxxxxxxx
很可惜,穷,买不起顶配的