Skip to content

Commit

Permalink
Merge pull request nshttpd#92 from valer-cara/master
Browse files Browse the repository at this point in the history
Add tools/netwatch exporter
  • Loading branch information
nshttpd authored Apr 13, 2021
2 parents 8de2156 + c04b215 commit 6f53244
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 0 deletions.
7 changes: 7 additions & 0 deletions collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,13 @@ func WithLte() Option {
}
}

// WithNetwatch enables netwatch metrics
func WithNetwatch() Option {
return func(c *collector) {
c.collectors = append(c.collectors, newNetwatchCollector())
}
}

// Option applies options to collector
type Option func(*collector)

Expand Down
95 changes: 95 additions & 0 deletions collector/netwatch_collector.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package collector

import (
"fmt"
"strings"

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

type netwatchCollector struct {
props []string
descriptions map[string]*prometheus.Desc
}

func newNetwatchCollector() routerOSCollector {
c := &netwatchCollector{}
c.init()
return c
}

func (c *netwatchCollector) init() {
c.props = []string{"host", "comment", "status"}
labelNames := []string{"name", "address", "host", "comment"}
c.descriptions = make(map[string]*prometheus.Desc)
for _, p := range c.props[1:] {
c.descriptions[p] = descriptionForPropertyName("netwatch", p, labelNames)
}
}

func (c *netwatchCollector) describe(ch chan<- *prometheus.Desc) {
for _, d := range c.descriptions {
ch <- d
}
}

func (c *netwatchCollector) collect(ctx *collectorContext) error {
stats, err := c.fetch(ctx)
if err != nil {
return err
}

for _, re := range stats {
c.collectForStat(re, ctx)
}

return nil
}

func (c *netwatchCollector) fetch(ctx *collectorContext) ([]*proto.Sentence, error) {
reply, err := ctx.client.Run("/tool/netwatch/print", "?disabled=false", "=.proplist="+strings.Join(c.props, ","))
if err != nil {
log.WithFields(log.Fields{
"device": ctx.device.Name,
"error": err,
}).Error("error fetching netwatch metrics")
return nil, err
}

return reply.Re, nil
}

func (c *netwatchCollector) collectForStat(re *proto.Sentence, ctx *collectorContext) {
host := re.Map["host"]
comment := re.Map["comment"]

for _, p := range c.props[2:] {
c.collectMetricForProperty(p, host, comment, re, ctx)
}
}

func (c *netwatchCollector) collectMetricForProperty(property, host, comment string, re *proto.Sentence, ctx *collectorContext) {
desc := c.descriptions[property]
if value := re.Map[property]; value != "" {
var numericValue float64
switch value {
case "up":
numericValue = 1
case "unknown":
numericValue = 0
case "down":
numericValue = -1
default:
log.WithFields(log.Fields{
"device": ctx.device.Name,
"host": host,
"property": property,
"value": value,
"error": fmt.Errorf("unexpected netwatch status value"),
}).Error("error parsing netwatch metric value")
}
ctx.ch <- prometheus.MustNewConstMetric(desc, prometheus.CounterValue, numericValue, ctx.device.Name, ctx.device.Address, host, comment)
}
}
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type Config struct {
Monitor bool `yaml:"monitor,omitempty"`
Ipsec bool `yaml:"ipsec,omitempty"`
Lte bool `yaml:"lte,omitempty"`
Netwatch bool `yaml:"netwatch,omitempty"`
} `yaml:"features,omitempty"`
}

Expand Down
1 change: 1 addition & 0 deletions config/config.test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ features:
wlanif: true
ipsec: true
lte: true
netwatch: true
1 change: 1 addition & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func TestShouldParse(t *testing.T) {
assertFeature("WlanIF", c.Features.WlanIF, t)
assertFeature("Ipsec", c.Features.Ipsec, t)
assertFeature("Lte", c.Features.Lte, t)
assertFeature("Netwatch", c.Features.Netwatch, t)
}

func loadTestFile(t *testing.T) []byte {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/prometheus/client_golang v1.4.1
github.com/prometheus/common v0.9.1
github.com/sirupsen/logrus v1.4.2
github.com/stretchr/testify v1.4.0
golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5 // indirect
gopkg.in/routeros.v2 v2.0.0-20190905230420-1bbf141cdd91
gopkg.in/yaml.v2 v2.2.5
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
Expand Down
5 changes: 5 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ var (
withMonitor = flag.Bool("with-monitor", false, "retrieves ethernet interface monitor info")
withIpsec = flag.Bool("with-ipsec", false, "retrieves ipsec metrics")
withLte = flag.Bool("with-lte", false, "retrieves lte metrics")
withNetwatch = flag.Bool("with-netwatch", false, "retrieves netwatch metrics")

cfg *config.Config

Expand Down Expand Up @@ -258,6 +259,10 @@ func collectorOptions() []collector.Option {
opts = append(opts, collector.WithLte())
}

if *withNetwatch || cfg.Features.Netwatch {
opts = append(opts, collector.WithNetwatch())
}

if *timeout != collector.DefaultTimeout {
opts = append(opts, collector.WithTimeout(*timeout))
}
Expand Down

0 comments on commit 6f53244

Please sign in to comment.