From 7d2a1629472707af21cdcbcc0925100094974bfe Mon Sep 17 00:00:00 2001 From: bin64 Date: Mon, 11 May 2026 19:59:41 +0800 Subject: [PATCH] fix: parse supervisor uptime with optional days segment --- agent/app/service/host_tool.go | 19 +++++++++-- agent/app/service/host_tool_test.go | 52 +++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 agent/app/service/host_tool_test.go diff --git a/agent/app/service/host_tool.go b/agent/app/service/host_tool.go index cf91d8528d6d..a79545f68471 100644 --- a/agent/app/service/host_tool.go +++ b/agent/app/service/host_tool.go @@ -613,8 +613,7 @@ func getProcessStatus(config *response.SupervisorProcessConfig, containerName st Status: fields[1], } if fields[1] == "RUNNING" { - status.PID = strings.TrimSuffix(fields[3], ",") - status.Uptime = fields[5] + status.PID, status.Uptime = parseSupervisorRunningDetails(fields) } else { status.Msg = strings.Join(fields[2:], " ") } @@ -623,3 +622,19 @@ func getProcessStatus(config *response.SupervisorProcessConfig, containerName st } return nil } + +func parseSupervisorRunningDetails(fields []string) (string, string) { + var pid, uptime string + if len(fields) > 3 && fields[2] == "pid" { + pid = strings.TrimSuffix(fields[3], ",") + } + for i := 4; i < len(fields); i++ { + if strings.TrimSuffix(fields[i], ",") == "uptime" { + if i+1 < len(fields) { + uptime = strings.Join(fields[i+1:], " ") + } + break + } + } + return pid, uptime +} diff --git a/agent/app/service/host_tool_test.go b/agent/app/service/host_tool_test.go new file mode 100644 index 000000000000..31c11bc58349 --- /dev/null +++ b/agent/app/service/host_tool_test.go @@ -0,0 +1,52 @@ +package service + +import ( + "strings" + "testing" +) + +func TestParseSupervisorRunningDetails(t *testing.T) { + tests := []struct { + name string + line string + wantPID string + wantUptime string + }{ + { + name: "short uptime", + line: "test:test_00 RUNNING pid 123, uptime 0:12:34", + wantPID: "123", + wantUptime: "0:12:34", + }, + { + name: "single day uptime", + line: "test:test_00 RUNNING pid 123, uptime 1 day, 0:12:34", + wantPID: "123", + wantUptime: "1 day, 0:12:34", + }, + { + name: "multiple days uptime", + line: "test:test_00 RUNNING pid 123, uptime 103 days, 0:12:34", + wantPID: "123", + wantUptime: "103 days, 0:12:34", + }, + { + name: "process name is uptime", + line: "uptime RUNNING pid 123, uptime 0:12:34", + wantPID: "123", + wantUptime: "0:12:34", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + pid, uptime := parseSupervisorRunningDetails(strings.Fields(tt.line)) + if pid != tt.wantPID { + t.Fatalf("pid = %q, want %q", pid, tt.wantPID) + } + if uptime != tt.wantUptime { + t.Fatalf("uptime = %q, want %q", uptime, tt.wantUptime) + } + }) + } +}