This repository has been archived by the owner on Apr 10, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathreports.go
151 lines (136 loc) · 4.22 KB
/
reports.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
// OBSOLETED by webui-report.go
// Web reports generator
package main
import (
"fmt"
"github.com/hoisie/mustache" // TODO migrate back to native golang templates
"strconv"
"time"
)
type ReportData struct {
Vars map[string]string
TableData []map[string]string // array of table rows
}
func ReportMainPage() []byte {
return []byte(ReportMainPageTemplate)
}
// Errors for 3 last hours for all groups
// TODO number of hours make variable
func Report3Hours(vars map[string]string) []byte {
var values []map[string]string
var page string
ErrHistory.RLock()
defer ErrHistory.RUnlock()
now := time.Now()
curhour := now.Format("06010215")
// h2ago := now.Add(-2 * time.Hour).Format("06010215")
//h3ago := now.Add(-3 * time.Hour).Format("06010215")
tmptbl := make(map[string]map[string]map[string]string)
for key, val := range ErrHistory.count {
errtype := ""
if key.Curhour == curhour {
switch key.ErrType {
case SLOW, VERYSLOW:
errtype = "sw"
case BADSTATUS:
errtype = "bs"
case BADURI:
errtype = "bu"
case RTIMEOUT:
errtype = "rt"
case CTIMEOUT:
errtype = "ct"
case REFUSED:
errtype = "cr"
default:
continue // count only errors listed above
}
if _, exists := tmptbl[key.Group]; !exists {
tmptbl[key.Group] = make(map[string]map[string]string)
}
if _, exists := tmptbl[key.Group][key.Name]; !exists {
tmptbl[key.Group][key.Name] = make(map[string]string)
}
tmptbl[key.Group][key.Name][errtype] = strconv.FormatInt(int64(val), 10)
tmptbl[key.Group][key.Name]["group"] = key.Group
tmptbl[key.Group][key.Name]["name"] = key.Name
tmptbl[key.Group][key.Name]["uri"] = key.URI
switch { // how much errors per hour?
case val == 1:
tmptbl[key.Group][key.Name][fmt.Sprintf("%s-severity", errtype)] = "info"
case val > 1 && val <= 6:
tmptbl[key.Group][key.Name][fmt.Sprintf("%s-severity", errtype)] = "warning"
case val > 6:
tmptbl[key.Group][key.Name][fmt.Sprintf("%s-severity", errtype)] = "error"
default:
tmptbl[key.Group][key.Name][fmt.Sprintf("%s-severity", errtype)] = "success"
}
}
}
for _, val := range tmptbl {
for _, counter := range val {
values = append(values, counter)
}
}
page = mustache.Render(Report3HoursTemplate, ReportData{TableData: values})
return []byte(page)
}
// Report last state of reported streams. Only failures counted.
func ReportLast(vars map[string]string, critical bool) []byte {
var values []map[string]string
var page string
ReportedStreams.RLock()
defer ReportedStreams.RUnlock()
if _, exists := vars["group"]; exists { // report for selected group
for _, value := range ReportedStreams.data[vars["group"]] {
rprtLastAddRow(&values, value, critical)
}
page = mustache.Render(ReportGroupLastTemplate, ReportData{Vars: vars, TableData: values})
} else { // report for all groups
for _, group := range ReportedStreams.data {
for _, value := range group {
rprtLastAddRow(&values, value, critical)
}
}
page = mustache.Render(ReportLastTemplate, ReportData{TableData: values})
}
return []byte(page)
}
// Helper. For "last*" report.
func rprtLastAddRow(values *[]map[string]string, value StreamStats, critical bool) {
var severity, report string
switch {
case value.Last.ErrType > WARNING_LEVEL && value.Last.ErrType < ERROR_LEVEL:
if critical {
return
}
severity = "warning"
case value.Last.ErrType > ERROR_LEVEL && value.Last.ErrType < CRITICAL_LEVEL:
if critical {
return
}
severity = "error"
case value.Last.ErrType > CRITICAL_LEVEL:
severity = "critical"
default:
return
}
if critical {
report = "last-critical"
} else {
report = "last"
}
*values = append(*values, map[string]string{
"uri": value.Stream.URI,
"name": value.Stream.Name,
"group": value.Stream.Group,
"status": value.Last.HTTPStatus,
"contentlength": strconv.FormatInt(value.Last.ContentLength, 10),
"started": value.Last.Started.Format(TimeFormat),
"elapsed": strconv.FormatFloat(value.Last.Elapsed.Seconds(), 'f', 3, 64),
"error": StreamErrText(value.Last.ErrType),
"totalerrs": strconv.FormatUint(uint64(value.Last.TotalErrs), 10),
"severity": severity,
"report": report, // report name
})
}