This commit is contained in:
david 2024-03-01 23:12:35 +08:00
parent 708542ff72
commit f0e4250488
3 changed files with 30 additions and 16 deletions

View File

@ -36,13 +36,13 @@ type ActionsReleases struct {
} }
type PsTable struct { type PsTable struct {
ServiceKey string ServiceKey string `table:"Service Key"`
Origin string Origin string
Status string Status string
Performance string Performance string
CurrentVersion string CurrentVersion string `table:"Current Version"`
RegisterVersion string RegisterVersion string `table:"Register Version"`
UpdatedAt string UpdatedAt string `table:"Updated At"`
} }
var psCmd = &cobra.Command{ var psCmd = &cobra.Command{
@ -63,6 +63,10 @@ func psExecute() {
err = json.Unmarshal(body, &ms) err = json.Unmarshal(body, &ms)
checkError(err) checkError(err)
if len(ms.Data) == 0 {
pt = append(pt, PsTable{})
}
for _, v := range ms.Data { for _, v := range ms.Data {
srvLine := getSrvStatus(v.ServiceKey) srvLine := getSrvStatus(v.ServiceKey)
srvLine.ServiceKey = v.ServiceKey srvLine.ServiceKey = v.ServiceKey
@ -70,7 +74,6 @@ func psExecute() {
srvLine.RegisterVersion = v.Version srvLine.RegisterVersion = v.Version
srvLine.UpdatedAt = v.UpdatedAt.Format(vars.YYYY_MM_DD_HH_MM_SS) srvLine.UpdatedAt = v.UpdatedAt.Format(vars.YYYY_MM_DD_HH_MM_SS)
pt = append(pt, srvLine) pt = append(pt, srvLine)
} }
// Output to stdout // Output to stdout
@ -78,12 +81,18 @@ func psExecute() {
} }
func getSrvStatus(srv string) PsTable { func getSrvStatus(srv string) PsTable {
isRun := checkProcessRunning(srv)
var status string = " - " var status string = " - "
if isRun { binExists := utils.PathExists(env.MeshEnv.Prefix + srv)
if binExists {
status = "Running" status = "Running"
} }
isRun := checkProcessRunning(srv)
if binExists && !isRun {
status = "Stop"
}
return PsTable{ return PsTable{
Status: status, Status: status,
CurrentVersion: getCurrentVersion(srv), CurrentVersion: getCurrentVersion(srv),
@ -115,7 +124,7 @@ func getCurrentVersion(srv string) string {
func getProcessInfo(processName string) string { func getProcessInfo(processName string) string {
var info string = " - " var info string = " - "
cmd := exec.Command("ps", "-eo", "pid,%cpu,%mem") // 使用ps命令查询进程信息 cmd := exec.Command("ps", "-eo", "pid,%cpu,%mem,cmd") // 使用ps命令查询进程信息
output, err := cmd.Output() // 获取命令输出结果 output, err := cmd.Output() // 获取命令输出结果
if err != nil { if err != nil {
return info return info
@ -125,8 +134,9 @@ func getProcessInfo(processName string) string {
lines := strings.Split(string(output), "\n")[1:] lines := strings.Split(string(output), "\n")[1:]
for _, line := range lines { for _, line := range lines {
if strings.Contains(line, processName) {
fields := strings.Fields(line) // 将每行按空格分隔为字段数组 fields := strings.Fields(line) // 将每行按空格分隔为字段数组
if len(fields) >= 3 && fields[2] == processName { if len(fields) >= 3 {
pid := fields[0] // PID进程标识 pid := fields[0] // PID进程标识
cpuUsage := fields[1] // CPU使用情况 cpuUsage := fields[1] // CPU使用情况
memoryUsage := fields[2] // 内存使用情况 memoryUsage := fields[2] // 内存使用情况
@ -134,6 +144,7 @@ func getProcessInfo(processName string) string {
return info return info
} }
} }
}
return info return info
} }

1
go.mod
View File

@ -14,6 +14,7 @@ require (
github.com/jaevor/go-nanoid v1.3.0 // indirect github.com/jaevor/go-nanoid v1.3.0 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect github.com/jinzhu/now v1.1.5 // indirect
github.com/keybase/go-ps v0.0.0-20190827175125-91aafc93ba19
github.com/modood/table v0.0.0-20220527013332-8d47e76dad33 github.com/modood/table v0.0.0-20220527013332-8d47e76dad33
github.com/spf13/cobra v1.8.0 github.com/spf13/cobra v1.8.0
gorm.io/gorm v1.25.7 // indirect gorm.io/gorm v1.25.7 // indirect

2
go.sum
View File

@ -11,6 +11,8 @@ github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/keybase/go-ps v0.0.0-20190827175125-91aafc93ba19 h1:WjT3fLi9n8YWh/Ih8Q1LHAPsTqGddPcHqscN+PJ3i68=
github.com/keybase/go-ps v0.0.0-20190827175125-91aafc93ba19/go.mod h1:hY+WOq6m2FpbvyrI93sMaypsttvaIL5nhVR92dTMUcQ=
github.com/modood/table v0.0.0-20220527013332-8d47e76dad33 h1:T5IbS9C1G2zeHb6eBy6OfIvj5tfQB23kGFpewCJuGDg= github.com/modood/table v0.0.0-20220527013332-8d47e76dad33 h1:T5IbS9C1G2zeHb6eBy6OfIvj5tfQB23kGFpewCJuGDg=
github.com/modood/table v0.0.0-20220527013332-8d47e76dad33/go.mod h1:41qyXVI5QH9/ObyPj27CGCVau5v/njfc3Gjj7yzr0HQ= github.com/modood/table v0.0.0-20220527013332-8d47e76dad33/go.mod h1:41qyXVI5QH9/ObyPj27CGCVau5v/njfc3Gjj7yzr0HQ=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=