[GO][EDIT] detect-internet collector now sends the status as a string and returns time since last status change

This commit is contained in:
BuildTools
2020-12-02 05:06:25 +01:00
parent b55e137dba
commit ddddc2d1a5

View File

@@ -1,7 +1,10 @@
package collector package collector
import ( import (
"time"
"strings" "strings"
"gopkg.in/routeros.v2/proto"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
@@ -19,9 +22,9 @@ func newInternetCollector() routerOSCollector {
func (c *internetCollector) init() { func (c *internetCollector) init() {
// Properties other than 'name' that will be used in .proplist // Properties other than 'name' that will be used in .proplist
c.props = []string{"state"} c.props = []string{"state", "state-change-time"}
// Description for the returned values in the metric // Description for the returned values in the metric
labelNames := []string{"devicename", "interface"} labelNames := []string{"devicename", "interface", "state"}
c.descriptions = make(map[string]*prometheus.Desc) c.descriptions = make(map[string]*prometheus.Desc)
for _, p := range c.props { for _, p := range c.props {
c.descriptions[p] = descriptionForPropertyName("internet", p, labelNames) c.descriptions[p] = descriptionForPropertyName("internet", p, labelNames)
@@ -44,38 +47,34 @@ func (c *internetCollector) collect(ctx *collectorContext) error {
return err return err
} }
for _, e := range reply.Re {
for _, prop := range c.props {
v, ok := e.Map[prop]
if !ok {
continue
}
value := float64(c.valueForProp(prop, v)) for _, e := range reply.Re {
ctx.ch <- prometheus.MustNewConstMetric(c.descriptions[prop], prometheus.GaugeValue, value, ctx.device.Name, e.Map["name"]) c.collectStatusForEth(e.Map["name"], e, ctx)
}
} }
return nil return nil
} }
// Here are the corresponding values for detect-internet's output: // This function parses the status and the last change time of a specific interface
// - internet: 2 func (c *internetCollector) collectStatusForEth(name string, se *proto.Sentence, ctx *collectorContext) {
// - wan: 1 layout := "Jan/02/2006 15:04:05"
// - lan: 0 // Parse date
func (c *internetCollector) valueForProp(name, value string) int { v, ok := se.Map["state-change-time"]
switch { if !ok {
case name == "state": return
return func(v string) int {
if v == "internet" {
return 2
}
if v == "wan" {
return 1
}
return 0
}(value)
default:
return 0
} }
t, err := time.Parse(layout, v)
if err != nil {
log.WithFields(log.Fields{
"device": ctx.device.Name,
"error": err,
}).Error("error parsing detect-internet last state date")
return
}
value := time.Since(t).Seconds()
ctx.ch <- prometheus.MustNewConstMetric(c.descriptions["state-change-time"], prometheus.GaugeValue, value, ctx.device.Name, se.Map["name"], se.Map["state"])
} }