-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.go
178 lines (162 loc) · 5.28 KB
/
metrics.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
package main
import (
"errors"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/lazyfrosch/dslmodem_exporter/pkg/zyxel"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"time"
)
var (
up = promauto.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Name: "up",
Help: "If we are connected to the modem.",
})
collectionSeconds = promauto.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Name: "collection_seconds",
Help: "Retrieval time for the DSL statistics from the modem.",
})
status = promauto.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: "status",
Name: "info",
Help: "Metadata.",
}, []string{"status", "mode", "profile", "traffic_type"})
linkUptime = promauto.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: "link",
Name: "uptime_seconds",
Help: "Since when the link is established.",
})
linkRateUp = promauto.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: "link",
Name: "rate_up_bytes",
Help: "Rate of the upstream link in bits/s.",
})
linkRateDown = promauto.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: "link",
Name: "rate_down_bytes",
Help: "Rate of the downstream link in bits/s.",
})
snrMarginUp = promauto.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: "link",
Name: "snr_margin_up_db",
Help: "Signal to noise margin for upstream in Decibel.",
})
snrMarginDown = promauto.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: "link",
Name: "snr_margin_down_db",
Help: "Signal to noise margin for downstream in Decibel.",
})
transmitPowerUp = promauto.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: "link",
Name: "transmit_power_up_dbm",
Help: "Current transmitting power to upstream in Decibel milliwatt.",
})
transmitPowerDown = promauto.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: "link",
Name: "transmit_power_down_dbm",
Help: "Current transmitting power to downstream in Decibel milliwatt.",
})
receivePowerUp = promauto.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: "link",
Name: "receive_power_up_dbm",
Help: "Current receiving power on upstream in Decibel milliwatt.",
})
receivePowerDown = promauto.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: "link",
Name: "receive_power_down_dbm",
Help: "Current receiving power on downstream in Decibel milliwatt.",
})
attenuationUp = promauto.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: "link",
Name: "attenuation_up_dbm",
Help: "Total attenuation for upstream in Decibel.",
})
attenuationDown = promauto.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: "link",
Name: "attenuation_down_dbm",
Help: "Total attenuation for downstream in Decibel.",
})
attainableDataRateUp = promauto.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: "link",
Name: "attainable_rate_up_bytes",
Help: "Attainable data rate for upstream in bits/s.",
})
attainableDataRateDown = promauto.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: "link",
Name: "attainable_rate_down_bytes",
Help: "Attainable data rate for downstream in bits/s.",
})
)
func updateLoop(client *zyxel.Client, interval time.Duration, logger log.Logger) {
var (
loggedIn bool
err error
stats *zyxel.VDSLStatus
)
for {
if !loggedIn {
loggedIn = true
_ = level.Info(logger).Log("msg", "Logging into DSL modem", "url", client.BaseURL.String())
err = client.Login()
if err != nil {
_ = level.Error(logger).Log("msg", "Error during login", "err", err)
time.Sleep(interval)
continue
}
} else {
time.Sleep(interval)
}
_ = level.Debug(logger).Log("msg", "Retrieving DSL statistics")
begin := time.Now()
stats, err = client.GetXDSLStatistics()
if err != nil {
if errors.Is(err, zyxel.ErrHTTPUnauthorized) {
// Force re-login
loggedIn = false
continue
}
// TODO: clear other states?
up.Set(0)
_ = level.Error(logger).Log("msg", "Error during retrieving statistics", "err", err)
} else {
up.Set(1)
collectionSeconds.Set(time.Since(begin).Seconds())
status.With(prometheus.Labels{
"status": stats.Status,
"mode": stats.Mode,
"profile": stats.Profile,
"traffic_type": stats.TrafficType,
}).Set(1)
linkUptime.Set(stats.LinkUptime.Seconds())
linkRateUp.Set(stats.LineRateUp.BitsPerSec())
linkRateDown.Set(stats.LineRateDown.BitsPerSec())
snrMarginUp.Set(float64(stats.SNRMarginUp))
snrMarginDown.Set(float64(stats.SNRMarginDown))
transmitPowerUp.Set(float64(stats.TransmitPowerUp))
transmitPowerDown.Set(float64(stats.TransmitPowerDown))
receivePowerUp.Set(float64(stats.ReceivePowerUp))
receivePowerDown.Set(float64(stats.ReceivePowerDown))
attenuationUp.Set(float64(stats.AttenuationUp))
attenuationDown.Set(float64(stats.AttenuationDown))
attainableDataRateUp.Set(stats.AttainableDataRateUp.BitsPerSec())
attainableDataRateDown.Set(stats.AttainableDataRateDown.BitsPerSec())
}
}
}