diff --git a/cmd/agent/gui/agent.go b/cmd/agent/gui/agent.go index 308436263fe873..ecbf7d96c9b0af 100644 --- a/cmd/agent/gui/agent.go +++ b/cmd/agent/gui/agent.go @@ -23,6 +23,7 @@ import ( "github.com/DataDog/datadog-agent/comp/core/flare/helpers" "github.com/DataDog/datadog-agent/pkg/config" "github.com/DataDog/datadog-agent/pkg/status" + "github.com/DataDog/datadog-agent/pkg/status/collector" "github.com/DataDog/datadog-agent/pkg/util/hostname" "github.com/DataDog/datadog-agent/pkg/util/log" "github.com/DataDog/datadog-agent/pkg/version" @@ -52,14 +53,23 @@ func ping(w http.ResponseWriter, r *http.Request) { func getStatus(w http.ResponseWriter, r *http.Request) { statusType := mux.Vars(r)["type"] - verbose := r.URL.Query().Get("verbose") == "true" - status, e := status.GetStatus(verbose) - if e != nil { - log.Errorf("Error getting status: " + e.Error()) - w.Write([]byte("Error getting status: " + e.Error())) + var stats map[string]interface{} + var err error + if statusType == "collector" { + stats = collector.GetStatus() + err = nil + } else { + verbose := r.URL.Query().Get("verbose") == "true" + stats, err = status.GetStatus(verbose) + } + + if err != nil { + log.Errorf("Error getting status: " + err.Error()) + w.Write([]byte("Error getting status: " + err.Error())) return } - json, _ := json.Marshal(status) + + json, _ := json.Marshal(stats) html, e := renderStatus(json, statusType) if e != nil { w.Write([]byte("Error generating status html: " + e.Error())) @@ -68,6 +78,7 @@ func getStatus(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/html") w.Write([]byte(html)) + } // Sends the current agent version diff --git a/pkg/status/status.go b/pkg/status/status.go index 9f80e1fa0c17dc..db5399829546a0 100644 --- a/pkg/status/status.go +++ b/pkg/status/status.go @@ -31,6 +31,7 @@ import ( "github.com/DataDog/datadog-agent/pkg/config/utils" logsStatus "github.com/DataDog/datadog-agent/pkg/logs/status" "github.com/DataDog/datadog-agent/pkg/snmp/traps" + "github.com/DataDog/datadog-agent/pkg/status/collector" "github.com/DataDog/datadog-agent/pkg/util/containers" "github.com/DataDog/datadog-agent/pkg/util/flavor" httputils "github.com/DataDog/datadog-agent/pkg/util/http" @@ -124,10 +125,8 @@ func GetAndFormatStatus() ([]byte, error) { // GetCheckStatusJSON gets the status of a single check as JSON func GetCheckStatusJSON(c check.Check, cs *checkstats.Stats) ([]byte, error) { - s, err := GetStatus(false) - if err != nil { - return nil, err - } + s := collector.GetStatus() + checks := s["runnerStats"].(map[string]interface{})["Checks"].(map[string]interface{}) checks[c.String()] = make(map[checkid.ID]interface{}) checks[c.String()].(map[checkid.ID]interface{})[c.ID()] = cs @@ -356,20 +355,7 @@ func expvarStats(stats map[string]interface{}) (map[string]interface{}, error) { json.Unmarshal(forwarderStatsJSON, &forwarderStats) //nolint:errcheck stats["forwarderStats"] = forwarderStats - runnerStatsJSON := []byte(expvar.Get("runner").String()) - runnerStats := make(map[string]interface{}) - json.Unmarshal(runnerStatsJSON, &runnerStats) //nolint:errcheck - stats["runnerStats"] = runnerStats - - autoConfigStatsJSON := []byte(expvar.Get("autoconfig").String()) - autoConfigStats := make(map[string]interface{}) - json.Unmarshal(autoConfigStatsJSON, &autoConfigStats) //nolint:errcheck - stats["autoConfigStats"] = autoConfigStats - - checkSchedulerStatsJSON := []byte(expvar.Get("CheckScheduler").String()) - checkSchedulerStats := make(map[string]interface{}) - json.Unmarshal(checkSchedulerStatsJSON, &checkSchedulerStats) //nolint:errcheck - stats["checkSchedulerStats"] = checkSchedulerStats + collector.UpdateStatus(stats) aggregatorStatsJSON := []byte(expvar.Get("aggregator").String()) aggregatorStats := make(map[string]interface{}) @@ -401,26 +387,6 @@ func expvarStats(stats map[string]interface{}) (map[string]interface{}, error) { stats["dogstatsdStats"] = dogstatsdStats } - pyLoaderData := expvar.Get("pyLoader") - if pyLoaderData != nil { - pyLoaderStatsJSON := []byte(pyLoaderData.String()) - pyLoaderStats := make(map[string]interface{}) - json.Unmarshal(pyLoaderStatsJSON, &pyLoaderStats) //nolint:errcheck - stats["pyLoaderStats"] = pyLoaderStats - } else { - stats["pyLoaderStats"] = nil - } - - pythonInitData := expvar.Get("pythonInit") - if pythonInitData != nil { - pythonInitJSON := []byte(pythonInitData.String()) - pythonInit := make(map[string]interface{}) - json.Unmarshal(pythonInitJSON, &pythonInit) //nolint:errcheck - stats["pythonInit"] = pythonInit - } else { - stats["pythonInit"] = nil - } - hostnameStatsJSON := []byte(expvar.Get("hostname").String()) hostnameStats := make(map[string]interface{}) json.Unmarshal(hostnameStatsJSON, &hostnameStats) //nolint:errcheck @@ -431,41 +397,6 @@ func expvarStats(stats map[string]interface{}) (map[string]interface{}, error) { stats["ntpOffset"], err = strconv.ParseFloat(expvar.Get("ntpOffset").String(), 64) } - inventories := expvar.Get("inventories") - var inventoriesStats map[string]interface{} - if inventories != nil { - inventoriesStatsJSON := []byte(inventories.String()) - json.Unmarshal(inventoriesStatsJSON, &inventoriesStats) //nolint:errcheck - } - - checkMetadata := map[string]map[string]string{} - if data, ok := inventoriesStats["check_metadata"]; ok { - for _, instances := range data.(map[string]interface{}) { - for _, instance := range instances.([]interface{}) { - metadata := map[string]string{} - checkHash := "" - for k, v := range instance.(map[string]interface{}) { - if vStr, ok := v.(string); ok { - if k == "config.hash" { - checkHash = vStr - } else if k != "config.provider" { - metadata[k] = vStr - } - } - } - if checkHash != "" && len(metadata) != 0 { - checkMetadata[checkHash] = metadata - } - } - } - } - stats["inventories"] = checkMetadata - if data, ok := inventoriesStats["agent_metadata"]; ok { - stats["agent_metadata"] = data - } else { - stats["agent_metadata"] = map[string]string{} - } - stats["snmpTrapsStats"] = traps.GetStatus() stats["netflowStats"] = netflowServer.GetStatus()