This repository has been archived by the owner on Jan 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 175
/
clients.go
122 lines (99 loc) · 2.78 KB
/
clients.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
package daemon
import (
"fmt"
"sync"
"github.com/mitchellh/mapstructure"
"github.com/sensu/uchiwa/uchiwa/helpers"
"github.com/sensu/uchiwa/uchiwa/logger"
"github.com/sensu/uchiwa/uchiwa/structs"
)
// buildClients constructs clients objects for frontend consumption
func (d *Daemon) buildClients() {
wg := &sync.WaitGroup{}
for _, c := range d.Data.Clients {
wg.Add(1)
go d.buildClient(c, wg)
}
wg.Wait()
}
func (d *Daemon) buildClient(c interface{}, wg *sync.WaitGroup) {
defer wg.Done()
client, ok := c.(map[string]interface{})
if !ok {
return
}
dc, ok := client["dc"].(string)
if !ok {
return
}
name, ok := client["name"].(string)
if !ok {
return
}
client["_id"] = fmt.Sprintf("%s/%s", dc, name)
client = findClientEvents(client, &d.Data.Events)
client["silenced"] = helpers.IsClientSilenced(name, dc, d.Data.Silenced)
}
// findClientEvents searches for all events related to a particular client
// and set the status and output attributes of this client based on the events found
func findClientEvents(client map[string]interface{}, events *[]interface{}) map[string]interface{} {
if len(*events) == 0 {
client["status"] = 0
} else {
var criticals, warnings int
var results []string
for _, e := range *events {
eventMap, ok := e.(map[string]interface{})
if !ok {
logger.Warningf("Could not convert the event to a map: %+v", eventMap)
continue
}
// skip this event if the check attribute does not exist
if eventMap["check"] == nil {
continue
}
// skip this event if the datacenter isn't the right one
if eventMap["dc"] == nil || eventMap["dc"] != client["dc"] {
continue
}
clientMap, ok := eventMap["client"].(map[string]interface{})
if !ok {
logger.Warningf("Could not convert the event's client to a map: %+v", eventMap)
continue
}
// skip this event if the client isn't the right one
if clientMap["name"] == nil || clientMap["name"] != client["name"] {
continue
}
// convert the check to a structure for easier handling
var check structs.GenericCheck
err := mapstructure.Decode(eventMap["check"], &check)
if err != nil {
logger.Warningf("Could not convert the event's check to a generic check structure: %s", err)
continue
}
if check.Status == 2 {
criticals++
} else if check.Status == 1 {
warnings++
}
results = append(results, check.Output)
}
if len(results) == 0 {
client["status"] = 0
} else if criticals > 0 {
client["status"] = 2
} else if warnings > 0 {
client["status"] = 1
} else {
client["status"] = 3
}
if len(results) == 1 {
client["output"] = results[0]
} else if len(results) > 1 {
output := fmt.Sprintf("%s and %d more...", results[0], (len(results) - 1))
client["output"] = output
}
}
return client
}