-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
status.go
104 lines (84 loc) · 2.78 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
// 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 2022-present Datadog, Inc.
package server
import (
"embed"
"io"
"github.com/DataDog/datadog-agent/comp/core/status"
nfconfig "github.com/DataDog/datadog-agent/comp/netflow/config"
)
//go:embed status_templates
var templatesFS embed.FS
// netflowServerStatus represents the status of the server including details about
// listeners which are working and those which have closed.
type netflowServerStatus struct {
TotalListeners int
OpenListeners int
ClosedListeners int
WorkingListenerDetails []netflowListenerStatus
ClosedListenerDetails []netflowListenerStatus
}
// netflowListenerStatus handles logic related to pulling config information and associating it to an error.
type netflowListenerStatus struct {
Config nfconfig.ListenerConfig
Error string
FlowCount int64
}
// Provider provides the functionality to populate the status output
type Provider struct {
server *Server
}
// Name returns the name
func (Provider) Name() string {
return "NetFlow"
}
// Section return the section
func (Provider) Section() string {
return "NetFlow"
}
// JSON populates the status map
func (p Provider) JSON(_ bool, stats map[string]interface{}) error {
p.getStatus(stats)
return nil
}
// Text renders the text output
func (p Provider) Text(_ bool, buffer io.Writer) error {
return status.RenderText(templatesFS, "netflow.tmpl", buffer, p.populateStatus())
}
// HTML renders the html output
func (p Provider) HTML(_ bool, buffer io.Writer) error {
return status.RenderHTML(templatesFS, "netflowHTML.tmpl", buffer, p.populateStatus())
}
func (p Provider) getStatus(stats map[string]interface{}) {
workingListeners := []netflowListenerStatus{}
closedListenersList := []netflowListenerStatus{}
for _, listener := range p.server.listeners {
errorString := listener.error.Load()
if errorString != "" {
closedListenersList = append(closedListenersList, netflowListenerStatus{
Config: listener.config,
Error: errorString,
})
} else {
workingListeners = append(workingListeners, netflowListenerStatus{
Config: listener.config,
FlowCount: listener.flowCount.Load(),
})
}
}
status := netflowServerStatus{
TotalListeners: int(len(p.server.listeners)),
OpenListeners: int(len(workingListeners)),
ClosedListeners: int(len(closedListenersList)),
WorkingListenerDetails: workingListeners,
ClosedListenerDetails: closedListenersList,
}
stats["netflowStats"] = status
}
func (p Provider) populateStatus() map[string]interface{} {
stats := make(map[string]interface{})
p.getStatus(stats)
return stats
}