/
showSubs.go
402 lines (382 loc) · 12.1 KB
/
showSubs.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 herd
import (
"bufio"
"encoding/csv"
"fmt"
"io"
"net/http"
"strings"
"time"
"github.com/Cloud-Foundations/Dominator/lib/constants"
"github.com/Cloud-Foundations/Dominator/lib/format"
"github.com/Cloud-Foundations/Dominator/lib/html"
"github.com/Cloud-Foundations/Dominator/lib/json"
"github.com/Cloud-Foundations/Dominator/lib/srpc"
"github.com/Cloud-Foundations/Dominator/lib/stringutil"
"github.com/Cloud-Foundations/Dominator/lib/tags/tagmatcher"
"github.com/Cloud-Foundations/Dominator/lib/url"
proto "github.com/Cloud-Foundations/Dominator/proto/dominator"
)
func makeSelector(locationsToMatch []string, statusesToMatch []string,
tagsToMatch *tagmatcher.TagMatcher) func(sub *Sub) bool {
if len(locationsToMatch) < 1 &&
len(statusesToMatch) < 1 &&
tagsToMatch == nil {
return selectAll
}
locationsToMatchMap := stringutil.ConvertListToMap(locationsToMatch, false)
statusesToMatchMap := stringutil.ConvertListToMap(statusesToMatch, false)
return func(sub *Sub) bool {
if len(locationsToMatch) > 0 {
subLocationLength := len(sub.mdb.Location)
if subLocationLength < 1 {
return false
}
_, matched := locationsToMatchMap[sub.mdb.Location]
if !matched {
for _, locationToMatch := range locationsToMatch {
index := len(locationToMatch)
if index < subLocationLength &&
sub.mdb.Location[index] == '/' &&
strings.HasPrefix(sub.mdb.Location, locationToMatch) {
matched = true
break
}
}
}
if !matched {
return false
}
}
if len(statusesToMatch) > 0 {
if _, ok := statusesToMatchMap[sub.status.String()]; !ok {
return false
}
}
if !tagsToMatch.MatchEach(sub.mdb.Tags) {
return false
}
return true
}
}
func makeUrlQuerySelector(queryValues map[string][]string) func(sub *Sub) bool {
if len(queryValues) < 1 {
return selectAll
}
tagsToMatch := make(map[string][]string)
for _, queryTag := range queryValues["tag"] {
split := strings.Split(queryTag, "=")
if len(split) != 2 {
continue
}
key := split[0]
value := split[1]
tagsToMatch[key] = append(tagsToMatch[key], value)
}
return makeSelector(queryValues["location"], queryValues["status"],
tagmatcher.New(tagsToMatch, false))
}
func selectAll(sub *Sub) bool {
return true
}
func (herd *Herd) makeShowSubsHandler(selectFunc func(*Sub) bool,
subType string) func(http.ResponseWriter, *http.Request) {
return func(writer http.ResponseWriter, req *http.Request) {
herd.showSubsHandler(writer, req, selectFunc, subType)
}
}
func (herd *Herd) showReachableSubsHandler(writer http.ResponseWriter,
req *http.Request) {
selectFunc, _ := herd.getReachableSelector(url.ParseQuery(req.URL))
herd.showSubsHandler(writer, req, selectFunc, "reachable ")
}
func (herd *Herd) showUnreachableSubsHandler(writer http.ResponseWriter,
req *http.Request) {
selectFunc, _ := herd.getUnreachableSelector(url.ParseQuery(req.URL))
herd.showSubsHandler(writer, req, selectFunc, "unreachable ")
}
func (herd *Herd) showSubsCSV(writer io.Writer,
selectFunc func(*Sub) bool) {
subs := herd.getSelectedSubs(selectFunc)
w := csv.NewWriter(writer)
defer w.Flush()
w.Write([]string{
"Hostname",
"Required Image",
"Planned Image",
"Status",
"Last Image Update",
"Last Note",
})
for _, sub := range subs {
w.Write([]string{
sub.mdb.Hostname,
sub.mdb.RequiredImage,
sub.mdb.PlannedImage,
sub.publishedStatus.String(),
sub.lastSuccessfulImageName,
sub.lastNote,
})
}
}
func (herd *Herd) showSubsHandler(rWriter http.ResponseWriter,
req *http.Request, _selectFunc func(*Sub) bool,
subType string) {
querySelectFunc := makeUrlQuerySelector(req.URL.Query())
selectFunc := func(sub *Sub) bool {
return _selectFunc(sub) && querySelectFunc(sub)
}
writer := bufio.NewWriter(rWriter)
defer writer.Flush()
parsedQuery := url.ParseQuery(req.URL)
switch parsedQuery.OutputType() {
case url.OutputTypeCsv:
herd.showSubsCSV(writer, selectFunc)
case url.OutputTypeHtml:
herd.showSubsHTML(writer, selectFunc, subType)
case url.OutputTypeJson:
herd.showSubsJSON(writer, selectFunc)
case url.OutputTypeText:
fmt.Fprintln(writer, "Text output not supported")
default:
fmt.Fprintln(writer, "Unknown output type")
}
}
func (herd *Herd) showSubsHTML(writer *bufio.Writer, selectFunc func(*Sub) bool,
subType string) {
bd, _ := html.CreateBenchmarkData()
defer fmt.Fprintln(writer, "</body>")
fmt.Fprintf(writer, "<title>Dominator %s subs</title>", subType)
fmt.Fprintln(writer, `<style>
table, th, td {
border-collapse: collapse;
}
</style>`)
if srpc.CheckTlsRequired() {
fmt.Fprintln(writer, "<body>")
} else {
fmt.Fprintln(writer, "<body bgcolor=\"#ffb0b0\">")
fmt.Fprintln(writer,
`<h1><center><font color="red">Running in insecure mode. You can get pwned!!!</center></font></h1>`)
}
if herd.updatesDisabledReason != "" {
fmt.Fprintf(writer, "<center>")
herd.writeDisableStatus(writer)
fmt.Fprintln(writer, "</center>")
}
fmt.Fprintln(writer, `<table border="1" style="width:100%">`)
tw, _ := html.NewTableWriter(writer, true, "Name", "Required Image",
"Planned Image", "Busy", "Status", "Uptime", "Last Scan Duration",
"Staleness", "Last Update", "Last Sync", "Connect", "Short Poll",
"Full Poll", "Update Compute")
subs := herd.getSelectedSubs(selectFunc)
for _, sub := range subs {
showSub(tw, sub)
}
fmt.Fprintln(writer, "</table>")
bd.Write(writer)
}
func (herd *Herd) showSubsJSON(writer io.Writer,
selectFunc func(*Sub) bool) {
subs := herd.getSelectedSubs(selectFunc)
output := make([]proto.SubInfo, 0, len(subs))
for _, sub := range subs {
output = append(output, sub.makeInfo())
}
json.WriteWithIndent(writer, " ", output)
}
func (sub *Sub) makeInfo() proto.SubInfo {
return proto.SubInfo{
Machine: sub.mdb,
LastDisruptionState: sub.lastDisruptionState,
LastNote: sub.lastNote,
LastScanDuration: sub.lastScanDuration,
LastSuccessfulImage: sub.lastSuccessfulImageName,
LastSyncTime: sub.lastSyncTime,
LastUpdateTime: sub.lastUpdateTime,
StartTime: sub.startTime,
Status: sub.publishedStatus.String(),
SystemUptime: sub.systemUptime,
}
}
func showSub(tw *html.TableWriter, sub *Sub) {
var background string
if sub.isInsecure {
background = "yellow"
}
tw.OpenRow("", background)
defer tw.CloseRow()
subURL := fmt.Sprintf("http://%s:%d/",
strings.SplitN(sub.String(), "*", 2)[0], constants.SubPortNumber)
timeNow := time.Now()
tw.WriteData("", fmt.Sprintf("<a href=\"%s\">%s</a>", subURL, sub))
sub.herd.showImage(tw, sub.mdb.RequiredImage, true)
sub.herd.showImage(tw, sub.mdb.PlannedImage, false)
sub.showBusy(tw)
tw.WriteData("",
fmt.Sprintf("<a href=\"showSub?%s\">%s</a>",
sub.mdb.Hostname, sub.publishedStatus.html()))
showSince(tw, sub.pollTime, sub.startTime)
showDuration(tw, sub.lastScanDuration, false)
showSince(tw, timeNow, sub.lastPollSucceededTime)
showSince(tw, timeNow, sub.lastUpdateTime)
showSince(tw, timeNow, sub.lastSyncTime)
showDuration(tw, sub.lastConnectDuration, false)
showDuration(tw, sub.lastShortPollDuration, !sub.lastPollWasFull)
showDuration(tw, sub.lastFullPollDuration, sub.lastPollWasFull)
showDuration(tw, sub.lastComputeUpdateCpuDuration, false)
}
func (herd *Herd) showImage(tw *html.TableWriter, name string,
showDefault bool) error {
if name == "" {
if showDefault && herd.defaultImageName != "" {
return tw.WriteData("", fmt.Sprintf(
"<a style=\"color: #CCCC00\" href=\"http://%s/showImage?%s\">%s</a>",
herd.imageManager, herd.defaultImageName,
herd.defaultImageName))
} else {
return tw.WriteData("", "")
}
} else if image, err := herd.imageManager.Get(name, false); err != nil {
return tw.WriteData("red", err.Error())
} else if image != nil {
return tw.WriteData("",
fmt.Sprintf("<a href=\"http://%s/showImage?%s\">%s</a>",
herd.imageManager, name, name))
} else {
return tw.WriteData("grey", name)
}
}
func (herd *Herd) showSubHandler(writer http.ResponseWriter,
req *http.Request) {
w := bufio.NewWriter(writer)
defer w.Flush()
subName := strings.Split(req.URL.RawQuery, "&")[0]
parsedQuery := url.ParseQuery(req.URL)
sub := herd.getSub(subName)
if sub == nil {
http.NotFound(writer, req)
return
}
if parsedQuery.OutputType() == url.OutputTypeJson {
json.WriteWithIndent(w, " ", sub.makeInfo())
return
}
fmt.Fprintf(w, "<title>sub %s</title>", subName)
if srpc.CheckTlsRequired() {
fmt.Fprintln(w, "<body>")
} else {
fmt.Fprintln(w, "<body bgcolor=\"#ffb0b0\">")
fmt.Fprintln(w,
`<h1><center><font color="red">Running in insecure mode. You can get pwned!!!</center></font></h1>`)
}
if herd.updatesDisabledReason != "" {
fmt.Fprintf(w, "<center>")
herd.writeDisableStatus(w)
fmt.Fprintln(w, "</center>")
}
fmt.Fprintln(w, "<h3>")
timeNow := time.Now()
subURL := fmt.Sprintf("http://%s:%d/",
strings.SplitN(sub.String(), "*", 2)[0], constants.SubPortNumber)
fmt.Fprintf(w,
"Information for sub: <a href=\"%s\">%s</a> (<a href=\"%s?%s&output=json\">JSON</a>)<br>\n",
subURL, subName, req.URL.Path, subName)
fmt.Fprintln(w, "</h3>")
fmt.Fprint(w, "<table border=\"0\">\n")
tw, _ := html.NewTableWriter(w, false)
newRow(w, "Required Image", true)
sub.herd.showImage(tw, sub.mdb.RequiredImage, true)
newRow(w, "Planned Image", false)
sub.herd.showImage(tw, sub.mdb.PlannedImage, false)
newRow(w, "Last successful image update", false)
sub.herd.showImage(tw, sub.lastSuccessfulImageName, false)
if sub.lastNote != "" {
newRow(w, "Last note", false)
tw.WriteData("", sub.lastNote)
}
newRow(w, "Busy time", false)
sub.showBusy(tw)
newRow(w, "Status", false)
tw.WriteData("", sub.publishedStatus.html())
if sub.lastWriteError != "" {
newRow(w, "Last write error", false)
tw.WriteData("", sub.lastWriteError)
}
newRow(w, "Uptime", false)
showSince(tw, sub.pollTime, sub.startTime)
newRow(w, "Last scan duration", false)
showDuration(tw, sub.lastScanDuration, false)
if sub.mdb.Location != "" {
newRow(w, "Location", false)
tw.WriteData("", sub.mdb.Location)
}
newRow(w, "Time since last successful poll", false)
showSince(tw, timeNow, sub.lastPollSucceededTime)
newRow(w, "Time since last update", false)
showSince(tw, timeNow, sub.lastUpdateTime)
newRow(w, "Time since last sync", false)
showSince(tw, timeNow, sub.lastSyncTime)
newRow(w, "Last connection duration", false)
showDuration(tw, sub.lastConnectDuration, false)
newRow(w, "Last short poll duration", false)
showDuration(tw, sub.lastShortPollDuration, !sub.lastPollWasFull)
newRow(w, "Last full poll duration", false)
showDuration(tw, sub.lastFullPollDuration, sub.lastPollWasFull)
newRow(w, "Last compute duration", false)
showDuration(tw, sub.lastComputeUpdateCpuDuration, false)
newRow(w, "Last disruption state", false)
tw.WriteData("", sub.lastDisruptionState.String())
if sub.systemUptime != nil {
newRow(w, "System uptime", false)
showDuration(tw, *sub.systemUptime, false)
}
fmt.Fprint(w, " </tr>\n")
fmt.Fprint(w, "</table>\n")
fmt.Fprintln(w, "MDB Data:")
fmt.Fprintln(w, "<pre>")
json.WriteWithIndent(w, " ", sub.mdb)
fmt.Fprintln(w, "</pre>")
}
func newRow(w io.Writer, row string, first bool) {
if !first {
fmt.Fprint(w, " </tr>\n")
}
fmt.Fprint(w, " <tr>\n")
fmt.Fprintf(w, " <td>%s:</td>\n", row)
}
func (sub *Sub) showBusy(tw *html.TableWriter) {
if sub.busy {
if sub.busyStartTime.IsZero() {
tw.WriteData("", "busy")
} else {
tw.WriteData("", format.Duration(time.Since(sub.busyStartTime)))
}
} else {
if sub.busyStartTime.IsZero() {
tw.WriteData("", "")
} else {
tw.WriteData("grey",
format.Duration(sub.busyStopTime.Sub(sub.busyStartTime)))
}
}
}
func showSince(tw *html.TableWriter, now time.Time, since time.Time) {
if now.IsZero() || since.IsZero() {
tw.WriteData("", "")
} else {
showDuration(tw, now.Sub(since), false)
}
}
func showDuration(tw *html.TableWriter, duration time.Duration,
highlight bool) {
if duration < 1 {
tw.WriteData("", "")
} else {
str := format.Duration(duration)
if highlight {
str = "<b>" + str + "</b>"
}
tw.WriteData("", str)
}
}