-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
184 lines (174 loc) · 6.05 KB
/
main.go
File metadata and controls
184 lines (174 loc) · 6.05 KB
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
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io"
"log"
"net/http"
"os"
"sync"
"time"
)
var (
targetAddr string // 目标程序的地址
pprofDir string // pprof 文件保存目录
periodicInterval time.Duration //时间间隔
memoryThreshold uint64 // 内存阈值,单位为GB
)
// MemStats via expvar
type ExpvarMemStats struct {
Alloc uint64 `json:"Alloc"`
// 可以根据需要添加其他 memstats 字段
}
type ExpvarResponse struct {
Memstats ExpvarMemStats `json:"memstats"`
// 可以根据需要添加其他 expvar 变量
}
type pprofGet struct {
Name string `json:"name"`
Url string `json:"url"`
}
func main() {
log.SetFlags(log.LstdFlags | log.Lshortfile) //启用日志时间戳和文件名
flag.StringVar(&targetAddr, "target", "127.0.0.1:6060", "目标Go程序的HTTP地址")
flag.StringVar(&pprofDir, "save-dir", "./pprof_dumps", "指定proof文件保存目录")
flag.Uint64Var(&memoryThreshold, "memory-threshold", 15, "指定生成pprof文件的内存阈值(单位GB)")
flag.DurationVar(&periodicInterval, "periodic-interval", 10*time.Minute, "指定生成的pprof文件的间隔时间(单位:分钟)")
flag.Parse()
//尝试创建保存目录
_, err := os.Stat(pprofDir)
if err != nil {
if os.IsNotExist(err) {
if errMkdir := os.MkdirAll(pprofDir, 0755); errMkdir != nil {
log.Fatalf("无法创建 pprof 目录或上级目录 %s: %v", pprofDir, errMkdir)
}
} else {
log.Fatalf("无法访问 pprof 保存目录: %v", err)
}
}
log.Println("生成pprof文件的脚本已启动...")
log.Printf("监控目标程序的端口: %s,pprof文件保存目录: %s,间隔时间:%v \n", targetAddr, pprofDir, periodicInterval)
log.Printf("每隔%v生成一次%s的pprof文件\n", periodicInterval, targetAddr)
log.Printf("当%s占用内存达到%vGB时生成pprof文件\n", targetAddr, memoryThreshold)
go periodicPprofGenerator()
go memoryBasedPprofGenerator()
select {} // 阻塞主 goroutine,保持程序运行
}
func periodicPprofGenerator() {
ticker := time.NewTicker(periodicInterval)
defer ticker.Stop()
for range ticker.C {
generateRemoteProfile("periodic")
}
}
func memoryBasedPprofGenerator() {
ticker := time.NewTicker(15 * time.Second) // 每15秒检查一次
defer ticker.Stop()
client := http.Client{Timeout: 10 * time.Second}
for range ticker.C {
expvarURL := fmt.Sprintf("http://%s/debug/vars", targetAddr)
resp, err := client.Get(expvarURL)
if err != nil {
log.Printf("错误: 无法从 %s 获取 /debug/vars: %v \n", targetAddr, err)
continue
}
defer func() {
errClose := resp.Body.Close()
if errClose != nil {
log.Printf("错误: 关闭响应体时出错: %v \n", errClose)
}
}()
if resp.StatusCode != http.StatusOK {
log.Printf("错误: 从 %s 获取 /debug/vars 失败,状态码: %d \n", targetAddr, resp.StatusCode)
bodyBytes, _ := io.ReadAll(resp.Body)
log.Printf("响应体: %s \n", string(bodyBytes))
continue
}
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
log.Printf("错误: 无法读取 %s 的 /debug/vars 响应体: %v \n", targetAddr, err)
continue
}
var data ExpvarResponse
if err = json.NewDecoder(bytes.NewReader(bodyBytes)).Decode(&data); err != nil {
log.Printf("错误: 无法解析 %s 的 /debug/vars 响应: %v \n", targetAddr, err)
log.Printf("响应体: %s \n", string(bodyBytes))
continue
}
thresholdBytes := memoryThreshold * 1024 * 1024 * 1024
if data.Memstats.Alloc >= thresholdBytes {
generateRemoteProfile(fmt.Sprintf("memory_threshold_%vGB", data.Memstats.Alloc))
break
}
}
}
func generateRemoteProfile(reason string) {
timestamp := time.Now().Format("20060102T150405")
targetName := SanitizeFilename(targetAddr)
// 生成 pprof 文件的逻辑
var pprofUrl, fileName string
pprofFileNames := make(map[string]pprofGet)
pprofUrl = fmt.Sprintf("http://%s/debug/pprof/profile?seconds=%d", targetAddr, 5)
fileName = fmt.Sprintf("%s/cpu_%s_%s_%s.pprof", pprofDir, targetName, reason, timestamp)
pprofFileNames["cpu"] = pprofGet{fileName, pprofUrl}
pprofUrl = fmt.Sprintf("http://%s/debug/pprof/trace?seconds=%d", targetAddr, 5)
fileName = fmt.Sprintf("%s/trace_%s_%s_%s.pprof", pprofDir, targetName, reason, timestamp)
pprofFileNames["trace"] = pprofGet{fileName, pprofUrl}
pprofUrl = fmt.Sprintf("http://%s/debug/pprof/heap", targetAddr)
fileName = fmt.Sprintf("%s/heap_%s_%s_%s.pprof", pprofDir, targetName, reason, timestamp)
pprofFileNames["heap"] = pprofGet{fileName, pprofUrl}
var wg sync.WaitGroup
for _, pprof := range pprofFileNames {
wg.Add(1)
go func(url, name string) {
defer wg.Done()
log.Printf("正在为 %s 生成 pprof 文件%s (原因: %s)\n", targetAddr, name, reason)
resp, err := http.Get(url)
if err != nil {
log.Printf("错误: 无法从 %s 获取 pprof 数据: %v \n", url, err)
return
}
defer func() {
err = resp.Body.Close()
if err != nil {
log.Printf("错误: 关闭响应体时出错: %v (%s) \n", err, url)
}
}()
if resp.StatusCode != http.StatusOK {
log.Printf("错误: 从 %s 获取 pprof 数据失败,状态码: %d \n", url, resp.StatusCode)
return
}
file, err := os.Create(name)
if err != nil {
log.Printf("错误: 无法创建 pprof 文件 %s: %v \n", name, err)
return
}
defer func() {
err = file.Close()
if err != nil {
log.Printf("错误: 关闭 pprof 文件 %s 时出错: %v \n", name, err)
}
}()
_, err = io.Copy(file, resp.Body)
if err != nil {
log.Printf("错误: 无法将 pprof 数据写入文件 %s: %v \n", name, err)
return
}
log.Printf("成功为 %s 生成 pprof 文件: %s (原因: %s)\n", targetAddr, name, reason)
}(pprof.Url, pprof.Name)
}
wg.Wait()
log.Print("所有 pprof 文件已生成!")
}
// SanitizeFilename 用于整理文件名中的特殊字符
func SanitizeFilename(name string) string {
r := []rune(name)
for i, c := range r {
if c < 32 || c > 126 || c == '/' || c == '\\' || c == ':' || c == '*' || c == '?' || c == '"' || c == '<' || c == '>' || c == '|' {
r[i] = '_'
}
}
return string(r)
}