This repository has been archived by the owner on Nov 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
265 lines (240 loc) · 7.67 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
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
package main
import (
"encoding/json"
"html/template"
"log"
"math"
"net/http"
"path"
"sync"
"time"
"github.com/pkg/errors"
)
type Status struct {
mutex sync.RWMutex `json:"-"`
FastComSpeedKbps int `json:"fastcomKbps"`
SpeedtestNetUploadKbps int `json:"speedtestNetUploadKbps"`
SpeedtestNetDownloadKbps int `json:"speedtestNetDownloadKbps"`
GooglePing *PingResult `json:"google"`
CloudflarePing *PingResult `json:"cloudflare"`
OpenDNSPing *PingResult `json:"opendns"`
RouterPing *PingResult `json:"router"`
SwitchPing *PingResult `json:"switch"`
CloudKeyPing *PingResult `json:"cloudKey"`
DownstairsAPPing *PingResult `json:"downstairsAP"`
LoftAPPing *PingResult `json:"loftAP"`
PhoneRoomsAPPing *PingResult `json:"phoneRoomsAP"`
}
// Update applies the given function to this Status instance, using the struct's mutex for write protection.
func (s *Status) Update(updater func(s *Status)) {
s.mutex.Lock()
defer s.mutex.Unlock()
updater(s)
}
var CurrentStatus = Status{}
var ProbeMutex sync.Mutex
const serveAddr = ":8001"
const speedtestInterval = 5 * time.Minute
const pingInterval = 45 * time.Second
const googleAddr = "8.8.8.8"
const cloudflareAddr = "1.1.1.1"
const openDnsAddr = "208.67.220.220"
const routerAddr = "10.10.10.1"
const switchAddr = "10.10.10.2"
const cloudKeyAddr = "10.10.10.14"
const downstairsAPAddr = "10.10.10.5"
const loftAPAddr = "10.10.10.7"
const phoneRoomsAPAddr = "10.10.10.8"
func statusHandler(w http.ResponseWriter, r *http.Request) {
CurrentStatus.mutex.RLock()
defer CurrentStatus.mutex.RUnlock()
j, err := json.Marshal(CurrentStatus)
if err != nil {
http.Error(w, errors.Wrap(err, "marshal current status to JSON").Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write(j)
}
func rootHandler(w http.ResponseWriter, r *http.Request) {
fp := path.Join("templates", "index.html")
tmpl, err := template.ParseFiles(fp)
if err != nil {
http.Error(w, errors.Wrap(err, "read template").Error(), http.StatusInternalServerError)
return
}
if err := tmpl.Execute(w, struct{}{}); err != nil {
http.Error(w, errors.Wrap(err, "render template").Error(), http.StatusInternalServerError)
}
}
func main() {
startPing(pingInterval, googleAddr, func(stats *PingResult, err error) {
if err != nil {
log.Println("[warning] google ping:", err)
CurrentStatus.Update(func(s *Status) {
s.GooglePing = nil
})
return
}
log.Printf("[debug] google ping avg: %d ms\n", stats.AvgRtt)
log.Printf("[debug] google pkt loss: %d%%\n", stats.PacketLossPct)
CurrentStatus.Update(func(s *Status) {
s.GooglePing = stats
})
})
startPing(pingInterval, cloudflareAddr, func(stats *PingResult, err error) {
if err != nil {
log.Println("[warning] cloudflare ping:", err)
CurrentStatus.Update(func(s *Status) {
s.CloudflarePing = nil
})
return
}
log.Printf("[debug] cloudflare ping avg: %d ms\n", stats.AvgRtt)
log.Printf("[debug] cloudflare pkt loss: %d%%\n", stats.PacketLossPct)
CurrentStatus.Update(func(s *Status) {
s.CloudflarePing = stats
})
})
startPing(pingInterval, openDnsAddr, func(stats *PingResult, err error) {
if err != nil {
log.Println("[warning] opendns ping:", err)
CurrentStatus.Update(func(s *Status) {
s.OpenDNSPing = nil
})
return
}
log.Printf("[debug] opendns ping avg: %d ms\n", stats.AvgRtt)
log.Printf("[debug] opendns pkt loss: %d%%\n", stats.PacketLossPct)
CurrentStatus.Update(func(s *Status) {
s.OpenDNSPing = stats
})
})
startPing(pingInterval, routerAddr, func(stats *PingResult, err error) {
if err != nil {
log.Println("[warning] router ping:", err)
CurrentStatus.Update(func(s *Status) {
s.RouterPing = nil
})
return
}
log.Printf("[debug] router ping avg: %d ms\n", stats.AvgRtt)
log.Printf("[debug] router pkt loss: %d%%\n", stats.PacketLossPct)
CurrentStatus.Update(func(s *Status) {
s.RouterPing = stats
})
})
startPing(pingInterval, cloudKeyAddr, func(stats *PingResult, err error) {
if err != nil {
log.Println("[warning] controller ping:", err)
CurrentStatus.Update(func(s *Status) {
s.CloudKeyPing = nil
})
return
}
log.Printf("[debug] controller ping avg: %d ms\n", stats.AvgRtt)
log.Printf("[debug] controller pkt loss: %d%%\n", stats.PacketLossPct)
CurrentStatus.Update(func(s *Status) {
s.CloudKeyPing = stats
})
})
startPing(pingInterval, switchAddr, func(stats *PingResult, err error) {
if err != nil {
log.Println("[warning] switch ping:", err)
CurrentStatus.Update(func(s *Status) {
s.SwitchPing = nil
})
return
}
log.Printf("[debug] switch ping avg: %d ms\n", stats.AvgRtt)
log.Printf("[debug] switch pkt loss: %d%%\n", stats.PacketLossPct)
CurrentStatus.Update(func(s *Status) {
s.SwitchPing = stats
})
})
startPing(pingInterval, downstairsAPAddr, func(stats *PingResult, err error) {
if err != nil {
log.Println("[warning] downstairs AP ping:", err)
CurrentStatus.Update(func(s *Status) {
s.DownstairsAPPing = nil
})
return
}
log.Printf("[debug] downstairs AP ping avg: %d ms\n", stats.AvgRtt)
log.Printf("[debug] downstairs AP pkt loss: %d%%\n", stats.PacketLossPct)
CurrentStatus.Update(func(s *Status) {
s.DownstairsAPPing = stats
})
})
startPing(pingInterval, loftAPAddr, func(stats *PingResult, err error) {
if err != nil {
log.Println("[warning] loft AP ping:", err)
CurrentStatus.Update(func(s *Status) {
s.LoftAPPing = nil
})
return
}
log.Printf("[debug] loft AP ping avg: %d ms\n", stats.AvgRtt)
log.Printf("[debug] loft AP pkt loss: %d%%\n", stats.PacketLossPct)
CurrentStatus.Update(func(s *Status) {
s.LoftAPPing = stats
})
})
startPing(pingInterval, phoneRoomsAPAddr, func(stats *PingResult, err error) {
if err != nil {
log.Println("[warning] phone rooms AP ping:", err)
CurrentStatus.Update(func(s *Status) {
s.PhoneRoomsAPPing = nil
})
return
}
log.Printf("[debug] phone rooms AP ping avg: %d ms\n", stats.AvgRtt)
log.Printf("[debug] phone rooms AP pkt loss: %d%%\n", stats.PacketLossPct)
CurrentStatus.Update(func(s *Status) {
s.PhoneRoomsAPPing = stats
})
})
startFastComSpeedTest(speedtestInterval, func(kbps float64, err error) {
if err != nil {
log.Println("[warning] fast.com speedtest:", err)
CurrentStatus.Update(func(s *Status) {
s.FastComSpeedKbps = 0
})
return
}
log.Printf("[debug] fast.com speedtest result: %.2f Kbps\n", kbps)
CurrentStatus.Update(func(s *Status) {
s.FastComSpeedKbps = int(math.Round(kbps))
})
})
startSpeedtestNetUploadSpeedTest(speedtestInterval, func(kbps float64, err error) {
if err != nil {
log.Println("[warning] speedtest.net upload test:", err)
CurrentStatus.Update(func(s *Status) {
s.SpeedtestNetUploadKbps = 0
})
return
}
log.Printf("[debug] speedtest.net upload result: %.2f Kbps\n", kbps)
CurrentStatus.Update(func(s *Status) {
s.SpeedtestNetUploadKbps = int(math.Round(kbps))
})
})
startSpeedtestNetDownloadSpeedTest(speedtestInterval, func(kbps float64, err error) {
if err != nil {
log.Println("[warning] speedtest.net download test:", err)
CurrentStatus.Update(func(s *Status) {
s.SpeedtestNetDownloadKbps = 0
})
return
}
log.Printf("[debug] speedtest.net download result: %.2f Kbps\n", kbps)
CurrentStatus.Update(func(s *Status) {
s.SpeedtestNetDownloadKbps = int(math.Round(kbps))
})
})
http.HandleFunc("/", rootHandler)
http.HandleFunc("/status", statusHandler)
log.Printf("[info] starting server at %s...\n", serveAddr)
log.Fatal(http.ListenAndServe(serveAddr, nil))
}