Skip to content

Commit e1b06c6

Browse files
authored
Merge pull request #146 from klaper/health_collector_fix
Health collector fix for RouterOS 7
2 parents 8588952 + 30290e1 commit e1b06c6

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

collector/health_collector.go

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
package collector
22

33
import (
4-
"strconv"
5-
"strings"
6-
74
"github.com/prometheus/client_golang/prometheus"
85
log "github.com/sirupsen/logrus"
96
"gopkg.in/routeros.v2/proto"
7+
"strconv"
108
)
119

1210
type healthCollector struct {
@@ -44,14 +42,18 @@ func (c *healthCollector) collect(ctx *collectorContext) error {
4442
}
4543

4644
for _, re := range stats {
47-
c.collectForStat(re, ctx)
45+
if metric, ok := re.Map["name"]; ok {
46+
c.collectMetricForProperty(metric, re, ctx)
47+
} else {
48+
c.collectForStat(re, ctx)
49+
}
4850
}
4951

5052
return nil
5153
}
5254

5355
func (c *healthCollector) fetch(ctx *collectorContext) ([]*proto.Sentence, error) {
54-
reply, err := ctx.client.Run("/system/health/print", "=.proplist="+strings.Join(c.props, ","))
56+
reply, err := ctx.client.Run("/system/health/print")
5557
if err != nil {
5658
log.WithFields(log.Fields{
5759
"device": ctx.device.Name,
@@ -73,21 +75,27 @@ func (c *healthCollector) collectMetricForProperty(property string, re *proto.Se
7375
var v float64
7476
var err error
7577

76-
if re.Map[property] == "" {
77-
return
78+
name := property
79+
value := re.Map[property]
80+
81+
if value == "" {
82+
var ok bool
83+
if value, ok = re.Map["value"]; !ok {
84+
return
85+
}
7886
}
79-
v, err = strconv.ParseFloat(re.Map[property], 64)
87+
v, err = strconv.ParseFloat(value, 64)
8088

8189
if err != nil {
8290
log.WithFields(log.Fields{
8391
"device": ctx.device.Name,
84-
"property": property,
85-
"value": re.Map[property],
92+
"property": name,
93+
"value": value,
8694
"error": err,
8795
}).Error("error parsing system health metric value")
8896
return
8997
}
9098

91-
desc := c.descriptions[property]
99+
desc := c.descriptions[name]
92100
ctx.ch <- prometheus.MustNewConstMetric(desc, prometheus.GaugeValue, v, ctx.device.Name, ctx.device.Address)
93101
}

0 commit comments

Comments
 (0)