2018-03-21 02:28:10 +01:00
|
|
|
package collector
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"gopkg.in/routeros.v2/proto"
|
|
|
|
)
|
|
|
|
|
2018-04-11 15:21:38 +02:00
|
|
|
type interfaceCollector struct {
|
|
|
|
props []string
|
|
|
|
descriptions map[string]*prometheus.Desc
|
|
|
|
}
|
2018-03-21 02:28:10 +01:00
|
|
|
|
2018-04-11 15:21:38 +02:00
|
|
|
func newInterfaceCollector() routerOSCollector {
|
|
|
|
c := &interfaceCollector{}
|
|
|
|
c.init()
|
|
|
|
return c
|
2018-03-21 02:28:10 +01:00
|
|
|
}
|
|
|
|
|
2018-04-11 15:21:38 +02:00
|
|
|
func (c *interfaceCollector) init() {
|
2021-07-07 10:53:35 +02:00
|
|
|
c.props = []string{"name", "type", "disabled", "comment", "slave", "actual-mtu", "running", "rx-byte", "tx-byte", "rx-packet", "tx-packet", "rx-error", "tx-error", "rx-drop", "tx-drop", "link-downs"}
|
2020-12-12 21:49:31 +01:00
|
|
|
labelNames := []string{"name", "address", "interface", "type", "disabled", "comment", "running", "slave"}
|
2018-04-11 15:21:38 +02:00
|
|
|
c.descriptions = make(map[string]*prometheus.Desc)
|
2020-12-12 21:49:31 +01:00
|
|
|
for _, p := range c.props[5:] {
|
2018-04-11 15:21:38 +02:00
|
|
|
c.descriptions[p] = descriptionForPropertyName("interface", p, labelNames)
|
|
|
|
}
|
2018-03-21 02:28:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *interfaceCollector) describe(ch chan<- *prometheus.Desc) {
|
2018-04-11 15:21:38 +02:00
|
|
|
for _, d := range c.descriptions {
|
2018-03-21 02:28:10 +01:00
|
|
|
ch <- d
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-11 15:21:38 +02:00
|
|
|
func (c *interfaceCollector) collect(ctx *collectorContext) error {
|
|
|
|
stats, err := c.fetch(ctx)
|
2018-03-21 02:28:10 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, re := range stats {
|
2018-04-11 15:21:38 +02:00
|
|
|
c.collectForStat(re, ctx)
|
2018-03-21 02:28:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-04-11 15:21:38 +02:00
|
|
|
func (c *interfaceCollector) fetch(ctx *collectorContext) ([]*proto.Sentence, error) {
|
2020-12-12 19:27:43 +01:00
|
|
|
reply, err := ctx.client.Run("/interface/print", "=.proplist="+strings.Join(c.props, ","))
|
2018-03-21 02:28:10 +01:00
|
|
|
if err != nil {
|
|
|
|
log.WithFields(log.Fields{
|
2018-04-11 15:21:38 +02:00
|
|
|
"device": ctx.device.Name,
|
2018-03-21 02:28:10 +01:00
|
|
|
"error": err,
|
|
|
|
}).Error("error fetching interface metrics")
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return reply.Re, nil
|
|
|
|
}
|
|
|
|
|
2018-04-11 15:21:38 +02:00
|
|
|
func (c *interfaceCollector) collectForStat(re *proto.Sentence, ctx *collectorContext) {
|
2020-12-12 21:49:31 +01:00
|
|
|
for _, p := range c.props[5:] {
|
2020-12-12 19:27:43 +01:00
|
|
|
c.collectMetricForProperty(p, re, ctx)
|
2018-03-21 02:28:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-12 19:27:43 +01:00
|
|
|
func (c *interfaceCollector) collectMetricForProperty(property string, re *proto.Sentence, ctx *collectorContext) {
|
2018-04-11 15:21:38 +02:00
|
|
|
desc := c.descriptions[property]
|
2018-11-01 03:40:11 +01:00
|
|
|
if value := re.Map[property]; value != "" {
|
2020-12-12 21:49:31 +01:00
|
|
|
var (
|
2021-10-25 14:10:09 +02:00
|
|
|
v float64
|
|
|
|
vtype prometheus.ValueType
|
|
|
|
err error
|
2020-12-12 21:49:31 +01:00
|
|
|
)
|
2021-10-25 14:10:09 +02:00
|
|
|
vtype = prometheus.CounterValue
|
|
|
|
|
2020-12-12 19:34:12 +01:00
|
|
|
switch property {
|
|
|
|
case "running":
|
2021-10-25 14:10:09 +02:00
|
|
|
vtype = prometheus.GaugeValue
|
2020-12-12 19:34:12 +01:00
|
|
|
if value == "true" {
|
|
|
|
v = 1
|
|
|
|
} else {
|
|
|
|
v = 0
|
|
|
|
}
|
2021-10-25 14:10:09 +02:00
|
|
|
case "actual-mtu":
|
|
|
|
vtype = prometheus.GaugeValue
|
|
|
|
fallthrough
|
2020-12-12 19:34:12 +01:00
|
|
|
default:
|
|
|
|
v, err = strconv.ParseFloat(value, 64)
|
|
|
|
if err != nil {
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"device": ctx.device.Name,
|
|
|
|
"interface": re.Map["name"],
|
|
|
|
"property": property,
|
|
|
|
"value": value,
|
|
|
|
"error": err,
|
|
|
|
}).Error("error parsing interface metric value")
|
|
|
|
return
|
|
|
|
}
|
2018-11-01 03:40:11 +01:00
|
|
|
}
|
2021-10-25 14:10:09 +02:00
|
|
|
ctx.ch <- prometheus.MustNewConstMetric(desc, vtype, v, ctx.device.Name, ctx.device.Address,
|
2020-12-12 21:49:31 +01:00
|
|
|
re.Map["name"], re.Map["type"], re.Map["disabled"], re.Map["comment"], re.Map["running"], re.Map["slave"])
|
2021-10-25 14:10:09 +02:00
|
|
|
|
2018-03-21 02:28:10 +01:00
|
|
|
}
|
|
|
|
}
|