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

export new metrics (prefix_limits) in bgp module #201

Merged
merged 1 commit into from
Nov 22, 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
55 changes: 46 additions & 9 deletions bgp/collector.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package bgp

import (
"math"

"github.com/czerwonk/junos_exporter/collector"
"github.com/czerwonk/junos_exporter/rpc"
"github.com/prometheus/client_golang/prometheus"
Expand All @@ -11,15 +13,17 @@ import (
const prefix string = "junos_bgp_session_"

var (
upDesc *prometheus.Desc
receivedPrefixesDesc *prometheus.Desc
acceptedPrefixesDesc *prometheus.Desc
rejectedPrefixesDesc *prometheus.Desc
activePrefixesDesc *prometheus.Desc
advertisedPrefixesDesc *prometheus.Desc
inputMessagesDesc *prometheus.Desc
outputMessagesDesc *prometheus.Desc
flapsDesc *prometheus.Desc
upDesc *prometheus.Desc
receivedPrefixesDesc *prometheus.Desc
acceptedPrefixesDesc *prometheus.Desc
rejectedPrefixesDesc *prometheus.Desc
activePrefixesDesc *prometheus.Desc
advertisedPrefixesDesc *prometheus.Desc
inputMessagesDesc *prometheus.Desc
outputMessagesDesc *prometheus.Desc
flapsDesc *prometheus.Desc
prefixesLimitCountDesc *prometheus.Desc
prefixesLimitPercentageDesc *prometheus.Desc
)

func init() {
Expand All @@ -30,11 +34,14 @@ func init() {
flapsDesc = prometheus.NewDesc(prefix+"flap_count", "Number of session flaps", l, nil)

l = append(l, "table")

receivedPrefixesDesc = prometheus.NewDesc(prefix+"prefixes_received_count", "Number of received prefixes", l, nil)
acceptedPrefixesDesc = prometheus.NewDesc(prefix+"prefixes_accepted_count", "Number of accepted prefixes", l, nil)
rejectedPrefixesDesc = prometheus.NewDesc(prefix+"prefixes_rejected_count", "Number of rejected prefixes", l, nil)
activePrefixesDesc = prometheus.NewDesc(prefix+"prefixes_active_count", "Number of active prefixes (best route in RIB)", l, nil)
advertisedPrefixesDesc = prometheus.NewDesc(prefix+"prefixes_advertised_count", "Number of prefixes announced to peer", l, nil)
prefixesLimitPercentageDesc = prometheus.NewDesc(prefix+"prefixes_limit_percentage", "percentage of received prefixes against prefix-limit", l, nil)
prefixesLimitCountDesc = prometheus.NewDesc(prefix+"prefixes_limit_count", "prefix-count variable set in prefix-limit", l, nil)
}

type bgpCollector struct {
Expand Down Expand Up @@ -62,6 +69,8 @@ func (*bgpCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- inputMessagesDesc
ch <- outputMessagesDesc
ch <- flapsDesc
ch <- prefixesLimitCountDesc
ch <- prefixesLimitPercentageDesc
}

// Collect collects metrics from JunOS
Expand Down Expand Up @@ -112,12 +121,40 @@ func (c *bgpCollector) collectForPeer(p BGPPeer, ch chan<- prometheus.Metric, la
}

func (*bgpCollector) collectRIBForPeer(p BGPPeer, ch chan<- prometheus.Metric, labelValues []string) {
var rib_name string

// derive the name of the rib for which the prefix limit is configured by examining the NLRI type
switch nlri_type := p.BGPOI.PrefixLimit.NlriType; nlri_type {
case "inet-unicast":
rib_name = "inet.0"
case "inet6-unicast":
rib_name = "inet6.0"
default:
rib_name = ""
}

// if the prefix limit is configured inside a routing instance we need to prepend the RTI name to the rib name
if p.CFG_RTI != "" && p.CFG_RTI != "master" && rib_name != "" {
rib_name = p.CFG_RTI + "." + rib_name
}

if p.BGPOI.PrefixLimit.PrefixCount > 0 {
ch <- prometheus.MustNewConstMetric(prefixesLimitCountDesc, prometheus.GaugeValue, float64(p.BGPOI.PrefixLimit.PrefixCount), append(labelValues, rib_name)...)
}

for _, rib := range p.RIBs {
l := append(labelValues, rib.Name)
ch <- prometheus.MustNewConstMetric(receivedPrefixesDesc, prometheus.GaugeValue, float64(rib.ReceivedPrefixes), l...)
ch <- prometheus.MustNewConstMetric(acceptedPrefixesDesc, prometheus.GaugeValue, float64(rib.AcceptedPrefixes), l...)
ch <- prometheus.MustNewConstMetric(rejectedPrefixesDesc, prometheus.GaugeValue, float64(rib.RejectedPrefixes), l...)
ch <- prometheus.MustNewConstMetric(activePrefixesDesc, prometheus.GaugeValue, float64(rib.ActivePrefixes), l...)
ch <- prometheus.MustNewConstMetric(advertisedPrefixesDesc, prometheus.GaugeValue, float64(rib.AdvertisedPrefixes), l...)

if rib.Name == rib_name {
if p.BGPOI.PrefixLimit.PrefixCount > 0 {
prefixesLimitPercent := float64(rib.ReceivedPrefixes) / float64(p.BGPOI.PrefixLimit.PrefixCount)
ch <- prometheus.MustNewConstMetric(prefixesLimitPercentageDesc, prometheus.GaugeValue, math.Round(prefixesLimitPercent*100)/100, l...)
}
}
}
}
40 changes: 31 additions & 9 deletions bgp/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@ type BGPRPC struct {
}

type BGPPeer struct {
IP string `xml:"peer-address"`
ASN string `xml:"peer-as"`
State string `xml:"peer-state"`
Group string `xml:"peer-group"`
Description string `xml:"description"`
Flaps int64 `xml:"flap-count"`
InputMessages int64 `xml:"input-messages"`
OutputMessages int64 `xml:"output-messages"`
RIBs []RIB `xml:"bgp-rib"`
CFG_RTI string `xml:"peer-cfg-rti"`
IP string `xml:"peer-address"`
ASN string `xml:"peer-as"`
State string `xml:"peer-state"`
Group string `xml:"peer-group"`
Description string `xml:"description"`
Flaps int64 `xml:"flap-count"`
InputMessages int64 `xml:"input-messages"`
OutputMessages int64 `xml:"output-messages"`
RIBs []RIB `xml:"bgp-rib"`
BGPOI BGPOptionInformation `xml:"bgp-option-information"`
}

type RIB struct {
Expand All @@ -26,3 +28,23 @@ type RIB struct {
RejectedPrefixes int64 `xml:"suppressed-prefix-count"`
AdvertisedPrefixes int64 `xml:"advertised-prefix-count"`
}

type BGPOptionInformation struct {
ExportPolicy string `xml:"export-policy"`
ImportPolicy string `xml:"import-policy"`
AddressFamilies string `xml:"address-families"`
LocalAddress string `xml:"local-address"`
Holdtime int64 `xml:"holdtime"`
MetricOut int64 `xml:"metric-out"`
Preference int64 `xml:"preference"`
PrefixLimit PrefixLimit `xml:"prefix-limit"`
LocalAs int64 `xml:"local-as"`
LocalSystemAs int64 `xml:"local-system-as"`
}

type PrefixLimit struct {
NlriType string `xml:"nlri-type"`
PrefixCount int64 `xml:"prefix-count"`
LimitAction string `xml:"limit-action"`
WarningPercentage int64 `xml:"warning-percentage"`
}