forked from DataDog/datadog-agent
-
Notifications
You must be signed in to change notification settings - Fork 2
/
status.go
220 lines (188 loc) · 5.84 KB
/
status.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
212
213
214
215
216
217
218
219
220
// 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 app
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"time"
"github.com/StackVista/stackstate-agent/cmd/agent/common"
"github.com/StackVista/stackstate-agent/pkg/api/util"
"github.com/StackVista/stackstate-agent/pkg/config"
"github.com/StackVista/stackstate-agent/pkg/status"
"github.com/fatih/color"
"github.com/spf13/cobra"
)
var (
jsonStatus bool
prettyPrintJSON bool
statusFilePath string
)
func init() {
AgentCmd.AddCommand(statusCmd)
statusCmd.Flags().BoolVarP(&jsonStatus, "json", "j", false, "print out raw json")
statusCmd.Flags().BoolVarP(&prettyPrintJSON, "pretty-json", "p", false, "pretty print JSON")
statusCmd.Flags().StringVarP(&statusFilePath, "file", "o", "", "Output the status command to a file")
statusCmd.AddCommand(componentCmd)
componentCmd.Flags().BoolVarP(&prettyPrintJSON, "pretty-json", "p", false, "pretty print JSON")
componentCmd.Flags().StringVarP(&statusFilePath, "file", "o", "", "Output the status command to a file")
}
var statusCmd = &cobra.Command{
Use: "status [component [name]]",
Short: "Print the current status",
Long: ``,
RunE: func(cmd *cobra.Command, args []string) error {
if flagNoColor {
color.NoColor = true
}
// Prevent autoconfig to run when running status as it logs before logger is setup
// Cannot rely on config.Override as env detection is run before overrides are set
os.Setenv("DD_AUTOCONFIG_FROM_ENVIRONMENT", "false")
err := common.SetupConfigWithoutSecrets(confFilePath, "")
if err != nil {
return fmt.Errorf("unable to set up global agent configuration: %v", err)
}
err = config.SetupLogger(loggerName, config.GetEnvDefault("DD_LOG_LEVEL", "off"), "", "", false, true, false)
if err != nil {
fmt.Printf("Cannot setup logger, exiting: %v\n", err)
return err
}
_ = common.SetupSystemProbeConfig(sysProbeConfFilePath)
return requestStatus()
},
}
var componentCmd = &cobra.Command{
Use: "component",
Short: "Print the component status",
Long: ``,
RunE: func(cmd *cobra.Command, args []string) error {
err := common.SetupConfigWithoutSecrets(confFilePath, "")
if err != nil {
return fmt.Errorf("unable to set up global agent configuration: %v", err)
}
if flagNoColor {
color.NoColor = true
}
if len(args) != 1 {
return fmt.Errorf("a component name must be specified")
}
return componentStatus(args[0])
},
}
func requestStatus() error {
var s string
if !prettyPrintJSON && !jsonStatus {
fmt.Printf("Getting the status from the agent.\n\n")
}
ipcAddress, err := config.GetIPCAddress()
if err != nil {
return err
}
urlstr := fmt.Sprintf("https://%v:%v/agent/status", ipcAddress, config.Datadog.GetInt("cmd_port"))
r, err := makeRequest(urlstr)
if err != nil {
return err
}
// attach trace-agent status, if obtainable
temp := make(map[string]interface{})
if err := json.Unmarshal(r, &temp); err == nil {
temp["apmStats"] = getAPMStatus()
if newr, err := json.Marshal(temp); err == nil {
r = newr
}
}
// The rendering is done in the client so that the agent has less work to do
if prettyPrintJSON {
var prettyJSON bytes.Buffer
json.Indent(&prettyJSON, r, "", " ") //nolint:errcheck
s = prettyJSON.String()
} else if jsonStatus {
s = string(r)
} else {
formattedStatus, err := status.FormatStatus(r)
if err != nil {
return err
}
s = formattedStatus
}
if statusFilePath != "" {
ioutil.WriteFile(statusFilePath, []byte(s), 0644) //nolint:errcheck
} else {
fmt.Println(s)
}
return nil
}
// getAPMStatus returns a set of key/value pairs summarizing the status of the trace-agent.
// If the status can not be obtained for any reason, the returned map will contain an "error"
// key with an explanation.
func getAPMStatus() map[string]interface{} {
port := 8126
if config.Datadog.IsSet("apm_config.receiver_port") {
port = config.Datadog.GetInt("apm_config.receiver_port")
}
url := fmt.Sprintf("http://localhost:%d/debug/vars", port)
resp, err := (&http.Client{Timeout: 2 * time.Second}).Get(url)
if err != nil {
return map[string]interface{}{
"port": port,
"error": err.Error(),
}
}
defer resp.Body.Close()
status := make(map[string]interface{})
if err := json.NewDecoder(resp.Body).Decode(&status); err != nil {
return map[string]interface{}{
"port": port,
"error": err.Error(),
}
}
return status
}
func componentStatus(component string) error {
var s string
urlstr := fmt.Sprintf("https://localhost:%v/agent/%s/status", config.Datadog.GetInt("cmd_port"), component)
r, err := makeRequest(urlstr)
if err != nil {
return err
}
// The rendering is done in the client so that the agent has less work to do
if prettyPrintJSON {
var prettyJSON bytes.Buffer
json.Indent(&prettyJSON, r, "", " ") //nolint:errcheck
s = prettyJSON.String()
} else {
s = string(r)
}
if statusFilePath != "" {
ioutil.WriteFile(statusFilePath, []byte(s), 0644) //nolint:errcheck
} else {
fmt.Println(s)
}
return nil
}
func makeRequest(url string) ([]byte, error) {
var e error
c := util.GetClient(false) // FIX: get certificates right then make this true
// Set session token
e = util.SetAuthToken()
if e != nil {
return nil, e
}
r, e := util.DoGet(c, url)
if e != nil {
var errMap = make(map[string]string)
json.Unmarshal(r, &errMap) //nolint:errcheck
// If the error has been marshalled into a json object, check it and return it properly
if err, found := errMap["error"]; found {
e = fmt.Errorf(err)
}
fmt.Printf("Could not reach agent: %v \nMake sure the agent is running before requesting the status and contact support if you continue having issues. \n", e)
return nil, e
}
return r, nil
}