forked from infinityworks/prometheus-rancher-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.go
318 lines (294 loc) · 9.23 KB
/
metrics.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
package main
import (
"fmt"
"sort"
"strings"
"github.com/prometheus/client_golang/prometheus"
)
func joinLabels(labels map[string]string) string {
var result string
var labelsArray []string
for name, val := range labels {
labelsArray = append(labelsArray, fmt.Sprintf("%s=%s", name, val))
}
if len(labelsArray) > 0 {
// Sort for same order
sort.Strings(labelsArray)
result = fmt.Sprintf(",%s,", strings.Join(labelsArray, ","))
}
return result
}
// addMetrics - Add's all of the GuageVecs to the `guageVecs` map, returns the map.
func addMetrics() map[string]*prometheus.GaugeVec {
gaugeVecs := make(map[string]*prometheus.GaugeVec)
// Stack Metrics
gaugeVecs["stacksHealth"] = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "stack_health_status",
Help: "HealthState of defined stack as reported by Rancher",
}, []string{"name", "health_state", "system"})
gaugeVecs["stacksState"] = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "stack_state",
Help: "State of defined stack as reported by Rancher",
}, []string{"name", "state", "system"})
// Service Metrics
gaugeVecs["servicesScale"] = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "service_scale",
Help: "scale of defined service as reported by Rancher",
}, []string{"name", "stack_name", "labels"})
gaugeVecs["servicesHealth"] = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "service_health_status",
Help: "HealthState of the service, as reported by the Rancher API. Either (1) or (0)",
}, []string{"name", "stack_name", "health_state", "labels"})
gaugeVecs["servicesState"] = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "service_state",
Help: "State of the service, as reported by the Rancher API",
}, []string{"name", "stack_name", "state", "labels"})
// Host Metrics
gaugeVecs["hostsState"] = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "host_state",
Help: "State of defined host as reported by the Rancher API",
}, []string{"name", "state", "labels"})
gaugeVecs["hostAgentsState"] = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "host_agent_state",
Help: "State of defined host agent as reported by the Rancher API",
}, []string{"name", "state", "labels"})
gaugeVecs["hostCPUCount"] = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "host_cpu_count",
Help: "Number of CPU Cores on host",
}, []string{"name", "labels"})
gaugeVecs["hostMemTotal"] = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "host_mem_total",
Help: "Total memory size in MB",
}, []string{"name", "labels"})
gaugeVecs["hostMemFree"] = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "host_mem_free",
Help: "Free memory size in MB",
}, []string{"name", "labels"})
gaugeVecs["hostMountPointTotal"] = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "host_mountpoint_total",
Help: "Total size by mountpoint in MB",
}, []string{"name", "labels", "mountpoint"})
gaugeVecs["hostMountPointUsed"] = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "host_mountpoint_used",
Help: "Used size by mountpoint in MB",
}, []string{"name", "labels", "mountpoint"})
// Cluster Metrics
gaugeVecs["clusterState"] = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "cluster_state",
Help: "State of defined cluster as reported by the Rancher API",
}, []string{"cluster_name", "state"})
gaugeVecs["clusterComponentStatus"] = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "cluster_component_status",
Help: "State of components in defined cluster as reported by the Rancher API",
}, []string{"cluster_name", "status", "component_name"})
// Node Metrics
gaugeVecs["nodeState"] = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "node_state",
Help: "State of defined node as reported by the Rancher API",
}, []string{"cluster_name", "state", "node_name"})
return gaugeVecs
}
// checkMetric - Checks the base type stored in the API is correct, this ensures we are setting the right metric for the right endpoint.
func checkMetric(endpoint string, baseType string) bool {
e := strings.TrimSuffix(endpoint, "s")
// Backwards compatibility fix, the API in V1 wrong, this is to cover v1 usage.
if baseType == "environment" && e == "stack" {
return true
} else if e == "service" && (baseType == "externalService" || baseType == "loadBalancerService") {
return true
} else if e != baseType {
log.Errorf("API MisMatch, expected %s metric, got %s metric", e, baseType)
return false
}
return true
}
// setServiceMetrics - Logic to set the state of a system as a gauge metric
func (e *Exporter) setServiceMetrics(name string, stack string, state string, health string, scale int, labels map[string]string) {
labelsStr := joinLabels(labels)
e.gaugeVecs["servicesScale"].With(prometheus.Labels{
"name": name,
"stack_name": stack,
"labels": labelsStr,
}).Set(float64(scale))
for _, y := range healthStates {
gauge := e.gaugeVecs["servicesHealth"].With(prometheus.Labels{
"name": name,
"stack_name": stack,
"health_state": y,
"labels": labelsStr,
})
if health == y {
gauge.Set(1)
} else {
gauge.Set(0)
}
}
for _, y := range serviceStates {
gauge := e.gaugeVecs["servicesState"].With(prometheus.Labels{
"name": name,
"stack_name": stack,
"state": y,
"labels": labelsStr,
})
if state == y {
gauge.Set(1)
} else {
gauge.Set(0)
}
}
}
// setStackMetrics - Logic to set the state of a system as a gauge metric
func (e *Exporter) setStackMetrics(name string, state string, health string, system string) {
for _, y := range healthStates {
gauge := e.gaugeVecs["stacksHealth"].With(prometheus.Labels{
"name": name,
"health_state": y,
"system": system,
})
if health == y {
gauge.Set(1)
} else {
gauge.Set(0)
}
}
for _, y := range stackStates {
gauge := e.gaugeVecs["stacksState"].With(prometheus.Labels{
"name": name,
"state": y,
"system": system,
})
if state == y {
gauge.Set(1)
} else {
gauge.Set(0)
}
}
}
func (e *Exporter) setHostInfoMetrics(name string, hi *HostInfo, labels map[string]string) {
labelsStr := joinLabels(labels)
e.gaugeVecs["hostCPUCount"].With(prometheus.Labels{
"name": name,
"labels": labelsStr,
}).Set(float64(hi.CPUInfo.Count))
e.gaugeVecs["hostMemTotal"].With(prometheus.Labels{
"name": name,
"labels": labelsStr,
}).Set(float64(hi.MemoryInfo.MemTotal))
e.gaugeVecs["hostMemFree"].With(prometheus.Labels{
"name": name,
"labels": labelsStr,
}).Set(float64(hi.MemoryInfo.MemFree))
for mountName, mountPoint := range hi.DiskInfo.MountPoints {
e.gaugeVecs["hostMountPointTotal"].With(prometheus.Labels{
"name": name,
"labels": labelsStr,
"mountpoint": mountName,
}).Set(float64(mountPoint.Total))
e.gaugeVecs["hostMountPointUsed"].With(prometheus.Labels{
"name": name,
"labels": labelsStr,
"mountpoint": mountName,
}).Set(float64(mountPoint.Used))
}
}
// setHostStateMetrics - Logic to set the state of a system as a gauge metric
func (e *Exporter) setHostStateMetrics(name string, state, agentState string, labels map[string]string) {
labelsStr := joinLabels(labels)
for _, y := range hostStates {
gauge := e.gaugeVecs["hostsState"].With(prometheus.Labels{
"name": name,
"state": y,
"labels": labelsStr,
})
if state == y {
gauge.Set(1)
} else {
gauge.Set(0)
}
}
for _, y := range agentStates {
gauge := e.gaugeVecs["hostAgentsState"].With(prometheus.Labels{
"name": name,
"state": y,
"labels": labelsStr,
})
if agentState == y {
gauge.Set(1)
} else {
gauge.Set(0)
}
}
}
// setClusterMetrics - Logic to set the state of a system as a gauge metric
func (e *Exporter) setClusterMetrics(name string, state string, statuses []*ComponentStatuses) {
for _, y := range clusterStates {
gauge := e.gaugeVecs["clusterState"].With(prometheus.Labels{
"cluster_name": name,
"state": y,
})
if state == y {
gauge.Set(1)
} else {
gauge.Set(0)
}
}
for _, status := range statuses {
for _, y := range componentStatus {
gauge := e.gaugeVecs["clusterComponentStatus"].With(prometheus.Labels{
"cluster_name": name,
"status": y,
"component_name": status.Name,
})
if status.Conditions[0].Status == y {
gauge.Set(1)
} else {
gauge.Set(0)
}
}
}
}
// setNodeMetrics - Logic to set the state of a system as a gauge metric
func (e *Exporter) setNodeMetrics(nodeName string, state string, clusterName string) {
for _, y := range nodeStates {
gauge := e.gaugeVecs["nodeState"].With(prometheus.Labels{
"cluster_name": clusterName,
"state": y,
"node_name": nodeName,
})
if state == y {
gauge.Set(1)
} else {
gauge.Set(0)
}
}
}