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
This commit is contained in:
Daniel Czerwonk
2018-04-11 15:21:38 +02:00
committed by Steve Brunton
parent f2866a3a2f
commit d170b0a4d2
16 changed files with 559 additions and 175 deletions

View File

@@ -4,95 +4,101 @@ import (
"fmt"
"strconv"
"github.com/nshttpd/mikrotik-exporter/config"
"github.com/prometheus/client_golang/prometheus"
log "github.com/sirupsen/logrus"
"gopkg.in/routeros.v2"
)
const routesPrefiix = "routes"
var (
routesProtocols = []string{"bgp", "static", "ospf", "dynamic", "connect"}
)
var (
routesTotalDesc *prometheus.Desc
routesProtocolDesc *prometheus.Desc
)
func init() {
l := []string{"name", "address", "ip_version"}
routesTotalDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, routesPrefiix, "total_count"),
"number of routes in RIB",
l,
nil,
)
routesProtocolDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, routesPrefiix, "protocol_count"),
"number of routes per protocol in RIB",
append(l, "protocol"),
nil,
)
}
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 <- routesTotalDesc
ch <- routesProtocolDesc
ch <- c.countDesc
ch <- c.countProtocolDesc
}
func (c *routesCollector) collect(ch chan<- prometheus.Metric, device *config.Device, client *routeros.Client) error {
c.colllectForIPVersion(client, device, ch, "4", "ip")
c.colllectForIPVersion(client, device, ch, "6", "ipv6")
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(client *routeros.Client, device *config.Device, ch chan<- prometheus.Metric, ipVersion, topic string) {
c.colllectCount(client, device, ch, ipVersion, topic)
for _, p := range routesProtocols {
c.colllectCountProtcol(client, device, ch, ipVersion, topic, p)
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(client *routeros.Client, device *config.Device, ch chan<- prometheus.Metric, ipVersion, topic string) {
reply, err := client.Run(fmt.Sprintf("/%s/route/print", topic), "?disabled=false", "=count-only=")
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": device.Name,
"device": ctx.device.Name,
"error": err,
}).Error("error fetching routes metrics")
return
return err
}
v, err := strconv.ParseFloat(reply.Done.Map["ret"], 32)
if err != nil {
log.WithFields(log.Fields{
"ip_version": ipVersion,
"device": device.Name,
"device": ctx.device.Name,
"error": err,
}).Error("error parsing routes metrics")
return
return err
}
ch <- prometheus.MustNewConstMetric(routesTotalDesc, prometheus.GaugeValue, v, device.Name, device.Address, ipVersion)
ctx.ch <- prometheus.MustNewConstMetric(c.countDesc, prometheus.GaugeValue, v, ctx.device.Name, ctx.device.Address, ipVersion)
return nil
}
func (c *routesCollector) colllectCountProtcol(client *routeros.Client, device *config.Device, ch chan<- prometheus.Metric, ipVersion, topic, protocol string) {
reply, err := client.Run(fmt.Sprintf("/%s/route/print", topic), "?disabled=false", fmt.Sprintf("?%s", protocol), "=count-only=")
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": device.Name,
"device": ctx.device.Name,
"error": err,
}).Error("error fetching routes metrics")
return
return err
}
v, err := strconv.ParseFloat(reply.Done.Map["ret"], 32)
@@ -100,11 +106,12 @@ func (c *routesCollector) colllectCountProtcol(client *routeros.Client, device *
log.WithFields(log.Fields{
"ip_version": ipVersion,
"protocol": protocol,
"device": device.Name,
"device": ctx.device.Name,
"error": err,
}).Error("error parsing routes metrics")
return
return err
}
ch <- prometheus.MustNewConstMetric(routesProtocolDesc, prometheus.GaugeValue, v, device.Name, device.Address, ipVersion, protocol)
ctx.ch <- prometheus.MustNewConstMetric(c.countProtocolDesc, prometheus.GaugeValue, v, ctx.device.Name, ctx.device.Address, ipVersion, protocol)
return nil
}