-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
cluster_checks.go
162 lines (135 loc) · 4.39 KB
/
cluster_checks.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
// 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 flare
import (
"encoding/json"
"fmt"
"io"
"sort"
"text/tabwriter"
"github.com/fatih/color"
"github.com/DataDog/datadog-agent/comp/core/autodiscovery/providers/names"
"github.com/DataDog/datadog-agent/pkg/api/util"
"github.com/DataDog/datadog-agent/pkg/clusteragent/clusterchecks/types"
"github.com/DataDog/datadog-agent/pkg/config"
)
// GetClusterChecks dumps the clustercheck dispatching state to the writer
func GetClusterChecks(w io.Writer, checkName string) error {
urlstr := fmt.Sprintf("https://localhost:%v/api/v1/clusterchecks", config.Datadog.GetInt("cluster_agent.cmd_port"))
if w != color.Output {
color.NoColor = true
}
if !config.Datadog.GetBool("cluster_checks.enabled") {
fmt.Fprintln(w, "Cluster-checks are not enabled")
return nil
}
c := util.GetClient(false) // FIX: get certificates right then make this true
// Set session token
err := util.SetAuthToken(config.Datadog)
if err != nil {
return err
}
r, err := util.DoGet(c, urlstr, util.LeaveConnectionOpen)
if err != nil {
if r != nil && string(r) != "" {
fmt.Fprintf(w, "The agent ran into an error while checking config: %s\n", string(r))
} else {
fmt.Fprintf(w, "Failed to query the agent (running?): %s\n", err)
}
return err
}
var cr types.StateResponse
err = json.Unmarshal(r, &cr)
if err != nil {
return err
}
// Gracefully exit when dispatcher is not running
if len(cr.NotRunning) > 0 {
fmt.Fprintf(w, "Cluster-check dispatching logic not running: %s\n", cr.NotRunning)
return nil
}
// Print warmup message
if cr.Warmup {
fmt.Fprintf(w, "=== %s in progress ===\n", color.BlueString("Warmup"))
fmt.Fprintln(w, "No configuration has been processed yet")
fmt.Fprintln(w, "")
}
// Print dangling configs
if len(cr.Dangling) > 0 {
fmt.Fprintf(w, "=== %s configurations ===\n", color.RedString("Unassigned"))
for _, c := range cr.Dangling {
PrintConfig(w, c, checkName)
}
fmt.Fprintln(w, "")
}
// Print summary of agents
if len(cr.Nodes) == 0 {
fmt.Fprintf(w, "=== %s agent reporting ===\n", color.RedString("Zero"))
fmt.Fprintln(w, "No check will be dispatched until agents report to the cluster-agent")
return nil
}
fmt.Fprintf(w, "=== %d agents reporting ===\n", len(cr.Nodes))
sort.Slice(cr.Nodes, func(i, j int) bool { return cr.Nodes[i].Name < cr.Nodes[j].Name })
table := tabwriter.NewWriter(w, 0, 0, 3, ' ', 0)
fmt.Fprintln(table, "\nName\tRunning checks")
for _, n := range cr.Nodes {
fmt.Fprintf(table, "%s\t%d\n", n.Name, len(n.Configs))
}
table.Flush()
// Print per-node configurations
for _, node := range cr.Nodes {
if len(node.Configs) == 0 {
continue
}
fmt.Fprintf(w, "\n===== Checks on %s =====\n", color.HiMagentaString(node.Name))
for _, c := range node.Configs {
PrintConfig(w, c, checkName)
}
}
return nil
}
// GetEndpointsChecks dumps the endpointschecks dispatching state to the writer
func GetEndpointsChecks(w io.Writer, checkName string) error {
if !endpointschecksEnabled() {
return nil
}
urlstr := fmt.Sprintf("https://localhost:%v/api/v1/endpointschecks/configs", config.Datadog.GetInt("cluster_agent.cmd_port"))
if w != color.Output {
color.NoColor = true
}
c := util.GetClient(false) // FIX: get certificates right then make this true
// Set session token
if err := util.SetAuthToken(config.Datadog); err != nil {
return err
}
// Query the cluster agent API
r, err := util.DoGet(c, urlstr, util.LeaveConnectionOpen)
if err != nil {
if r != nil && string(r) != "" {
fmt.Fprintf(w, "The agent ran into an error while checking config: %s\n", string(r))
} else {
fmt.Fprintf(w, "Failed to query the agent (running?): %s\n", err)
}
return err
}
var cr types.ConfigResponse
if err = json.Unmarshal(r, &cr); err != nil {
return err
}
// Print summary of pod-backed endpointschecks
fmt.Fprintf(w, "\n===== %d Pod-backed Endpoints-Checks scheduled =====\n", len(cr.Configs))
for _, c := range cr.Configs {
PrintConfig(w, c, checkName)
}
return nil
}
func endpointschecksEnabled() bool {
for _, provider := range config.Datadog.GetStringSlice("extra_config_providers") {
if provider == names.KubeEndpointsRegisterName {
return true
}
}
return false
}