From 2bfe410fb79eeb26d9860f377f196a24441d7be1 Mon Sep 17 00:00:00 2001 From: Matt Layher Date: Mon, 20 Mar 2017 12:25:01 -0400 Subject: [PATCH] Expand wifi collector for more interface types --- collector/fixtures/e2e-output.txt | 1 + collector/fixtures/wifi/interfaces.json | 5 +++ collector/wifi_linux.go | 60 ++++++++++++++----------- 3 files changed, 40 insertions(+), 26 deletions(-) diff --git a/collector/fixtures/e2e-output.txt b/collector/fixtures/e2e-output.txt index 48df21b9..2999a831 100644 --- a/collector/fixtures/e2e-output.txt +++ b/collector/fixtures/e2e-output.txt @@ -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 diff --git a/collector/fixtures/wifi/interfaces.json b/collector/fixtures/wifi/interfaces.json index f5395d76..21b3164f 100644 --- a/collector/fixtures/wifi/interfaces.json +++ b/collector/fixtures/wifi/interfaces.json @@ -4,6 +4,11 @@ "type": 2, "frequency": 2412 }, + { + "name": "wlan1", + "type": 3, + "frequency": 2412 + }, { "type": 10 } diff --git a/collector/wifi_linux.go b/collector/wifi_linux.go index 3b67251e..0bfd162a 100644 --- a/collector/wifi_linux.go +++ b/collector/wifi_linux.go @@ -160,11 +160,13 @@ 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, @@ -172,43 +174,49 @@ func (c *wifiCollector) Update(ch chan<- prometheus.Metric) error { 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,