Skip to content

Commit 4389387

Browse files
committed
Feat: add info mode
1 parent aef479d commit 4389387

File tree

4 files changed

+120
-3
lines changed

4 files changed

+120
-3
lines changed

cmd/start/start.go

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,17 @@ import (
77
"io"
88
"os"
99
"runtime"
10+
"strconv"
1011

1112
pty "github.com/MCSManager/pty/console"
1213
"github.com/MCSManager/pty/utils"
1314
"github.com/mattn/go-colorable"
1415
)
1516

1617
var (
17-
dir, cmd, coder, ptySize, mode string
18-
cmds []string
19-
colorAble bool
18+
dir, cmd, coder, ptySize, pid, mode string
19+
cmds []string
20+
colorAble bool
2021
)
2122

2223
type PtyInfo struct {
@@ -32,6 +33,7 @@ func init() {
3233

3334
flag.BoolVar(&colorAble, "color", false, "colorable (default false)")
3435
flag.StringVar(&coder, "coder", "UTF-8", "Coder")
36+
flag.StringVar(&pid, "pid", "0", "detect pid info")
3537
flag.StringVar(&dir, "dir", ".", "command work path")
3638
flag.StringVar(&ptySize, "size", "80,50", "Initialize pty size, stdin will be forwarded directly")
3739
flag.StringVar(&mode, "m", "pty", "set mode")
@@ -42,16 +44,34 @@ func Main() {
4244
args := flag.Args()
4345
switch mode {
4446
case "zip":
47+
runtime.GOMAXPROCS(1)
4548
if err := utils.Zip(args[:len(args)-1], args[len(args)-1]); err != nil {
4649
fmt.Println(err.Error())
4750
os.Exit(1)
4851
}
4952
case "unzip":
53+
runtime.GOMAXPROCS(1)
5054
if err := utils.Unzip(args[0], args[1], coder); err != nil {
5155
fmt.Println(err.Error())
5256
os.Exit(1)
5357
}
58+
case "info":
59+
runtime.GOMAXPROCS(2)
60+
info := utils.NewInfo()
61+
upid, err := strconv.ParseInt(pid, 10, 32)
62+
if err != nil {
63+
fmt.Println(err.Error())
64+
os.Exit(1)
65+
}
66+
utils.Detect(int32(upid), info)
67+
pinfo, err := json.Marshal(info)
68+
if err != nil {
69+
fmt.Println(err.Error())
70+
os.Exit(1)
71+
}
72+
fmt.Println(string(pinfo))
5473
default:
74+
runtime.GOMAXPROCS(6)
5575
runPTY()
5676
}
5777
}

go.mod

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,19 @@ require (
66
github.com/creack/pty v1.1.18
77
github.com/juju/fslock v0.0.0-20160525022230-4d5c94c67b4b
88
github.com/mattn/go-colorable v0.1.13
9+
github.com/shirou/gopsutil/v3 v3.22.10
910
golang.org/x/text v0.3.7
1011
)
1112

1213
require (
14+
github.com/go-ole/go-ole v1.2.6 // indirect
1315
github.com/kr/text v0.2.0 // indirect
16+
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
1417
github.com/mattn/go-isatty v0.0.16 // indirect
18+
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
19+
github.com/tklauser/go-sysconf v0.3.10 // indirect
20+
github.com/tklauser/numcpus v0.4.0 // indirect
21+
github.com/yusufpapurcu/wmi v1.2.2 // indirect
1522
golang.org/x/sys v0.0.0-20220818161305-2296e01440c6 // indirect
1623
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
1724
)

go.sum

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
22
github.com/creack/pty v1.1.18 h1:n56/Zwd5o6whRC5PMGretI4IdRLlmBXYNjScPaBgsbY=
33
github.com/creack/pty v1.1.18/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4=
4+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
5+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
6+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
7+
github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY=
8+
github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
9+
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
10+
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
11+
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
412
github.com/juju/fslock v0.0.0-20160525022230-4d5c94c67b4b h1:FQ7+9fxhyp82ks9vAuyPzG0/vVbWwMwLJ+P6yJI5FN8=
513
github.com/juju/fslock v0.0.0-20160525022230-4d5c94c67b4b/go.mod h1:HMcgvsgd0Fjj4XXDkbjdmlbI505rUPBs6WBMYg2pXks=
614
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
@@ -9,14 +17,44 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
917
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
1018
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
1119
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
20+
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 h1:6E+4a0GO5zZEnZ81pIr0yLvtUWk2if982qA3F3QD6H4=
21+
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0/go.mod h1:zJYVVT2jmtg6P3p1VtQj7WsuWi/y4VnjVBn7F8KPB3I=
1222
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
1323
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
1424
github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peKQ=
1525
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
26+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
27+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
28+
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c h1:ncq/mPwQF4JjgDlrVEn3C11VoGHZN7m8qihwgMEtzYw=
29+
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c/go.mod h1:OmDBASR4679mdNQnz2pUhc2G8CO2JrUAVFDRBDP/hJE=
30+
github.com/shirou/gopsutil/v3 v3.22.10 h1:4KMHdfBRYXGF9skjDWiL4RA2N+E8dRdodU/bOZpPoVg=
31+
github.com/shirou/gopsutil/v3 v3.22.10/go.mod h1:QNza6r4YQoydyCfo6rH0blGfKahgibh4dQmV5xdFkQk=
32+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
33+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
34+
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
35+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
36+
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
37+
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
38+
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
39+
github.com/tklauser/go-sysconf v0.3.10 h1:IJ1AZGZRWbY8T5Vfk04D9WOA5WSejdflXxP03OUqALw=
40+
github.com/tklauser/go-sysconf v0.3.10/go.mod h1:C8XykCvCb+Gn0oNCWPIlcb0RuglQTYaQ2hGm7jmxEFk=
41+
github.com/tklauser/numcpus v0.4.0 h1:E53Dm1HjH1/R2/aoCtXtPgzmElmn51aOkhCFSuZq//o=
42+
github.com/tklauser/numcpus v0.4.0/go.mod h1:1+UI3pD8NW14VMwdgJNJ1ESk2UnwhAnz5hMwiKKqXCQ=
43+
github.com/yusufpapurcu/wmi v1.2.2 h1:KBNDSne4vP5mbSWnJbO+51IMOXJB67QiYCSBrubbPRg=
44+
github.com/yusufpapurcu/wmi v1.2.2/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0=
45+
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
46+
golang.org/x/sys v0.0.0-20201204225414-ed752295db88/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
47+
golang.org/x/sys v0.0.0-20220128215802-99c3d69c2c27/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
48+
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
1649
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
1750
golang.org/x/sys v0.0.0-20220818161305-2296e01440c6 h1:Sx/u41w+OwrInGdEckYmEuU5gHoGSL4QbDz3S9s6j4U=
1851
golang.org/x/sys v0.0.0-20220818161305-2296e01440c6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
1952
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
2053
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
54+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
55+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
2156
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
2257
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
58+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
59+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
60+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

utils/detect.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package utils
2+
3+
import (
4+
"sync"
5+
"sync/atomic"
6+
"time"
7+
8+
"github.com/shirou/gopsutil/v3/process"
9+
)
10+
11+
type Info struct {
12+
Mem uint64 `json:"mem"`
13+
Cpu float64 `json:"cpu"`
14+
NumConn int32 `json:"numConn"`
15+
IOReadSpeed uint64 `json:"ioReadSpeed"`
16+
IOWriteSpeed uint64 `json:"ioWriteSpeed"`
17+
lock *sync.Mutex
18+
}
19+
20+
func NewInfo() *Info {
21+
return &Info{lock: &sync.Mutex{}}
22+
}
23+
24+
func Detect(pid int32, info *Info) {
25+
p, err := process.NewProcess(pid)
26+
if err != nil {
27+
return
28+
}
29+
if children, err := p.Children(); err == nil {
30+
for _, v := range children {
31+
go Detect(v.Pid, info)
32+
}
33+
}
34+
if conn, err := p.Connections(); err == nil {
35+
atomic.AddInt32(&info.NumConn, int32(len(conn)))
36+
}
37+
if io1, err := p.IOCounters(); err == nil {
38+
time.Sleep(time.Millisecond * 250)
39+
if io2, err := p.IOCounters(); err == nil {
40+
atomic.AddUint64(&info.IOReadSpeed, (io2.ReadBytes-io1.ReadBytes)*4)
41+
atomic.AddUint64(&info.IOWriteSpeed, (io2.WriteBytes-io1.WriteBytes)*4)
42+
}
43+
}
44+
if mem, err := p.MemoryInfo(); err == nil {
45+
atomic.AddUint64(&info.Mem, mem.RSS)
46+
}
47+
if cpu, err := p.CPUPercent(); err == nil {
48+
info.lock.Lock()
49+
info.Cpu += cpu
50+
info.lock.Unlock()
51+
}
52+
}

0 commit comments

Comments
 (0)