-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
111 lines (105 loc) · 2.86 KB
/
main.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
package main
import (
"fmt"
"github.com/jroimartin/gocui"
"strings"
"time"
)
const DATE_FORMAT = "2006-01-02 15:04:05"
// Format duration to avoid unnecessary precision
func formatDuration(duration time.Duration) string {
durationAsString := duration.String()
index := strings.IndexRune(durationAsString, 'n')
// if the time is in nanoseconds
if index != - 1 {
return duration.Round(time.Nanosecond).String()
} else {
return duration.Round(time.Millisecond).String()
}
}
func main() {
parameters := parseParameterFile()
monitors := make(map[string]*WebsiteMonitor)
var domains = make([]string, 0)
alerts := make(chan Alert)
done := make(chan bool)
// Initialize monitoring
for _, v := range parameters {
param := v
m := newMonitor(param, alerts)
domain := param.url[strings.Index(param.url, "//")+2:]
monitors[domain] = m
domains = append(domains, domain)
// The "done" channel here is not useful since shared, see README
go func() {
m.monitor(done)
}()
}
// Init ui
g, err := gocui.NewGui(gocui.OutputNormal)
if err != nil {
panic(err)
}
defer g.Close()
g.Cursor = true
g.Mouse = true
g.SetManagerFunc(layout)
err = initKeyBindings(g)
if err != nil {
panic(err)
}
// Every 10 seconds, poll the values of the last 10 minutes
go func() {
tenSecTicker := time.NewTicker(10 * time.Second)
defer tenSecTicker.Stop()
for {
<-tenSecTicker.C
for _, domain := range domains {
m := monitors[domain]
m.last10Min.mu.RLock()
avgResp := formatDuration(m.last10Min.getAvgResponseTime())
maxResp := formatDuration(m.last10Min.maxResponseTime)
line := fmt.Sprintf("last 10 min %-30s : avg %-7s max %-7s avail %.0f%% ",
domain, avgResp, maxResp, m.last10Min.getAvailability())
m.last10Min.mu.RUnlock()
displayLine(g, "logs", line)
}
}
}()
//Every minute, poll the values of the past hour
go func() {
minuteTicker := time.NewTicker(time.Minute)
defer minuteTicker.Stop()
for {
<-minuteTicker.C
for _, domain := range domains {
m := monitors[domain]
m.lastHour.mu.RLock()
avgResp := formatDuration(m.lastHour.getAvgResponseTime())
maxResp := formatDuration(m.lastHour.maxResponseTime)
line := fmt.Sprintf("last hour %-30s : avg %-7s max %-7s avail %.0f%% ",
domain, avgResp, maxResp, m.lastHour.getAvailability())
m.lastHour.mu.RUnlock()
displayLine(g, "logs", line)
}
}
}()
//Handle incoming alerts
go func() {
for {
a := <-alerts
var line string
if a.isDown {
line = fmt.Sprintf("Website %s is down. availability=%.0f%%, time=%v",
a.url, a.availability, a.since.Format(DATE_FORMAT))
} else {
line = fmt.Sprintf("Website %s recovered. availability=%.0f%%, time=%v",
a.url, a.availability, a.since.Format(DATE_FORMAT))
}
displayLine(g, "alerts", line)
}
}()
if err := g.MainLoop(); err != nil && err != gocui.ErrQuit {
panic(err)
}
}