mikrotik-exporter/collector/routes_collector.go
Daniel Czerwonk d170b0a4d2 More features (#9)
* added config file implementation, refactoring
* add gitignore
* improved test
* preperations for more metrics
* added resource metrics
* added first bgp metrics
* added asn as label for bgp metrics
* added prefix and message counts to bgp metrics
* simplified
* Update README.md
* added yaml dependency
* fixed go routine call
* added timeout
* clean up
* added TLS support
* set default api port for TLS
* added routes metric
* added missing log information
* added type collectorContext to reduce the count of parameters for better readability
* added DHCP and DHCPv6 metrics
* filter for active dhcp leases only
* added pool metrics
* enable/disable feature in config file
* refactoring

* clean up

* comment fix
2018-04-11 09:21:38 -04:00

118 lines
3.1 KiB
Go

package collector
import (
"fmt"
"strconv"
"github.com/prometheus/client_golang/prometheus"
log "github.com/sirupsen/logrus"
)
type routesCollector struct {
protocols []string
countDesc *prometheus.Desc
countProtocolDesc *prometheus.Desc
}
func newRoutesCollector() routerOSCollector {
c := &routesCollector{}
c.init()
return c
}
func (c *routesCollector) init() {
const prefix = "routes"
labelNames := []string{"name", "address", "ip_version"}
c.countDesc = description(prefix, "total_count", "number of routes in RIB", labelNames)
c.countProtocolDesc = description(prefix, "protocol_count", "number of routes per protocol in RIB", append(labelNames, "protocol"))
c.protocols = []string{"bgp", "static", "ospf", "dynamic", "connect"}
}
func (c *routesCollector) describe(ch chan<- *prometheus.Desc) {
ch <- c.countDesc
ch <- c.countProtocolDesc
}
func (c *routesCollector) collect(ctx *collectorContext) error {
err := c.colllectForIPVersion("4", "ip", ctx)
if err != nil {
return err
}
err = c.colllectForIPVersion("6", "ipv6", ctx)
if err != nil {
return err
}
return nil
}
func (c *routesCollector) colllectForIPVersion(ipVersion, topic string, ctx *collectorContext) error {
err := c.colllectCount(ipVersion, topic, ctx)
if err != nil {
return err
}
for _, p := range c.protocols {
err := c.colllectCountProtcol(ipVersion, topic, p, ctx)
if err != nil {
return err
}
}
return nil
}
func (c *routesCollector) colllectCount(ipVersion, topic string, ctx *collectorContext) error {
reply, err := ctx.client.Run(fmt.Sprintf("/%s/route/print", topic), "?disabled=false", "=count-only=")
if err != nil {
log.WithFields(log.Fields{
"ip_version": ipVersion,
"device": ctx.device.Name,
"error": err,
}).Error("error fetching routes metrics")
return err
}
v, err := strconv.ParseFloat(reply.Done.Map["ret"], 32)
if err != nil {
log.WithFields(log.Fields{
"ip_version": ipVersion,
"device": ctx.device.Name,
"error": err,
}).Error("error parsing routes metrics")
return err
}
ctx.ch <- prometheus.MustNewConstMetric(c.countDesc, prometheus.GaugeValue, v, ctx.device.Name, ctx.device.Address, ipVersion)
return nil
}
func (c *routesCollector) colllectCountProtcol(ipVersion, topic, protocol string, ctx *collectorContext) error {
reply, err := ctx.client.Run(fmt.Sprintf("/%s/route/print", topic), "?disabled=false", fmt.Sprintf("?%s", protocol), "=count-only=")
if err != nil {
log.WithFields(log.Fields{
"ip_version": ipVersion,
"protocol": protocol,
"device": ctx.device.Name,
"error": err,
}).Error("error fetching routes metrics")
return err
}
v, err := strconv.ParseFloat(reply.Done.Map["ret"], 32)
if err != nil {
log.WithFields(log.Fields{
"ip_version": ipVersion,
"protocol": protocol,
"device": ctx.device.Name,
"error": err,
}).Error("error parsing routes metrics")
return err
}
ctx.ch <- prometheus.MustNewConstMetric(c.countProtocolDesc, prometheus.GaugeValue, v, ctx.device.Name, ctx.device.Address, ipVersion, protocol)
return nil
}