From 6b9487b9b2a42d1709d89d3b17dd21aa6d64e1ae Mon Sep 17 00:00:00 2001 From: zeaslity Date: Wed, 19 Apr 2023 11:25:55 +0800 Subject: [PATCH] [agent-go] [executor] real time executor --- agent-go/executor/RealTimeExecutor.go | 40 ++++++++++++++++++++++ agent-go/executor/RealTimeExecutor_test.go | 13 +++++++ agent-go/status/Status.go | 14 ++++++++ agent-go/status/Status_test.go | 7 ++++ 4 files changed, 74 insertions(+) create mode 100644 agent-go/executor/RealTimeExecutor.go create mode 100644 agent-go/executor/RealTimeExecutor_test.go diff --git a/agent-go/executor/RealTimeExecutor.go b/agent-go/executor/RealTimeExecutor.go new file mode 100644 index 0000000..b3c033b --- /dev/null +++ b/agent-go/executor/RealTimeExecutor.go @@ -0,0 +1,40 @@ +package executor + +import ( + "bufio" + "fmt" + "io" + "os/exec" +) + +func ReadTimeOutput(singleLineCommand []string) { + + cmd := exec.Command(singleLineCommand[0], singleLineCommand[1:]...) + stdout, err := cmd.StdoutPipe() + if err != nil { + panic(err) + } + stderr, err := cmd.StderrPipe() + if err != nil { + panic(err) + } + + if err := cmd.Start(); err != nil { + panic(err) + } + + go copyOutput(stdout) + go copyOutput(stderr) + + if err := cmd.Wait(); err != nil { + panic(err) + } + +} + +func copyOutput(r io.Reader) { + scanner := bufio.NewScanner(r) + for scanner.Scan() { + fmt.Println(scanner.Text()) + } +} diff --git a/agent-go/executor/RealTimeExecutor_test.go b/agent-go/executor/RealTimeExecutor_test.go new file mode 100644 index 0000000..ae0ea65 --- /dev/null +++ b/agent-go/executor/RealTimeExecutor_test.go @@ -0,0 +1,13 @@ +package executor + +import "testing" + +func TestReadTimeOutput(t *testing.T) { + strings := []string{ + "/bin/bash", + "/root/simple.sh", + } + + ReadTimeOutput(strings) + +} diff --git a/agent-go/status/Status.go b/agent-go/status/Status.go index 5e067a6..5e4e666 100644 --- a/agent-go/status/Status.go +++ b/agent-go/status/Status.go @@ -2,6 +2,8 @@ package status import ( logger2 "agent-go/logger" + "fmt" + "time" ) var log = logger2.Log @@ -13,6 +15,18 @@ type AgentStatus struct { DiskStatus *DiskStatus } +func ConvertToFormat(eventData float64) string { + duration := time.Duration(eventData) * time.Second + + fmt.Println(duration) + + hours := int(duration.Hours()) + minutes := int(duration.Minutes()) % 60 + seconds := int(duration.Seconds()) % 60 + milliseconds := duration.Milliseconds() % 1000 + return fmt.Sprintf("%02d:%02d:%02d,%03d", hours, minutes, seconds, milliseconds) +} + func Ping() string { return "PONG" } diff --git a/agent-go/status/Status_test.go b/agent-go/status/Status_test.go index b041447..2ba647d 100644 --- a/agent-go/status/Status_test.go +++ b/agent-go/status/Status_test.go @@ -6,6 +6,13 @@ import ( "testing" ) +func TestConvertToFormat(t *testing.T) { + + convertToFormat := ConvertToFormat(99.92) + + fmt.Println(convertToFormat) +} + func TestReportAppStatus(t *testing.T) { agentStatus := ReportAppStatus()