Skip to content

Commit

Permalink
Merge pull request prometheus#524 from mdlayher/wifi-expand
Browse files Browse the repository at this point in the history
Expand wifi collector for more interface types
  • Loading branch information
discordianfish authored Mar 21, 2017
2 parents 53279f2 + 2bfe410 commit 620e993
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 26 deletions.
1 change: 1 addition & 0 deletions collector/fixtures/e2e-output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2149,6 +2149,7 @@ node_textfile_scrape_error 0
# HELP node_wifi_interface_frequency_hertz The current frequency a WiFi interface is operating at, in hertz.
# TYPE node_wifi_interface_frequency_hertz gauge
node_wifi_interface_frequency_hertz{device="wlan0"} 2.412e+09
node_wifi_interface_frequency_hertz{device="wlan1"} 2.412e+09
# HELP node_wifi_station_beacon_loss_total The total number of times a station has detected a beacon loss.
# TYPE node_wifi_station_beacon_loss_total counter
node_wifi_station_beacon_loss_total{device="wlan0"} 1
Expand Down
5 changes: 5 additions & 0 deletions collector/fixtures/wifi/interfaces.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
"type": 2,
"frequency": 2412
},
{
"name": "wlan1",
"type": 3,
"frequency": 2412
},
{
"type": 10
}
Expand Down
60 changes: 34 additions & 26 deletions collector/wifi_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,55 +160,63 @@ func (c *wifiCollector) Update(ch chan<- prometheus.Metric) error {
}

for _, ifi := range ifis {
// Only collect metrics on stations for now
if ifi.Type != wifi.InterfaceTypeStation {
// Some virtual devices have no "name" and should be skipped.
if ifi.Name == "" {
continue
}

log.Debugf("probing wifi device %q with type %q", ifi.Name, ifi.Type)

ch <- prometheus.MustNewConstMetric(
c.interfaceFrequencyHertz,
prometheus.GaugeValue,
mHzToHz(ifi.Frequency),
ifi.Name,
)

bss, err := stat.BSS(ifi)
if err != nil {
if os.IsNotExist(err) {
continue
}
// When a statistic is not available for a given interface, package wifi
// returns an error compatible with os.IsNotExist. We leverage this to
// only export metrics which are actually valid for given interface types.

bss, err := stat.BSS(ifi)
switch {
case err == nil:
c.updateBSSStats(ch, ifi.Name, bss)
case os.IsNotExist(err):
log.Debugf("BSS information not found for wifi device %q", ifi.Name)
default:
return fmt.Errorf("failed to retrieve BSS for device %s: %v",
ifi.Name, err)
}

// Synthetic metric which provides WiFi station info, such as SSID, BSSID, etc.
ch <- prometheus.MustNewConstMetric(
c.stationInfo,
prometheus.GaugeValue,
1,
ifi.Name,
bss.BSSID.String(),
bss.SSID,
bssStatusMode(bss.Status),
)

info, err := stat.StationInfo(ifi)
if err != nil {
if os.IsNotExist(err) {
continue
}

return fmt.Errorf("failed to retrieve station info for device %s: %v",
switch {
case err == nil:
c.updateStationStats(ch, ifi.Name, info)
case os.IsNotExist(err):
log.Debugf("station information not found for wifi device %q", ifi.Name)
default:
return fmt.Errorf("failed to retrieve station info for device %q: %v",
ifi.Name, err)
}

c.updateStationStats(ch, ifi.Name, info)
}

return nil
}

func (c *wifiCollector) updateBSSStats(ch chan<- prometheus.Metric, device string, bss *wifi.BSS) {
// Synthetic metric which provides wifi station info, such as SSID, BSSID, etc.
ch <- prometheus.MustNewConstMetric(
c.stationInfo,
prometheus.GaugeValue,
1,
device,
bss.BSSID.String(),
bss.SSID,
bssStatusMode(bss.Status),
)
}

func (c *wifiCollector) updateStationStats(ch chan<- prometheus.Metric, device string, info *wifi.StationInfo) {
ch <- prometheus.MustNewConstMetric(
c.stationConnectedSecondsTotal,
Expand Down

0 comments on commit 620e993

Please sign in to comment.