forked from sensu/uchiwa
-
Notifications
You must be signed in to change notification settings - Fork 3
/
helpers.go
402 lines (334 loc) · 9.31 KB
/
helpers.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
package helpers
import (
"encoding/json"
"errors"
"fmt"
"math/rand"
"net"
"net/http"
"reflect"
"time"
"github.com/sensu/uchiwa/uchiwa/logger"
"github.com/sensu/uchiwa/uchiwa/structs"
)
// return true if String in Slice, false otherwise
func StringInSlice(a string, list []string) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}
// BuildClientsMetrics builds the metrics for the events
func BuildClientsMetrics(clients *[]interface{}) *structs.StatusMetrics {
metrics := structs.StatusMetrics{}
metrics.Total = len(*clients)
for _, c := range *clients {
client := c.(map[string]interface{})
silenced, ok := client["silenced"].(bool)
if ok { // Do not ignore the client if we don't have a silenced attribute
if silenced {
metrics.Silenced++
continue
}
}
status, ok := client["status"].(int)
if !ok {
logger.Warningf("Could not assert the status to an int: %+v", client["status"])
continue
}
if status == 2.0 {
metrics.Critical++
continue
} else if status == 1.0 {
metrics.Warning++
continue
} else if status == 0.0 {
metrics.Healthy++
continue
}
metrics.Unknown++
}
return &metrics
}
// BuildEventsMetrics builds the metrics for the events
func BuildEventsMetrics(events *[]interface{}) *structs.StatusMetrics {
metrics := structs.StatusMetrics{}
metrics.Total = len(*events)
for _, e := range *events {
event := e.(map[string]interface{})
silenced, ok := event["silenced"].(bool)
if ok { // Do not ignore the event if we don't have a silenced attribute
if silenced {
metrics.Silenced++
continue
}
}
check, ok := event["check"].(map[string]interface{})
if !ok {
logger.Warningf("Could not assert this check to an interface: %+v", event["check"])
continue
}
status, ok := check["status"].(float64)
if !ok {
logger.Warningf("Could not assert this status to a flot64: %+v", check["status"])
continue
}
if status == 2.0 {
metrics.Critical++
continue
} else if status == 1.0 {
metrics.Warning++
continue
}
metrics.Unknown++
}
return &metrics
}
// GetBoolFromInterface ...
func GetBoolFromInterface(i interface{}) (bool, error) {
if i == nil {
logger.Debug("The interface is nil")
return false, errors.New("The interface is nil")
}
b, ok := i.(bool)
if !ok {
logger.Debugf("Could not assert to a boolean the interface: %+v", i)
return false, errors.New("Could not assert to a boolean the interface")
}
return b, nil
}
// GetEvent returns an event associated to a specific check
func GetEvent(check, client, dc string, events *[]interface{}) (map[string]interface{}, error) {
if check == "" || client == "" || dc == "" || len(*events) == 0 {
return nil, errors.New("No parameters should be empty")
}
for _, e := range *events {
event, ok := e.(map[string]interface{})
if !ok {
continue
}
if event["dc"] == nil || event["dc"] != dc {
continue
}
c, ok := event["client"].(map[string]interface{})
if !ok {
if event["client"] == nil || event["client"] != client {
continue
}
} else if c["name"] == nil || c["name"] != client {
continue
}
k, ok := event["check"].(map[string]interface{})
if !ok {
if event["check"] == nil || event["check"] != check {
continue
} else {
return map[string]interface{}{"check": event["check"], "client": event["client"], "occurrences": event["occurrences"], "output": event["output"], "status": event["status"]}, nil
}
} else if k["name"] == nil || k["name"] != check {
continue
}
if event["action"] != nil {
k["action"] = event["action"]
}
if event["occurrences"] != nil {
k["occurrences"] = event["occurrences"]
}
return k, nil
}
return nil, errors.New("No event found")
}
// GetInterfacesFromBytes returns a slice of interfaces from a slice of byte
func GetInterfacesFromBytes(bytes []byte) ([]interface{}, error) {
var interfaces []interface{}
if err := json.Unmarshal(bytes, &interfaces); err != nil {
return nil, err
}
return interfaces, nil
}
// GetMapFromBytes returns a map from a slice of byte
func GetMapFromBytes(bytes []byte) (map[string]interface{}, error) {
var m map[string]interface{}
if len(bytes) == 0 {
return m, nil
}
if err := json.Unmarshal(bytes, &m); err != nil {
return nil, err
}
return m, nil
}
// GetMapFromInterface returns a map from an interface
func GetMapFromInterface(i interface{}) map[string]interface{} {
m, ok := i.(map[string]interface{})
if !ok {
logger.Debugf("Could not assert to a map the interface: %+v", i)
return nil
}
return m
}
// GetIP returns the real user IP address
func GetIP(r *http.Request) string {
if xForwardedFor := r.Header.Get("X-FORWARDED-FOR"); len(xForwardedFor) > 0 {
return xForwardedFor
}
ip, _, _ := net.SplitHostPort(r.RemoteAddr)
return ip
}
// InterfaceToSlice takes a slice of type interface{} and returns a slice of interface
func InterfaceToSlice(slice interface{}) ([]interface{}, error) {
value := reflect.ValueOf(slice)
if value.Kind() != reflect.Slice {
return nil, fmt.Errorf("The interface provided is not a slice: %+v", slice)
}
result := make([]interface{}, value.Len())
for i := 0; i < value.Len(); i++ {
result[i] = value.Index(i).Interface()
}
return result, nil
}
// InterfaceToString takes a slice of interface{} a slice of string
func InterfaceToString(i []interface{}) []string {
var result []string
for _, value := range i {
v, ok := value.(string)
if ok {
result = append(result, v)
}
}
return result
}
// IsCheckSilenced determines whether a check for a particular client is silenced.
// Returns true if the check is silenced and a slice of silence entries IDs
func IsCheckSilenced(check, client map[string]interface{}, dc string, silenced []interface{}) (bool, []string) {
var isSilenced bool
var commonSubscriptions, isSilencedBy, subscribers, subscriptions []string
if dc == "" || len(silenced) == 0 {
return false, isSilencedBy
}
checkName, ok := check["name"].(string)
if !ok {
return false, isSilencedBy
}
clientName, ok := client["name"].(string)
if !ok {
clientName = ""
}
// Retrieve the check subscribers
if check["subscribers"] != nil {
s, ok := check["subscribers"].([]interface{})
if !ok {
return false, isSilencedBy
}
subscribers = InterfaceToString(s)
}
// Retrieve the client subscriptions
if client["subscriptions"] != nil {
s, ok := client["subscriptions"].([]interface{})
if !ok {
return false, isSilencedBy
}
subscriptions = InterfaceToString(s)
}
// Get the subscriptions both check and client have in common
for _, subscriber := range subscribers {
if IsStringInArray(subscriber, subscriptions) {
commonSubscriptions = append(commonSubscriptions, subscriber)
}
}
for _, silence := range silenced {
m, ok := silence.(map[string]interface{})
if !ok {
logger.Warningf("Could not assert this silence entry to a map: %+v", silence)
continue
}
if m["dc"] != dc {
continue
}
// Ignore silenced entries that have not begun yet
if m["begin"] != nil {
begin := time.Unix(int64(m["begin"].(float64)), 0)
now := time.Now()
if now.Before(begin) {
continue
}
}
// Check (e.g. *:check_cpu)
if m["id"] == fmt.Sprintf("*:%s", checkName) {
isSilenced = true
isSilencedBy = append(isSilencedBy, m["id"].(string))
continue
}
// Client subscription (e.g. client:foo:* )
if m["id"] == fmt.Sprintf("client:%s:*", clientName) {
isSilenced = true
isSilencedBy = append(isSilencedBy, m["id"].(string))
continue
}
// Client's check subscription (e.g. client:foo:check_cpu )
if m["id"] == fmt.Sprintf("client:%s:%s", clientName, checkName) {
isSilenced = true
isSilencedBy = append(isSilencedBy, m["id"].(string))
continue
}
for _, subscription := range commonSubscriptions {
// Subscription (e.g. load-balancer:* )
if m["id"] == fmt.Sprintf("%s:*", subscription) {
isSilenced = true
isSilencedBy = append(isSilencedBy, m["id"].(string))
continue
}
// Subscription' check (e.g. load-balancer:check_cpu)
if m["id"] == fmt.Sprintf("%s:%s", subscription, checkName) {
isSilenced = true
isSilencedBy = append(isSilencedBy, m["id"].(string))
continue
}
}
}
return isSilenced, isSilencedBy
}
// IsClientSilenced determines whether a client is silenced.
// Returns true if the client is silenced.
func IsClientSilenced(client, dc string, silenced []interface{}) bool {
if client == "" || dc == "" || len(silenced) == 0 {
return false
}
for _, silence := range silenced {
m, ok := silence.(map[string]interface{})
if !ok {
continue
}
if m["dc"] == dc && m["id"] == fmt.Sprintf("client:%s:*", client) {
return true
}
}
return false
}
// IsStringInArray searches 'array' for 'item' string
// Returns true 'item' is a value of 'array'
func IsStringInArray(item string, array []string) bool {
if item == "" || len(array) == 0 {
return false
}
for _, element := range array {
if element == item {
return true
}
}
return false
}
// RandomString generates a random string of the provided length
func RandomString(length int) string {
if length == 0 {
length = 32
}
char := "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
rand.Seed(time.Now().UTC().UnixNano())
buf := make([]byte, length)
for i := 0; i < length; i++ {
buf[i] = char[rand.Intn(len(char)-1)]
}
return string(buf)
}