News features and improvements (#8)

* 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
This commit is contained in:
Daniel Czerwonk
2018-03-21 02:28:10 +01:00
committed by Steve Brunton
parent c37abb638f
commit f2866a3a2f
340 changed files with 25181 additions and 3416 deletions

112
collector/bgp_collector.go Normal file
View File

@@ -0,0 +1,112 @@
package collector
import (
"strconv"
"strings"
"github.com/nshttpd/mikrotik-exporter/config"
"github.com/prometheus/client_golang/prometheus"
log "github.com/sirupsen/logrus"
routeros "gopkg.in/routeros.v2"
"gopkg.in/routeros.v2/proto"
)
var (
bgpabelNames = []string{"name", "address", "session", "asn"}
bgpProps = []string{"name", "remote-as", "state", "prefix-count", "updates-sent", "updates-received", "withdrawn-sent", "withdrawn-received"}
bgpDescriptions map[string]*prometheus.Desc
)
func init() {
bgpDescriptions = make(map[string]*prometheus.Desc)
bgpDescriptions["state"] = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "bgp", "up"),
"BGP session is established (up = 1)",
bgpabelNames,
nil,
)
for _, p := range bgpProps[3:] {
bgpDescriptions[p] = descriptionForPropertyName("bgp", p, bgpabelNames)
}
}
type bgpCollector struct {
}
func (c *bgpCollector) describe(ch chan<- *prometheus.Desc) {
for _, d := range bgpDescriptions {
ch <- d
}
}
func (c *bgpCollector) collect(ch chan<- prometheus.Metric, device *config.Device, client *routeros.Client) error {
stats, err := c.fetch(client, device)
if err != nil {
return err
}
for _, re := range stats {
c.collectForStat(re, device, ch)
}
return nil
}
func (c *bgpCollector) fetch(client *routeros.Client, device *config.Device) ([]*proto.Sentence, error) {
reply, err := client.Run("/routing/bgp/peer/print", "=.proplist="+strings.Join(bgpProps, ","))
if err != nil {
log.WithFields(log.Fields{
"device": device.Name,
"error": err,
}).Error("error fetching bgp metrics")
return nil, err
}
return reply.Re, nil
}
func (c *bgpCollector) collectForStat(re *proto.Sentence, device *config.Device, ch chan<- prometheus.Metric) {
var session, asn string
for _, p := range bgpProps {
if p == "name" {
session = re.Map[p]
} else if p == "remote-as" {
asn = re.Map[p]
} else {
c.collectMetricForProperty(p, session, asn, device, re, ch)
}
}
}
func (c *bgpCollector) collectMetricForProperty(property, session, asn string, device *config.Device, re *proto.Sentence, ch chan<- prometheus.Metric) {
desc := bgpDescriptions[property]
v, err := c.parseValueForProperty(property, re.Map[property])
if err != nil {
log.WithFields(log.Fields{
"device": device.Name,
"session": session,
"property": property,
"value": re.Map[property],
"error": err,
}).Error("error parsing bgp metric value")
return
}
ch <- prometheus.MustNewConstMetric(desc, prometheus.GaugeValue, v, device.Name, device.Address, session, asn)
}
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)
}

View File

