-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
status.go
153 lines (125 loc) · 3.41 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
// 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.
//nolint:revive // TODO(PROC) Fix revive linter
package status
import (
"fmt"
"io"
"os"
"text/template"
"github.com/spf13/cobra"
"go.uber.org/fx"
"github.com/DataDog/datadog-agent/cmd/process-agent/command"
"github.com/DataDog/datadog-agent/comp/core"
"github.com/DataDog/datadog-agent/comp/core/config"
"github.com/DataDog/datadog-agent/comp/core/log"
compStatus "github.com/DataDog/datadog-agent/comp/core/status"
"github.com/DataDog/datadog-agent/comp/process"
apiutil "github.com/DataDog/datadog-agent/pkg/api/util"
"github.com/DataDog/datadog-agent/pkg/collector/python"
ddconfig "github.com/DataDog/datadog-agent/pkg/config"
"github.com/DataDog/datadog-agent/pkg/process/util/status"
"github.com/DataDog/datadog-agent/pkg/util/fxutil"
)
var httpClient = apiutil.GetClient(false)
const (
notRunning = `
=============
Process Agent
=============
The Process Agent is not running
`
errorMessage = `
=====
Error
=====
{{ . }}
`
)
type cliParams struct {
*command.GlobalParams
}
type dependencies struct {
fx.In
CliParams *cliParams
Config config.Component
Log log.Component
}
// Commands returns a slice of subcommands for the `status` command in the Process Agent
func Commands(globalParams *command.GlobalParams) []*cobra.Command {
cliParams := &cliParams{
GlobalParams: globalParams,
}
// statusCmd is a cobra command that prints the current status
statusCmd := &cobra.Command{
Use: "status",
Short: "Print the current status",
Long: ``,
RunE: func(cmd *cobra.Command, args []string) error {
return fxutil.OneShot(runStatus,
fx.Supply(cliParams, command.GetCoreBundleParamsForOneShot(globalParams)),
fx.Supply(
compStatus.Params{
PythonVersionGetFunc: python.GetPythonVersion,
},
),
core.Bundle(),
process.Bundle(),
)
},
}
return []*cobra.Command{statusCmd}
}
func writeNotRunning(log log.Component, w io.Writer) {
_, err := fmt.Fprint(w, notRunning)
if err != nil {
_ = log.Error(err)
}
}
func writeError(log log.Component, w io.Writer, e error) {
tpl, err := template.New("").Funcs(compStatus.TextFmap()).Parse(errorMessage)
if err != nil {
_ = log.Error(err)
}
err = tpl.Execute(w, e)
if err != nil {
_ = log.Error(err)
}
}
func fetchStatus(statusURL string) ([]byte, error) {
body, err := apiutil.DoGet(httpClient, statusURL, apiutil.LeaveConnectionOpen)
if err != nil {
return nil, status.NewConnectionError(err)
}
return body, nil
}
// getAndWriteStatus calls the status server and writes it to `w`
func getAndWriteStatus(log log.Component, statusURL string, w io.Writer) {
body, err := fetchStatus(statusURL)
if err != nil {
writeNotRunning(log, w)
return
}
_, err = w.Write([]byte(body))
if err != nil {
_ = log.Error(err)
}
}
func getStatusURL() (string, error) {
addressPort, err := ddconfig.GetProcessAPIAddressPort()
if err != nil {
return "", fmt.Errorf("config error: %s", err.Error())
}
return fmt.Sprintf("http://%s/agent/status", addressPort), nil
}
func runStatus(deps dependencies) error {
statusURL, err := getStatusURL()
if err != nil {
writeError(deps.Log, os.Stdout, err)
return err
}
getAndWriteStatus(deps.Log, statusURL, os.Stdout)
return nil
}