2018-03-21 02:28:10 +01:00
|
|
|
package collector
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"gopkg.in/routeros.v2/proto"
|
|
|
|
)
|
|
|
|
|
2018-04-11 15:21:38 +02:00
|
|
|
type bgpCollector struct {
|
|
|
|
props []string
|
|
|
|
descriptions map[string]*prometheus.Desc
|
|
|
|
}
|
2018-03-21 02:28:10 +01:00
|
|
|
|
2018-04-11 15:21:38 +02:00
|
|
|
func newBGPCollector() routerOSCollector {
|
|
|
|
c := &bgpCollector{}
|
|
|
|
c.init()
|
|
|
|
return c
|
2018-03-21 02:28:10 +01:00
|
|
|
}
|
|
|
|
|
2018-04-11 15:21:38 +02:00
|
|
|
func (c *bgpCollector) init() {
|
|
|
|
c.props = []string{"name", "remote-as", "state", "prefix-count", "updates-sent", "updates-received", "withdrawn-sent", "withdrawn-received"}
|
|
|
|
|
|
|
|
const prefix = "bgp"
|
|
|
|
labelNames := []string{"name", "address", "session", "asn"}
|
|
|
|
|
|
|
|
c.descriptions = make(map[string]*prometheus.Desc)
|
|
|
|
c.descriptions["state"] = description(prefix, "up", "BGP session is established (up = 1)", labelNames)
|
|
|
|
|
|
|
|
for _, p := range c.props[3:] {
|
|
|
|
c.descriptions[p] = descriptionForPropertyName(prefix, p, labelNames)
|
|
|
|
}
|
2018-03-21 02:28:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *bgpCollector) describe(ch chan<- *prometheus.Desc) {
|
2018-04-11 15:21:38 +02:00
|
|
|
for _, d := range c.descriptions {
|
2018-03-21 02:28:10 +01:00
|
|
|
ch <- d
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-11 15:21:38 +02:00
|
|
|
func (c *bgpCollector) collect(ctx *collectorContext) error {
|
|
|
|
stats, err := c.fetch(ctx)
|
2018-03-21 02:28:10 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, re := range stats {
|
2018-04-11 15:21:38 +02:00
|
|
|
c.collectForStat(re, ctx)
|
2018-03-21 02:28:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-04-11 15:21:38 +02:00
|
|
|
func (c *bgpCollector) fetch(ctx *collectorContext) ([]*proto.Sentence, error) {
|
|
|
|
reply, err := ctx.client.Run("/routing/bgp/peer/print", "=.proplist="+strings.Join(c.props, ","))
|
2018-03-21 02:28:10 +01:00
|
|
|
if err != nil {
|
|
|
|
log.WithFields(log.Fields{
|
2018-04-11 15:21:38 +02:00
|
|
|
"device": ctx.device.Name,
|
2018-03-21 02:28:10 +01:00
|
|
|
"error": err,
|
|
|
|
}).Error("error fetching bgp metrics")
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return reply.Re, nil
|
|
|
|
}
|
|
|
|
|
2018-04-11 15:21:38 +02:00
|
|
|
func (c *bgpCollector) collectForStat(re *proto.Sentence, ctx *collectorContext) {
|
2018-05-21 22:56:21 +02:00
|
|
|
asn := re.Map["remote-as"]
|
|
|
|
session := re.Map["name"]
|
|
|
|
|
|
|
|
for _, p := range c.props[2:] {
|
|
|
|
c.collectMetricForProperty(p, session, asn, re, ctx)
|
2018-03-21 02:28:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-11 15:21:38 +02:00
|
|
|
func (c *bgpCollector) collectMetricForProperty(property, session, asn string, re *proto.Sentence, ctx *collectorContext) {
|
|
|
|
desc := c.descriptions[property]
|
2018-03-21 02:28:10 +01:00
|
|
|
v, err := c.parseValueForProperty(property, re.Map[property])
|
|
|
|
if err != nil {
|
|
|
|
log.WithFields(log.Fields{
|
2018-04-11 15:21:38 +02:00
|
|
|
"device": ctx.device.Name,
|
2018-03-21 02:28:10 +01:00
|
|
|
"session": session,
|
|
|
|
"property": property,
|
|
|
|
"value": re.Map[property],
|
|
|
|
"error": err,
|
|
|
|
}).Error("error parsing bgp metric value")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-04-11 15:21:38 +02:00
|
|
|
ctx.ch <- prometheus.MustNewConstMetric(desc, prometheus.GaugeValue, v, ctx.device.Name, ctx.device.Address, session, asn)
|
2018-03-21 02:28:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *bgpCollector) parseValueForProperty(property, value string) (float64, error) {
|
|
|
|
if property == "state" {
|
|
|
|
if value == "established" {
|
|
|
|
return 1, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if value == "" {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return strconv.ParseFloat(value, 64)
|
|
|
|
}
|