-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp_dashboard.go
117 lines (107 loc) · 2.65 KB
/
http_dashboard.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
package tessen
import (
"bytes"
"fmt"
"net/http"
)
type DisplayWidget struct {
Name string
CritCount int
WarnCount int
UnknCount int
}
func getDashboardQueries() (queries []Query) {
opts := getOpts()
if q := opts["queries"]; q != nil {
qList := q.([]interface{})
for _, v := range qList {
q1 := v.(map[interface{}]interface{})
q2 := make(map[string]string)
for k, v := range q1 {
switch k := k.(type) {
case string:
switch v := v.(type) {
case string:
q2[k] = v
}
}
}
if q2["dashboard"] == "true" {
queries = append(queries, Query{q2["name"], q2["filter"], q2["template"], FindSourceByName(q2["source"])})
}
}
}
return queries
}
func countCritEvents(s *Source, qr []interface{}) (count int) {
if s.Provider == "uchiwa" {
return countUchiwaEvents(2, qr)
} else if s.Provider == "pagerduty" {
return countPagerDutyEvents("triggered", qr)
} else {
return 0
}
}
func countWarnEvents(s *Source, qr []interface{}) (count int) {
if s.Provider == "uchiwa" {
return countUchiwaEvents(1, qr)
} else if s.Provider == "pagerduty" {
return countPagerDutyEvents("acknowledged", qr)
} else {
return 0
}
}
func countUnknEvents(s *Source, qr []interface{}) (count int) {
if s.Provider == "uchiwa" {
return countUchiwaEvents(3, qr)
} else {
return 0
}
}
func countUchiwaEvents(status float64, qr []interface{}) (count int) {
for _, ev := range qr {
data := ev.(QueryResult).Data
if data == nil {
continue
}
check := data.(map[string]interface{})["check"].(map[string]interface{})
if check["status"].(float64) == status {
count++
}
}
return count
}
func countPagerDutyEvents(status string, qr []interface{}) (count int) {
for _, ev := range qr {
data := ev.(QueryResult).Data
log.Debugf("countPagerDutyEvents: ev data == %q", data)
if data == nil {
continue
}
incident := data.(map[string]interface{})
if incident["status"].(string) == status {
count++
}
}
return count
}
func dashboardHandler(w http.ResponseWriter, r *http.Request) {
queries := getDashboardQueries()
template := GetTemplate("event_http_dashboard_home")
displayWidgets := make([]DisplayWidget, 0)
for _, q := range queries {
qr := GetQueryResults(q)
s := q.Source
numCrit := countCritEvents(s, qr)
numWarn := countWarnEvents(s, qr)
numUnkn := countUnknEvents(s, qr)
displayWidgets = append(displayWidgets, DisplayWidget{q.Name, numCrit, numWarn, numUnkn})
}
buf := new(bytes.Buffer)
RunTemplate(template, displayWidgets, buf)
fmt.Fprintf(w, buf.String())
}
func StartHttpDashboard(listen string) error {
http.HandleFunc("/", dashboardHandler)
return http.ListenAndServe(listen, nil)
}