-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
87 lines (72 loc) · 1.81 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
package main
import (
"log"
"net/http"
"os"
"time"
"github.com/mrmarble/deco"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var (
uploadSpeed = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "deco_upload_speed",
Help: "Online devices",
}, []string{"device"})
downloadSpeed = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "deco_download_speed",
Help: "Online devices",
}, []string{"device"})
errors = prometheus.NewCounter(prometheus.CounterOpts{
Name: "deco_errors",
Help: "Errors",
})
names *map[string]struct{}
exists = struct{}{}
)
func main() {
c := deco.New(os.Getenv("DECO_EXPORTER_ADDR"))
err := c.Authenticate(os.Getenv("DECO_EXPORTER_PASSWORD"))
if err != nil {
log.Fatal(err.Error())
}
prometheus.MustRegister(uploadSpeed)
prometheus.MustRegister(downloadSpeed)
prometheus.MustRegister(errors)
go func() {
for {
updateDevices(c)
time.Sleep(time.Second * 10)
}
}()
http.Handle("/metrics", promhttp.Handler())
log.Fatal(http.ListenAndServe(":9919", nil))
}
func updateDevices(c *deco.Client) {
currentNames := make(map[string]struct{})
devices, err := c.ClientList()
if err != nil {
log.Println(err)
errors.Inc()
err := c.Authenticate(os.Getenv("DECO_EXPORTER_PASSWORD"))
if err != nil {
log.Println(err)
}
return
}
for _, client := range devices.Result.ClientList {
currentNames[client.Name] = exists
uploadSpeed.WithLabelValues(client.Name).Set(float64(client.UpSpeed))
downloadSpeed.WithLabelValues(client.Name).Set(float64(client.DownSpeed))
}
if names != nil {
for client := range *names {
_, present := currentNames[client]
if !present {
uploadSpeed.DeleteLabelValues(client)
downloadSpeed.DeleteLabelValues(client)
}
}
}
names = ¤tNames
}