-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcollector.go
83 lines (63 loc) · 1.62 KB
/
collector.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
package main
import (
"context"
"log/slog"
hitron "github.com/hairyhenderson/hitron_coda"
"github.com/prometheus/client_golang/prometheus"
)
type collector struct {
ctx context.Context
client *hitron.CableModem
rc routerCollector
cc cmCollector
wc wifiCollector
up prometheus.Gauge
config config
}
func newCollector(ctx context.Context, conf config) *collector {
c := &collector{ctx: ctx, config: conf}
c.rc = newRouterCollector(ctx, c.getClient)
c.cc = newCMCollector(ctx, c.getClient)
c.wc = newWiFiCollector(ctx, c.getClient)
c.up = prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: metricsNS,
Name: "up",
Help: "Whether the device is reachable (1), or not (0)",
})
return c
}
func (c *collector) getClient() *hitron.CableModem {
return c.client
}
// Describe implements Prometheus.Collector.
func (c collector) Describe(ch chan<- *prometheus.Desc) {
c.rc.Describe(ch)
c.cc.Describe(ch)
c.wc.Describe(ch)
c.up.Describe(ch)
}
// Collect implements Prometheus.Collector.
func (c *collector) Collect(ch chan<- prometheus.Metric) {
// Assume the worst...
c.up.Set(0)
defer c.up.Collect(ch)
var err error
c.client, err = hitron.New(c.config.Host, c.config.Username, c.config.Password)
if err != nil {
slog.ErrorContext(c.ctx, "Error creating client", "err", err)
exporterClientErrors.Inc()
return
}
err = c.client.Login(c.ctx)
if err != nil {
slog.ErrorContext(c.ctx, "Error logging in", "err", err)
exporterClientErrors.Inc()
return
}
defer c.client.Logout(c.ctx)
c.rc.Collect(ch)
c.cc.Collect(ch)
c.wc.Collect(ch)
// collect is deferred
c.up.Set(1)
}