From 105db35920997fbf12e99ea24ca25096ca08eb29 Mon Sep 17 00:00:00 2001 From: Moises Botarro Date: Thu, 10 Feb 2022 19:30:34 +0100 Subject: [PATCH] fecth process-agent status in core agent --- pkg/status/render.go | 11 +++ pkg/status/status_process_agent.go | 43 ++++++---- pkg/status/templates/process-agent.tmpl | 106 +++++++++--------------- 3 files changed, 73 insertions(+), 87 deletions(-) diff --git a/pkg/status/render.go b/pkg/status/render.go index d6588e276e80b..c81fc71b67f66 100644 --- a/pkg/status/render.go +++ b/pkg/status/render.go @@ -139,6 +139,17 @@ func FormatSecurityAgentStatus(data []byte) (string, error) { return b.String(), nil } +// FormatProcessAgentStatus takes a json bytestring and prints out the formatted status for process-agent +func FormatProcessAgentStatus(data []byte) (string, error) { + var b = new(bytes.Buffer) + + stats := make(map[string]interface{}) + json.Unmarshal(data, &stats) //nolint:errcheck + renderStatusTemplate(b, "/process-agent.tmpl", stats) + + return b.String(), nil +} + // FormatMetadataMapCLI builds the rendering in the metadataMapper template. func FormatMetadataMapCLI(data []byte) (string, error) { var b = new(bytes.Buffer) diff --git a/pkg/status/status_process_agent.go b/pkg/status/status_process_agent.go index 63512737044aa..15deaa4d17686 100644 --- a/pkg/status/status_process_agent.go +++ b/pkg/status/status_process_agent.go @@ -6,30 +6,37 @@ package status import ( + "encoding/json" "fmt" + + "github.com/DataDog/datadog-agent/cmd/process-agent/api" + apiutil "github.com/DataDog/datadog-agent/pkg/api/util" ) -// GetProcessAgentStats returns the status command of the process-agent -func GetProcessAgentStatus() map[string]interface{} { - //net.SetSystemProbePath(socketPath) - //probeUtil, err := net.GetRemoteSystemProbeUtil() +//TODO: do not use a global var for this +var httpClient = apiutil.GetClient(false) - //if err != nil { - // return map[string]interface{}{ - // "Errors": fmt.Sprintf("%v", err), - // } - //} +// GetProcessAgentStatus returns the status command of the process-agent +func GetProcessAgentStatus() map[string]interface{} { + s := make(map[string]interface{}) + addressPort, err := api.GetAPIAddressPort() + if err != nil { + s["error"] = fmt.Sprintf("%v", err.Error()) + return s + } - processAgentStatus := map[string]interface{}{ - "Errors": fmt.Sprintf("Testing process-agent status"), + statusEndpoint := fmt.Sprintf("http://%s/agent/status", addressPort) + b, err := apiutil.DoGet(httpClient, statusEndpoint) + if err != nil { + s["error"] = fmt.Sprintf("%v", err.Error()) + return s } - //systemProbeDetails, err := probeUtil.GetStats() - //if err != nil { - // return map[string]interface{}{ - // "Errors": fmt.Sprintf("issue querying stats from system probe: %v", err), - // } - //} + err = json.Unmarshal(b, &s) + if err != nil { + s["error"] = fmt.Sprintf("%v", err.Error()) + return s + } - return processAgentStatus + return s } diff --git a/pkg/status/templates/process-agent.tmpl b/pkg/status/templates/process-agent.tmpl index cd94f3d92d079..29110f76bf908 100644 --- a/pkg/status/templates/process-agent.tmpl +++ b/pkg/status/templates/process-agent.tmpl @@ -2,79 +2,47 @@ Process Agent ============= -{{- if .Errors }} - Status: Testing Process Agent Status on main agent - Error: {{ .Errors }} +{{- if .error }} + Status: Not running or unreachable {{- else }} - Status: Running - Uptime: {{ .uptime }} - Last Updated: {{ formatUnixTime .updated_at }} -{{- end }} -{{- if .network_tracer }} + Version: {{ .core.version }} + Status date: {{ formatUnixTime .date }} + Process Agent Start: {{ formatUnixTime .expvars.uptime_nano }} + Pid: {{ .expvars.pid }} + Go Version: {{ .core.go_version }} + Build arch: {{ .core.build_arch }} + Log Level: {{ .core.config.log_level }} + Enabled Checks: {{ .expvars.enabled_checks }} + Allocated Memory: {{ .expvars.memstats.alloc }} bytes + Hostname: {{ .core.metadata.meta.hostname }} - NPM - === - {{- if .network_tracer.Error }} - Status: Not running - Error: {{ .network_tracer.Error }} + ================= + Process Endpoints + ================= + {{- with .expvars.endpoints}} + {{- range $key, $value := .}} + {{$key}} - API Key{{ if gt (len $value) 1}}s{{end}} ending with: + {{- range $idx, $apikey := $value }} + - {{$apikey}} + {{- end}} + {{- end}} {{- else }} - Status: Running - {{- if .network_tracer.tracer.last_check }} - Last Check: {{ formatUnixTime .network_tracer.tracer.last_check }} - {{- end }} - {{- if .network_tracer.state.clients }} - Client Count: {{ len .network_tracer.state.clients }} - {{- end }} - {{- end }} -{{- end }} -{{- if .oom_kill_probe }} - - OOM Kill - ======== - {{- if .oom_kill_probe.Error }} - Status: Not running - Error: {{ .oom_kill_probe.Error }} - {{- else }} - Status: Running - {{- if .oom_kill_probe.last_check }} - Last Check: {{ formatUnixTime .oom_kill_probe.last_check }} - {{- end }} - {{- end }} -{{- end }} -{{- if .tcp_queue_length_tracer }} - - TCP Queue Length - ================ - {{- if .tcp_queue_length_tracer.Error }} - Status: Not running - Error: {{ .tcp_queue_length_tracer.Error }} - {{- else }} - Status: Running - {{- if .tcp_queue_length_tracer.last_check }} - Last Check: {{ formatUnixTime .tcp_queue_length_tracer.last_check }} - {{- end }} - {{- end }} -{{- end }} -{{- if .security_runtime }} - Runtime Security - ================ - {{- if .security_runtime.Error }} - Status: Not running - Error: {{ .security_runtime.Error }} - {{- else }} - Status: Running + No endpoints information. The agent may be misconfigured. {{- end }} -{{- end }} -{{- if .process }} - Process - ======= - {{- if .process.Error }} - Status: Not running - Error: {{ .process.Error }} - {{- else }} - Status: Running - {{- end }} -{{- end }} + ========= + Collector + ========= + Last collection time: {{.expvars.last_collect_time}} + Docker socket: {{.expvars.docker_socket}} + Number of processes: {{.expvars.process_count}} + Number of containers: {{.expvars.container_count}} + Process Queue length: {{.expvars.process_queue_size}} + RTProcess Queue length: {{.expvars.rtprocess_queue_size}} + Pod Queue length: {{.expvars.pod_queue_size}} + Process Bytes enqueued: {{.expvars.process_queue_bytes}} + RTProcess Bytes enqueued: {{.expvars.rtprocess_queue_bytes}} + Pod Bytes enqueued: {{.expvars.pod_queue_bytes}} +{{ end }}