-
Notifications
You must be signed in to change notification settings - Fork 0
/
pac_weiyu.go
693 lines (607 loc) · 16.9 KB
/
pac_weiyu.go
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
package main
import (
"bufio"
"bytes"
"context"
"encoding/hex"
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"os/signal"
"path/filepath"
"regexp"
"sort"
"strconv"
"strings"
"sync"
"time"
"github.com/fsnotify/fsnotify"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/metric"
)
var mu sync.Mutex
var logfile *os.File
func init() {
var err error
logfile, err = os.OpenFile("pac_weiyu.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
fmt.Printf("Error opening log file: %v\n", err)
os.Exit(1)
}
}
func main() {
if len(os.Args) < 2 {
fmt.Println("Usage: ./pac_weiyu <function> [arguments]")
fmt.Println("\nFunctions:")
fmt.Println(" executeAndTime <script> <times> <pacingTime>")
fmt.Println(" getStack <coreFile>")
fmt.Println(" monitorLogs")
fmt.Println(" retrieveStackAndPackLogFiles")
fmt.Println(" writeStackToFile <coreFile>")
fmt.Println(" getJavaHeapSize <pid>")
fmt.Println(" exportHeapSizeMetric <pid>")
fmt.Println(" printProcessesInCurrentPath")
fmt.Println(" encryptText <text>")
fmt.Println(" decryptText <ciphertext>")
os.Exit(1)
}
// Call the function based on the first argument
switch os.Args[1] {
case "executeAndTime":
if len(os.Args) < 5 {
fmt.Println("Usage: ./pac_weiyu executeAndTime <script> <times> <pacingTime>")
os.Exit(1)
}
times, _ := strconv.Atoi(os.Args[3]) // Convert os.Args[3] to int
pacingTime, _ := strconv.Atoi(os.Args[4]) // Convert os.Args[4] to int
executeAndTime(os.Args[2], times, pacingTime)
case "retrieveStackAndPackLogFiles":
retrieveStackAndPackLogFiles()
case "getStack":
if len(os.Args) < 3 {
fmt.Println("Usage: ./pac_weiyu getStack <coreFile>")
os.Exit(1)
}
stack, err := getStack(os.Args[2])
if err != nil {
fmt.Printf("Failed to get stack for %s: %v\n", os.Args[2], err)
os.Exit(1)
}
fmt.Printf("Stack for %s: %s\n", os.Args[2], stack)
case "writeStackToFile":
if len(os.Args) < 3 {
fmt.Println("Usage: ./pac_weiyu writeStackToFile <coreFile>")
os.Exit(1)
}
err := writeStackToFile(os.Args[2])
if err != nil {
fmt.Printf("Error writing stack to file: %v\n", err)
os.Exit(1)
}
case "monitorLogs":
monitorLogs()
case "getJavaHeapSize": // Add case for getJavaHeapSize
if len(os.Args) < 3 {
fmt.Println("Usage: ./pac_weiyu getJavaHeapSize <pid>")
os.Exit(1)
}
pid, _ := strconv.Atoi(os.Args[2]) // Convert os.Args[2] to int
heapSize, err := getJavaHeapSize(pid)
if err != nil {
fmt.Printf("Failed to get Java heap size for PID %d: %v\n", pid, err)
os.Exit(1)
}
fmt.Printf("Java heap size for PID %d: %dMB\n", pid, int(heapSize))
case "exportHeapSizeMetric":
if len(os.Args) < 3 {
fmt.Println("Usage: ./pac_weiyu exportHeapSizeMetric <pid>")
os.Exit(1)
}
pid, _ := strconv.Atoi(os.Args[2]) // Convert os.Args[2] to int
exportHeapSizeMetric(pid)
case "printProcessesInCurrentPath":
err := printProcessesInCurrentPath()
if err != nil {
fmt.Printf("Error printing processes in current path: %v\n", err)
os.Exit(1)
}
case "encryptText":
if len(os.Args) < 3 {
fmt.Println("Usage: ./pac_weiyu encryptText <text>")
os.Exit(1)
}
keyBytes, err := ioutil.ReadFile("mx.txt")
if err != nil {
fmt.Println("Error reading key file:", err)
os.Exit(1)
}
props := SecretKeyProperties{
Algorithm: "AES",
CypherTransformation: "AES/CBC/PKCS5Padding",
HexaKey: hex.EncodeToString(keyBytes),
}
crypto, err := NewPasswordCryptography(props)
if err != nil {
fmt.Println("Error initializing cryptography:", err)
os.Exit(1)
}
ciphertext, err := cryptText(crypto, os.Args[2])
if err != nil {
fmt.Println("Error encrypting text:", err)
os.Exit(1)
}
fmt.Println("Ciphertext:", ciphertext)
case "decryptText":
if len(os.Args) < 3 {
fmt.Println("Usage: ./pac_weiyu decryptText <ciphertext>")
os.Exit(1)
}
keyBytes, err := ioutil.ReadFile("mx.txt")
if err != nil {
fmt.Println("Error reading key file:", err)
os.Exit(1)
}
props := SecretKeyProperties{
Algorithm: "AES",
CypherTransformation: "AES/CBC/PKCS5Padding",
HexaKey: hex.EncodeToString(keyBytes),
}
crypto, err := NewPasswordCryptography(props)
if err != nil {
fmt.Println("Error initializing cryptography:", err)
os.Exit(1)
}
cleartext, err := decryptText(crypto, os.Args[2])
if err != nil {
fmt.Println("Error decrypting text:", err)
os.Exit(1)
}
fmt.Println("Cleartext:", cleartext)
default:
fmt.Println("Unknown function:", os.Args[1])
}
defer logfile.Close()
}
func writeLog(logfile *os.File, message string) {
_, err := fmt.Fprint(logfile, message)
if err != nil {
fmt.Printf("Error writing to log file: %v\n", err)
}
}
func getLast100thLinePos(filePath string) (int64, error) {
file, err := os.Open(filePath)
if err != nil {
return 0, err
}
defer file.Close()
scanner := bufio.NewScanner(file)
var lines []int64
var pos int64
for scanner.Scan() {
lines = append(lines, pos)
pos += int64(len(scanner.Bytes())) + 1 // +1 for newline character
if len(lines) > 100 {
lines = lines[1:]
}
}
if err := scanner.Err(); err != nil {
return 0, err
}
if len(lines) == 0 {
return 0, nil
}
return lines[0], nil
}
func watchFile(filePath string, logfile *os.File) error {
watcher, err := fsnotify.NewWatcher()
if err != nil {
return err
}
defer watcher.Close()
var lastPos int64 = 0
done := make(chan bool)
go func() {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
//writeLog(logfile, "event: "+event.String())
if event.Op&fsnotify.Write == fsnotify.Write {
//writeLog(logfile, "modified file: "+event.Name)
file, err := os.Open(event.Name)
if err != nil {
writeLog(logfile, "error opening file: "+err.Error())
continue
}
lastPos, err = getLast100thLinePos(event.Name)
if err != nil {
writeLog(logfile, "error getting last 100th line position: "+err.Error())
continue
}
file.Seek(lastPos, 0)
reader := io.Reader(file)
contents, err := io.ReadAll(reader)
if err != nil {
writeLog(logfile, "error reading file: "+err.Error())
} else {
lines := strings.Split(string(contents), "\n")
for _, line := range lines {
lineLower := strings.ToLower(line)
if strings.Contains(lineLower, "error") || strings.Contains(lineLower, "fail") || strings.Contains(lineLower, "exception") {
fmt.Println(filePath + ": " + line)
}
}
lastPos, _ = file.Seek(0, io.SeekEnd)
}
file.Close()
}
case err, ok := <-watcher.Errors:
if !ok {
return
}
writeLog(logfile, "error: "+err.Error())
}
}
}()
err = watcher.Add(filePath)
if err != nil {
return err
}
<-done
return nil
}
func monitorLogs() {
dir := "logs"
_, err := ioutil.ReadDir(dir)
if err != nil {
fmt.Println("Error reading directory:", err)
return
}
type FileInfo struct {
Path string
ModTime time.Time
}
var logFiles []FileInfo
err = filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
// Ignore directories
if !info.IsDir() {
logFiles = append(logFiles, FileInfo{Path: path, ModTime: info.ModTime()})
}
return nil
})
// Sort the files by modification time
sort.Slice(logFiles, func(i, j int) bool {
return logFiles[i].ModTime.Before(logFiles[j].ModTime)
})
// If there are more than 10 files, take the last 10
if len(logFiles) > 10 {
logFiles = logFiles[len(logFiles)-10:]
}
// Automatically watch the 10 latest files
for _, file := range logFiles {
go func(filePath string) {
err := watchFile(filePath, logfile)
if err != nil {
fmt.Println("Error watching file:", err)
}
}(file.Path)
}
// Only block if at least one watchFile goroutine was started
if len(logFiles) > 0 {
// Prevent the program from exiting
select {}
}
}
func mustParseInt(s string) int {
i, err := strconv.Atoi(strings.TrimSpace(s))
if err != nil {
return -1
}
return i
}
func tailFile(filename string) {
cmd := exec.Command("tail", "-f", filename)
stdout, err := cmd.StdoutPipe()
if err != nil {
fmt.Println("Error creating stdout pipe:", err)
return
}
stderr, err := cmd.StderrPipe()
if err != nil {
fmt.Println("Error creating stderr pipe:", err)
return
}
if err := cmd.Start(); err != nil {
fmt.Println("Error starting command:", err)
return
}
go func() {
scanner := bufio.NewScanner(stderr)
for scanner.Scan() {
fmt.Println("Error output from tail command:", scanner.Text())
}
}()
scanner := bufio.NewScanner(stdout)
for scanner.Scan() {
fmt.Printf("%s: %s\n", filename, scanner.Text())
}
if err := scanner.Err(); err != nil {
fmt.Println("Error reading command output:", err)
}
if err := cmd.Wait(); err != nil {
fmt.Println("Error waiting for command:", err)
}
}
func executeAndTime(script string, times int, pacingTime int) {
for i := 1; i <= times; i++ {
start := time.Now()
message := fmt.Sprintf("Starting %s run %d at %s\n", script, i, start)
writeLog(logfile, message)
cmd := exec.Command("bash", script)
err := cmd.Run()
if err != nil {
message := fmt.Sprintf("%s failed with exit status %v\n", script, err)
writeLog(logfile, message)
break
}
end := time.Now()
duration := end.Sub(start)
message = fmt.Sprintf("Execution time for %s run %d: %v seconds\n", script, i, duration.Seconds())
writeLog(logfile, message)
message = fmt.Sprintf("Ended %s run %d at %s\n", script, i, end)
writeLog(logfile, message)
time.Sleep(time.Duration(pacingTime) * time.Second)
}
}
func getStack(coreFile string) ([]byte, error) {
cmd := exec.Command("./pmx", "-e", coreFile)
return cmd.Output()
}
func getPid(coreFile string) string {
pid := strings.Split(coreFile, ".")[1]
return pid
}
func writeStackToFile(coreFile string) error {
pid := getPid(coreFile)
stack, err := getStack(coreFile)
if err != nil {
writeLog(logfile, fmt.Sprintf("Failed to get stack for %s: %v\n", coreFile, err))
return fmt.Errorf("Failed to get stack for %s: %v", coreFile, err)
}
stackFileName := fmt.Sprintf("stack.%s", pid)
if len(stack) > 0 && pid != "" {
err := ioutil.WriteFile(stackFileName, stack, 0644)
if err != nil {
writeLog(logfile, fmt.Sprintf("Failed to write to %s: %v\n", stackFileName, err))
return fmt.Errorf("Failed to write to %s: %v", stackFileName, err)
}
writeLog(logfile, fmt.Sprintf("Successfully wrote to %s\n", stackFileName))
} else {
if len(stack) == 0 {
writeLog(logfile, "No data to write.\n")
}
if pid == "" {
writeLog(logfile, "Invalid PID.\n")
}
}
return nil
}
func findLogFiles(pid string) ([]string, error) {
var logFiles []string
err := filepath.Walk("logs", func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() && strings.Contains(path, pid) {
logFiles = append(logFiles, path)
}
return nil
})
if err != nil {
return nil, err
}
return logFiles, nil
}
func retrieveStackAndPackLogFiles() {
coreFiles, _ := filepath.Glob("core.*")
for _, coreFile := range coreFiles {
pid := getPid(coreFile)
message := fmt.Sprintf("Retrieving stack and packing log files for core file %s\n", coreFile)
writeLog(logfile, message)
err := writeStackToFile(coreFile)
if err != nil {
fmt.Println(err)
}
logFiles, err := findLogFiles(pid)
if err != nil {
writeLog(logfile, fmt.Sprintf("Failed to find log files: %v\n", err))
return
}
writeLog(logfile, fmt.Sprintf("Log files: %s\n", strings.Join(logFiles, ", ")))
args := append([]string{"zip", "-r", fmt.Sprintf("stack_and_log_%s.zip", pid), fmt.Sprintf("stack.%s", pid)}, logFiles...)
cmd := exec.Command(args[0], args[1:]...)
writeLog(logfile, fmt.Sprintf("Running command: %s\n", strings.Join(cmd.Args, " ")))
cmdOutput, err := cmd.CombinedOutput()
if err != nil {
writeLog(logfile, fmt.Sprintf("Failed to zip files: %v\n", err))
} else {
writeLog(logfile, fmt.Sprintf("Zip command output: %s\n", cmdOutput))
}
os.Remove(fmt.Sprintf("stack.%s", pid))
}
files, err := filepath.Glob("stack_and_log_*.zip")
if err != nil {
writeLog(logfile, fmt.Sprintf("Failed to find files: %v\n", err))
return
}
if len(files) == 0 {
writeLog(logfile, "No files match the pattern stack_and_log_*.zip\n")
return
}
writeLog(logfile, fmt.Sprintf("%d files match the pattern stack_and_log_*.zip\n", len(files)))
finalZipFile := fmt.Sprintf("final_stack_and_log_%s.zip", time.Now().Format("20060102_150405"))
cmd := exec.Command("zip", "-r", finalZipFile)
cmd.Args = append(cmd.Args, files...)
writeLog(logfile, fmt.Sprintf("Running command: %s\n", strings.Join(cmd.Args, " ")))
cmdOutput, err := cmd.CombinedOutput()
if err != nil {
writeLog(logfile, fmt.Sprintf("Failed to create final zip file: %v\n", err))
} else {
writeLog(logfile, fmt.Sprintf("Successfully created final zip file: %s\n", finalZipFile))
writeLog(logfile, fmt.Sprintf("Zip command output: %s\n", cmdOutput))
}
files, _ = filepath.Glob("stack_and_log_*.zip")
for _, file := range files {
os.Remove(file)
}
}
func getJavaHeapSize(pid int) (float64, error) {
cmd := exec.Command("bash", "-c", "source mxg2000_settings.sh && $JAVA_HOME/bin/jstat -gc "+strconv.Itoa(pid))
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
return 0, err
}
lines := strings.Split(out.String(), "\n")
if len(lines) < 2 {
return 0, errors.New("unexpected jstat output")
}
fields := strings.Fields(lines[1])
if len(fields) < 7 {
return 0, errors.New("unexpected jstat output")
}
EU, err := strconv.ParseFloat(fields[5], 64)
if err != nil {
return 0, err
}
S0U, err := strconv.ParseFloat(fields[2], 64)
if err != nil {
return 0, err
}
S1U, err := strconv.ParseFloat(fields[3], 64)
if err != nil {
return 0, err
}
OU, err := strconv.ParseFloat(fields[7], 64)
if err != nil {
return 0, err
}
heapSize := EU + S0U + S1U + OU
heapSizeMB := heapSize / 1024
if err != nil {
return 0, err
}
return heapSizeMB, nil
}
func exportHeapSizeMetric(pid int) (err error) {
// Handle SIGINT (CTRL+C) gracefully.
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
defer stop()
// Set up OpenTelemetry.
serviceName := "PAC Metrics"
serviceVersion := "1.0"
otelShutdown, err := setupOTelSDK(ctx, serviceName, serviceVersion)
meter := otel.Meter("Java Heap Size")
if _, err := meter.Int64ObservableGauge("JavaHeapSize",
metric.WithDescription(
"Java Heap Size",
),
metric.WithUnit("MB"),
metric.WithInt64Callback(func(_ context.Context, o metric.Int64Observer) error {
heapSize, err := getJavaHeapSize(pid)
if err != nil {
errorMessage := fmt.Sprintf("Failed to get heap size: %v", err)
writeLog(logfile, errorMessage)
return err
}
// Record the heap size
o.Observe(int64(heapSize))
return nil
}),
); err != nil {
panic(err)
}
if err != nil {
return
}
// Handle shutdown properly so nothing leaks.
defer func() {
err = errors.Join(err, otelShutdown(context.Background()))
}()
// Run recordCPUUsage in a goroutine.
go serveMetrics()
// Wait for interruption.
<-ctx.Done()
// Stop receiving signal notifications as soon as possible.
stop()
return
}
func printProcessesInCurrentPath() error {
// Get the current path
currentPath, err := os.Getwd()
if err != nil {
return err
}
// Run the 'ps -e' command to get the list of process IDs
cmd := exec.Command("ps", "-e", "-o", "pid=")
output, err := cmd.Output()
if err != nil {
return err
}
// Split the output into lines
lines := strings.Split(string(output), "\n")
// Check each line
for _, line := range lines {
// Trim the line and parse the process ID
pidStr := strings.TrimSpace(line)
pid, err := strconv.Atoi(pidStr)
if err != nil {
continue
}
// Run the 'pwdx' command to get the current working directory of the process
cmd := exec.Command("pwdx", pidStr)
output, err := cmd.Output()
if err != nil {
continue
}
// Check if the output contains the current path
match, _ := regexp.MatchString(currentPath, string(output))
if match {
fmt.Println("Process ID:", pid)
fmt.Println("Current working directory:", string(output))
// Run the 'ps -p' command to get the command line of the process
cmdLineCmd := exec.Command("ps", "-p", pidStr, "-o", "command=")
cmdLineOutput, err := cmdLineCmd.Output()
if err != nil {
fmt.Println("Error getting command line:", err)
} else {
fmt.Println("Command line:", string(cmdLineOutput))
}
}
}
return nil
}
func cryptText(crypto *PasswordCryptography, text string) (string, error) {
ciphertext, err := crypto.Encrypt([]byte(text))
if err != nil {
return "", err
}
return crypto.ChangeToEncryptedString(ciphertext), nil
}
func decryptText(crypto *PasswordCryptography, cryptoText string) (string, error) {
ciphertext := crypto.ChangeToDecryptedString(cryptoText)
cleartext, err := crypto.Decrypt(ciphertext)
if err != nil {
return "", err
}
return string(cleartext), nil
}