-
Notifications
You must be signed in to change notification settings - Fork 24
/
audit_template.go
373 lines (363 loc) · 16.9 KB
/
audit_template.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
/*
* Copyright 2022-present Kuei-chun Chen. All rights reserved.
* audit_template.go
*/
package hatchet
import (
"fmt"
"html/template"
"math/rand"
"strings"
"time"
"github.com/simagix/gox"
"golang.org/x/text/language"
"golang.org/x/text/message"
)
// GetAuditTablesTemplate returns HTML
func GetAuditTablesTemplate() (*template.Template, error) {
html := headers + getContentHTML()
html += `{{$name := .Hatchet}}
<div style='margin: 5px 5px; width=100%; clear: left;'>
<table style='border: none; margin: 10px 10px; width=100%; clear: left;' width='100%'>
{{$flag := coinToss}}
<tr><td style='border:none; vertical-align: top; padding: 5px; background-color: #F3F7F4;'>
<img class='rotate23' src='data:image/png;base64,{{ assignConsultant $flag }}'></img></td>
<td class='summary'>
{{getInfoSummary .Info $flag}}<p/>{{getStatsSummary .Data}}</td>
</tr>
</table>
</div>
{{if hasData .Data "exception"}}
<table style='float: left; margin: 10px 10px; clear: left;'>
<caption><button class='btn'
onClick="javascript:loadData('/hatchets/{{.Hatchet}}/logs/all?severity=W'); return false;">
<i class='fa fa-search'></i></button>Exceptions</caption>
<tr><th></th><th>Severity</th><th>Total</th></tr>
{{range $n, $val := index .Data "exception"}}
<tr><td align=right>{{add $n 1}}</td>
<td>
<button class='btn' onClick="javascript:loadData('/hatchets/{{$name}}/logs/all?severity={{slice $val.Name 0 1}}'); return false;"><i class='fa fa-search'></i></button>{{$val.Name}}
</td>
<td align=right>{{getFormattedNumber $val.Values 0}}</td></tr>
{{end}}
</table>
{{end}}
{{if hasData .Data "driver"}}
<table style='float: left; margin: 10px 10px;'>
<caption><button class='btn'><i class='fa fa-comment-o'></i></button>Drivers Compatibility</caption>
<tr><th></th><th>Driver</th><th>Version</th><th>IP</th><th>Compatibility</th></tr>
{{$mver := .Info.Version}}
{{range $n, $val := index .Data "driver"}}
<tr><td align=right>{{add $n 1}}</td>
<td>{{index $val.Values 0}}</td><td>{{index $val.Values 1}}</td>
<td>{{$val.Name}}</td>
{{$err := checkDriver $mver $val.Values}}
{{if eq $err nil}}
<td align='center'><i class='fa fa-check'></i></td>
{{else}}
<td><mark>{{$err}}</mark></td>
{{end}}
</tr>
{{end}}
</table>
{{end}}
{{if hasData .Data "failed"}}
<table style='float: left; margin: 10px 10px; clear: left;'>
<caption><button class='btn'
onClick="javascript:loadData('/hatchets/{{.Hatchet}}/logs/all?context=failed'); return false;">
<i class='fa fa-search'></i></button>Failed Operations</caption>
<tr><th></th><th>Failed Operation</th><th>Total</th></tr>
{{range $n, $val := index .Data "failed"}}
<tr><td align=right>{{add $n 1}}</td>
<td>
<button class='btn' onClick="javascript:loadData('/hatchets/{{$name}}/logs/all?context={{$val.Name}}'); return false;"><i class='fa fa-search'></i></button>{{$val.Name}}
</td>
<td align=right>{{getFormattedNumber $val.Values 0}}</td>
</tr>
{{end}}
</table>
{{end}}
{{if hasData .Data "ip"}}
<table style='float: left; margin: 10px 10px;'>
<caption><button class='btn'
onClick="javascript:loadData('/hatchets/{{.Hatchet}}/charts/connections?type=accepted'); return false;">
<i class='fa fa-pie-chart'></i></button>Stats by IPs</caption>
<tr><th></th><th>IP</th><th>Accepted Connections</th><th>Response Length</th></tr>
{{range $n, $val := index .Data "ip"}}
<tr><td align=right>{{add $n 1}}</td>
<td>
<button class='btn' onClick="javascript:loadData('/hatchets/{{$name}}/charts/reslen-ip?ip={{$val.Name}}'); return false;"><i class='fa fa-pie-chart'></i></button>{{$val.Name}}
</td>
<td align=right>{{getFormattedNumber $val.Values 0}}</td><td align=right>{{getFormattedSize $val.Values 1}}</td></tr>
{{end}}
</table>
{{end}}
{{if hasData .Data "op"}}
<table style='float: left; margin: 10px 10px; clear: left;'>
<caption><button class='btn'
onClick="javascript:loadData('/hatchets/{{.Hatchet}}/charts/ops?type=stats'); return false;">
<i class='fa fa-area-chart'></i></button>Operations Stats</caption>
<tr><th></th><th>Operation</th><th>Total</th></tr>
{{range $n, $val := index .Data "op"}}
<tr><td align=right>{{add $n 1}}</td>
<td>
<button class='btn' onClick="javascript:loadData('/hatchets/{{$name}}/charts/ops?type=stats&op={{$val.Name}}'); return false;"><i class='fa fa-area-chart'></i></button>{{$val.Name}}
</td>
<td align=right>{{getFormattedNumber $val.Values 0}}</td></tr>
{{end}}
</table>
{{end}}
{{if hasData .Data "ns"}}
<table style='float: left; margin: 10px 10px;'>
<caption><button class='btn'
onClick="javascript:loadData('/hatchets/{{.Hatchet}}/charts/reslen-ns?ns='); return false;">
<i class='fa fa-pie-chart'></i></button>Stats by Namespaces</caption>
<tr><th></th><th>Namespace</th><th>Accessed</th><th>Response Length</th></tr>
{{range $n, $val := index .Data "ns"}}
<tr><td align=right>{{add $n 1}}</td>
<td>
<button class='btn' onClick="javascript:loadData('/hatchets/{{$name}}/logs/all?context={{$val.Name}}'); return false;"><i class='fa fa-search'></i></button>{{$val.Name}}
</td>
<td align=right>{{getFormattedNumber $val.Values 0}}</td><td align=right>{{getFormattedSize $val.Values 1}}</td></tr>
{{end}}
</table>
{{end}}
{{if hasData .Data "duration"}}
<table style='float: left; margin: 10px 10px; clear: left;'>
<caption><span style="font-size: 16px; padding: 5px 5px;"><i class="fa fa-shield"></i></span>Top N Long Lasting Connections</caption>
<tr><th></th><th>Context</th><th>Duration</th></tr>
{{range $n, $val := index .Data "duration"}}
<tr><td align=right>{{add $n 1}}</td>
<td><button class='btn' onClick="javascript:loadData('/hatchets/{{$name}}/logs/all?context={{getContext $val.Name}}'); return false;">
<i class='fa fa-search'></i></button>{{$val.Name}}
</td>
<td align=right>{{getFormattedDuration $val.Values 0}}</td>
</tr>
{{end}}
</table>
{{end}}
<div style='clear: left;' align='center'><hr/><p/>@simagix</div>
`
html += "</body></html>"
return template.New("hatchet").Funcs(template.FuncMap{
"add": func(a int, b int) int {
return a + b
},
"hasData": func(data map[string][]NameValues, key string) bool {
return len(data[key]) > 0
},
"numPrinter": func(n interface{}) string {
printer := message.NewPrinter(language.English)
return printer.Sprintf("%v", ToInt(n))
},
"getContext": func(s string) string {
toks := strings.Split(s, " ")
if len(toks) == 0 {
return s
}
return toks[0]
},
"assignConsultant": func(sage bool) string {
if sage {
return SAGE_PNG
}
return SIMONE_PNG
},
"checkDriver": func(version string, values []interface{}) error {
return CheckDriverCompatibility(version, values[0].(string), values[1].(string))
},
"getFormattedNumber": func(numbers []interface{}, i int) string {
printer := message.NewPrinter(language.English)
return printer.Sprintf("%v", numbers[i])
},
"coinToss": func() bool {
rand.Seed(time.Now().UnixNano())
randomNum := rand.Intn(2)
return (randomNum%2 == 0)
},
"getDurationFromSeconds": func(s int) string {
return gox.GetDurationFromSeconds(float64(s))
},
"getFormattedDuration": func(numbers []interface{}, i int) string {
return gox.GetDurationFromSeconds(float64(numbers[i].(int)))
},
"getStorageSize": func(s int) string {
return gox.GetStorageSize(float64(s))
},
"getFormattedSize": func(numbers []interface{}, i int) string {
return gox.GetStorageSize(numbers[i])
},
"getInfoSummary": func(info HatchetInfo, sage bool) template.HTML {
var html = "Hey there! My name is <i>Simone</i> and here is the summary I've prepared for you. "
if sage {
html = "Hello, my name is <i>Sage</i> and I'd like to share my thoughts with you on the findings. "
}
if info.Version == "" {
html += "There is not enough information in the log to determine what MongoDB version is used."
} else {
html += fmt.Sprintf("So, the server running MongoDB is using the <span style='color: orange;'>%v</span> edition of version <span style='color: orange;'>%v</span>", info.Module, info.Version)
if strings.Compare(MIN_MONGO_VER, info.Version) == 1 {
html += ", which is kinda old now. <mark>It's probably a good idea to upgrade to a more recent version of MongoDB soon</mark>. "
} else {
html += ". "
}
if info.Arch != "" && info.OS != "" {
html += fmt.Sprintf("The server is on a <span style='color: orange;'>%v</span> architecture server that running the <span style='color: orange;'>%v</span> operating system. ", info.Arch, info.OS)
}
if info.Module != "enterprise" {
html += fmt.Sprintf("Although %v edition works, it is recommended to upgrade to the enterprise edition or migrate to Atlas. ", info.Module)
}
if info.Start != "" && info.End != "" {
layout := "2006-01-02T15:04:05"
startTime := info.Start[:19]
endTime := info.End[:19]
stime, _ := time.Parse(layout, startTime)
etime, _ := time.Parse(layout, endTime)
seconds := etime.Unix() - stime.Unix()
days := gox.GetDurationFromSeconds(float64(seconds))
if days == " 1.0 days" {
days = "1 day"
}
html += fmt.Sprintf("The first log was dated on <span style='color: orange;'>%v</span>, it ran for %v, ending on <span style='color: orange;'>%v</span>. ",
stime.Format(time.ANSIC), days, etime.Format(time.ANSIC))
if seconds > (24*60*60 + 5*60) {
html += fmt.Sprintf("<mark>The duration of %v is greater than a day for a single log file, and rotating logs regularly is recommended.</mark> ",
gox.GetDurationFromSeconds(float64(seconds)))
}
}
if info.Provider != "" && info.Region != "" {
html += fmt.Sprintf("Bravo for the decision to host your servers on Atlas <span style='color: orange;'>%s</span> in the <span style='color: orange;'>%s</span> region. ",
info.Provider, info.Region)
html += "A gentle reminder to <mark>ensure your application servers reside in the same region</mark> which can save you more than a few bucks from data transfer charges. "
}
if len(info.Drivers) == 1 {
for _, driver := range info.Drivers {
for key, value := range driver {
html += "For the application driver, I found a driver information and it was "
html += fmt.Sprintf("<span style='color: orange;'>%s</span> version <span style='color: orange;'>%s</span>. ", key, value)
}
}
html += "See Drivers Compatibility table below for a list of drivers in use. "
} else if len(info.Drivers) > 1 {
html += "It looks like your applications have used a number of drivers, and they are "
cnt := 0
for _, driver := range info.Drivers {
for key, value := range driver {
cnt++
if cnt == len(info.Drivers) {
html += fmt.Sprintf("and <span style='color: orange;'>%s</span> version <span style='color: orange;'>%s</span>. ", key, value)
} else {
html += fmt.Sprintf("<span style='color: orange;'>%s</span> version <span style='color: orange;'>%s</span>, ", key, value)
}
}
}
html += "See Drivers Compatibility table below for a list of drivers in use. "
}
}
return template.HTML(html)
},
"getStatsSummary": func(data map[string][]NameValues) template.HTML {
var html string
printer := message.NewPrinter(language.English)
var totalImpact float64
for key, docs := range data {
if key == "exception" && len(docs) > 0 {
html += printer.Sprintf("Hmmm, I found <span style='color: orange;'>%d</span> warning (or more severe) ", len(docs))
if len(docs) < 2 {
html += "message. "
} else {
html += "messages. "
}
} else if key == "ip" && len(docs) > 0 {
conns := 0
for _, doc := range docs {
conns += doc.Values[0].(int)
}
html += printer.Sprintf("During the time, there were a total of <span style='color: orange;'>%d</span> accepted connections from <span style='color: orange;'>%d</span> ", conns, len(docs))
if len(docs) < 2 {
html += "client. "
} else {
html += "different clients. "
}
} else if key == "ns" && len(docs) > 0 {
count := 0
for _, doc := range docs {
count += doc.Values[0].(int)
}
html += printer.Sprintf("As many as <span style='color: orange;'>%d</span> different namespaces were accessed a total of <span style='color: orange;'>%d</span> times. ", len(docs), count)
reslen := 0
for _, doc := range docs {
reslen += doc.Values[1].(int)
}
html += printer.Sprintf("The total response length was around <span style='color: orange;'>%v</span>. ", gox.GetStorageSize(reslen))
} else if key == "stats" && len(docs) > 0 {
for _, doc := range docs {
if doc.Name == "maxConns" {
milli := doc.Values[0].(int)
if milli == 0 {
html += "I didn't find any connections information. "
continue
}
html += printer.Sprintf("At one point, the number of opened connections reached <span style='color: orange;'>%d</span>", doc.Values[0])
if milli > 1000 {
mem := milli * (1024 * 1024)
html += printer.Sprintf(", <mark>which could take up about %s of memory</mark>. ", gox.GetStorageSize(float64(mem)))
} else {
html += ". "
}
} else if doc.Name == "maxMilli" && doc.Values[0].(int) > 0 {
milli := doc.Values[0].(int)
html += "The slowest operation took "
if milli < 1000 {
html += printer.Sprintf("<span style='color: orange;'>%d</span> milliseconds. ", doc.Values[0])
} else {
seconds := float64(milli) / 1000
html += printer.Sprintf("<span style='color: orange;'>%s</span>. ", gox.GetDurationFromSeconds(seconds))
}
} else if doc.Name == "avgMilli" && doc.Values[0].(int) > 0 {
milli := doc.Values[0].(int)
html += printer.Sprintf("Moreover, the average operation time was <span style='color: orange;'>%d</span> milliseconds", milli)
if milli > 100 {
html += `, where operation time <mark>greater than 100 milliseconds is, IMO, "slow"</mark>. `
} else {
html += ". "
}
} else if doc.Name == "totalMilli" && doc.Values[0].(int) > 0 {
seconds := float64(doc.Values[0].(int)) / 1000
if seconds < (10 * 60) { // should be calculated with duration
html += printer.Sprintf("The total impact time from slowest operations was %s. ", gox.GetDurationFromSeconds(seconds))
} else if seconds < (60 * 60) {
html += printer.Sprintf("The total impact time from slowest operations was, ouch, <span style='color: orange;'>%s</span>. ", gox.GetDurationFromSeconds(seconds))
} else {
totalImpact = seconds
}
}
}
} else if key == "collscan" && len(docs) > 0 {
html += "Let's move to the performance evaluation. "
for _, doc := range docs {
if doc.Name == "count" {
html += printer.Sprintf(`I found <span style='color: orange;'>%d</span> with <mark><i>COLLSCAN</i> </mark>plan summary. `, doc.Values[0])
} else if doc.Name == "totalMilli" {
seconds := float64(doc.Values[0].(int)) / 1000
html += printer.Sprintf(`The <i>COLLSCAN</i> caused a total of <span style='color: orange;'>%s</span> wasted. `, gox.GetDurationFromSeconds(seconds))
}
}
}
}
if totalImpact > 0 {
html += "OMG, the total impact from slowest operations was "
html += printer.Sprintf("<span style='color: orange;'>%s</span>, this may be a problem of lacking resources. Please review the sizing training videos below. ", gox.GetDurationFromSeconds(totalImpact))
html += "<div style='padding: 10px'>"
html += `<iframe width="400" height="225" src="https://www.youtube.com/embed/kObLsYJAruI" title="Survery Your MongoDB Land" style="margin-right: 5px;” frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen></iframe>`
html += `<iframe width="400" height="225" src="https://www.youtube.com/embed/equz1z0igv0" title="Bond - MongoDB Sharded Cluster Analysis" style="margin-right: 5px;” frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen></iframe>`
html += `<iframe width="400" height="225" src="https://www.youtube.com/embed/0AAMw_q1E4o" title="Sizing a MongoDB Cluster 1" style="margin-right: 5px;” frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen></iframe>`
html += `<iframe width="400" height="225" src="https://www.youtube.com/embed/n1wORkr_1xI" title="Sizing a MongoDB Cluster 2" style="margin-right: 5px;” frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen></iframe>`
html += "</div>"
}
if len(data) > 0 {
html += "<p/>Check out the details below for additional information about this server."
}
return template.HTML(html)
}}).Parse(html)
}