-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathfritzDocsis.go
227 lines (208 loc) · 7.11 KB
/
fritzDocsis.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
package main
import (
"flag"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"time"
"github.com/joho/godotenv"
"github.com/philippfranke/go-fritzbox/fritzbox"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
log "github.com/sirupsen/logrus"
)
type channelInfo []struct {
Activesub string `json:"activesub"`
Channel int `json:"channel"`
ChannelID int `json:"channelID"`
CorrErrors float64 `json:"corrErrors"`
FFT string `json:"fft"`
Frequency string `json:"frequency"`
Latency float64 `json:"latency"`
Mer string `json:"mer"`
Modulation string `json:"modulation"`
Mse string `json:"mse"`
Multiplex string `json:"multiplex"`
NonCorrErrors float64 `json:"nonCorrErrors"`
PLC string `json:"plc"`
PowerLevel string `json:"powerLevel"`
Type string `json:"type"`
}
type docInfo struct {
Pid string `json:"pid"`
Hide struct {
Wps bool `json:"wps"`
ShareUsb bool `json:"shareUsb"`
LiveTv bool `json:"liveTv"`
FaxSet bool `json:"faxSet"`
WGuest bool `json:"wGuest"`
DectMoniEx bool `json:"dectMoniEx"`
Rss bool `json:"rss"`
Mobile bool `json:"mobile"`
WKey bool `json:"wKey"`
SsoSet bool `json:"ssoSet"`
DectMail bool `json:"dectMail"`
DectMoni bool `json:"dectMoni"`
Chan bool `json:"chan"`
LiveImg bool `json:"liveImg"`
} `json:"hide"`
Time []interface{} `json:"time"`
Data struct {
ChannelDs struct {
Docsis31 channelInfo `json:"docsis31"`
Docsis30 channelInfo `json:"docsis30"`
} `json:"channelDs"`
Oem string `json:"oem"`
ChannelUs struct {
Docsis31 channelInfo `json:"docsis31"`
Docsis30 channelInfo `json:"docsis30"`
} `json:"channelUs"`
} `json:"data"`
Sid string `json:"sid"`
}
func startPrometheusExporter() {
http.Handle("/metrics", promhttp.Handler())
log.Info("Starting Exporter on Port 2112")
log.WithError(http.ListenAndServe(":2112", nil)).Fatal("Issues with serving exporter on port")
}
func collectFritzMetrics(client *fritzbox.Client) (docInfo, error) {
requestValues := url.Values{}
requestValues.Add("page", "docInfo")
request, err := client.NewRequest("POST", "data.lua", requestValues)
if err != nil {
log.WithError(err).Fatal("Issues creating fritzbox request")
}
var data docInfo
_, err = client.Do(request, &data)
if err != nil {
log.WithError(err).Fatal("Issues getting fritzbox response")
}
return data, nil
}
var (
data docInfo
correctableErrors = promauto.NewGaugeVec(
prometheus.GaugeOpts{
Name: "fritz_channel_correctable_errors",
Help: "The total number of correctable errors on the channel",
},
[]string{"channel", "channelID", "direction", "frequency", "frequency_minor", "docsisVersion"})
uncorrectableErrors = promauto.NewGaugeVec(
prometheus.GaugeOpts{
Name: "fritz_channel_uncorrectable_errors",
Help: "The total number of uncorrectable errors on the channel",
},
[]string{"channel", "channelID", "direction", "frequency", "frequency_minor", "docsisVersion"})
mse = promauto.NewGaugeVec(
prometheus.GaugeOpts{
Name: "fritz_channel_mse",
Help: "The current Mean Squared Error of the channel",
},
[]string{"channel", "channelID", "direction", "frequency", "frequency_minor", "docsisVersion"})
mer = promauto.NewGaugeVec(
prometheus.GaugeOpts{
Name: "fritz_channel_mer",
Help: "The current modulation error ratio of the channel",
},
[]string{"channel", "channelID", "direction", "frequency", "frequency_minor", "docsisVersion"})
powerLevel = promauto.NewGaugeVec(
prometheus.GaugeOpts{
Name: "fritz_channel_power_level",
Help: "The current power level of the channel",
},
[]string{"channel", "channelID", "direction", "frequency", "frequency_minor", "docsisVersion"})
connectionType = promauto.NewGaugeVec(
prometheus.GaugeOpts{
Name: "fritz_channel_connection_type",
Help: "The current QAM type of the channel",
},
[]string{"channel", "channelID", "direction", "frequency", "frequency_minor", "docsisVersion"})
)
func exportChannelInfo(channels channelInfo, direction string, docsisVersion string) {
for _, channel := range channels {
var channel_label int
// keep channel and channelid same on 7.39
if channel.Channel == 0 {
channel_label = channel.ChannelID
} else {
channel_label = channel.Channel
}
frequency, frequency_minor, _ := strings.Cut(channel.Frequency, ".")
labels := prometheus.Labels{
"channel": strconv.Itoa(channel_label),
"channelID": strconv.Itoa(channel.ChannelID),
"direction": direction,
"frequency": frequency,
"frequency_minor": frequency_minor,
"docsisVersion": docsisVersion,
}
correctableErrors.With(labels).Set(float64(channel.CorrErrors))
uncorrectableErrors.With(labels).Set(float64(channel.NonCorrErrors))
mseData, _ := strconv.ParseFloat(channel.Mse, 64)
mse.With(labels).Set(mseData)
merData, _ := strconv.ParseFloat(channel.Mer, 64)
mer.With(labels).Set(merData)
powerLevelData, _ := strconv.ParseFloat(channel.PowerLevel, 64)
powerLevel.With(labels).Set(powerLevelData)
var connectionTypeData float64
if channel.Type == "" {
connectionTypeData, _ = strconv.ParseFloat(strings.TrimSuffix(channel.Modulation, "QAM"), 64)
} else {
connectionTypeData, _ = strconv.ParseFloat(strings.TrimSuffix(channel.Type, "QAM"), 64)
}
connectionType.With(labels).Set(connectionTypeData)
}
}
func setMetrics(data *docInfo) {
correctableErrors.Reset()
uncorrectableErrors.Reset()
mse.Reset()
mer.Reset()
powerLevel.Reset()
connectionType.Reset()
exportChannelInfo(data.Data.ChannelDs.Docsis30, "downstream", "3.0")
exportChannelInfo(data.Data.ChannelDs.Docsis31, "downstream", "3.1")
exportChannelInfo(data.Data.ChannelUs.Docsis30, "upstream", "3.0")
exportChannelInfo(data.Data.ChannelUs.Docsis31, "upstream", "3.1")
}
func main() {
err := godotenv.Load()
if err != nil {
log.WithError(err).Warn("Error loading .env file")
}
dotenvUsername := os.Getenv("FRITZ_USERNAME")
dotenvPassword := os.Getenv("FRITZ_PASSWORD")
dotenvURL := os.Getenv("FRITZ_URL")
flagFritzURL := flag.String("url", "http://192.168.178.1", "URL of Fritzbox")
flagUsername := flag.String("username", "", "Username used for FritzBox authentication [Required]")
flagPassword := flag.String("password", "", "Password used for FritzBox authentication [Required]")
flag.Parse()
if *flagUsername == "" || *flagPassword == "" {
if dotenvUsername == "" || dotenvPassword == "" {
log.Fatal("Username and Password need to be supplied")
} else {
*flagUsername = dotenvUsername
*flagPassword = dotenvPassword
}
}
if *flagFritzURL == "" && dotenvURL != "" {
*flagFritzURL = dotenvURL
}
client := fritzbox.NewClient(nil)
client.BaseURL, _ = url.Parse(*flagFritzURL)
err = client.Auth(*flagUsername, *flagPassword)
if err != nil {
log.WithError(err).Fatal("Could not log in")
}
go func() {
for {
data, err = collectFritzMetrics(client)
setMetrics(&data)
time.Sleep(2 * time.Second)
}
}()
startPrometheusExporter()
}