Add wlan station and interface metrics collection. (#14)

* Fix newlines.
* Added wlan metrics.
* Fix WlanSTA collector - return on errors.
This commit is contained in:
Robert S. Gerus
2018-09-11 05:30:13 +02:00
committed by Steve Brunton
parent f6b4764691
commit 25911b4e60
8 changed files with 391 additions and 125 deletions

View File

@@ -1,45 +1,47 @@
package config
import (
"io"
"io/ioutil"
yaml "gopkg.in/yaml.v2"
)
// Config represents the configuration for the exporter
type Config struct {
Devices []Device `yaml:"devices"`
Features struct {
BGP bool `yaml:"bgp,omitempty"`
DHCP bool `yaml:"dhcp,omitempty"`
DHCPv6 bool `yaml:"dhcpv6,omitempty"`
Routes bool `yaml:"routes,omitempty"`
Pools bool `yaml:"pools,omitempty"`
Optics bool `yaml:"optics,omitempty"`
} `yaml:"features,omitempty"`
}
// Device represents a target device
type Device struct {
Name string `yaml:"name"`
Address string `yaml:"address"`
User string `yaml:"user"`
Password string `yaml:"password"`
}
// Load reads YAML from reader and unmashals in Config
func Load(r io.Reader) (*Config, error) {
b, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}
c := &Config{}
err = yaml.Unmarshal(b, c)
if err != nil {
return nil, err
}
return c, nil
}
package config
import (
"io"
"io/ioutil"
yaml "gopkg.in/yaml.v2"
)
// Config represents the configuration for the exporter
type Config struct {
Devices []Device `yaml:"devices"`
Features struct {
BGP bool `yaml:"bgp,omitempty"`
DHCP bool `yaml:"dhcp,omitempty"`
DHCPv6 bool `yaml:"dhcpv6,omitempty"`
Routes bool `yaml:"routes,omitempty"`
Pools bool `yaml:"pools,omitempty"`
Optics bool `yaml:"optics,omitempty"`
WlanSTA bool `yaml:"wlansta,omitempty"`
WlanIF bool `yaml:"wlanif,omitempty"`
} `yaml:"features,omitempty"`
}
// Device represents a target device
type Device struct {
Name string `yaml:"name"`
Address string `yaml:"address"`
User string `yaml:"user"`
Password string `yaml:"password"`
}
// Load reads YAML from reader and unmashals in Config
func Load(r io.Reader) (*Config, error) {
b, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}
c := &Config{}
err = yaml.Unmarshal(b, c)
if err != nil {
return nil, err
}
return c, nil
}

View File

@@ -1,61 +1,63 @@
package config
import (
"bytes"
"io/ioutil"
"testing"
)
func TestShouldParse(t *testing.T) {
b := loadTestFile(t)
c, err := Load(bytes.NewReader(b))
if err != nil {
t.Fatalf("could not parse: %v", err)
}
if len(c.Devices) != 2 {
t.Fatalf("expected 2 devices, got %v", len(c.Devices))
}
assertDevice("test1", "192.168.1.1", "foo", "bar", c.Devices[0], t)
assertDevice("test2", "192.168.2.1", "test", "123", c.Devices[1], t)
assertFeature("BGP", c.Features.BGP, t)
assertFeature("DHCP", c.Features.DHCP, t)
assertFeature("DHCPv6", c.Features.DHCPv6, t)
assertFeature("Pools", c.Features.Pools, t)
assertFeature("Routes", c.Features.Routes, t)
assertFeature("Optics", c.Features.Optics, t)
}
func loadTestFile(t *testing.T) []byte {
b, err := ioutil.ReadFile("test/config.test.yml")
if err != nil {
t.Fatalf("could not load config: %v", err)
}
return b
}
func assertDevice(name, address, user, password string, c Device, t *testing.T) {
if c.Name != name {
t.Fatalf("expected name %s, got %s", name, c.Name)
}
if c.Address != address {
t.Fatalf("expected address %s, got %s", address, c.Address)
}
if c.User != user {
t.Fatalf("expected user %s, got %s", user, c.User)
}
if c.Password != password {
t.Fatalf("expected password %s, got %s", password, c.Password)
}
}
func assertFeature(name string, v bool, t *testing.T) {
if !v {
t.Fatalf("exprected feature %s to be enabled", name)
}
}
package config
import (
"bytes"
"io/ioutil"
"testing"
)
func TestShouldParse(t *testing.T) {
b := loadTestFile(t)
c, err := Load(bytes.NewReader(b))
if err != nil {
t.Fatalf("could not parse: %v", err)
}
if len(c.Devices) != 2 {
t.Fatalf("expected 2 devices, got %v", len(c.Devices))
}
assertDevice("test1", "192.168.1.1", "foo", "bar", c.Devices[0], t)
assertDevice("test2", "192.168.2.1", "test", "123", c.Devices[1], t)
assertFeature("BGP", c.Features.BGP, t)
assertFeature("DHCP", c.Features.DHCP, t)
assertFeature("DHCPv6", c.Features.DHCPv6, t)
assertFeature("Pools", c.Features.Pools, t)
assertFeature("Routes", c.Features.Routes, t)
assertFeature("Optics", c.Features.Optics, t)
assertFeature("WlanSTA", c.Features.WlanSTA, t)
assertFeature("WlanIF", c.Features.WlanIF, t)
}
func loadTestFile(t *testing.T) []byte {
b, err := ioutil.ReadFile("test/config.test.yml")
if err != nil {
t.Fatalf("could not load config: %v", err)
}
return b
}
func assertDevice(name, address, user, password string, c Device, t *testing.T) {
if c.Name != name {
t.Fatalf("expected name %s, got %s", name, c.Name)
}
if c.Address != address {
t.Fatalf("expected address %s, got %s", address, c.Address)
}
if c.User != user {
t.Fatalf("expected user %s, got %s", user, c.User)
}
if c.Password != password {
t.Fatalf("expected password %s, got %s", password, c.Password)
}
}
func assertFeature(name string, v bool, t *testing.T) {
if !v {
t.Fatalf("exprected feature %s to be enabled", name)
}
}

View File

@@ -1,19 +1,21 @@
---
devices:
- name: test1
address: 192.168.1.1
user: foo
password: bar
- name: test2
address: 192.168.2.1
user: test
password: 123
features:
bgp: true
dhcp: true
dhcpv6: true
routes: true
pools: true
optics: true
---
devices:
- name: test1
address: 192.168.1.1
user: foo
password: bar
- name: test2
address: 192.168.2.1
user: test
password: 123
features:
bgp: true
dhcp: true
dhcpv6: true
routes: true
pools: true
optics: true
wlansta: true
wlanif: true