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,54 +4,56 @@ 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
)
type interfaceCollector struct {
props []string
descriptions map[string]*prometheus.Desc
}
func init() {
interfaceDescriptions = make(map[string]*prometheus.Desc)
for _, p := range interfaceProps[1:] {
interfaceDescriptions[p] = descriptionForPropertyName("interface", p, interfaceLabelNames)
func newInterfaceCollector() routerOSCollector {
c := &interfaceCollector{}
c.init()
return c
}
func (c *interfaceCollector) init() {
c.props = []string{"name", "rx-byte", "tx-byte", "rx-packet", "tx-packet", "rx-error", "tx-error", "rx-drop", "tx-drop"}
labelNames := []string{"name", "address", "interface"}
c.descriptions = make(map[string]*prometheus.Desc)
for _, p := range c.props[1:] {
c.descriptions[p] = descriptionForPropertyName("interface", p, labelNames)
}
}
type interfaceCollector struct {
}
func (c *interfaceCollector) describe(ch chan<- *prometheus.Desc) {
for _, d := range interfaceDescriptions {
for _, d := range c.descriptions {
ch <- d
}
}
func (c *interfaceCollector) collect(ch chan<- prometheus.Metric, device *config.Device, client *routeros.Client) error {
stats, err := c.fetch(client, device)
func (c *interfaceCollector) collect(ctx *collectorContext) error {
stats, err := c.fetch(ctx)
if err != nil {
return err
}
for _, re := range stats {
c.collectForStat(re, device, ch)
c.collectForStat(re, ctx)
}
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, ","))
func (c *interfaceCollector) fetch(ctx *collectorContext) ([]*proto.Sentence, error) {
reply, err := ctx.client.Run("/interface/print", "?disabled=false", "?running=true", "=.proplist="+strings.Join(c.props, ","))
if err != nil {
log.WithFields(log.Fields{
"device": device.Name,
"device": ctx.device.Name,
"error": err,
}).Error("error fetching interface metrics")
return nil, err
@@ -60,23 +62,23 @@ func (c *interfaceCollector) fetch(client *routeros.Client, device *config.Devic
return reply.Re, nil
}
func (c *interfaceCollector) collectForStat(re *proto.Sentence, device *config.Device, ch chan<- prometheus.Metric) {
func (c *interfaceCollector) collectForStat(re *proto.Sentence, ctx *collectorContext) {
var iface string
for _, p := range interfaceProps {
for _, p := range c.props {
if p == "name" {
iface = re.Map[p]
} else {
c.collectMetricForProperty(p, iface, device, re, ch)
c.collectMetricForProperty(p, iface, re, ctx)
}
}
}
func (c *interfaceCollector) collectMetricForProperty(property, iface string, device *config.Device, re *proto.Sentence, ch chan<- prometheus.Metric) {
desc := interfaceDescriptions[property]
func (c *interfaceCollector) collectMetricForProperty(property, iface string, re *proto.Sentence, ctx *collectorContext) {
desc := c.descriptions[property]
v, err := strconv.ParseFloat(re.Map[property], 64)
if err != nil {
log.WithFields(log.Fields{
"device": device.Name,
"device": ctx.device.Name,
"interface": iface,
"property": property,
"value": re.Map[property],
@@ -85,5 +87,5 @@ func (c *interfaceCollector) collectMetricForProperty(property, iface string, de
return
}
ch <- prometheus.MustNewConstMetric(desc, prometheus.CounterValue, v, device.Name, device.Address, iface)
ctx.ch <- prometheus.MustNewConstMetric(desc, prometheus.CounterValue, v, ctx.device.Name, ctx.device.Address, iface)
}