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
|
package main
import (
"fmt"
"getslow/models"
"getslow/pkg/settings"
"getslow/pkg/utils"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
"github.com/aliyun/alibaba-cloud-sdk-go/services/rds"
"net/http"
"os"
"time"
)
var (
PageSize = 100
MaxPage int
Message = []string{"订单主库","订单从库","营销主库","营销从库","erp主库","erp从库","java主库","java从库","crm主库","crm从库"}
)
func main() {
if err := settings.Setup();err != nil{
fmt.Println(err)
return
}
//获取慢日志明细时间区间
DetailStartTime := time.Now().Add(-(1*24*time.Hour)).Format("2006-01-02T00:00Z")
DetailEndTime := time.Now().Format("2006-01-02T00:00Z")
//获取慢日志统计时间区间
//由于取7天的数据,如果数据量多有时候会导致取不到数据,所以这里就只取4天的数据
SlowStartTime := time.Now().Add(-(3 * 24 * time.Hour)).Format("2006-01-02Z")
SlowEndTime := time.Now().Add(-(1 * 24 * time.Hour)).Format("2006-01-02Z")
fmt.Println(SlowStartTime,SlowEndTime)
// // 创建client 实例
client,err := rds.NewClientWithAccessKey(
settings.RdsSetting.RegionId,
settings.RdsSetting.AccessKeyId,
settings.RdsSetting.AccessKeySecret,
)
if err != nil{
fmt.Println(err)
return
}
var slowLogList models.SlowList
var slowDetailList models.SlowDetailList
//创建慢日志统计请求并设置参数
SlowRequest := rds.CreateDescribeSlowLogsRequest()
SlowRequest.Scheme = "https"
SlowRequest.StartTime = SlowStartTime //按照阿里云的api文档 这里的时间结尾都会加上 Z
SlowRequest.EndTime = SlowEndTime
SlowRequest.PageSize = requests.NewInteger(PageSize)
slowLogs := utils.SlowLogs{
DetailStartTime: SlowStartTime,
DetailEndTime: SlowEndTime,
Client: client,
Request: SlowRequest,
}
//创建慢日志详细请求并设置参数
RecordRequest := rds.CreateDescribeSlowLogRecordsRequest()
RecordRequest.Scheme = "https"
RecordRequest.StartTime = DetailStartTime //按照阿里云的api文档 这里的时间结尾都会加上 Z
RecordRequest.EndTime = DetailEndTime
//每页条数,范围:30~100
RecordRequest.PageSize = requests.NewInteger(PageSize)
slowDetail := utils.SlowDetailRecords{
DetailStartTime: DetailEndTime,
DetailEndTime: DetailEndTime,
Client: client,
Request: RecordRequest,
}
if err := utils.ConnSmtp();err != nil{
panic(err)
}
for index,value := range settings.RdsSetting.DBInstanceID {
slowLogs.Request.PageNumber = requests.NewInteger(1)
slowLogs.Request.DBInstanceId = value
if total := slowLogs.GetTotal();total > 0{
if total % PageSize >0{
MaxPage = total / PageSize + 1
}
}else {
fmt.Printf("实例:%s 没有查到慢日志\n",value)
continue
}
for sl:=1;sl<=MaxPage;sl++{
response ,err := slowLogs.GetSlowData(sl)
if err != nil{
fmt.Println(err.Error())
continue
}
if response.GetHttpStatus() != http.StatusOK {
fmt.Println(response.GetHttpStatus())
continue
}
if response.TotalRecordCount == 0 {
fmt.Printf("实例: %s 没有慢日志查询\n",value)
continue
}
slowLogList = append(slowLogList,utils.SlowParse(response.Items.SQLSlowLog)...)
}
total,slows := utils.Removal(slowLogList)
if total > 0 {
// 页码,范围:大于0并且不超过int的最大值
slowDetail.Request.PageNumber = requests.NewInteger(1)
slowDetail.Request.DBInstanceId = value
if total := slowDetail.GetTotal();total > 0{
if total % PageSize >0{
MaxPage = total / PageSize + 1
}
}
for di:=1;di<=MaxPage;di++{
response ,err := slowDetail.GetSlowDetailData(di)
if err != nil{
fmt.Println(err.Error())
continue
}
if response.GetHttpStatus() != http.StatusOK {
fmt.Println(response.GetHttpStatus())
continue
}
if response.TotalRecordCount == 0 {
fmt.Printf("实例: %s 没有慢日志查询\n",value)
continue
}
slowDetailList = append(slowDetailList,utils.SlowDetailParse(response.Items.SQLSlowRecord)...)
}
slowDetail := models.SlowDetail{
Instance: value,
SlowDetailList: slowDetailList,
}
slowDetail.Save()
body := models.TemplateBody{
TotalRecordCount: total,
TRData: slows,
Path: fmt.Sprintf("./%s.xlsx",value),
}
err = utils.SendMail(index,time.Now().Format("2006-01-02"),Message[index],body)
if err != nil{
fmt.Println("email---",err.Error())
}
os.Remove(fmt.Sprintf("./%s.xlsx",value))
time.Sleep(2*time.Second)
}
}
}
|