* 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
92 lines
2.3 KiB
Go
92 lines
2.3 KiB
Go
package collector
|
|
|
|
import (
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
log "github.com/sirupsen/logrus"
|
|
"gopkg.in/routeros.v2/proto"
|
|
)
|
|
|
|
type interfaceCollector struct {
|
|
props []string
|
|
descriptions map[string]*prometheus.Desc
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|
|
|
|
func (c *interfaceCollector) describe(ch chan<- *prometheus.Desc) {
|
|
for _, d := range c.descriptions {
|
|
ch <- d
|
|
}
|
|
}
|
|
|
|
func (c *interfaceCollector) collect(ctx *collectorContext) error {
|
|
stats, err := c.fetch(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
for _, re := range stats {
|
|
c.collectForStat(re, ctx)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
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": ctx.device.Name,
|
|
"error": err,
|
|
}).Error("error fetching interface metrics")
|
|
return nil, err
|
|
}
|
|
|
|
return reply.Re, nil
|
|
}
|
|
|
|
func (c *interfaceCollector) collectForStat(re *proto.Sentence, ctx *collectorContext) {
|
|
var iface string
|
|
for _, p := range c.props {
|
|
if p == "name" {
|
|
iface = re.Map[p]
|
|
} else {
|
|
c.collectMetricForProperty(p, iface, re, ctx)
|
|
}
|
|
}
|
|
}
|
|
|
|
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": ctx.device.Name,
|
|
"interface": iface,
|
|
"property": property,
|
|
"value": re.Map[property],
|
|
"error": err,
|
|
}).Error("error parsing interface metric value")
|
|
return
|
|
}
|
|
|
|
ctx.ch <- prometheus.MustNewConstMetric(desc, prometheus.CounterValue, v, ctx.device.Name, ctx.device.Address, iface)
|
|
}
|