/
config.go
211 lines (191 loc) · 5.72 KB
/
config.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// 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-2020 Datadog, Inc.
package app
import (
"bytes"
"encoding/json"
"fmt"
"html"
"github.com/DataDog/datadog-agent/cmd/agent/common"
"github.com/DataDog/datadog-agent/pkg/api/util"
"github.com/DataDog/datadog-agent/pkg/config"
"github.com/fatih/color"
"github.com/spf13/cobra"
)
func init() {
AgentCmd.AddCommand(configCommand)
configCommand.AddCommand(listRuntimeCommand)
configCommand.AddCommand(setCommand)
configCommand.AddCommand(getCommand)
}
var (
configCommand = &cobra.Command{
Use: "config",
Short: "Print the runtime configuration of a running agent",
Long: ``,
RunE: showRuntimeConfiguration,
}
listRuntimeCommand = &cobra.Command{
Use: "list-runtime",
Short: "List settings that can be changed at runtime",
Long: ``,
RunE: listRuntimeConfigurableValue,
}
setCommand = &cobra.Command{
Use: "set [setting] [value]",
Short: "Set, for the current runtime, the value of a given configuration setting",
Long: ``,
RunE: setConfigValue,
}
getCommand = &cobra.Command{
Use: "get [setting]",
Short: "Get, for the current runtime, the value of a given configuration setting",
Long: ``,
RunE: getConfigValue,
}
agentConfigURLPath = "/agent/config"
listRuntimeURLPath = agentConfigURLPath + "/list-runtime"
)
func setupConfig() error {
if flagNoColor {
color.NoColor = true
}
err := common.SetupConfigWithoutSecrets(confFilePath, "")
if err != nil {
return fmt.Errorf("unable to set up global agent configuration: %v", err)
}
err = config.SetupLogger(loggerName, config.GetEnv("DD_LOG_LEVEL", "off"), "", "", false, true, false)
if err != nil {
fmt.Printf("Cannot setup logger, exiting: %v\n", err)
return err
}
return util.SetAuthToken()
}
func showRuntimeConfiguration(cmd *cobra.Command, args []string) error {
err := setupConfig()
if err != nil {
return err
}
runtimeConfig, err := requestConfig()
if err != nil {
return err
}
fmt.Println(runtimeConfig)
return nil
}
func requestConfig() (string, error) {
c := util.GetClient(false)
ipcAddress, err := config.GetIPCAddress()
if err != nil {
return "", err
}
apiConfigURL := fmt.Sprintf("https://%v:%v"+agentConfigURLPath, ipcAddress, config.Datadog.GetInt("cmd_port"))
r, err := util.DoGet(c, apiConfigURL)
if err != nil {
var errMap = make(map[string]string)
json.Unmarshal(r, &errMap)
// If the error has been marshalled into a json object, check it and return it properly
if e, found := errMap["error"]; found {
return "", fmt.Errorf(e)
}
return "", fmt.Errorf("Could not reach agent: %v \nMake sure the agent is running before requesting the runtime configuration and contact support if you continue having issues", err)
}
return string(r), nil
}
func listRuntimeConfigurableValue(cmd *cobra.Command, args []string) error {
err := setupConfig()
if err != nil {
return err
}
c := util.GetClient(false)
ipcAddress, err := config.GetIPCAddress()
if err != nil {
return err
}
url := fmt.Sprintf("https://%v:%v"+listRuntimeURLPath, ipcAddress, config.Datadog.GetInt("cmd_port"))
r, err := util.DoGet(c, url)
if err != nil {
var errMap = make(map[string]string)
json.Unmarshal(r, &errMap)
// If the error has been marshalled into a json object, check it and return it properly
if e, found := errMap["error"]; found {
return fmt.Errorf(e)
}
return err
}
var settings = make(map[string]string)
err = json.Unmarshal(r, &settings)
if err != nil {
return err
}
fmt.Println("=== Settings that can be changed at runtime ===")
for setting, desc := range settings {
fmt.Printf("%s:\t\t\t%s\n", setting, desc)
}
return nil
}
func setConfigValue(cmd *cobra.Command, args []string) error {
if len(args) != 2 {
return fmt.Errorf("Exactly two parameters are required: the setting name and its value")
}
err := setupConfig()
if err != nil {
return err
}
c := util.GetClient(false)
ipcAddress, err := config.GetIPCAddress()
if err != nil {
return err
}
url := fmt.Sprintf("https://%v:%v"+agentConfigURLPath+"/%v", ipcAddress, config.Datadog.GetInt("cmd_port"), args[0])
body := fmt.Sprintf("value=%s", html.EscapeString(args[1]))
r, err := util.DoPost(c, url, "application/x-www-form-urlencoded", bytes.NewBuffer([]byte(body)))
if err != nil {
var errMap = make(map[string]string)
json.Unmarshal(r, &errMap)
// If the error has been marshalled into a json object, check it and return it properly
if e, found := errMap["error"]; found {
return fmt.Errorf(e)
}
return err
}
fmt.Printf("Configuration setting %s is now set to: %s\n", args[0], args[1])
return nil
}
func getConfigValue(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return fmt.Errorf("A single setting name must be specified")
}
err := setupConfig()
if err != nil {
return err
}
c := util.GetClient(false)
ipcAddress, err := config.GetIPCAddress()
if err != nil {
return err
}
url := fmt.Sprintf("https://%v:%v"+agentConfigURLPath+"/%v", ipcAddress, config.Datadog.GetInt("cmd_port"), args[0])
r, err := util.DoGet(c, url)
if err != nil {
var errMap = make(map[string]string)
json.Unmarshal(r, &errMap)
// If the error has been marshalled into a json object, check it and return it properly
if e, found := errMap["error"]; found {
return fmt.Errorf(e)
}
return err
}
var setting = make(map[string]interface{})
err = json.Unmarshal(r, &setting)
if err != nil {
return err
}
if value, found := setting["value"]; found {
fmt.Printf("%s is set to: %s\n", args[0], value)
return nil
}
return fmt.Errorf("unable to get value for this setting: %v", args[0])
}