Python调用阿里云sdk

准备

安装sdk

阿里云封装了各个产品的api,方便集成开发,sdk列表

1
pip install aliyun-python-sdk-rds

申请Access Key

新建RAM账号,创建ak

脚本编写

下面是archer获取慢日志相关的脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# -*- coding: UTF-8 -*-
import datetime
from aliyunsdkcore import client
from aliyunsdkrds.request.v20140815 import DescribeSlowLogsRequest, DescribeSlowLogRecordsRequest, \
RequestServiceOfCloudDBARequest
import simplejson as json


class Aliyun(object):
# ak认证
def __init__(self):
self.clt = client.AcsClient(
ak=ak,
secret=secret)
# 调用方法
def request_api(self, request, *values):
if values:
for value in values:
for k, v in value.items():
request.add_query_param(k, v)
request.set_accept_format('json')
result = self.clt.do_action_with_exception(request)
return json.dumps(json.loads(result.decode('utf-8')), indent=4, sort_keys=False, ensure_ascii=False)

def DescribeSlowLogs(self, DBInstanceId, StartTime, EndTime, **kwargs):
'''
获取集群慢日志列表
DBName,SortKey、PageSize、PageNumber
'''
request = DescribeSlowLogsRequest.DescribeSlowLogsRequest()
values = {"action_name": "DescribeSlowLogs", "DBInstanceId": DBInstanceId,
"StartTime": StartTime, "EndTime": EndTime, "SortKey": "TotalExecutionCounts"}
values = dict(values, **kwargs)
result = self.request_api(request, values)
return result

def DescribeSlowLogRecords(self, DBInstanceId, StartTime, EndTime, **kwargs):
'''
查看慢日志明细
SQLId,DBName、PageSize、PageNumber
'''
request = DescribeSlowLogRecordsRequest.DescribeSlowLogRecordsRequest()
values = {"action_name": "DescribeSlowLogRecords", "DBInstanceId": DBInstanceId,
"StartTime": StartTime, "EndTime": EndTime}
values = dict(values, **kwargs)
result = self.request_api(request, values)
return result

def RequestServiceOfCloudDBA(self, DBInstanceId, ServiceRequestType, ServiceRequestParam, **kwargs):
'''
获取统计信息:'GetTimedMonData',{"Language":"zh","KeyGroup":"mem_cpu_usage","KeyName":"","StartTime":"2018-01-15T04:03:26Z","EndTime":"2018-01-15T05:03:26Z"}
mem_cpu_usage、iops_usage、detailed_disk_space
获取process信息:'ShowProcessList',{"Language":"zh","Command":"Query"} -- Not Sleep , All
终止进程:'ConfirmKillSessionRequest',{"Language":"zh","SQLRequestID":75865,"SQLStatement":"kill 34022786;"}
获取表空间信息:'GetSpaceStatForTables',{"Language": "zh", "OrderType": "Data"}
获取资源利用信息:'GetResourceUsage',{"Language":"zh"}
'''
request = RequestServiceOfCloudDBARequest.RequestServiceOfCloudDBARequest()
values = {"action_name": "RequestServiceOfCloudDBA", "DBInstanceId": DBInstanceId,
"ServiceRequestType": ServiceRequestType, "ServiceRequestParam": ServiceRequestParam}
values = dict(values, **kwargs)
result = self.request_api(request, values)
return result