Merge pull request #146 from klaper/health_collector_fix

Health collector fix for RouterOS 7
This commit is contained in:
Steve Brunton 2022-06-16 21:51:58 -04:00 committed by GitHub
commit e1b06c6ebe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 19 additions and 11 deletions

View File

@ -1,12 +1,10 @@
package collector package collector
import ( import (
"strconv"
"strings"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"gopkg.in/routeros.v2/proto" "gopkg.in/routeros.v2/proto"
"strconv"
) )
type healthCollector struct { type healthCollector struct {
@ -44,14 +42,18 @@ func (c *healthCollector) collect(ctx *collectorContext) error {
} }
for _, re := range stats { for _, re := range stats {
if metric, ok := re.Map["name"]; ok {
c.collectMetricForProperty(metric, re, ctx)
} else {
c.collectForStat(re, ctx) c.collectForStat(re, ctx)
} }
}
return nil return nil
} }
func (c *healthCollector) fetch(ctx *collectorContext) ([]*proto.Sentence, error) { func (c *healthCollector) fetch(ctx *collectorContext) ([]*proto.Sentence, error) {
reply, err := ctx.client.Run("/system/health/print", "=.proplist="+strings.Join(c.props, ",")) reply, err := ctx.client.Run("/system/health/print")
if err != nil { if err != nil {
log.WithFields(log.Fields{ log.WithFields(log.Fields{
"device": ctx.device.Name, "device": ctx.device.Name,
@ -73,21 +75,27 @@ func (c *healthCollector) collectMetricForProperty(property string, re *proto.Se
var v float64 var v float64
var err error var err error
if re.Map[property] == "" { name := property
value := re.Map[property]
if value == "" {
var ok bool
if value, ok = re.Map["value"]; !ok {
return return
} }
v, err = strconv.ParseFloat(re.Map[property], 64) }
v, err = strconv.ParseFloat(value, 64)
if err != nil { if err != nil {
log.WithFields(log.Fields{ log.WithFields(log.Fields{
"device": ctx.device.Name, "device": ctx.device.Name,
"property": property, "property": name,
"value": re.Map[property], "value": value,
"error": err, "error": err,
}).Error("error parsing system health metric value") }).Error("error parsing system health metric value")
return return
} }
desc := c.descriptions[property] desc := c.descriptions[name]
ctx.ch <- prometheus.MustNewConstMetric(desc, prometheus.GaugeValue, v, ctx.device.Name, ctx.device.Address) ctx.ch <- prometheus.MustNewConstMetric(desc, prometheus.GaugeValue, v, ctx.device.Name, ctx.device.Address)
} }