-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
233 lines (197 loc) · 4.88 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
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
package main
import (
"bufio"
"errors"
"flag"
"fmt"
ui "github.com/gizak/termui"
"log"
"os"
"strconv"
"strings"
"suricata/cui"
"suricata/monitor"
"time"
)
//
const DEFAULT_CHECKING_INTERVAL int = 1000
const REFRESH_INTERVAL_MEDIUM = time.Second * 10
const REFRESH_INTERVAL_LONG = time.Minute
// Info to display to the user
var info = []string{
"press q to quit",
"press s to resume monitoring",
"press p to pause monitoring",
"availability: % of status code 200",
"Unsuccessful requests can have timed out",
}
// Loads ./config.sample by default
var configFile = flag.String("cfg", "./config.sample", "Config file containing the websites to monitor and the check inbtervals")
var websites []monitor.Website
var urls []string
func main() {
flag.Parse()
w, u, err := parseConfig(*configFile)
if err != nil {
panic("Failed to read config file !")
}
websites = w
urls = u
err = ui.Init()
if err != nil {
log.Fatal(err)
}
defer ui.Close()
pipeline := make(chan monitor.PingLog)
alerts := make(chan monitor.Alert)
messages := make([]string, 0)
orchestrator := monitor.GetOrchestrator(pipeline, alerts)
display := cui.GetDisplay()
go func(display *cui.Display) {
stopTick := time.NewTimer(30 * time.Minute)
mediumTick := time.NewTicker(REFRESH_INTERVAL_MEDIUM)
longTick := time.NewTicker(REFRESH_INTERVAL_LONG)
loop := true
for loop {
select {
// Every 10s, update medium term data
case <-mediumTick.C:
updateMedium(orchestrator)
display.UpdateMeasures(urls, orchestrator)
render(display)
// Every 1mn, update long term data
case <-longTick.C:
updateLong(orchestrator)
display.UpdateMeasures(urls, orchestrator)
render(display)
case alert := <-alerts:
// TODO - write message in log file
messages = append(messages, alert.Message)
if len(messages) > 8 {
messages = messages[len(messages)-8:]
}
display.UpdateMessages(messages)
render(display)
case <-stopTick.C:
ui.StopLoop()
loop = false
}
}
}(display)
launch(orchestrator, websites)
defer stop(orchestrator)
orchestrator.StartAll()
display.UpdateInfo(info)
display.UpdateMessages(messages)
display.UpdateMeasures(urls, orchestrator)
render(display)
// Launch orchestrator and register websites
ui.Handle("q", func(ui.Event) {
ui.StopLoop()
})
ui.Handle("s", func(ui.Event) {
orchestrator.StartAll()
display.UpdateMeasures(urls, orchestrator)
render(display)
})
ui.Handle("p", func(ui.Event) {
orchestrator.PauseAll()
display.UpdateMeasures(urls, orchestrator)
render(display)
})
ui.Handle("<Resize>", func(e ui.Event) {
payload := e.Payload.(ui.Resize)
ui.Body.Width = payload.Width
ui.Body.Align()
ui.Clear()
ui.Render(ui.Body)
})
ui.Loop()
}
func launch(orchestrator *monitor.Orchestrator, websites []monitor.Website) {
for _, website := range websites {
err := orchestrator.Register(website)
if err != nil {
fmt.Println("ERROR - ", err)
}
}
}
func stop(orchestrator *monitor.Orchestrator) {
err := orchestrator.PauseAll()
if err != nil {
panic(err)
}
err = orchestrator.UnregisterAll()
if err != nil {
panic(err)
}
}
func updateMedium(orchestrator *monitor.Orchestrator) error {
for _, website := range websites {
err := orchestrator.UpdateMediumReport(website.Url)
if err != nil {
return err
}
}
return nil
}
func updateLong(orchestrator *monitor.Orchestrator) error {
for _, website := range websites {
err := orchestrator.UpdateLongReport(website.Url)
if err != nil {
return err
}
}
return nil
}
func render(display *cui.Display) error {
ui.Body.Rows = make([]*ui.Row, 0)
rows, err := display.Render()
ui.Body.AddRows(rows...)
ui.Body.Align()
ui.Render(ui.Body)
return err
}
func parseConfig(fileLocation string) ([]monitor.Website, []string, error) {
file, err := os.Open(fileLocation)
if err != nil {
log.Fatal(err)
}
defer file.Close()
websites := make([]monitor.Website, 0)
urls := make([]string, 0)
foundUrls := make(map[string]bool)
scanner := bufio.NewScanner(file)
for scanner.Scan() {
var url string
var interval int
line := scanner.Text()
params := strings.Split(line, ",")
if len(params) > 2 {
return nil, nil, errors.New("INVALID CONFIG FILE: 1 WEBSITE PER lINE")
}
if !(strings.Index(params[0], "http://") == 0 || strings.Index(params[0], "https://") == 0) {
url = "http://" + params[0]
} else {
url = params[0]
}
if len(params) == 2 {
interval, err = strconv.Atoi(params[1])
if err != nil {
return nil, nil, errors.New("INVALID CONFIG FILE: INTERVAL MUST BE INTEGER")
}
} else {
interval = DEFAULT_CHECKING_INTERVAL
}
website := monitor.Website{Url: url, CheckInterval: interval}
if _, ok := foundUrls[url]; !ok {
websites = append(websites, website)
urls = append(urls, url)
foundUrls[url] = true
}
}
if err := scanner.Err(); err != nil {
return nil, nil, err
}
return websites, urls, nil
}