forked from jly8866/archer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aliyun_function.py
184 lines (141 loc) · 6.6 KB
/
aliyun_function.py
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# -*- coding: UTF-8 -*-
import simplejson as json
import datetime
from django.views.decorators.csrf import csrf_exempt
from django.http import HttpResponse
from .aliyun_api import Aliyun
from .permission import superuser_required
from .models import users, AliyunRdsConfig
aliyun = Aliyun()
# 获取SQL慢日志统计
def slowquery_review(request):
cluster_name = request.POST.get('cluster_name')
DBName = request.POST.get('db_name')
StartTime = request.POST.get('StartTime')
EndTime = request.POST.get('EndTime')
limit = request.POST.get('limit')
offset = request.POST.get('offset')
# 计算页数
PageNumber = (int(offset) + int(limit)) / int(limit)
values = {"PageSize": int(limit), "PageNumber": int(PageNumber)}
# DBName非必传
if DBName:
values['DBName'] = DBName
# UTC时间转化成阿里云需求的时间格式
StartTime = '%sZ' % StartTime
EndTime = '%sZ' % EndTime
# 通过集群名称获取关联的rds实例id
cluster_info = AliyunRdsConfig.objects.get(cluster_name=cluster_name)
# 调用aliyun接口获取SQL慢日志统计
slowsql = aliyun.DescribeSlowLogs(cluster_info.rds_dbinstanceid, StartTime, EndTime, **values)
# 解决table数据丢失精度、格式化时间
SQLSlowLog = json.loads(slowsql)['Items']['SQLSlowLog']
for SlowLog in SQLSlowLog:
SlowLog['SQLId'] = str(SlowLog['SQLHASH'])
SlowLog['CreateTime'] = aliyun.aliyun_time_format(SlowLog['CreateTime'])
result = {"total": json.loads(slowsql)['TotalRecordCount'], "rows": SQLSlowLog,
"PageSize": json.loads(slowsql)['PageRecordCount'], "PageNumber": json.loads(slowsql)['PageNumber']}
# 返回查询结果
return result
# 获取SQL慢日志明细
def slowquery_review_history(request):
cluster_name = request.POST.get('cluster_name')
StartTime = request.POST.get('StartTime')
EndTime = request.POST.get('EndTime')
DBName = request.POST.get('db_name')
SQLId = request.POST.get('SQLId')
limit = request.POST.get('limit')
offset = request.POST.get('offset')
# 计算页数
PageNumber = (int(offset) + int(limit)) / int(limit)
values = {"PageSize": int(limit), "PageNumber": int(PageNumber)}
# SQLId、DBName非必传
if SQLId:
values['SQLHASH'] = SQLId
if DBName:
values['DBName'] = DBName
# UTC时间转化成阿里云需求的时间格式
StartTime = datetime.datetime.strptime(StartTime, "%Y-%m-%d").date() - datetime.timedelta(days=1)
StartTime = '%sT16:00Z' % StartTime
EndTime = '%sT15:59Z' % EndTime
# 通过集群名称获取关联的rds实例id
cluster_info = AliyunRdsConfig.objects.get(cluster_name=cluster_name)
# 调用aliyun接口获取SQL慢日志统计
slowsql = aliyun.DescribeSlowLogRecords(cluster_info.rds_dbinstanceid, StartTime, EndTime, **values)
# 格式化时间\过滤HostAddress
SQLSlowRecord = json.loads(slowsql)['Items']['SQLSlowRecord']
for SlowRecord in SQLSlowRecord:
SlowRecord['ExecutionStartTime'] = aliyun.aliyun_time_format(SlowRecord['ExecutionStartTime']).strftime(
"%Y-%m-%d %H:%M:%S")
SlowRecord['HostAddress'] = SlowRecord['HostAddress'].split('[')[0]
result = {"total": json.loads(slowsql)['TotalRecordCount'], "rows": SQLSlowRecord,
"PageSize": json.loads(slowsql)['PageRecordCount'], "PageNumber": json.loads(slowsql)['PageNumber']}
# 返回查询结果
return result
# 问题诊断--进程列表
def process_status(request):
cluster_name = request.POST.get('cluster_name')
command_type = request.POST.get('command_type')
if command_type is None or command_type == '':
command_type = 'Query'
# 通过集群名称获取关联的rds实例id
cluster_info = AliyunRdsConfig.objects.get(cluster_name=cluster_name)
# 调用aliyun接口获取进程数据
process_info = aliyun.RequestServiceOfCloudDBA(cluster_info.rds_dbinstanceid, 'ShowProcessList',
{"Language": "zh", "Command": command_type})
# 提取进程列表
process_list = json.loads(process_info)['AttrData']
process_list = json.loads(process_list)['ProcessList']
result = {'status': 0, 'msg': 'ok', 'data': process_list}
# 返回查询结果
return result
# 问题诊断--通过进程id构建请求id
def create_kill_session(request):
cluster_name = request.POST.get('cluster_name')
ThreadIDs = request.POST.get('ThreadIDs')
result = {'status': 0, 'msg': 'ok', 'data': []}
# 通过集群名称获取关联的rds实例id
cluster_info = AliyunRdsConfig.objects.get(cluster_name=cluster_name)
# 调用aliyun接口获取进程数据
request_info = aliyun.RequestServiceOfCloudDBA(cluster_info.rds_dbinstanceid, 'CreateKillSessionRequest',
{"Language": "zh", "ThreadIDs": json.loads(ThreadIDs)})
# 提取进程列表
request_list = json.loads(request_info)['AttrData']
result['data'] = request_list
# 返回查询结果
return result
# 问题诊断--终止会话
def kill_session(request):
cluster_name = request.POST.get('cluster_name')
request_params = request.POST.get('request_params')
result = {'status': 0, 'msg': 'ok', 'data': []}
# 通过集群名称获取关联的rds实例id
cluster_info = AliyunRdsConfig.objects.get(cluster_name=cluster_name)
# 调用aliyun接口获取进程数据
request_params = json.loads(request_params)
ServiceRequestParam = dict({"Language": "zh"}, **request_params)
kill_result = aliyun.RequestServiceOfCloudDBA(cluster_info.rds_dbinstanceid, 'ConfirmKillSessionRequest',
ServiceRequestParam)
# 获取处理结果
kill_result = json.loads(kill_result)['AttrData']
result['data'] = kill_result
# 返回查询结果
return result
# 问题诊断--空间列表
@csrf_exempt
def sapce_status(request):
cluster_name = request.POST.get('cluster_name')
# 通过集群名称获取关联的rds实例id
cluster_info = AliyunRdsConfig.objects.get(cluster_name=cluster_name)
# 调用aliyun接口获取进程数据
space_info = aliyun.RequestServiceOfCloudDBA(cluster_info.rds_dbinstanceid, 'GetSpaceStatForTables',
{"Language": "zh", "OrderType": "Data"})
# 提取进程列表
space_list = json.loads(space_info)['ListData']
if space_list:
space_list = json.loads(space_list)
else:
space_list = []
result = {'status': 0, 'msg': 'ok', 'data': space_list}
# 返回查询结果
return result