This repository has been archived by the owner on Nov 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
tracking.go
496 lines (438 loc) · 13.6 KB
/
tracking.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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
package main
import (
"context"
"errors"
"fmt"
"net"
"os"
"os/exec"
"strconv"
"strings"
"sync"
"time"
"github.com/COSI-Lab/logging"
influxdb2 "github.com/influxdata/influxdb-client-go/v2"
"github.com/influxdata/influxdb-client-go/v2/api"
)
type NetStat struct {
BytesSent int64
BytesRecv int64
Requests int64
}
type DistroStatistics map[string]*NetStat
type TransmissionStatistics struct {
Uploaded int64
Downloaded int64
Torrents int
Ratio float64
}
type Statistics struct {
sync.RWMutex
nginx DistroStatistics
clarkson DistroStatistics
transmission TransmissionStatistics
rsyncd NetStat
}
var statistics Statistics
var clarksonIPv4net *net.IPNet
var clarksonIPv6net *net.IPNet
// Prepare filters and regular expressions
func init() {
var err error
_, clarksonIPv4net, err = net.ParseCIDR("128.153.0.0/16")
if err != nil {
logging.Panic(err)
os.Exit(1)
}
_, clarksonIPv6net, err = net.ParseCIDR("2605:6480::/32")
if err != nil {
logging.Panic(err)
os.Exit(1)
}
}
// HandleStatistics receives parsed log entries over channels and tracks the useful information
// The statistics object should be created before this function can be run.
func HandleStatistics(nginxEntries chan *NginxLogEntry, rsyncdEntries chan *RsyncdLogEntry) {
// We send the latest stats to influxdb every minute
ticker := time.NewTicker(1 * time.Minute)
for {
select {
case <-ticker.C:
err := SetTransmissionStatistics()
if err != nil {
logging.Error(err)
}
Sendstatistics()
case entry := <-nginxEntries:
statistics.Lock()
// Track all distro usage
if _, ok := statistics.nginx[entry.Distro]; ok {
statistics.nginx[entry.Distro].BytesSent += entry.BytesSent
statistics.nginx[entry.Distro].BytesRecv += entry.BytesRecv
statistics.nginx[entry.Distro].Requests++
} else {
statistics.nginx["other"].BytesSent += entry.BytesSent
statistics.nginx["other"].BytesRecv += entry.BytesRecv
statistics.nginx["other"].Requests++
}
statistics.nginx["total"].BytesSent += entry.BytesSent
statistics.nginx["total"].BytesRecv += entry.BytesRecv
statistics.nginx["total"].Requests++
// Additionally track usage from within the clarkson network
if clarksonIPv4net.Contains(entry.IP) || clarksonIPv6net.Contains(entry.IP) {
if _, ok := statistics.clarkson[entry.Distro]; ok {
statistics.clarkson[entry.Distro].BytesSent += entry.BytesSent
statistics.clarkson[entry.Distro].BytesRecv += entry.BytesRecv
statistics.clarkson[entry.Distro].Requests++
} else {
statistics.clarkson["other"].BytesSent += entry.BytesSent
statistics.clarkson["other"].BytesRecv += entry.BytesRecv
statistics.clarkson["other"].Requests++
}
statistics.clarkson["total"].BytesSent += entry.BytesSent
statistics.clarkson["total"].BytesRecv += entry.BytesRecv
statistics.clarkson["total"].Requests++
}
statistics.Unlock()
case entry := <-rsyncdEntries:
statistics.Lock()
statistics.rsyncd.BytesSent += entry.sent
statistics.rsyncd.BytesRecv += entry.recv
statistics.rsyncd.Requests++
statistics.Unlock()
}
}
}
// Start a command and allow it to cancel after a certain amount of time
func runCommand(cmd *exec.Cmd, d time.Duration) error {
cmd.Start()
done := make(chan error, 1)
go func() {
done <- cmd.Wait()
}()
select {
case <-time.After(d):
if err := cmd.Process.Kill(); err != nil {
return err
}
return errors.New("transmission-remote timed out")
case err := <-done:
if err != nil {
return err
}
}
return nil
}
// Get the latest statistics from Transmission
func SetTransmissionStatistics() error {
// Get the count by running transmission-remote -l
// The output is in the form of a table, so we can just count the lines - 2 for the head and tail
cmd := exec.Command("transmission-remote", "-ne", "-l")
cmd.Env = append(os.Environ(), "TR_AUTH=transmission:")
err := runCommand(cmd, 5*time.Second)
if err != nil {
return err
}
out, err := cmd.Output()
if err != nil {
return err
}
lines := strings.Split(string(out), "\n")
torrents := len(lines) - 2
// Get the total upload and download by running transmission-remote -st
cmd = exec.Command("transmission-remote", "-ne", "-st")
cmd.Env = append(os.Environ(), "TR_AUTH=transmission:")
err = runCommand(cmd, 5*time.Second)
if err != nil {
return err
}
out, err = cmd.Output()
if err != nil {
return err
}
// Get the TOTAL uploaded, downloaded and ratio
lines = strings.Split(string(out), "\n")
uploaded := strings.Split(lines[9], ":")[1]
downloaded := strings.Split(lines[10], ":")[1]
ratio := strings.Split(lines[11], ":")[1]
// Convert the human readable sizes to bytes
uploadedBytes, err := HumanReadableSizeToBytes(uploaded)
if err != nil {
return err
}
downloadedBytes, err := HumanReadableSizeToBytes(downloaded)
if err != nil {
return err
}
ratioFloat, err := strconv.ParseFloat(strings.TrimSpace(ratio), 64)
if err != nil {
return err
}
// Set the statistics
statistics.Lock()
statistics.transmission.Torrents = torrents
statistics.transmission.Uploaded = uploadedBytes
statistics.transmission.Downloaded = downloadedBytes
statistics.transmission.Ratio = ratioFloat
statistics.Unlock()
return nil
}
// HumanReadableSizeToBytes converts a human readable size to bytes
//
// Examples:
//
// "1.0 KB" -> 1000
// "1.0 MB" -> 1000000
// "1.0 GB" -> 1000000000
func HumanReadableSizeToBytes(size string) (int64, error) {
// Get the size and unit
size = strings.TrimSpace(size)
unit := size[len(size)-2:]
size = size[:len(size)-2]
// Convert the size to an int
sizeFloat, err := strconv.ParseFloat(strings.TrimSpace(size), 64)
if err != nil {
return 0, err
}
// Convert the unit to bytes
switch unit {
case "KB":
return int64(sizeFloat * 1000), nil
case "MB":
return int64(sizeFloat * 1000 * 1000), nil
case "GB":
return int64(sizeFloat * 1000 * 1000 * 1000), nil
case "TB":
return int64(sizeFloat * 1000 * 1000 * 1000 * 1000), nil
case "PB":
return int64(sizeFloat * 1000 * 1000 * 1000 * 1000 * 1000), nil
default:
return 0, fmt.Errorf("Unknown unit %s", unit)
}
}
// Sends the latest statistics to the database
func Sendstatistics() {
if influxReadOnly {
logging.Info("INFLUX_READ_ONLY is set, not sending data to influx")
return
}
t := time.Now()
statistics.RLock()
for short, stat := range statistics.nginx {
p := influxdb2.NewPoint("nginx",
map[string]string{"distro": short},
map[string]interface{}{
"bytes_sent": stat.BytesSent,
"bytes_recv": stat.BytesRecv,
"requests": stat.Requests,
}, t)
writer.WritePoint(p)
}
for short, stat := range statistics.clarkson {
p := influxdb2.NewPoint("clarkson",
map[string]string{"distro": short},
map[string]interface{}{
"bytes_sent": stat.BytesSent,
"bytes_recv": stat.BytesRecv,
"requests": stat.Requests,
}, t)
writer.WritePoint(p)
}
p := influxdb2.NewPoint("transmission", map[string]string{}, map[string]interface{}{
"downloaded": statistics.transmission.Downloaded,
"uploaded": statistics.transmission.Uploaded,
"torrents": statistics.transmission.Torrents,
"ratio": statistics.transmission.Ratio,
}, t)
writer.WritePoint(p)
p = influxdb2.NewPoint("rsyncd", map[string]string{}, map[string]interface{}{
"bytes_sent": statistics.rsyncd.BytesSent,
"bytes_recv": statistics.rsyncd.BytesRecv,
"requests": statistics.rsyncd.Requests,
}, t)
writer.WritePoint(p)
// To be safe we release the lock before logging because logging takes a seperate lock
statistics.RUnlock()
logging.Info("Sent statistics")
}
// InitStatistics queries the database for the all of the latest statistics
// In general everything in `statistics` should be monotonically increasing
// lastUpdated should be the same no matter where we check
func InitStatistics(projects map[string]*Project) (lastUpdated time.Time, err error) {
// Map from short names to bytes sent
statistics = Statistics{}
lastUpdated, statistics.nginx, err = QueryDistroStatistics(projects, "nginx")
if err != nil {
return lastUpdated, err
}
lastUpdated, statistics.clarkson, err = QueryDistroStatistics(projects, "clarkson")
if err != nil {
return lastUpdated, err
}
statistics.rsyncd, err = QueryRsyncdStatistics()
if err != nil {
return lastUpdated, err
}
return lastUpdated, nil
}
// measurement is the particular filter you want `DistroStatistics` from
// current "clarkson" and "nginx" (all) are supported
func QueryDistroStatistics(projects map[string]*Project, measurement string) (lastUpdated time.Time, stats DistroStatistics, err error) {
// You can paste this into the influxdb data explorer
// Replace MEASUREMENT with "nginx" or "clarkson"
/*
from(bucket: "stats")
|> range(start: 0, stop: now())
|> filter(fn: (r) => r["_measurement"] == "MEASUREMENT")
|> filter(fn: (r) => r["_field"] == "bytes_sent" or r["_field"] == "bytes_recv" or r["_field"] == "requests")
|> last()
|> group(columns: ["distro"], mode: "by")
*/
request := fmt.Sprintf("from(bucket: \"stats\") |> range(start: 0, stop: now()) |> filter(fn: (r) => r[\"_measurement\"] == \"%s\") |> filter(fn: (r) => r[\"_field\"] == \"bytes_sent\" or r[\"_field\"] == \"bytes_recv\" or r[\"_field\"] == \"requests\") |> last() |> group(columns: [\"distro\"], mode: \"by\")", measurement)
// try the query at most 5 times
var result *api.QueryTableResult
for i := 0; i < 5; i++ {
result, err = reader.Query(context.Background(), request)
if err != nil {
logging.Warn("Failed to querying influxdb nginx statistics", err)
// Sleep for some time before retrying
time.Sleep(time.Duration(i) * time.Second)
continue
}
break
}
if err != nil {
return lastUpdated, stats, errors.New("Error querying influxdb")
}
stats = make(DistroStatistics)
for short := range projects {
stats[short] = &NetStat{}
}
stats["other"] = &NetStat{}
stats["total"] = &NetStat{}
for result.Next() {
if result.Err() == nil {
// Get the data point
dp := result.Record()
// Update the time of the measurement
lastUpdated = dp.Time()
// Get the distro short name
distro, ok := dp.ValueByKey("distro").(string)
if !ok {
logging.Warn("Error getting distro short name")
fmt.Printf("%T %v\n", distro, distro)
continue
}
if stats[distro] == nil {
continue
}
// Get the field
field, ok := dp.ValueByKey("_field").(string)
if !ok {
logging.Warn("Error getting field")
fmt.Printf("%T %v\n", field, field)
continue
}
// Switch on the field
switch field {
case "bytes_sent":
sent, ok := dp.ValueByKey("_value").(int64)
if !ok {
logging.Warn("Error getting bytes sent")
fmt.Printf("%T %v\n", dp.ValueByKey("_value"), dp.ValueByKey("_value"))
continue
}
stats[distro].BytesSent = sent
case "bytes_recv":
received, ok := dp.ValueByKey("_value").(int64)
if !ok {
logging.Warn("Error getting bytes recv")
fmt.Printf("%T %v\n", dp.ValueByKey("_value"), dp.ValueByKey("_value"))
continue
}
stats[distro].BytesRecv = received
case "requests":
requests, ok := dp.ValueByKey("_value").(int64)
if !ok {
logging.Warn("Error getting requests")
fmt.Printf("%T %v\n", dp.ValueByKey("_value"), dp.ValueByKey("_value"))
continue
}
stats[distro].Requests = requests
}
} else {
logging.Warn("QueryDistroStatistics Flux Query Error", result.Err())
}
}
result.Close()
return lastUpdated, stats, nil
}
func QueryRsyncdStatistics() (stat NetStat, err error) {
// You can paste this into the influxdb data explorer
/*
from(bucket: "stats")
|> range(start: 0, stop: now())
|> filter(fn: (r) => r["_measurement"] == "rsyncd")
|> filter(fn: (r) => r["_field"] == "bytes_sent" or r["_field"] == "bytes_recv" or r["_field"] == "requests")
|> last()
*/
const request = "from(bucket: \"stats\") |> range(start: 0, stop: now()) |> filter(fn: (r) => r[\"_measurement\"] == \"rsyncd\") |> filter(fn: (r) => r[\"_field\"] == \"bytes_sent\" or r[\"_field\"] == \"bytes_recv\") |> last()"
// try the query at most 5 times
var result *api.QueryTableResult
for i := 0; i < 5; i++ {
result, err = reader.Query(context.Background(), request)
if err != nil {
logging.Warn("Failed to querying influxdb rsyncd statistics", err)
// Sleep for some time before retrying
time.Sleep(time.Duration(i) * time.Second)
continue
}
break
}
if result == nil {
return stat, errors.New("Error querying influxdb for rsyncd stat")
}
for result.Next() {
if result.Err() == nil {
// Get the data point
dp := result.Record()
// Get the field
field, ok := dp.ValueByKey("_field").(string)
if !ok {
logging.Warn("Error getting field")
fmt.Printf("%T %v\n", field, field)
continue
}
// Switch on the field
switch field {
case "bytes_sent":
sent, ok := dp.ValueByKey("_value").(int64)
if !ok {
logging.Warn("Error getting bytes sent")
fmt.Printf("%T %v\n", dp.ValueByKey("_value"), dp.ValueByKey("_value"))
continue
}
statistics.rsyncd.BytesSent = sent
case "bytes_recv":
received, ok := dp.ValueByKey("_value").(int64)
if !ok {
logging.Warn("Error getting bytes recv")
fmt.Printf("%T %v\n", dp.ValueByKey("_value"), dp.ValueByKey("_value"))
continue
}
statistics.rsyncd.BytesRecv = received
case "requests":
requests, ok := dp.ValueByKey("_value").(int64)
if !ok {
logging.Warn("Error getting requests")
fmt.Printf("%T %v\n", dp.ValueByKey("_value"), dp.ValueByKey("_value"))
continue
}
statistics.rsyncd.Requests = requests
}
} else {
logging.Warn("InitNGINXStats Flux Query Error", result.Err())
}
}
return stat, nil
}