-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
render.go
263 lines (232 loc) · 8.43 KB
/
render.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
// 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 render has all the formating options for status output
package render
import (
"bytes"
"embed"
"encoding/json"
"fmt"
"io"
"path"
"text/template"
htmlTemplate "html/template"
"github.com/DataDog/datadog-agent/comp/core/status"
"github.com/DataDog/datadog-agent/pkg/config"
)
var fmap = status.TextFmap()
var htmlfmap = status.HTMLFmap()
// FormatStatus takes a json bytestring and prints out the formatted statuspage
func FormatStatus(data []byte) (string, error) {
stats, renderError, err := unmarshalStatus(data)
if renderError != "" || err != nil {
return renderError, err
}
title := fmt.Sprintf("Agent (v%s)", stats["version"])
stats["title"] = title
var b = new(bytes.Buffer)
headerFunc := func() error { return ParseTemplate(b, "/header.tmpl", stats) }
checkStatsFunc := func() error {
return ParseTemplate(b, "/collector.tmpl", stats)
}
jmxFetchFunc := func() error { return ParseTemplate(b, "/jmxfetch.tmpl", stats) }
forwarderFunc := func() error { return ParseTemplate(b, "/forwarder.tmpl", stats) }
endpointsFunc := func() error { return ParseTemplate(b, "/endpoints.tmpl", stats) }
logsAgentFunc := func() error { return ParseTemplate(b, "/logsagent.tmpl", stats) }
systemProbeFunc := func() error { return ParseTemplate(b, "/systemprobe.tmpl", stats) }
processAgentFunc := func() error { return ParseTemplate(b, "/process-agent.tmpl", stats) }
traceAgentFunc := func() error { return ParseTemplate(b, "/trace-agent.tmpl", stats) }
aggregatorFunc := func() error { return ParseTemplate(b, "/aggregator.tmpl", stats) }
dogstatsdFunc := func() error { return ParseTemplate(b, "/dogstatsd.tmpl", stats) }
clusterAgentFunc := func() error { return ParseTemplate(b, "/clusteragent.tmpl", stats) }
snmpTrapFunc := func() error { return ParseTemplate(b, "/snmp-traps.tmpl", stats) }
netflowFunc := func() error { return ParseTemplate(b, "/netflow.tmpl", stats) }
autodiscoveryFunc := func() error { return ParseTemplate(b, "/autodiscovery.tmpl", stats) }
remoteConfigFunc := func() error { return ParseTemplate(b, "/remoteconfig.tmpl", stats) }
otlpFunc := func() error { return ParseTemplate(b, "/otlp.tmpl", stats) }
var renderFuncs []func() error
if config.IsCLCRunner() {
renderFuncs = []func() error{headerFunc, checkStatsFunc, aggregatorFunc, endpointsFunc, clusterAgentFunc,
autodiscoveryFunc}
} else {
renderFuncs = []func() error{headerFunc, checkStatsFunc, jmxFetchFunc, forwarderFunc, endpointsFunc,
logsAgentFunc, systemProbeFunc, processAgentFunc, traceAgentFunc, aggregatorFunc, dogstatsdFunc,
clusterAgentFunc, snmpTrapFunc, netflowFunc, autodiscoveryFunc, remoteConfigFunc, otlpFunc}
}
var errs []error
for _, f := range renderFuncs {
if err := f(); err != nil {
errs = append(errs, err)
}
}
if err := renderErrors(b, errs); err != nil {
fmt.Println(err)
}
return b.String(), nil
}
// FormatDCAStatus takes a json bytestring and prints out the formatted statuspage
func FormatDCAStatus(data []byte) (string, error) {
stats, renderError, err := unmarshalStatus(data)
if renderError != "" || err != nil {
return renderError, err
}
// We nil these keys because we do not want to display that information in the collector template
stats["pyLoaderStats"] = nil
stats["pythonInit"] = nil
stats["inventories"] = nil
title := fmt.Sprintf("Datadog Cluster Agent (v%s)", stats["version"])
stats["title"] = title
var b = new(bytes.Buffer)
var errs []error
if err := ParseTemplate(b, "/header.tmpl", stats); err != nil {
errs = append(errs, err)
}
if err := ParseTemplate(b, "/collector.tmpl", stats); err != nil {
errs = append(errs, err)
}
if err := ParseTemplate(b, "/forwarder.tmpl", stats); err != nil {
errs = append(errs, err)
}
if err := ParseTemplate(b, "/endpoints.tmpl", stats); err != nil {
errs = append(errs, err)
}
if err := ParseTemplate(b, "/logsagent.tmpl", stats); err != nil {
errs = append(errs, err)
}
if err := ParseTemplate(b, "/autodiscovery.tmpl", stats); err != nil {
errs = append(errs, err)
}
if err := ParseTemplate(b, "/orchestrator.tmpl", stats); err != nil {
errs = append(errs, err)
}
if err := renderErrors(b, errs); err != nil {
fmt.Println(err)
}
return b.String(), nil
}
// FormatHPAStatus takes a json bytestring and prints out the formatted statuspage
func FormatHPAStatus(data []byte) (string, error) {
stats, renderError, err := unmarshalStatus(data)
if renderError != "" || err != nil {
return renderError, err
}
var b = new(bytes.Buffer)
var errs []error
if err := ParseTemplate(b, "/custommetricsprovider.tmpl", stats); err != nil {
errs = append(errs, err)
}
if err := renderErrors(b, errs); err != nil {
fmt.Println(err)
}
return b.String(), nil
}
// FormatSecurityAgentStatus takes a json bytestring and prints out the formatted status for security agent
func FormatSecurityAgentStatus(data []byte) (string, error) {
stats, renderError, err := unmarshalStatus(data)
if renderError != "" || err != nil {
return renderError, err
}
title := fmt.Sprintf("Datadog Security Agent (v%s)", stats["version"])
stats["title"] = title
var b = new(bytes.Buffer)
var errs []error
if err := ParseTemplate(b, "/header.tmpl", stats); err != nil {
errs = append(errs, err)
}
if err := ParseTemplate(b, "/runtimesecurity.tmpl", stats); err != nil {
errs = append(errs, err)
}
if err := ParseTemplate(b, "/compliance.tmpl", stats); err != nil {
errs = append(errs, err)
}
if err := renderErrors(b, errs); err != nil {
fmt.Println(err)
}
return b.String(), nil
}
// FormatProcessAgentStatus takes a json bytestring and prints out the formatted status for process-agent
func FormatProcessAgentStatus(data []byte) (string, error) {
stats, renderError, err := unmarshalStatus(data)
if renderError != "" || err != nil {
return renderError, err
}
var b = new(bytes.Buffer)
var errs []error
if err := ParseTemplate(b, "/process-agent.tmpl", stats); err != nil {
errs = append(errs, err)
}
if err := renderErrors(b, errs); err != nil {
fmt.Println(err)
}
return b.String(), nil
}
// FormatMetadataMapCLI builds the rendering in the metadataMapper template.
func FormatMetadataMapCLI(data []byte) (string, error) {
stats, renderError, err := unmarshalStatus(data)
if renderError != "" || err != nil {
return renderError, err
}
var b = new(bytes.Buffer)
var errs []error
if err := ParseTemplate(b, "/metadatamapper.tmpl", stats); err != nil {
errs = append(errs, err)
}
if err := renderErrors(b, errs); err != nil {
return "", err
}
return b.String(), nil
}
// FormatCheckStats takes a json bytestring and prints out the formatted collector template.
func FormatCheckStats(data []byte) (string, error) {
stats, renderError, err := unmarshalStatus(data)
if renderError != "" || err != nil {
return renderError, err
}
var b = new(bytes.Buffer)
var errs []error
if err := ParseTemplate(b, "/collector.tmpl", stats); err != nil {
errs = append(errs, err)
}
if err := renderErrors(b, errs); err != nil {
return "", err
}
return b.String(), nil
}
//go:embed templates
var templatesFS embed.FS
// ParseTemplate renders the text template with the data provided
func ParseTemplate(w io.Writer, templateName string, stats interface{}) error {
tmpl, tmplErr := templatesFS.ReadFile(path.Join("templates", templateName))
if tmplErr != nil {
return tmplErr
}
t := template.Must(template.New(templateName).Funcs(fmap).Parse(string(tmpl)))
return t.Execute(w, stats)
}
// ParseHTMLTemplate the HTML template with the data provided
func ParseHTMLTemplate(w io.Writer, templateName string, stats interface{}) error {
tmpl, tmplErr := templatesFS.ReadFile(path.Join("templates", templateName))
if tmplErr != nil {
return tmplErr
}
t := htmlTemplate.Must(htmlTemplate.New(templateName).Funcs(htmlfmap).Parse(string(tmpl)))
return t.Execute(w, stats)
}
func renderErrors(w io.Writer, errs []error) error {
if len(errs) > 0 {
return ParseTemplate(w, "/rendererrors.tmpl", errs)
}
return nil
}
func unmarshalStatus(data []byte) (stats map[string]interface{}, renderError string, err error) {
if err := json.Unmarshal(data, &stats); err != nil {
var b = new(bytes.Buffer)
if err := renderErrors(b, []error{err}); err != nil {
return nil, "", err
}
return nil, b.String(), nil
}
return stats, "", nil
}