From dfdb18f83f7a957f99196369d97827d6209eeb9a Mon Sep 17 00:00:00 2001 From: We-unite <3205135446@qq.com> Date: Tue, 13 Aug 2024 10:53:24 +0800 Subject: Filtering process data from mongodb First of all, fix sth in listener to fit the function of filter. Ori- ginally, listener mark the /usr/bin/containerd process id with star, but the children in db is updated by ppid, which is pid of parent but not tgid, so the stared pid has no children. To Fix this, we add all the pid of /usr/bin/containerd into the db, and set their ptgid/tgid, so that they're just normal process as others. Maybe we should finish the info of these processes? haha. Then, the filter of pid. There're some designed steps to do, and their methods are as follows: - Initially, because of the multithreading execution of listener, there may be several entries for the same process, and we should merge them. Extract data from database into a slice, and use a map to record process info. Iterate the slice, if the pid is in the map, then merge them, else insert into the map. - Then, we should build process tree, but what we have is pid. So use another data structure, iterate merged process map, and build a map from tgid to a slice of processes. Find out the star. Build a map from pid to its tgid. - BFS. Design a simple queue, and build the tree from the root(stared tgid), record all the visited tgid in another map. That's just the tree. As usual, let's talk about the remaining issues: - Some pids did not recieve exit message. Check the exit time of its tgid, or even its ppid. - Optimize the data structure, record the tree by itself. Now the tree is recorded by not only the last helloTree map from tgid to slice but the map from pid to tgid. It's hard to store in the database. Design a better ds, so the viewer can build the tree quickly from the data in db. - For future file filter, the close time, the same file for the same pid, and the pathName of a file, should be paid mych attention. Fighting! --- filter/global.go | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 filter/global.go (limited to 'filter/global.go') diff --git a/filter/global.go b/filter/global.go new file mode 100644 index 0000000..45706d4 --- /dev/null +++ b/filter/global.go @@ -0,0 +1,92 @@ +package main + +import ( + "fmt" + "time" +) + +type Exec struct { + Timestamp time.Time `bson:"timestamp"` + ExecArgs []string `bson:"execArgs"` +} + +type Process struct { + Star bool `bson:"star"` + StartTimestamp time.Time `bson:"start_timestamp"` + Ppid int `bson:"ppid"` + ParentTgid int `bson:"parentTgid"` + Pid int `bson:"pid"` + Tgid int `bson:"tgid"` + Args []string `bson:"args"` + Comm string `bson:"comm"` + RootFS string `bson:"rootfs"` + Cwd string `bson:"cwd"` + Children []int `bson:"children"` + Execve []Exec `bson:"execve"` + ExitCode int `bson:"exit_code"` + ExitSignal int `bson:"exit_signal"` + ExitTimestamp time.Time `bson:"exit_timestamp"` +} + +func (p Process) String() string { + var res string + res = "" + res += fmt.Sprintf("timestamp\t%v\n", p.StartTimestamp) + res += fmt.Sprintf("ppid\t%d\nparentTgid\t%d\n", p.Ppid, p.ParentTgid) + res += fmt.Sprintf("pid\t%d\ntgid\t%d\nargs: ", p.Pid, p.Tgid) + for i := 0; i < len(p.Args); i++ { + res += fmt.Sprintf("%s ", p.Args[i]) + } + res += fmt.Sprintf("\ncomm\t%s\ncwd\t%s\n", p.Comm, p.Cwd) + if len(p.Execve) != 0 { + res += fmt.Sprintf("exec:\n") + for i := 0; i < len(p.Execve); i++ { + res += fmt.Sprintf("\ttimestamp: %v\n\texecArgs:\t", p.Execve[i].Timestamp) + for j := 0; j < len(p.Execve[i].ExecArgs); j++ { + res += fmt.Sprintf("%s ", p.Execve[i].ExecArgs[j]) + } + res += fmt.Sprintf("\n") + } + } + res += fmt.Sprintf("children: ") + for i := 0; i < len(p.Children); i++ { + res += fmt.Sprintf("%d ", p.Children[i]) + } + res += fmt.Sprintf("\n") + return res +} + +// Queue 定义一个队列结构体 +type Queue struct { + items []interface{} +} + +// NewQueue 创建一个新的队列 +func NewQueue() *Queue { + return &Queue{items: make([]interface{}, 0)} +} + +// Enqueue 向队列中添加一个元素 +func (q *Queue) Enqueue(item interface{}) { + q.items = append(q.items, item) +} + +// Dequeue 从队列中移除并返回队列前面的元素 +func (q *Queue) Dequeue() (interface{}, bool) { + if len(q.items) == 0 { + return nil, false + } + item := q.items[0] + q.items = q.items[1:] + return item, true +} + +// Size 返回队列中的元素数量 +func (q *Queue) Size() int { + return len(q.items) +} + +// IsEmpty 检查队列是否为空 +func (q *Queue) IsEmpty() bool { + return len(q.items) == 0 +} -- cgit v1.2.3-70-g09d2