Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.17
go-version: 1.18

- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v2
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
test:
strategy:
matrix:
go-version: [1.17.x]
go-version: [1.18.x]
platform: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.platform }}
steps:
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,13 @@ devices:
# interface_description_regex: '\[([^=\]]+)(=[^\]]+)?\]'
features:
isis: true
- host: switch\d+
# Tell the exporter that this hostname should be used as a pattern when loading
# device-specific configurations. This example would match against a hostname
# like "switch123".
host_pattern: true
features:
bgp: false

# Optional
# interface_description_regex: '\[([^=\]]+)(=[^\]]+)?\]'
Expand Down
14 changes: 6 additions & 8 deletions bfd/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ const prefix = "junos_bfd_"
var (
bfdState *prometheus.Desc
bfdStateMap = map[string]int{
"Down": 0,
"Up": 1,
"Down": 0,
"Up": 1,
}

)

func init() {
Expand All @@ -40,18 +39,17 @@ func (*bfdCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- bfdState
}


// Collect collects metrics from JunOS
func (c *bfdCollector) Collect(client *rpc.Client, ch chan<- prometheus.Metric, labelValues []string) error {
var x = bfdRpc{}
err := client.RunCommandAndParse("show bfd session extensive", &x)
var x = bfdRpc{}
err := client.RunCommandAndParse("show bfd session extensive", &x)
if err != nil {
return err
}

for _, bfds := range x.Information.BfdSessions {
l := append(labelValues, bfds.Neighbor, bfds.Interface, bfds.Client.Name)
ch <- prometheus.MustNewConstMetric(bfdState, prometheus.GaugeValue, float64(bfdStateMap[bfds.State]), l...)
l := append(labelValues, bfds.Neighbor, bfds.Interface, bfds.Client.Name)
ch <- prometheus.MustNewConstMetric(bfdState, prometheus.GaugeValue, float64(bfdStateMap[bfds.State]), l...)
}

return nil
Expand Down
21 changes: 10 additions & 11 deletions bfd/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,19 @@ package bfd

type bfdRpc struct {
Information struct {
BfdSessions []bfdSession `xml:"bfd-session"`
sessions int64 `xml:"sessions"`
clients int64 `xml:"clients"`
CumTransRate float64 `xml:"cumulative-transmission-rate"`
CumRecRate float64 `xml:"cumulative-reception-rate"`
BfdSessions []bfdSession `xml:"bfd-session"`
Sessions int64 `xml:"sessions"`
Clients int64 `xml:"clients"`
CumTransRate float64 `xml:"cumulative-transmission-rate"`
CumRecRate float64 `xml:"cumulative-reception-rate"`
} `xml:"bfd-session-information"`
}

type bfdSession struct {
Neighbor string `xml:"session-neighbor"`
State string `xml:"session-state"`
Interface string `xml:"session-interface"`
Client struct {
Name string `xml:"client-name"`
Neighbor string `xml:"session-neighbor"`
State string `xml:"session-state"`
Interface string `xml:"session-interface"`
Client struct {
Name string `xml:"client-name"`
} `xml:"bfd-client"`
}

36 changes: 28 additions & 8 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package config
import (
"io"
"io/ioutil"
"regexp"

"gopkg.in/yaml.v2"
)
Expand All @@ -19,12 +20,14 @@ type Config struct {

// DeviceConfig is the config representation of 1 device
type DeviceConfig struct {
Host string `yaml:"host"`
Username string `yaml:"username,omitempty"`
Password string `yaml:"password,omitempty"`
KeyFile string `yaml:"key_file,omitempty"`
Features *FeatureConfig `yaml:"features,omitempty"`
IfDescReg string `yaml:"interface_description_regex,omitempty"`
Host string `yaml:"host"`
Username string `yaml:"username,omitempty"`
Password string `yaml:"password,omitempty"`
KeyFile string `yaml:"key_file,omitempty"`
Features *FeatureConfig `yaml:"features,omitempty"`
IfDescReg string `yaml:"interface_description_regex,omitempty"`
IsHostPattern bool `yaml:"host_pattern,omitempty"`
HostPattern *regexp.Regexp
}

// FeatureConfig is the list of collectors enabled or disabled
Expand Down Expand Up @@ -85,6 +88,16 @@ func Load(reader io.Reader) (*Config, error) {
return nil, err
}

for _, device := range c.Devices {
if device.IsHostPattern {
hostPattern, err := regexp.Compile(device.Host)
if err != nil {
return nil, err
}
device.HostPattern = hostPattern
}
}

return c, nil
}

Expand Down Expand Up @@ -119,6 +132,7 @@ func setDefaultValues(c *Config) {
f.MPLS_LSP = false
f.VPWS = false
f.VRRP = false
f.BFD = false
}

// FeaturesForDevice gets the feature set configured for a device
Expand All @@ -134,8 +148,14 @@ func (c *Config) FeaturesForDevice(host string) *FeatureConfig {

func (c *Config) findDeviceConfig(host string) *DeviceConfig {
for _, dc := range c.Devices {
if dc.Host == host {
return dc
if dc.HostPattern != nil {
if dc.HostPattern.MatchString(host) {
return dc
}
} else {
if dc.Host == host {
return dc
}
}
}

Expand Down
88 changes: 88 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ func TestShouldParseDevices(t *testing.T) {
assert.Equal(t, "router1", d1.Host, "Device 1: Host")
assert.Equal(t, "keyfile_user", d1.Username, "Device 1: Username")
assert.Equal(t, "/path/to/key", d1.KeyFile, "Device 1: Keyfile")
assert.Equal(t, false, d1.IsHostPattern, "Device 1: Host pattern")

d2 := c.Devices[1]
assert.Equal(t, "router2", d2.Host, "Device 2: Host")
Expand Down Expand Up @@ -115,8 +116,95 @@ func TestShouldParseDevices(t *testing.T) {
assertFeature("Power", f.Power, true, t)
}

func TestShouldParseDevicesWithPattern(t *testing.T) {
b, err := ioutil.ReadFile("tests/config4.yml")
if err != nil {
t.Fatal(err)
}

c, err := Load(bytes.NewReader(b))
if err != nil {
t.Fatal(err)
}

assert.Equal(t, 2, len(c.Devices), "devices")

d2 := c.Devices[1]
assert.Equal(t, "switch\\-[a-z]{3}\\d+", d2.Host, "Device 1: Host")
assert.Equal(t, true, d2.IsHostPattern, "Device 1: Host pattern")
assert.Equal(t, "switch", d2.Username, "Device 1: Username")
assert.Equal(t, "secret", d2.Password, "Device 1: Keyfile")

f := d2.Features
assertFeature("Alarm", f.Alarm, false, t)
assertFeature("Environment", f.Environment, true, t)
assertFeature("BGP", f.BGP, false, t)
assertFeature("OSPF", f.OSPF, false, t)
assertFeature("ISIS", f.ISIS, false, t)
assertFeature("NAT", f.NAT, false, t)
assertFeature("L2Circuit", f.L2Circuit, false, t)
assertFeature("LDP", f.LDP, false, t)
assertFeature("Routes", f.Routes, false, t)
assertFeature("RoutingEngine", f.RoutingEngine, false, t)
assertFeature("Firewall", f.Firewall, false, t)
assertFeature("Interfaces", f.Interfaces, true, t)
assertFeature("InterfaceDiagnostic", f.InterfaceDiagnostic, true, t)
assertFeature("Storage", f.Storage, false, t)
assertFeature("Accounting", f.Accounting, false, t)
assertFeature("IPSec", f.IPSec, false, t)
assertFeature("FPC", f.FPC, false, t)
assertFeature("RPKI", f.RPKI, false, t)
assertFeature("Power", f.Power, true, t)
}

func TestShouldParseDevicesWithPatternInvalid(t *testing.T) {
b, err := ioutil.ReadFile("tests/config5.yml")
if err != nil {
t.Fatal(err)
}

c, err := Load(bytes.NewReader(b))
if c != nil {
t.Fatal("Parsing should fail because of invalid pattern")
}
if err.Error() != "error parsing regexp: invalid escape sequence: `\\k`" {
t.Fatalf("Unexpected error: %s", err.Error())
}
}

func assertFeature(name string, actual, expected bool, t *testing.T) {
if actual != expected {
t.Fatalf("feature %s should be %v, but is %v", name, expected, actual)
}
}

func TestFindDeviceConfig(t *testing.T) {
b, err := ioutil.ReadFile("tests/config4.yml")
if err != nil {
t.Fatal(err)
}
c, err := Load(bytes.NewReader(b))
if err != nil {
t.Fatal(err)
}

device1 := c.findDeviceConfig("router1")
if device1.Username != "router" {
t.Fatalf("Unexpected username for router1: %s", device1.Username)
}

device2 := c.findDeviceConfig("router2")
if device2 != nil {
t.Fatal("Unexpected device for router2")
}

device3 := c.findDeviceConfig("switch-ber01")
if device3.Username != "switch" {
t.Fatalf("Unexpected username for switch-ber01: %s", device1.Username)
}

device4 := c.findDeviceConfig("switch-oob")
if device4 != nil {
t.Fatal("Unexpected device for switch-oob")
}
}
28 changes: 28 additions & 0 deletions config/tests/config4.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
devices:
- host: router1
username: router
- host: switch\-[a-z]{3}\d+
host_pattern: true
username: switch
password: secret
features:
bgp: false
ospf: false
isis: false
nat: false
environment: true
routes: false
routing_engine: false
interface_diagnostic: true
interface_queue: true
interfaces: true
l2circuit: false
storage: false
fpc: false
firewall: false
ldp: false
accounting: false
ipsec: false
rpki: false
power: true

3 changes: 3 additions & 0 deletions config/tests/config5.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
devices:
- host: asw[a-z]3}\k+-\d+
host_pattern: true
3 changes: 2 additions & 1 deletion fpc/rpc_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package fpc

import (
"github.com/stretchr/testify/assert"
"testing"

"github.com/stretchr/testify/assert"
)

func TestParseEXOutput(t *testing.T) {
Expand Down
22 changes: 11 additions & 11 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
module github.com/czerwonk/junos_exporter

go 1.17
go 1.18

require (
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.11.0
github.com/prometheus/client_golang v1.12.2
github.com/prometheus/client_model v0.2.0
github.com/sirupsen/logrus v1.8.1
github.com/stretchr/testify v1.7.0
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97
github.com/stretchr/testify v1.7.1
golang.org/x/crypto v0.0.0-20220513210258-46612604a0f9
gopkg.in/yaml.v2 v2.4.0
)

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.1.1 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/golang/protobuf v1.4.3 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/common v0.26.0 // indirect
github.com/prometheus/procfs v0.6.0 // indirect
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 // indirect
google.golang.org/protobuf v1.26.0-rc.1 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
github.com/prometheus/common v0.34.0 // indirect
github.com/prometheus/procfs v0.7.3 // indirect
golang.org/x/sys v0.0.0-20220513210249-45d2b4557a2a // indirect
google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20220512140231-539c8e751b99 // indirect
)
Loading