diff options
author | We-unite <3205135446@qq.com> | 2024-08-09 13:56:37 +0800 |
---|---|---|
committer | We-unite <3205135446@qq.com> | 2024-08-12 14:16:51 +0800 |
commit | 3e49a044d22635157916651f0acb5a062397b34b (patch) | |
tree | 254cd9a2605fa003f4579e7c5510e6e2aea19375 /src/basefunc.go | |
parent | ea32e017e579f168d87732893335c38d539ac2f1 (diff) | |
download | godo-3e49a044d22635157916651f0acb5a062397b34b.tar.gz godo-3e49a044d22635157916651f0acb5a062397b34b.zip |
Add db structure, fix filePath, start filtering
This commit I made several changes:
- Use structure instead of simple bson.M(interface{}). bson.M has
some shortcomings: 1) It makes the database in chaos and hard to
read, but this's not important; 2) Some entrys may has more or
less content than others, which makes it hard to decode and filt.
So I design new data structure to encode and decode. Hopes that
there's no bugs.
- Fix the way to calculate file path. The original method is to add
all the PATH entries together, that's totally wrong! PATH entry
has several types, as it shows in "objtype". I can't find it in
the kernel src code, so what i know is just "PARENT" means the dir
the file is in, while the filename itself has the path, so we
whould ignore all "PARENT"s. When the src code is found, we should
check it again.
- Fix bugs in updating. The update function of mongodb is set to
required to has a '$' such as 'set'/'push', so when we update a
whole doc, we should use replace but not update function. And,
we should never ignore the error infomation it gives us.
Hope that there's no more bugs for this Big Change.
Now its' time to write filter as well as viewer. Best wishes with
NO BUGS!
Diffstat (limited to 'src/basefunc.go')
-rw-r--r-- | src/basefunc.go | 129 |
1 files changed, 0 insertions, 129 deletions
diff --git a/src/basefunc.go b/src/basefunc.go deleted file mode 100644 index 2f39507..0000000 --- a/src/basefunc.go +++ /dev/null | |||
@@ -1,129 +0,0 @@ | |||
1 | package main | ||
2 | |||
3 | import ( | ||
4 | "bufio" | ||
5 | "fmt" | ||
6 | "os" | ||
7 | "os/exec" | ||
8 | "path/filepath" | ||
9 | "strconv" | ||
10 | "strings" | ||
11 | "time" | ||
12 | ) | ||
13 | |||
14 | func figureOutSyscalls() error { | ||
15 | cmd := exec.Command("ausyscall", "--dump") | ||
16 | stdout, err := cmd.StdoutPipe() | ||
17 | if err != nil { | ||
18 | return err | ||
19 | } | ||
20 | |||
21 | if err := cmd.Start(); err != nil { | ||
22 | return err | ||
23 | } | ||
24 | |||
25 | scanner := bufio.NewScanner(stdout) | ||
26 | for i := 0; scanner.Scan(); i++ { | ||
27 | if i == 0 { | ||
28 | continue | ||
29 | } | ||
30 | line := scanner.Text() | ||
31 | parts := strings.Split(line, "\t") | ||
32 | if len(parts) != 2 { | ||
33 | return fmt.Errorf("invalid ausyscall format") | ||
34 | } | ||
35 | num, err := strconv.Atoi(parts[0]) | ||
36 | if err != nil { | ||
37 | return err | ||
38 | } | ||
39 | syscallTable[num] = parts[1] | ||
40 | } | ||
41 | |||
42 | if err := scanner.Err(); err != nil { | ||
43 | return err | ||
44 | } | ||
45 | if err := cmd.Wait(); err != nil { | ||
46 | return err | ||
47 | } | ||
48 | return nil | ||
49 | } | ||
50 | |||
51 | func getPid() (int, error) { | ||
52 | // 指定要搜索的关键词 | ||
53 | keyword := "/usr/bin/containerd" | ||
54 | |||
55 | // 获取/proc目录下的所有子目录 | ||
56 | procDir, err := filepath.Glob("/proc/*") | ||
57 | if err != nil { | ||
58 | return 0, err | ||
59 | } | ||
60 | |||
61 | // 遍历子目录,查找包含关键词的进程 | ||
62 | for _, dir := range procDir { | ||
63 | pid, err := strconv.Atoi(filepath.Base(dir)) | ||
64 | if err != nil { | ||
65 | continue // 跳过非PID的目录 | ||
66 | } | ||
67 | |||
68 | // 检查进程是否包含关键词 | ||
69 | if containsKeyword(pid, keyword) { | ||
70 | return pid, nil | ||
71 | } | ||
72 | } | ||
73 | err = fmt.Errorf("Error: no containerd process found.") | ||
74 | return 0, err | ||
75 | } | ||
76 | |||
77 | func containsKeyword(pid int, keyword string) bool { | ||
78 | // 构造完整的进程命令路径 | ||
79 | cmdPath := fmt.Sprintf("/proc/%d/cmdline", pid) | ||
80 | |||
81 | // 打开文件 | ||
82 | file, err := os.Open(cmdPath) | ||
83 | if err != nil { | ||
84 | return false | ||
85 | } | ||
86 | defer file.Close() | ||
87 | |||
88 | // 读取文件内容 | ||
89 | scanner := bufio.NewScanner(file) | ||
90 | scanner.Split(bufio.ScanLines) | ||
91 | for scanner.Scan() { | ||
92 | line := scanner.Text() | ||
93 | if strings.Contains(line, keyword) { | ||
94 | return true | ||
95 | } | ||
96 | } | ||
97 | return false | ||
98 | } | ||
99 | |||
100 | func getTimeFromStr(timeStr string) (time.Time, error) { | ||
101 | timestampFloat, err := strconv.ParseFloat(timeStr, 64) | ||
102 | if err != nil { | ||
103 | return time.Unix(0, 0), err | ||
104 | } | ||
105 | secs := int64(timestampFloat) | ||
106 | nsecs := int64((timestampFloat - float64(secs)) * 1e9) | ||
107 | |||
108 | // 只精确到毫秒就够了 | ||
109 | t := time.Unix(secs, nsecs).Truncate(time.Millisecond) | ||
110 | return t, nil | ||
111 | } | ||
112 | |||
113 | func hexToAscii(hexString string) string { | ||
114 | bytes := []byte{} | ||
115 | for i := 0; i < len(hexString); i += 2 { | ||
116 | hexPair := hexString[i : i+2] | ||
117 | // 将十六进制数转换为十进制数 | ||
118 | decimal, err := strconv.ParseInt(hexPair, 16, 8) | ||
119 | if err != nil { | ||
120 | return "Invalid hex string" | ||
121 | } | ||
122 | char := byte(decimal) | ||
123 | bytes = append(bytes, char) | ||
124 | } | ||
125 | |||
126 | asciiString := strings.ReplaceAll(string(bytes), "\000", " ") | ||
127 | |||
128 | return asciiString | ||
129 | } | ||