Skip to content

Commit

Permalink
use collector package to just collect check information
Browse files Browse the repository at this point in the history
  • Loading branch information
GustavoCaso committed Nov 8, 2023
1 parent cebc686 commit ff86696
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 79 deletions.
23 changes: 17 additions & 6 deletions cmd/agent/gui/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()))
Expand All @@ -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
Expand Down
77 changes: 4 additions & 73 deletions pkg/status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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{})
Expand Down Expand Up @@ -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
Expand All @@ -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()
Expand Down

0 comments on commit ff86696

Please sign in to comment.