summaryrefslogtreecommitdiffstats
path: root/src/godo.go
diff options
context:
space:
mode:
authorWe-unite <3205135446@qq.com>2024-08-07 19:08:59 +0800
committerWe-unite <3205135446@qq.com>2024-08-07 19:08:59 +0800
commitea32e017e579f168d87732893335c38d539ac2f1 (patch)
tree96a893ae0ffd4c5186e1c87f2fd7c60a125e970a /src/godo.go
parent2104c8ac26f320eacc3fa04d608843c3bf0fdc57 (diff)
downloadgodo-collector.tar.gz
godo-collector.zip
Print err in stderr, Find out docker rootfs.collector
When I use godo, error infomation comes along with other output, so change all err report into stderr. And I listen to `pivot_root` sys- call to find out the root file system of dockers. However, I'm afraid of causing too more delay, so don't check rootfs of ppid and record in the pid. Besides, the method to deal with pivot_root is hardcoded, which may causes crush. Shall I listen to the chdir syscall to find out exact cwd? Maybe It's useful to the pivot_root? Next step: Find out appropriate data stracture, and add more file operations to be watched. This task must be completed this week.
Diffstat (limited to 'src/godo.go')
-rw-r--r--src/godo.go24
1 files changed, 10 insertions, 14 deletions
diff --git a/src/godo.go b/src/godo.go
index 923ef85..a30aa88 100644
--- a/src/godo.go
+++ b/src/godo.go
@@ -27,14 +27,14 @@ var (
27func main() { 27func main() {
28 // 检查用户身份,并添加auditd规则,监听所有syscall 28 // 检查用户身份,并添加auditd规则,监听所有syscall
29 if os.Geteuid() != 0 { 29 if os.Geteuid() != 0 {
30 fmt.Printf("Err: Please run me as root, %d!\n", os.Getegid()) 30 fmt.Fprintf(os.Stderr, "Err: Please run me as root, %d!\n", os.Getegid())
31 return 31 return
32 } 32 }
33 33
34 // 所有的系统调用号与名称的关系 34 // 所有的系统调用号与名称的关系
35 err := figureOutSyscalls() 35 err := figureOutSyscalls()
36 if err != nil { 36 if err != nil {
37 fmt.Printf("Error figuring out syscall numbers: %v\n", err) 37 fmt.Fprintf(os.Stderr, "Error figuring out syscall numbers: %v\n", err)
38 } 38 }
39 39
40 exec.Command("auditctl", "-D").Run() 40 exec.Command("auditctl", "-D").Run()
@@ -43,7 +43,7 @@ func main() {
43 43
44 var auditCmd *exec.Cmd 44 var auditCmd *exec.Cmd
45 45
46 pidSyscall := []string{"execve"} 46 pidSyscall := []string{"execve", "pivot_root"}
47 // // 设置监听规则 47 // // 设置监听规则
48 for i := 0; i < len(pidSyscall); i++ { 48 for i := 0; i < len(pidSyscall); i++ {
49 auditCmd = exec.Command("auditctl", "-a", "exit,always", "-F", "arch=b64", "-S", pidSyscall[i]) 49 auditCmd = exec.Command("auditctl", "-a", "exit,always", "-F", "arch=b64", "-S", pidSyscall[i])
@@ -61,14 +61,10 @@ func main() {
61 // 查找pid 61 // 查找pid
62 containerdPid, err = getPid() 62 containerdPid, err = getPid()
63 if err != nil { 63 if err != nil {
64 fmt.Printf("Error finding containerd: %v\n", err) 64 fmt.Fprintf(os.Stderr, "Error finding containerd: %v\n", err)
65 return 65 return
66 } 66 }
67 67
68 // 创世之神,1号进程
69 // 1号进程还是不要在进程树上直接出现了,不然它的小儿子们都会出现
70 // /usr/bin/containerd,也就是我们最关注的进程
71
72 // 开始运行,解析命令行参数后监听 68 // 开始运行,解析命令行参数后监听
73 if err := fs.Parse(os.Args[1:]); err != nil { 69 if err := fs.Parse(os.Args[1:]); err != nil {
74 log.Fatal(err) 70 log.Fatal(err)
@@ -81,8 +77,8 @@ func main() {
81 77
82func coroutine(client *libaudit.AuditClient) { 78func coroutine(client *libaudit.AuditClient) {
83 // 各协程至此开始 79 // 各协程至此开始
84 rawChan = make(chan interface{}) 80 rawChan = make(chan interface{}, 65536)
85 cookedChan = make(chan Event) 81 cookedChan = make(chan Event, 65536)
86 82
87 wg.Add(1) 83 wg.Add(1)
88 go procWatch() 84 go procWatch()
@@ -101,14 +97,14 @@ func coroutine(client *libaudit.AuditClient) {
101func procWatch() error { 97func procWatch() error {
102 ns, err := netlink.NewNetlinkSocket(syscall.NETLINK_CONNECTOR, 12345) 98 ns, err := netlink.NewNetlinkSocket(syscall.NETLINK_CONNECTOR, 12345)
103 if err != nil { 99 if err != nil {
104 fmt.Printf("Error creating socket: %v\n", err) 100 fmt.Fprintf(os.Stderr, "Error creating socket: %v\n", err)
105 return err 101 return err
106 } 102 }
107 defer ns.Close() 103 defer ns.Close()
108 for { 104 for {
109 res, err := ns.Receive(20) 105 res, err := ns.Receive(20)
110 if err != nil { 106 if err != nil {
111 fmt.Printf("Error recv: %v\n", err) 107 fmt.Fprintf(os.Stderr, "Error recv: %v\n", err)
112 continue 108 continue
113 } 109 }
114 for i := 0; i < len(res); i++ { 110 for i := 0; i < len(res); i++ {
@@ -146,7 +142,7 @@ func checkProc(pCooked *Event) {
146 fileName := fmt.Sprintf("/proc/%d/task/%d/cmdline", pCooked.tgid, pCooked.pid) 142 fileName := fmt.Sprintf("/proc/%d/task/%d/cmdline", pCooked.tgid, pCooked.pid)
147 fd, err := os.Open(fileName) 143 fd, err := os.Open(fileName)
148 if err != nil { 144 if err != nil {
149 fmt.Printf("Err: %v\n", err) 145 fmt.Fprintf(os.Stderr, "Err: %v\n", err)
150 return 146 return
151 } 147 }
152 148
@@ -162,7 +158,7 @@ func checkProc(pCooked *Event) {
162 fileName = fmt.Sprintf("/proc/%d/task/%d/cwd", pCooked.tgid, pCooked.pid) 158 fileName = fmt.Sprintf("/proc/%d/task/%d/cwd", pCooked.tgid, pCooked.pid)
163 pCooked.cwd, err = os.Readlink(fileName) 159 pCooked.cwd, err = os.Readlink(fileName)
164 if err != nil { 160 if err != nil {
165 fmt.Printf("Err readlink %s: %v\n", fileName, err) 161 fmt.Fprintf(os.Stderr, "Err: %v\n", err)
166 pCooked.cwd = "" 162 pCooked.cwd = ""
167 } 163 }
168} 164}