forked from DataDog/datadog-agent
-
Notifications
You must be signed in to change notification settings - Fork 2
/
status.go
116 lines (98 loc) · 3.1 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
// 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"
"github.com/fatih/color"
"github.com/spf13/cobra"
"github.com/StackVista/stackstate-agent/cmd/security-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/StackVista/stackstate-agent/pkg/util/log"
)
var (
statusCmd = &cobra.Command{
Use: "status",
Short: "Print the current status",
Long: ``,
RunE: runStatus,
}
statusArgs = struct {
json bool
prettyPrintJSON bool
file string
}{}
)
func init() {
SecurityAgentCmd.AddCommand(statusCmd)
statusCmd.Flags().BoolVarP(&statusArgs.json, "json", "j", false, "print out raw json")
statusCmd.Flags().BoolVarP(&statusArgs.prettyPrintJSON, "pretty-json", "p", false, "pretty print JSON")
statusCmd.Flags().StringVarP(&statusArgs.file, "file", "o", "", "Output the status command to a file")
}
func runStatus(cmd *cobra.Command, args []string) error {
if flagNoColor {
color.NoColor = true
}
// Read configuration files received from the command line arguments '-c'
err := common.MergeConfigurationFiles("datadog", confPathArray, cmd.Flags().Lookup("cfgpath").Changed)
if err != nil {
return err
}
err = config.SetupLogger(loggerName, config.GetEnvDefault("DD_LOG_LEVEL", "off"), "", "", false, true, false)
if err != nil {
return log.Errorf("Cannot setup logger, exiting: %v", err)
}
return requestStatus()
}
func requestStatus() error {
fmt.Printf("Getting the status from the agent.\n")
var e error
var s string
c := util.GetClient(false) // FIX: get certificates right then make this true
urlstr := fmt.Sprintf("https://localhost:%v/agent/status", config.Datadog.GetInt("security_agent.cmd_port"))
// Set session token
e = util.SetAuthToken()
if e != nil {
return e
}
r, e := util.DoGet(c, urlstr)
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 security agent: %v
Make sure the agent is running before requesting the status.
Contact support if you continue having issues.`, e)
return e
}
// The rendering is done in the client so that the agent has less work to do
if statusArgs.prettyPrintJSON {
var prettyJSON bytes.Buffer
json.Indent(&prettyJSON, r, "", " ") //nolint:errcheck
s = prettyJSON.String()
} else if statusArgs.json {
s = string(r)
} else {
formattedStatus, err := status.FormatSecurityAgentStatus(r)
if err != nil {
return err
}
s = formattedStatus
}
if statusArgs.file != "" {
ioutil.WriteFile(statusArgs.file, []byte(s), 0644) //nolint:errcheck
} else {
fmt.Println(s)
}
return nil
}