-
Notifications
You must be signed in to change notification settings - Fork 10
/
config_check.go
148 lines (132 loc) · 4.93 KB
/
config_check.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.
package flare
import (
"encoding/json"
"fmt"
"io"
"github.com/fatih/color"
"github.com/n9e/n9e-agentd/pkg/apiserver/response"
"github.com/n9e/n9e-agentd/staging/datadog-agent/pkg/api/util"
"github.com/n9e/n9e-agentd/pkg/autodiscovery/integration"
"github.com/n9e/n9e-agentd/staging/datadog-agent/pkg/collector/check"
"github.com/n9e/n9e-agentd/pkg/config"
)
// configCheckURL contains the Agent API endpoint URL exposing the loaded checks
var configCheckURL string
// GetConfigCheck dump all loaded configurations to the writer
func GetConfigCheck(w io.Writer, withDebug bool) error {
if w != color.Output {
color.NoColor = true
}
c := util.GetClient(false) // FIX: get certificates right then make this true
// Set session token
err := util.SetAuthToken()
if err != nil {
return err
}
ipcAddress, err := config.GetIPCAddress()
if err != nil {
return err
}
if configCheckURL == "" {
configCheckURL = fmt.Sprintf("https://%v:%v/agent/config-check", ipcAddress, config.Datadog.GetInt("cmd_port"))
}
r, err := util.DoGet(c, configCheckURL)
if err != nil {
if r != nil && string(r) != "" {
return fmt.Errorf("the agent ran into an error while checking config: %s", string(r))
}
return fmt.Errorf("failed to query the agent (running?): %s", err)
}
cr := response.ConfigCheckResponse{}
err = json.Unmarshal(r, &cr)
if err != nil {
return err
}
if len(cr.ConfigErrors) > 0 {
fmt.Fprintln(w, fmt.Sprintf("=== Configuration %s ===", color.RedString("errors")))
for check, error := range cr.ConfigErrors {
fmt.Fprintln(w, fmt.Sprintf("\n%s: %s", color.RedString(check), error))
}
}
for _, c := range cr.Configs {
PrintConfig(w, c)
}
if withDebug {
if len(cr.ResolveWarnings) > 0 {
fmt.Fprintln(w, fmt.Sprintf("\n=== Resolve %s ===", color.YellowString("warnings")))
for check, warnings := range cr.ResolveWarnings {
fmt.Fprintln(w, fmt.Sprintf("\n%s", color.YellowString(check)))
for _, warning := range warnings {
fmt.Fprintln(w, fmt.Sprintf("* %s", warning))
}
}
}
if len(cr.Unresolved) > 0 {
fmt.Fprintln(w, fmt.Sprintf("\n=== %s Configs ===", color.YellowString("Unresolved")))
for ids, configs := range cr.Unresolved {
fmt.Fprintln(w, fmt.Sprintf("\n%s: %s", color.BlueString("Auto-discovery IDs"), color.YellowString(ids)))
fmt.Fprintln(w, fmt.Sprintf("%s:", color.BlueString("Templates")))
for _, config := range configs {
fmt.Fprintln(w, config.String())
}
}
}
}
return nil
}
// GetClusterAgentConfigCheck proxies GetConfigCheck overidding the URL
func GetClusterAgentConfigCheck(w io.Writer, withDebug bool) error {
configCheckURL = fmt.Sprintf("https://localhost:%v/config-check", config.Datadog.GetInt("cluster_agent.cmd_port"))
return GetConfigCheck(w, withDebug)
}
// PrintConfig prints a human-readable representation of a configuration
func PrintConfig(w io.Writer, c integration.Config) {
if !c.ClusterCheck {
fmt.Fprintln(w, fmt.Sprintf("\n=== %s check ===", color.GreenString(c.Name)))
} else {
fmt.Fprintln(w, fmt.Sprintf("\n=== %s cluster check ===", color.GreenString(c.Name)))
}
if c.Provider != "" {
fmt.Fprintln(w, fmt.Sprintf("%s: %s", color.BlueString("Configuration provider"), color.CyanString(c.Provider)))
} else {
fmt.Fprintln(w, fmt.Sprintf("%s: %s", color.BlueString("Configuration provider"), color.RedString("Unknown provider")))
}
if c.Source != "" {
fmt.Fprintln(w, fmt.Sprintf("%s: %s", color.BlueString("Configuration source"), color.CyanString(c.Source)))
} else {
fmt.Fprintln(w, fmt.Sprintf("%s: %s", color.BlueString("Configuration source"), color.RedString("Unknown configuration source")))
}
for _, inst := range c.Instances {
ID := string(check.BuildID(c.Name, inst, c.InitConfig))
fmt.Fprintln(w, fmt.Sprintf("%s: %s", color.BlueString("Instance ID"), color.CyanString(ID)))
fmt.Fprint(w, fmt.Sprintf("%s", inst))
fmt.Fprintln(w, "~")
}
if len(c.InitConfig) > 0 {
fmt.Fprintln(w, fmt.Sprintf("%s:", color.BlueString("Init Config")))
fmt.Fprintln(w, string(c.InitConfig))
}
if len(c.MetricConfig) > 0 {
fmt.Fprintln(w, fmt.Sprintf("%s:", color.BlueString("Metric Config")))
fmt.Fprintln(w, string(c.MetricConfig))
}
if len(c.LogsConfig) > 0 {
fmt.Fprintln(w, fmt.Sprintf("%s:", color.BlueString("Log Config")))
fmt.Fprintln(w, string(c.LogsConfig))
}
if len(c.ADIdentifiers) > 0 {
fmt.Fprintln(w, fmt.Sprintf("%s:", color.BlueString("Auto-discovery IDs")))
for _, id := range c.ADIdentifiers {
fmt.Fprintln(w, fmt.Sprintf("* %s", color.CyanString(id)))
}
}
if c.NodeName != "" {
state := fmt.Sprintf("dispatched to %s", c.NodeName)
fmt.Fprintln(w, fmt.Sprintf("%s: %s", color.BlueString("State"), color.CyanString(state)))
}
fmt.Fprintln(w, "===")
}