This commit is contained in:
IT 老营长 @云轩领航-创始人 2025-03-01 11:36:56 +08:00
parent c63ebf3304
commit c8562f0272

View File

@ -4,22 +4,37 @@ import (
"strings" "strings"
"time" "time"
"github.com/shirou/gopsutil/v4/process"
"shylinux.com/x/ice" "shylinux.com/x/ice"
kit "shylinux.com/x/toolkits" kit "shylinux.com/x/toolkits"
"github.com/shirou/gopsutil/v4/mem"
"github.com/shirou/gopsutil/v4/process"
) )
type Process struct { type Process struct {
list string `name:"list name auto" help:"进程"` list string `name:"list name auto" help:"进程"`
} }
func (s Process) Weight(m *ice.Message, arg ...string) {
v, _ := mem.VirtualMemory()
stat := map[string]int{}
s.getList(func(p *process.Process, cmd string) {
name, _ := p.Name()
mem, _ := p.MemoryInfo()
stat[name] += int(mem.RSS)
})
kit.For(stat, func(key string, value int) {
m.Push("name", key).Push("rss", kit.FmtSize(value))
})
m.DisplayStoryPie().SortIntR("rss").Action(s.Weight)
m.Push("name", "rest").Push("rss", kit.FmtSize(v.Available))
}
func (s Process) List(m *ice.Message, arg ...string) { func (s Process) List(m *ice.Message, arg ...string) {
stat := map[string]int{} stat := map[string]int{}
ps, _ := process.Processes() m.Action(s.Weight)
for _, p := range ps { s.getList(func(p *process.Process, cmd string) {
cmd, _ := p.Cmdline() if len(arg) > 0 && !strings.Contains(cmd, arg[0]) {
if cmd == "" { return
continue
} }
t, _ := p.CreateTime() t, _ := p.CreateTime()
ts, _ := p.NumThreads() ts, _ := p.NumThreads()
@ -41,6 +56,7 @@ func (s Process) List(m *ice.Message, arg ...string) {
m.Push("reads", kit.FmtSize(rw.ReadBytes)) m.Push("reads", kit.FmtSize(rw.ReadBytes))
m.Push("writes", kit.FmtSize(rw.WriteBytes)) m.Push("writes", kit.FmtSize(rw.WriteBytes))
m.Push("user", user) m.Push("user", user)
m.Push("name", name)
m.Push("cmd", kit.Select(name, cmd)) m.Push("cmd", kit.Select(name, cmd))
stat["ts"] += int(ts) stat["ts"] += int(ts)
stat["cs"] += len(conn) stat["cs"] += len(conn)
@ -50,12 +66,12 @@ func (s Process) List(m *ice.Message, arg ...string) {
stat["ice.bin"] += int(mem.RSS) stat["ice.bin"] += int(mem.RSS)
stat["ice.num"]++ stat["ice.num"]++
if strings.Contains(cmd, "-bash -c") { if strings.Contains(cmd, "-bash -c") {
continue return
} }
kit.If(strings.Contains(cmd, "bin/ice.bin space"), func() { stat["ice.space"]++ }) kit.If(strings.Contains(cmd, "bin/ice.bin space"), func() { stat["ice.space"]++ })
kit.If(strings.Contains(cmd, "bin/ice.bin serve"), func() { stat["ice.serve"]++ }) kit.If(strings.Contains(cmd, "bin/ice.bin serve"), func() { stat["ice.serve"]++ })
} }
} })
stats := map[string]string{} stats := map[string]string{}
kit.For(stat, func(key string, value int) { kit.For(stat, func(key string, value int) {
if kit.IsIn(key, "rss", "ice.bin") { if kit.IsIn(key, "rss", "ice.bin") {
@ -68,3 +84,14 @@ func (s Process) List(m *ice.Message, arg ...string) {
} }
func init() { ice.Cmd("web.chat.dev.machine.process", Process{}) } func init() { ice.Cmd("web.chat.dev.machine.process", Process{}) }
func (s Process) getList(cb func(p *process.Process, cmd string)) {
ps, _ := process.Processes()
for _, p := range ps {
cmd, _ := p.Cmdline()
if cmd == "" {
continue
}
cb(p, cmd)
}
}