Skip to content

Commit

Permalink
uptime metric reporting (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
nshttpd authored Jan 2, 2019
1 parent d4288ae commit aee1517
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 10 deletions.
44 changes: 38 additions & 6 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.0.4-DEVEL
1.0.5
43 changes: 41 additions & 2 deletions collector/resource_collector.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
package collector

import (
"regexp"
"strconv"
"strings"
"time"

"github.com/prometheus/client_golang/prometheus"
log "github.com/sirupsen/logrus"
"gopkg.in/routeros.v2/proto"
)

var uptimeRegex *regexp.Regexp
var uptimeParts [5]time.Duration

func init() {
uptimeRegex = regexp.MustCompile(`(?:(\d*)w)?(?:(\d*)d)?(?:(\d*)h)?(?:(\d*)m)?(?:(\d*)s)`)
uptimeParts = [5]time.Duration{time.Hour * 168, time.Hour * 24, time.Hour, time.Minute, time.Second}
}

type resourceCollector struct {
props []string
descriptions map[string]*prometheus.Desc
Expand All @@ -21,7 +31,7 @@ func newResourceCollector() routerOSCollector {
}

func (c *resourceCollector) init() {
c.props = []string{"free-memory", "total-memory", "cpu-load", "free-hdd-space", "total-hdd-space"}
c.props = []string{"free-memory", "total-memory", "cpu-load", "free-hdd-space", "total-hdd-space", "uptime"}

labelNames := []string{"name", "address"}
c.descriptions = make(map[string]*prometheus.Desc)
Expand Down Expand Up @@ -69,7 +79,15 @@ func (c *resourceCollector) collectForStat(re *proto.Sentence, ctx *collectorCon
}

func (c *resourceCollector) collectMetricForProperty(property string, re *proto.Sentence, ctx *collectorContext) {
v, err := strconv.ParseFloat(re.Map[property], 64)
var v float64
var err error

if property == "uptime" {
v, err = parseUptime(re.Map[property])
} else {
v, err = strconv.ParseFloat(re.Map[property], 64)
}

if err != nil {
log.WithFields(log.Fields{
"device": ctx.device.Name,
Expand All @@ -83,3 +101,24 @@ func (c *resourceCollector) collectMetricForProperty(property string, re *proto.
desc := c.descriptions[property]
ctx.ch <- prometheus.MustNewConstMetric(desc, prometheus.CounterValue, v, ctx.device.Name, ctx.device.Address)
}

func parseUptime(uptime string) (float64, error) {
var u time.Duration

for i, match := range uptimeRegex.FindAllStringSubmatch(uptime, -1)[0] {
if match != "" && i != 0 {
v, err := strconv.Atoi(match)
if err != nil {
log.WithFields(log.Fields{
"uptime": uptime,
"value": match,
"error": err,
}).Error("error parsing uptime field value")
return float64(0), err
}
u += time.Duration(v) * uptimeParts[i-1]
}
}

return u.Seconds(), nil
}
27 changes: 27 additions & 0 deletions collector/resource_collector_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package collector

import (
"testing"
)

func TestParseUptime(t *testing.T) {

uptimes := []struct {
u string
v float64
}{
{"3d3h42m53s", 272573},
{"15w3d3h42m53s", 9344573},
{"42m53s", 2573},
}

for _, uptime := range uptimes {
seconds, err := parseUptime(uptime.u)
if err != nil {
t.Error(err)
}
if seconds != uptime.v {
t.Errorf("seconds : %f != v : %f\n", seconds, uptime.v)
}
}
}
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func startServer() {
</html>`))
})

log.Info("Listening on", *port)
log.Info("Listening on ", *port)
log.Fatal(http.ListenAndServe(*port, nil))
}

Expand Down

0 comments on commit aee1517

Please sign in to comment.