This commit is contained in:
david 2024-03-01 23:53:20 +08:00
parent 5c2136561c
commit 1a5d90c706
1 changed files with 10 additions and 6 deletions

View File

@ -40,6 +40,7 @@ type PsTable struct {
ServiceKey string `table:"Service Key"` ServiceKey string `table:"Service Key"`
Origin string Origin string
Status string Status string
PID string
Performance string Performance string
CurrentVersion string `table:"Current Version"` CurrentVersion string `table:"Current Version"`
RegisterVersion string `table:"Register Version"` RegisterVersion string `table:"Register Version"`
@ -94,10 +95,13 @@ func getSrvStatus(srv string) PsTable {
status = "\033[31mStop\033[0m\n" status = "\033[31mStop\033[0m\n"
} }
pid, cm := getProcessInfo(srv)
return PsTable{ return PsTable{
Status: status, Status: status,
CurrentVersion: getCurrentVersion(srv), CurrentVersion: getCurrentVersion(srv),
Performance: getProcessInfo(srv), PID: pid,
Performance: cm,
} }
} }
@ -124,12 +128,12 @@ func getCurrentVersion(srv string) string {
return " - " return " - "
} }
func getProcessInfo(processName string) string { func getProcessInfo(processName string) (string, string) {
var info string = " - " var info string = " - "
cmd := exec.Command("ps", "-eo", "pid,%cpu,%mem,cmd") // 使用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, info
} }
// 将输出按换行符分隔成多行字符串数组 // 将输出按换行符分隔成多行字符串数组
@ -142,13 +146,13 @@ func getProcessInfo(processName string) string {
pid := fields[0] // PID进程标识 pid := fields[0] // PID进程标识
cpuUsage := fields[1] // CPU使用情况 cpuUsage := fields[1] // CPU使用情况
memoryUsage := fields[2] // 内存使用情况 memoryUsage := fields[2] // 内存使用情况
info = fmt.Sprintf("PID:%s / CPU:%s%% / MEM:%s%%", pid, cpuUsage, memoryUsage) info = fmt.Sprintf("CPU:%s%% / MEM:%s%%", pid, cpuUsage, memoryUsage)
return info return pid, info
} }
} }
} }
return info return info, info
} }
func checkProcessRunning(processName string) bool { func checkProcessRunning(processName string) bool {