diff options
author | Matt Layher <mdlayher@gmail.com> | 2017-01-12 12:41:35 -0500 |
---|---|---|
committer | Matt Layher <mdlayher@gmail.com> | 2017-01-13 13:35:20 -0500 |
commit | d3089f2ce805f6296c004f39fcb4bb69a181143a (patch) | |
tree | eee54b13a531f8fc0123343d743127b860176234 /collector/wifi_linux.go | |
parent | 2884181cce9698b5224ec018431155bab27b06a1 (diff) | |
download | prometheus_node_collector-d3089f2ce805f6296c004f39fcb4bb69a181143a.tar.bz2 prometheus_node_collector-d3089f2ce805f6296c004f39fcb4bb69a181143a.tar.xz prometheus_node_collector-d3089f2ce805f6296c004f39fcb4bb69a181143a.zip |
Make wifi collector fail gracefully if metrics not available
Diffstat (limited to 'collector/wifi_linux.go')
-rw-r--r-- | collector/wifi_linux.go | 30 |
1 files changed, 19 insertions, 11 deletions
diff --git a/collector/wifi_linux.go b/collector/wifi_linux.go index 4eb1b68..994f808 100644 --- a/collector/wifi_linux.go +++ b/collector/wifi_linux.go | |||
@@ -18,10 +18,12 @@ import ( | |||
18 | "flag" | 18 | "flag" |
19 | "fmt" | 19 | "fmt" |
20 | "io/ioutil" | 20 | "io/ioutil" |
21 | "os" | ||
21 | "path/filepath" | 22 | "path/filepath" |
22 | 23 | ||
23 | "github.com/mdlayher/wifi" | 24 | "github.com/mdlayher/wifi" |
24 | "github.com/prometheus/client_golang/prometheus" | 25 | "github.com/prometheus/client_golang/prometheus" |
26 | "github.com/prometheus/common/log" | ||
25 | ) | 27 | ) |
26 | 28 | ||
27 | type wifiCollector struct { | 29 | type wifiCollector struct { |
@@ -35,8 +37,6 @@ type wifiCollector struct { | |||
35 | StationTransmitRetriesTotal *prometheus.Desc | 37 | StationTransmitRetriesTotal *prometheus.Desc |
36 | StationTransmitFailedTotal *prometheus.Desc | 38 | StationTransmitFailedTotal *prometheus.Desc |
37 | StationBeaconLossTotal *prometheus.Desc | 39 | StationBeaconLossTotal *prometheus.Desc |
38 | |||
39 | stat wifiStater | ||
40 | } | 40 | } |
41 | 41 | ||
42 | var ( | 42 | var ( |
@@ -51,16 +51,12 @@ var _ wifiStater = &wifi.Client{} | |||
51 | 51 | ||
52 | // wifiStater is an interface used to swap out a *wifi.Client for end to end tests. | 52 | // wifiStater is an interface used to swap out a *wifi.Client for end to end tests. |
53 | type wifiStater interface { | 53 | type wifiStater interface { |
54 | Close() error | ||
54 | Interfaces() ([]*wifi.Interface, error) | 55 | Interfaces() ([]*wifi.Interface, error) |
55 | StationInfo(ifi *wifi.Interface) (*wifi.StationInfo, error) | 56 | StationInfo(ifi *wifi.Interface) (*wifi.StationInfo, error) |
56 | } | 57 | } |
57 | 58 | ||
58 | func NewWifiCollector() (Collector, error) { | 59 | func NewWifiCollector() (Collector, error) { |
59 | stat, err := newWifiStater(*collectorWifi) | ||
60 | if err != nil { | ||
61 | return nil, fmt.Errorf("failed to access wifi data: %v", err) | ||
62 | } | ||
63 | |||
64 | const ( | 60 | const ( |
65 | subsystem = "wifi" | 61 | subsystem = "wifi" |
66 | ) | 62 | ) |
@@ -132,13 +128,23 @@ func NewWifiCollector() (Collector, error) { | |||
132 | labels, | 128 | labels, |
133 | nil, | 129 | nil, |
134 | ), | 130 | ), |
135 | |||
136 | stat: stat, | ||
137 | }, nil | 131 | }, nil |
138 | } | 132 | } |
139 | 133 | ||
140 | func (c *wifiCollector) Update(ch chan<- prometheus.Metric) error { | 134 | func (c *wifiCollector) Update(ch chan<- prometheus.Metric) error { |
141 | ifis, err := c.stat.Interfaces() | 135 | stat, err := newWifiStater(*collectorWifi) |
136 | if err != nil { | ||
137 | // Cannot access wifi metrics, report no error | ||
138 | if os.IsNotExist(err) { | ||
139 | log.Debug("wifi collector metrics are not available for this system") | ||
140 | return nil | ||
141 | } | ||
142 | |||
143 | return fmt.Errorf("failed to access wifi data: %v", err) | ||
144 | } | ||
145 | defer stat.Close() | ||
146 | |||
147 | ifis, err := stat.Interfaces() | ||
142 | if err != nil { | 148 | if err != nil { |
143 | return fmt.Errorf("failed to retrieve wifi interfaces: %v", err) | 149 | return fmt.Errorf("failed to retrieve wifi interfaces: %v", err) |
144 | } | 150 | } |
@@ -149,7 +155,7 @@ func (c *wifiCollector) Update(ch chan<- prometheus.Metric) error { | |||
149 | continue | 155 | continue |
150 | } | 156 | } |
151 | 157 | ||
152 | info, err := c.stat.StationInfo(ifi) | 158 | info, err := stat.StationInfo(ifi) |
153 | if err != nil { | 159 | if err != nil { |
154 | return fmt.Errorf("failed to retrieve station info for device %s: %v", | 160 | return fmt.Errorf("failed to retrieve station info for device %s: %v", |
155 | ifi.Name, err) | 161 | ifi.Name, err) |
@@ -260,6 +266,8 @@ func (s *mockWifiStater) unmarshalJSONFile(filename string, v interface{}) error | |||
260 | return json.Unmarshal(b, v) | 266 | return json.Unmarshal(b, v) |
261 | } | 267 | } |
262 | 268 | ||
269 | func (s *mockWifiStater) Close() error { return nil } | ||
270 | |||
263 | func (s *mockWifiStater) Interfaces() ([]*wifi.Interface, error) { | 271 | func (s *mockWifiStater) Interfaces() ([]*wifi.Interface, error) { |
264 | var ifis []*wifi.Interface | 272 | var ifis []*wifi.Interface |
265 | if err := s.unmarshalJSONFile("interfaces.json", &ifis); err != nil { | 273 | if err := s.unmarshalJSONFile("interfaces.json", &ifis); err != nil { |