Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add bfd #183

Merged
merged 3 commits into from
May 16, 2022
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
58 changes: 58 additions & 0 deletions bfd/collector.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package bfd

import (
"github.com/czerwonk/junos_exporter/collector"
"github.com/czerwonk/junos_exporter/rpc"
"github.com/prometheus/client_golang/prometheus"
)

const prefix = "junos_bfd_"

var (
bfdState *prometheus.Desc
bfdStateMap = map[string]int{
"Down": 0,
"Up": 1,
}

)

func init() {
l := []string{"target", "neighbor", "interface", "client"}
bfdState = prometheus.NewDesc(prefix+"state", "bfd state (0: down, 1:up)", l, nil)
}

type bfdCollector struct {
}

// Name returns the name of the collector
func (*bfdCollector) Name() string {
return "bfd"
}

// NewCollector creates a new collector
func NewCollector() collector.RPCCollector {
return &bfdCollector{}
}

// Describe describes the metrics
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)
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...)
}

return nil
}
21 changes: 21 additions & 0 deletions bfd/rpc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
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"`
} `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"`
} `xml:"bfd-client"`
}

2 changes: 2 additions & 0 deletions collectors.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"github.com/czerwonk/junos_exporter/accounting"
"github.com/czerwonk/junos_exporter/alarm"
"github.com/czerwonk/junos_exporter/bfd"
"github.com/czerwonk/junos_exporter/bgp"
"github.com/czerwonk/junos_exporter/collector"
"github.com/czerwonk/junos_exporter/config"
Expand Down Expand Up @@ -67,6 +68,7 @@ func (c *collectors) initCollectorsForDevices(device *connector.Device) {
c.addCollectorIfEnabledForDevice(device, "alarm", f.Alarm, func() collector.RPCCollector {
return alarm.NewCollector(*alarmFilter)
})
c.addCollectorIfEnabledForDevice(device, "bfd", f.BFD, bfd.NewCollector)
c.addCollectorIfEnabledForDevice(device, "bgp", f.BGP, func() collector.RPCCollector {
return bgp.NewCollector(c.logicalSystem)
})
Expand Down
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type DeviceConfig struct {
type FeatureConfig struct {
Alarm bool `yaml:"alarm,omitempty"`
Environment bool `yaml:"environment,omitempty"`
BFD bool `yaml:"bfd,omitempty"`
BGP bool `yaml:"bgp,omitempty"`
OSPF bool `yaml:"ospf,omitempty"`
ISIS bool `yaml:"isis,omitempty"`
Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ var (
interfaceDescriptionRegex = flag.String("interface-description-regex", "", "give a regex to retrieve the interface description labels")
lsEnabled = flag.Bool("logical-systems.enabled", false, "Enable logical systems support")
powerEnabled = flag.Bool("power.enabled", true, "Scrape power metrics")
bfdEnabled = flag.Bool("bfd.enabled", true, "Scrape bfd metrics")
cfg *config.Config
devices []*connector.Device
connManager *connector.SSHConnectionManager
Expand Down