diff --git a/pkg/cluster/manager/display.go b/pkg/cluster/manager/display.go index 1441b6039e..8965b14bb0 100644 --- a/pkg/cluster/manager/display.go +++ b/pkg/cluster/manager/display.go @@ -42,7 +42,6 @@ import ( "github.com/pingcap/tiup/pkg/set" "github.com/pingcap/tiup/pkg/tui" "github.com/pingcap/tiup/pkg/utils" - "go.uber.org/zap" ) // DisplayOption represents option of display command @@ -610,8 +609,9 @@ func (m *Manager) GetClusterTopology(dopt DisplayOption, opt operator.Options) ( e, found := ctxt.GetInner(ctx).GetExecutor(ins.GetManageHost()) if found { var active string + var systemdSince time.Duration nctx := checkpoint.NewContext(ctx) - active, memory, _ = operator.GetServiceStatus(nctx, e, ins.ServiceName()) + active, memory, systemdSince, _ = operator.GetServiceStatus(nctx, e, ins.ServiceName()) if status == "-" { if active == "active" { status = "Up" @@ -620,7 +620,7 @@ func (m *Manager) GetClusterTopology(dopt DisplayOption, opt operator.Options) ( } } if dopt.ShowUptime && since == "-" { - since = formatInstanceSince(parseSystemctlSince(active)) + since = formatInstanceSince(systemdSince) } } } @@ -733,37 +733,6 @@ func formatInstanceSince(uptime time.Duration) string { return strings.Join(parts, "") } -// `systemctl status xxx.service` returns as below -// Active: active (running) since Sat 2021-03-27 10:51:11 CST; 41min ago -func parseSystemctlSince(str string) (dur time.Duration) { - // if service is not found or other error, don't need to parse it - if str == "" { - return 0 - } - defer func() { - if dur == 0 { - zap.L().Warn("failed to parse systemctl since", zap.String("value", str)) - } - }() - parts := strings.Split(str, ";") - if len(parts) != 2 { - return - } - parts = strings.Split(parts[0], " ") - if len(parts) < 3 { - return - } - - dateStr := strings.Join(parts[len(parts)-3:], " ") - - tm, err := time.Parse("2006-01-02 15:04:05 MST", dateStr) - if err != nil { - return - } - - return time.Since(tm) -} - // SetSSHKeySet set ssh key set. func SetSSHKeySet(ctx context.Context, privateKeyPath string, publicKeyPath string) error { ctxt.GetInner(ctx).PrivateKeyPath = privateKeyPath diff --git a/pkg/cluster/operation/check.go b/pkg/cluster/operation/check.go index 3784bc69a4..f04981518e 100644 --- a/pkg/cluster/operation/check.go +++ b/pkg/cluster/operation/check.go @@ -520,7 +520,7 @@ func CheckServices(ctx context.Context, e ctxt.Executor, host, service string, d return result } - active, _, err := GetServiceStatus(ctx, e, service+".service") + active, _, _, err := GetServiceStatus(ctx, e, service+".service") if err != nil { result.Err = err } diff --git a/pkg/cluster/operation/systemd.go b/pkg/cluster/operation/systemd.go index 497d05db22..8c5ba4e2cc 100644 --- a/pkg/cluster/operation/systemd.go +++ b/pkg/cluster/operation/systemd.go @@ -16,10 +16,12 @@ package operator import ( "context" "strings" + "time" "github.com/pingcap/errors" "github.com/pingcap/tiup/pkg/cluster/ctxt" "github.com/pingcap/tiup/pkg/cluster/module" + "go.uber.org/zap" ) // GetServiceStatus return the Acitive line of status. @@ -34,7 +36,7 @@ import ( Mar 09 13:56:19 ip-172-16-5-70 systemd[1]: Started drainer-8249 service. */ -func GetServiceStatus(ctx context.Context, e ctxt.Executor, name string) (active, memory string, err error) { +func GetServiceStatus(ctx context.Context, e ctxt.Executor, name string) (active, memory string, since time.Duration, err error) { c := module.SystemdModuleConfig{ Unit: name, Action: "status", @@ -50,6 +52,7 @@ func GetServiceStatus(ctx context.Context, e ctxt.Executor, name string) (active switch words[0] { case "Active:": active = words[1] + since = parseSystemctlSince(line) case "Memory:": memory = words[1] } @@ -60,3 +63,34 @@ func GetServiceStatus(ctx context.Context, e ctxt.Executor, name string) (active } return } + +// `systemctl status xxx.service` returns as below +// Active: active (running) since Sat 2021-03-27 10:51:11 CST; 41min ago +func parseSystemctlSince(str string) (dur time.Duration) { + // if service is not found or other error, don't need to parse it + if str == "" { + return 0 + } + defer func() { + if dur == 0 { + zap.L().Warn("failed to parse systemctl since", zap.String("value", str)) + } + }() + parts := strings.Split(str, ";") + if len(parts) != 2 { + return + } + parts = strings.Split(parts[0], " ") + if len(parts) < 3 { + return + } + + dateStr := strings.Join(parts[len(parts)-3:], " ") + + tm, err := time.Parse("2006-01-02 15:04:05 MST", dateStr) + if err != nil { + return + } + + return time.Since(tm) +}