forked from prometheus/node_exporter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
wifi_linux.go
289 lines (239 loc) · 7.29 KB
/
wifi_linux.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
// Copyright 2017 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package collector
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/mdlayher/wifi"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/log"
)
type wifiCollector struct {
InterfaceFrequencyHertz *prometheus.Desc
StationConnectedSecondsTotal *prometheus.Desc
StationInactiveSeconds *prometheus.Desc
StationReceiveBitsPerSecond *prometheus.Desc
StationTransmitBitsPerSecond *prometheus.Desc
StationSignalDBM *prometheus.Desc
StationTransmitRetriesTotal *prometheus.Desc
StationTransmitFailedTotal *prometheus.Desc
StationBeaconLossTotal *prometheus.Desc
}
var (
collectorWifi = flag.String("collector.wifi", "", "test fixtures to use for wifi collector metrics")
)
func init() {
Factories["wifi"] = NewWifiCollector
}
var _ wifiStater = &wifi.Client{}
// wifiStater is an interface used to swap out a *wifi.Client for end to end tests.
type wifiStater interface {
Close() error
Interfaces() ([]*wifi.Interface, error)
StationInfo(ifi *wifi.Interface) (*wifi.StationInfo, error)
}
func NewWifiCollector() (Collector, error) {
const (
subsystem = "wifi"
)
var (
labels = []string{"device"}
)
return &wifiCollector{
InterfaceFrequencyHertz: prometheus.NewDesc(
prometheus.BuildFQName(Namespace, subsystem, "interface_frequency_hertz"),
"The current frequency a WiFi interface is operating at, in hertz.",
labels,
nil,
),
StationConnectedSecondsTotal: prometheus.NewDesc(
prometheus.BuildFQName(Namespace, subsystem, "station_connected_seconds_total"),
"The total number of seconds a station has been connected to an access point.",
labels,
nil,
),
StationInactiveSeconds: prometheus.NewDesc(
prometheus.BuildFQName(Namespace, subsystem, "station_inactive_seconds"),
"The number of seconds since any wireless activity has occurred on a station.",
labels,
nil,
),
StationReceiveBitsPerSecond: prometheus.NewDesc(
prometheus.BuildFQName(Namespace, subsystem, "station_receive_bits_per_second"),
"The current WiFi receive bitrate of a station, in bits per second.",
labels,
nil,
),
StationTransmitBitsPerSecond: prometheus.NewDesc(
prometheus.BuildFQName(Namespace, subsystem, "station_transmit_bits_per_second"),
"The current WiFi transmit bitrate of a station, in bits per second.",
labels,
nil,
),
StationSignalDBM: prometheus.NewDesc(
prometheus.BuildFQName(Namespace, subsystem, "station_signal_dbm"),
"The current WiFi signal strength, in decibel-milliwatts (dBm).",
labels,
nil,
),
StationTransmitRetriesTotal: prometheus.NewDesc(
prometheus.BuildFQName(Namespace, subsystem, "station_transmit_retries_total"),
"The total number of times a station has had to retry while sending a packet.",
labels,
nil,
),
StationTransmitFailedTotal: prometheus.NewDesc(
prometheus.BuildFQName(Namespace, subsystem, "station_transmit_failed_total"),
"The total number of times a station has failed to send a packet.",
labels,
nil,
),
StationBeaconLossTotal: prometheus.NewDesc(
prometheus.BuildFQName(Namespace, subsystem, "station_beacon_loss_total"),
"The total number of times a station has detected a beacon loss.",
labels,
nil,
),
}, nil
}
func (c *wifiCollector) Update(ch chan<- prometheus.Metric) error {
stat, err := newWifiStater(*collectorWifi)
if err != nil {
// Cannot access wifi metrics, report no error
if os.IsNotExist(err) {
log.Debug("wifi collector metrics are not available for this system")
return nil
}
return fmt.Errorf("failed to access wifi data: %v", err)
}
defer stat.Close()
ifis, err := stat.Interfaces()
if err != nil {
return fmt.Errorf("failed to retrieve wifi interfaces: %v", err)
}
for _, ifi := range ifis {
// Only collect metrics on stations for now
if ifi.Type != wifi.InterfaceTypeStation {
continue
}
info, err := stat.StationInfo(ifi)
if err != nil {
return fmt.Errorf("failed to retrieve station info for device %s: %v",
ifi.Name, err)
}
ch <- prometheus.MustNewConstMetric(
c.InterfaceFrequencyHertz,
prometheus.GaugeValue,
mHzToHz(ifi.Frequency),
ifi.Name,
)
c.updateStationStats(ch, ifi.Name, info)
}
return nil
}
func (c *wifiCollector) updateStationStats(ch chan<- prometheus.Metric, device string, info *wifi.StationInfo) {
ch <- prometheus.MustNewConstMetric(
c.StationConnectedSecondsTotal,
prometheus.CounterValue,
info.Connected.Seconds(),
device,
)
ch <- prometheus.MustNewConstMetric(
c.StationInactiveSeconds,
prometheus.GaugeValue,
info.Inactive.Seconds(),
device,
)
ch <- prometheus.MustNewConstMetric(
c.StationReceiveBitsPerSecond,
prometheus.GaugeValue,
float64(info.ReceiveBitrate),
device,
)
ch <- prometheus.MustNewConstMetric(
c.StationTransmitBitsPerSecond,
prometheus.GaugeValue,
float64(info.TransmitBitrate),
device,
)
ch <- prometheus.MustNewConstMetric(
c.StationSignalDBM,
prometheus.GaugeValue,
float64(info.Signal),
device,
)
ch <- prometheus.MustNewConstMetric(
c.StationTransmitRetriesTotal,
prometheus.CounterValue,
float64(info.TransmitRetries),
device,
)
ch <- prometheus.MustNewConstMetric(
c.StationTransmitFailedTotal,
prometheus.CounterValue,
float64(info.TransmitFailed),
device,
)
ch <- prometheus.MustNewConstMetric(
c.StationBeaconLossTotal,
prometheus.CounterValue,
float64(info.BeaconLoss),
device,
)
}
func mHzToHz(mHz int) float64 {
return float64(mHz) * 1000 * 1000
}
// All code below this point is used to assist with end-to-end tests for
// the wifi collector, since wifi devices are not available in CI.
// newWifiStater determines if mocked test fixtures from files should be used for
// collecting wifi metrics, or if package wifi should be used.
func newWifiStater(fixtures string) (wifiStater, error) {
if fixtures != "" {
return &mockWifiStater{
fixtures: fixtures,
}, nil
}
return wifi.New()
}
var _ wifiStater = &mockWifiStater{}
type mockWifiStater struct {
fixtures string
}
func (s *mockWifiStater) unmarshalJSONFile(filename string, v interface{}) error {
b, err := ioutil.ReadFile(filepath.Join(s.fixtures, filename))
if err != nil {
return err
}
return json.Unmarshal(b, v)
}
func (s *mockWifiStater) Close() error { return nil }
func (s *mockWifiStater) Interfaces() ([]*wifi.Interface, error) {
var ifis []*wifi.Interface
if err := s.unmarshalJSONFile("interfaces.json", &ifis); err != nil {
return nil, err
}
return ifis, nil
}
func (s *mockWifiStater) StationInfo(ifi *wifi.Interface) (*wifi.StationInfo, error) {
p := filepath.Join(ifi.Name, "stationinfo.json")
var info wifi.StationInfo
if err := s.unmarshalJSONFile(p, &info); err != nil {
return nil, err
}
return &info, nil
}