From af44f06e75068060e20df170cc87e3b1d8aef64c Mon Sep 17 00:00:00 2001 From: GoEdgeLab Date: Wed, 8 May 2024 19:42:55 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0edge-node=20top=E5=91=BD?= =?UTF-8?q?=E4=BB=A4=EF=BC=8C=E7=94=A8=E6=9D=A5=E8=B0=83=E7=94=A8=E7=B3=BB?= =?UTF-8?q?=E7=BB=9F=E7=9A=84top=E5=91=BD=E4=BB=A4=E5=B9=B6=E4=BC=A0?= =?UTF-8?q?=E5=85=A5edge-node=E8=BF=9B=E7=A8=8BID?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/edge-node/main.go | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/cmd/edge-node/main.go b/cmd/edge-node/main.go index 4cf4233..a28deb1 100644 --- a/cmd/edge-node/main.go +++ b/cmd/edge-node/main.go @@ -9,6 +9,7 @@ import ( teaconst "github.com/TeaOSLab/EdgeNode/internal/const" "github.com/TeaOSLab/EdgeNode/internal/nodes" "github.com/TeaOSLab/EdgeNode/internal/utils" + executils "github.com/TeaOSLab/EdgeNode/internal/utils/exec" fsutils "github.com/TeaOSLab/EdgeNode/internal/utils/fs" "github.com/iwind/TeaGo/Tea" _ "github.com/iwind/TeaGo/bootstrap" @@ -22,8 +23,10 @@ import ( _ "net/http/pprof" "os" "path/filepath" + "runtime" "sort" "strings" + "syscall" "time" ) @@ -31,7 +34,7 @@ func main() { var app = apps.NewAppCmd(). Version(teaconst.Version). Product(teaconst.ProductName). - Usage(teaconst.ProcessName + " [-v|start|stop|restart|status|quit|test|reload|service|daemon|config|pprof|accesslog|uninstall]"). + Usage(teaconst.ProcessName + " [-v|start|stop|restart|status|quit|test|reload|service|daemon|config|pprof|top|accesslog|uninstall]"). Usage(teaconst.ProcessName + " [trackers|goman|conns|gc|bandwidth|disk|cache.garbage]"). Usage(teaconst.ProcessName + " [ip.drop|ip.reject|ip.remove|ip.close] IP") @@ -552,7 +555,7 @@ func main() { return } - err = os.WriteFile(Tea.Root + "/configs/api_node.yaml", configYAML, 0666) + err = os.WriteFile(Tea.Root+"/configs/api_node.yaml", configYAML, 0666) if err != nil { fmt.Println("[ERROR]write config failed: " + err.Error()) return @@ -560,6 +563,37 @@ func main() { fmt.Println("success") }) + app.On("top", func() { + var sock = gosock.NewTmpSock(teaconst.ProcessName) + reply, err := sock.Send(&gosock.Command{Code: "pid"}) + if err != nil { + fmt.Println("[ERROR]not started yet") + return + } + + var pid = maps.NewMap(reply.Params).GetInt("pid") + if pid <= 0 { + fmt.Println("[ERROR]invalid pid '" + types.String(pid) + "'") + return + } + + topExe, _ := executils.LookPath("top") + if len(topExe) > 0 { + if runtime.GOOS == "linux" { + err = syscall.Exec(topExe, []string{topExe, "-p", types.String(pid)}, os.Environ()) + } else if runtime.GOOS == "darwin" { + err = syscall.Exec(topExe, []string{topExe, "-pid", types.String(pid)}, os.Environ()) + } else { + fmt.Println("[ERROR]not supported os '" + runtime.GOOS + "'") + return + } + if err != nil { + fmt.Println("[ERROR]start failed: " + err.Error()) + } + } else { + fmt.Println("[ERROR]could not found 'top' command in this system") + } + }) app.Run(func() { var node = nodes.NewNode() node.Start()