@@ -1,14 +1,24 @@
package collector
import (
"crypto/tls"
"sync"
"time"
"github.com/nshttpd/mikrotik-exporter/config"
"github.com/prometheus/client_golang/prometheus"
log "github.com/sirupsen/logrus"
routeros "gopkg.in/routeros.v2"
)
const namespace = "mikrotik"
const (
namespace = "mikrotik"
apiPort = ":8728"
apiPortTLS = ":8729"
// DefaultTimeout defines the default timeout when connecting to a router
DefaultTimeout = 5 * time.Second
)
var (
scrapeDurationDesc = prometheus.NewDesc(
@@ -25,54 +35,140 @@ var (
)
)
type deviceCollector struct {
Devices []Device
type collector struct {
devices []config.Device
collectors []metricCollector
timeout time.Duration
enableTLS bool
insecureTLS bool
}
func NewDeviceCollector(cfg Config) (*deviceCollector, error) {
devices := make([]Device, len(cfg.Devices))
// WithBGP enables BGP routing metrics
func WithBGP() Option {
return func(c *collector) {
c.collectors = append(c.collectors, &bgpCollector{})
}
}
// WithRoutes enables routing table metrics
func WithRoutes() Option {
return func(c *collector) {
c.collectors = append(c.collectors, &routesCollector{})
}
}
// WithTimeout sets timeout for connecting to router
func WithTimeout(d time.Duration) Option {
return func(c *collector) {
c.timeout = d
}
}
// WithTLS enables TLS
func WithTLS(insecure bool) Option {
return func(c *collector) {
c.enableTLS = true
c.insecureTLS = true
}
}
// Option applies options to collector
type Option func(*collector)
// NewCollector creates a collector instance
func NewCollector(cfg *config.Config, opts ...Option) (prometheus.Collector, error) {
log.WithFields(log.Fields{
"numDevices": len(cfg.Devices),
}).Info("setting up collector for devices")
copy(devices, cfg.Devices)
c := &collector{
devices: cfg.Devices,
timeout: DefaultTimeout,
collectors: []metricCollector{
&interfaceCollector{},
&resourceCollector{},
},
}
return &deviceCollector{Devices: devices}, nil
for _, o := range opts {
o(c)
}
return c, nil
}
// Describe implements the prometheus.Collector interface.
func (d deviceCollector) Describe(ch chan<- *prometheus.Desc) {
func (c *collector) Describe(ch chan<- *prometheus.Desc) {
ch <- scrapeDurationDesc
ch <- scrapeSuccessDesc
for _, co := range c.collectors {
co.describe(ch)
}
}
// Collect implements the prometheus.Collector interface.
func (d deviceCollector) Collect(ch chan<- prometheus.Metric) {
func (c *collector) Collect(ch chan<- prometheus.Metric) {
wg := sync.WaitGroup{}
wg.Add(len(d.Devices))
for _, device := range d.Devices {
go func(d Device) {
execute(d, ch)
wg.Add(len(c.devices))
for _, dev := range c.devices {
go func(d config.Device) {
c.collectForDevice(d, ch)
wg.Done()
}(device)
}(dev)
}
wg.Wait()
}
func execute(d Device, ch chan<- prometheus.Metric) {
func (c *collector) collectForDevice(d config.Device, ch chan<- prometheus.Metric) {
begin := time.Now()
err := d.Update(ch)
err := c.connectAndCollect(&d, ch)
duration := time.Since(begin)
var success float64
if err != nil {
log.Errorf("ERROR: %s collector failed after %fs: %s", d.name, duration.Seconds(), err)
log.Errorf("ERROR: %s collector failed after %fs: %s", d.Name, duration.Seconds(), err)
success = 0
} else {
log.Debugf("OK: %s collector succeeded after %fs.", d.name, duration.Seconds())
log.Debugf("OK: %s collector succeeded after %fs.", d.Name, duration.Seconds())
success = 1
}
ch <- prometheus.MustNewConstMetric(scrapeDurationDesc, prometheus.GaugeValue, duration.Seconds(), d.name)
ch <- prometheus.MustNewConstMetric(scrapeSuccessDesc, prometheus.GaugeValue, success, d.name)
ch <- prometheus.MustNewConstMetric(scrapeDurationDesc, prometheus.GaugeValue, duration.Seconds(), d.Name)
ch <- prometheus.MustNewConstMetric(scrapeSuccessDesc, prometheus.GaugeValue, success, d.Name)
}
func (c *collector) connectAndCollect(d *config.Device, ch chan<- prometheus.Metric) error {
cl, err := c.connect(d)
if err != nil {
log.WithFields(log.Fields{
"device": d.Name,
"error": err,
}).Error("error dialing device")
return err
}
defer cl.Close()
for _, co := range c.collectors {
err = co.collect(ch, d, cl)
if err != nil {
return err
}
}
return nil
}
func (c *collector) connect(d *config.Device) (*routeros.Client, error) {
if !c.enableTLS {
return routeros.DialTimeout(d.Address+apiPort, d.User, d.Password, c.timeout)
}
tls := &tls.Config{
InsecureSkipVerify: c.insecureTLS,
}
return routeros.DialTLSTimeout(d.Address+apiPortTLS, d.User, d.Password, tls, c.timeout)
}

View File

@@ -1,33 +0,0 @@
package collector
import (
"fmt"
"github.com/prometheus/client_golang/prometheus"
)
type Config struct {
Devices []Device
}
func (c *Config) FromFlags(device, address, user, password *string) error {
if *device == "" || *address == "" || *user == "" || *password == "" {
return fmt.Errorf("missing required param for single device configuration")
}
d := &Device{
address: *address,
name: *device,
user: *user,
password: *password,
iDesc: map[string]*prometheus.Desc{},
rDesc: map[string]*prometheus.Desc{},
}
*c = Config{
Devices: []Device{*d},
}
return nil
}

View File

@@ -1,108 +0,0 @@
package collector
import (
"strings"
"fmt"
"strconv"
"github.com/prometheus/client_golang/prometheus"
log "github.com/sirupsen/logrus"
"gopkg.in/routeros.v2"
"gopkg.in/routeros.v2/proto"
)
const (
apiPort = ":8728"
)
var (
interfaceLabelNames = []string{"name", "address", "interface"}
InterfaceProps = []string{"name", "rx-byte", "tx-byte", "rx-packet", "tx-packet", "rx-error", "tx-error", "rx-drop", "tx-drop"}
resourceLabelNames = []string{"name", "address"}
ResourceProps = []string{"free-memory", "total-memory", "cpu-load", "free-hdd-space", "total-hdd-space"}
)
type Device struct {
address string
name string
user string
password string
iDesc map[string]*prometheus.Desc // interface level descriptions for device
rDesc map[string]*prometheus.Desc // resource level descriptions for device
}
func metricStringCleanup(in string) string {
return strings.Replace(in, "-", "_", -1)
}
func (d *Device) fetchInterfaceMetrics() ([]*proto.Sentence, error) {
log.WithFields(log.Fields{
"device": d.name,
}).Debug("fetching interface metrics")
// grab a connection to the device
c, err := routeros.Dial(d.address+apiPort, d.user, d.password)
if err != nil {
log.WithFields(log.Fields{
"device": d.name,
"error": err,
}).Error("error dialing device")
return nil, err
}
defer c.Close()
reply, err := c.Run("/interface/print", "?disabled=false",
"?running=true", "=.proplist="+strings.Join(InterfaceProps, ","))
if err != nil {
log.WithFields(log.Fields{
"device": d.name,
"error": err,
}).Error("error fetching interface metrics")
return nil, err
}
return reply.Re, nil
}
func (d *Device) Update(ch chan<- prometheus.Metric) error {
stats, err := d.fetchInterfaceMetrics()
// if there is no error, deal with the response
if err == nil {
for _, re := range stats {
var intf string
for _, p := range InterfaceProps {
if p == "name" {
intf = re.Map[p]
} else {
desc, ok := d.iDesc[p]
if !ok {
desc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "interface", metricStringCleanup(p)),
fmt.Sprintf("interface property statistic %s", p),
interfaceLabelNames,
nil,
)
d.iDesc[p] = desc
}
v, err := strconv.ParseFloat(re.Map[p], 64)
if err == nil {
ch <- prometheus.MustNewConstMetric(desc, prometheus.CounterValue, v, d.name, d.address, intf)
} else {
log.WithFields(log.Fields{
"device": d.name,
"interface": intf,
"property": p,
"value": re.Map[p],
"error": err,
}).Error("error parsing interface metric value")
}
}
}
}
}
return nil
}

20
collector/helper.go Normal file
View File

@@ -0,0 +1,20 @@
package collector
import (
"strings"
"github.com/prometheus/client_golang/prometheus"
)
func metricStringCleanup(in string) string {
return strings.Replace(in, "-", "_", -1)
}
func descriptionForPropertyName(prefix, property string, labelNames []string) *prometheus.Desc {
return prometheus.NewDesc(
prometheus.BuildFQName(namespace, prefix, metricStringCleanup(property)),
property,
labelNames,
nil,
)
}

View File

@@ -0,0 +1,89 @@
package collector
import (
"strconv"
"strings"
"github.com/nshttpd/mikrotik-exporter/config"
"github.com/prometheus/client_golang/prometheus"
log "github.com/sirupsen/logrus"
"gopkg.in/routeros.v2"
"gopkg.in/routeros.v2/proto"
)
var (
interfaceLabelNames = []string{"name", "address", "interface"}
interfaceProps = []string{"name", "rx-byte", "tx-byte", "rx-packet", "tx-packet", "rx-error", "tx-error", "rx-drop", "tx-drop"}
interfaceDescriptions map[string]*prometheus.Desc
)
func init() {
interfaceDescriptions = make(map[string]*prometheus.Desc)
for _, p := range interfaceProps[1:] {
interfaceDescriptions[p] = descriptionForPropertyName("interface", p, interfaceLabelNames)
}
}
type interfaceCollector struct {
}
func (c *interfaceCollector) describe(ch chan<- *prometheus.Desc) {
for _, d := range interfaceDescriptions {
ch <- d
}
}
func (c *interfaceCollector) collect(ch chan<- prometheus.Metric, device *config.Device, client *routeros.Client) error {
stats, err := c.fetch(client, device)
if err != nil {
return err
}
for _, re := range stats {
c.collectForStat(re, device, ch)
}
return nil
}
func (c *interfaceCollector) fetch(client *routeros.Client, device *config.Device) ([]*proto.Sentence, error) {
reply, err := client.Run("/interface/print", "?disabled=false",
"?running=true", "=.proplist="+strings.Join(interfaceProps, ","))
if err != nil {
log.WithFields(log.Fields{
"device": device.Name,
"error": err,
}).Error("error fetching interface metrics")
return nil, err
}
return reply.Re, nil
}
func (c *interfaceCollector) collectForStat(re *proto.Sentence, device *config.Device, ch chan<- prometheus.Metric) {
var iface string
for _, p := range interfaceProps {
if p == "name" {
iface = re.Map[p]
} else {
c.collectMetricForProperty(p, iface, device, re, ch)
}
}
}
func (c *interfaceCollector) collectMetricForProperty(property, iface string, device *config.Device, re *proto.Sentence, ch chan<- prometheus.Metric) {
desc := interfaceDescriptions[property]
v, err := strconv.ParseFloat(re.Map[property], 64)
if err != nil {
log.WithFields(log.Fields{
"device": device.Name,
"interface": iface,
"property": property,
"value": re.Map[property],
"error": err,
}).Error("error parsing interface metric value")
return
}
ch <- prometheus.MustNewConstMetric(desc, prometheus.CounterValue, v, device.Name, device.Address, iface)
}

View File

@@ -0,0 +1,12 @@
package collector
import (
"github.com/nshttpd/mikrotik-exporter/config"
"github.com/prometheus/client_golang/prometheus"
routeros "gopkg.in/routeros.v2"
)
type metricCollector interface {
describe(ch chan<- *prometheus.Desc)
collect(ch chan<- prometheus.Metric, device *config.Device, client *routeros.Client) error
}

View File

@@ -1,86 +0,0 @@
package collector
import (
"fmt"
"net/http"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
type PromMetrics struct {
InterfaceMetrics map[string]*prometheus.CounterVec
ResourceMetrics map[string]*prometheus.GaugeVec
}
func (p *PromMetrics) makeLabels(name, address string) prometheus.Labels {
labels := make(prometheus.Labels)
labels["name"] = metricStringCleanup(name)
labels["address"] = metricStringCleanup(address)
return labels
}
func (p *PromMetrics) makeInterfaceLabels(name, address, intf string) prometheus.Labels {
l := p.makeLabels(name, address)
l["interface"] = intf
return l
}
func (p *PromMetrics) SetupPrometheus() (http.Handler, error) {
p.InterfaceMetrics = make(map[string]*prometheus.CounterVec)
p.ResourceMetrics = make(map[string]*prometheus.GaugeVec)
for _, v := range InterfaceProps {
n := metricStringCleanup(v)
c := prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: namespace,
Subsystem: "interface",
Name: n,
Help: fmt.Sprintf("Interface %s counter", v),
}, interfaceLabelNames)
if err := prometheus.Register(c); err != nil {
//l.Errorw("error creating interface counter vector",
// "property", v,
// "error", err,
//)
return nil, err
}
p.InterfaceMetrics[v] = c
}
for _, v := range ResourceProps {
n := metricStringCleanup(v)
c := prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: "resource",
Name: n,
Help: fmt.Sprintf("Resource %s counter", v),
}, resourceLabelNames)
if err := prometheus.Register(c); err != nil {
//l.Errorw("error creating resource counter vec",
// "property", v,
// "error", err,
//)
return nil, err
}
p.ResourceMetrics[v] = c
}
return promhttp.Handler(), nil
}
func (p *PromMetrics) IncrementInterface(prop, name, address, intf string, cnt float64) {
l := p.makeInterfaceLabels(name, address, intf)
p.InterfaceMetrics[prop].With(l).Add(cnt)
}
func (p *PromMetrics) UpdateResource(res, name, address string, v float64) {
l := p.makeLabels(name, address)
p.ResourceMetrics[res].With(l).Set(v)
}

View File

@@ -0,0 +1,82 @@
package collector
import (
"strconv"
"strings"
"github.com/nshttpd/mikrotik-exporter/config"
"github.com/prometheus/client_golang/prometheus"
log "github.com/sirupsen/logrus"
"gopkg.in/routeros.v2"
"gopkg.in/routeros.v2/proto"
)
var (
resourceLabelNames = []string{"name", "address"}
resourceProps = []string{"free-memory", "total-memory", "cpu-load", "free-hdd-space", "total-hdd-space"}
resourceDescriptions map[string]*prometheus.Desc
)
func init() {
resourceDescriptions = make(map[string]*prometheus.Desc)
for _, p := range resourceProps {
resourceDescriptions[p] = descriptionForPropertyName("system", p, resourceLabelNames)
}
}
type resourceCollector struct {
}
func (c *resourceCollector) describe(ch chan<- *prometheus.Desc) {
for _, d := range resourceDescriptions {
ch <- d
}
}
func (c *resourceCollector) collect(ch chan<- prometheus.Metric, device *config.Device, client *routeros.Client) error {
stats, err := c.fetch(client, device)
if err != nil {
return err
}
for _, re := range stats {
c.collectForStat(re, device, ch)
}
return nil
}
func (c *resourceCollector) fetch(client *routeros.Client, device *config.Device) ([]*proto.Sentence, error) {
reply, err := client.Run("/system/resource/print", "=.proplist="+strings.Join(resourceProps, ","))
if err != nil {
log.WithFields(log.Fields{
"device": device.Name,
"error": err,
}).Error("error fetching system resource metrics")
return nil, err
}
return reply.Re, nil
}
func (c *resourceCollector) collectForStat(re *proto.Sentence, device *config.Device, ch chan<- prometheus.Metric) {
for _, p := range resourceProps {
c.collectMetricForProperty(p, device, re, ch)
}
}
func (c *resourceCollector) collectMetricForProperty(property string, device *config.Device, re *proto.Sentence, ch chan<- prometheus.Metric) {
v, err := strconv.ParseFloat(re.Map[property], 64)
if err != nil {
log.WithFields(log.Fields{
"device": device.Name,
"property": property,
"value": re.Map[property],
"error": err,
}).Error("error parsing system resource metric value")
return
}
desc := resourceDescriptions[property]
ch <- prometheus.MustNewConstMetric(desc, prometheus.CounterValue, v, device.Name, device.Address)
}

View File

@@ -0,0 +1,110 @@
package collector
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 {
}
func (c *routesCollector) describe(ch chan<- *prometheus.Desc) {
ch <- routesTotalDesc
ch <- routesProtocolDesc
}
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")
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) 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=")
if err != nil {
log.WithFields(log.Fields{
"ip_version": ipVersion,
"device": device.Name,
"error": err,
}).Error("error fetching routes metrics")
return
}
v, err := strconv.ParseFloat(reply.Done.Map["ret"], 32)
if err != nil {
log.WithFields(log.Fields{
"ip_version": ipVersion,
"device": device.Name,
"error": err,
}).Error("error parsing routes metrics")
return
}
ch <- prometheus.MustNewConstMetric(routesTotalDesc, prometheus.GaugeValue, v, device.Name, device.Address, ipVersion)
}
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=")
if err != nil {
log.WithFields(log.Fields{
"ip_version": ipVersion,
"protocol": protocol,
"device": device.Name,
"error": err,
}).Error("error fetching routes metrics")
return
}
v, err := strconv.ParseFloat(reply.Done.Map["ret"], 32)
if err != nil {
log.WithFields(log.Fields{
"ip_version": ipVersion,
"protocol": protocol,
"device": device.Name,
"error": err,
}).Error("error parsing routes metrics")
return
}
ch <- prometheus.MustNewConstMetric(routesProtocolDesc, prometheus.GaugeValue, v, device.Name, device.Address, ipVersion, protocol)
}