Skip to content

Commit

Permalink
[ci skip] monitor data for ethernet ports (#45)
Browse files Browse the repository at this point in the history
* fix import name conflict
* adding in ethernet monitor information via prometheus gauge
* set CGO_ENABLED env var on build
  • Loading branch information
nshttpd authored Aug 21, 2019
1 parent ea9cc69 commit beb09ce
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ utils:
go get github.com/tcnksm/ghr

deploy: utils
gox -os="linux freebsd netbsd" -arch="amd64 arm arm64 386" -parallel=4 -ldflags "$(LDFLAGS)" -output "dist/mikrotik-exporter_{{.OS}}_{{.Arch}}"
CGO_ENABLED=0 gox -os="linux freebsd netbsd" -arch="amd64 arm arm64 386" -parallel=4 -ldflags "$(LDFLAGS)" -output "dist/mikrotik-exporter_{{.OS}}_{{.Arch}}"
ghr -t $(GITHUB_TOKEN) -u $(CIRCLE_PROJECT_USERNAME) -r $(CIRCLE_PROJECT_REPONAME) -replace $(VERSION) dist/

dockerhub: deploy
Expand Down
7 changes: 7 additions & 0 deletions collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,13 @@ func WithWlanIF() Option {
}
}

// WithMonitor enables ethernet monitor collector metrics
func Monitor() Option {
return func(c *collector) {
c.collectors = append(c.collectors, newMonitorCollector())
}
}

// WithTimeout sets timeout for connecting to router
func WithTimeout(d time.Duration) Option {
return func(c *collector) {
Expand Down
6 changes: 3 additions & 3 deletions collector/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ func description(prefix, name, helpText string, labelNames []string) *prometheus
}

func splitStringToFloats(metric string) (float64, float64, error) {
strings := strings.Split(metric, ",")
strs := strings.Split(metric, ",")

m1, err := strconv.ParseFloat(strings[0], 64)
m1, err := strconv.ParseFloat(strs[0], 64)
if err != nil {
return math.NaN(), math.NaN(), err
}
m2, err := strconv.ParseFloat(strings[1], 64)
m2, err := strconv.ParseFloat(strs[1], 64)
if err != nil {
return math.NaN(), math.NaN(), err
}
Expand Down
123 changes: 123 additions & 0 deletions collector/monitor_collector.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package collector

import (
"strings"

"gopkg.in/routeros.v2/proto"

"github.com/prometheus/client_golang/prometheus"
log "github.com/sirupsen/logrus"
)

type monitorCollector struct {
props []string // props from monitor, can add other ether props later if needed
descriptions map[string]*prometheus.Desc
}

func newMonitorCollector() routerOSCollector {
c := &monitorCollector{}
c.init()
return c
}

func (c *monitorCollector) init() {
c.props = []string{"status", "rate", "full-duplex"}
labelNames := []string{"name", "address", "interface"}
c.descriptions = make(map[string]*prometheus.Desc)
for _, p := range c.props {
c.descriptions[p] = descriptionForPropertyName("monitor", p, labelNames)
}
}

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

func (c *monitorCollector) collect(ctx *collectorContext) error {
reply, err := ctx.client.Run("/interface/ethernet/print", "=.proplist=name")
if err != nil {
log.WithFields(log.Fields{
"device": ctx.device.Name,
"error": err,
}).Error("error fetching ethernet interfaces")
return err
}

eths := make([]string, len(reply.Re))
for idx, eth := range reply.Re {
eths[idx] = eth.Map["name"]
}

return c.collectForMonitor(eths, ctx)
}

func (c *monitorCollector) collectForMonitor(eths []string, ctx *collectorContext) error {
reply, err := ctx.client.Run("/interface/ethernet/monitor",
"=numbers="+strings.Join(eths, ","),
"=once=",
"=.proplist=name,"+strings.Join(c.props, ","))

if err != nil {
log.WithFields(log.Fields{
"device": ctx.device.Name,
"error": err,
}).Error("error fetching ethernet monitor info")
return err
}

for _, e := range reply.Re {
c.collectMetricsForEth(e.Map["name"], e, ctx)
}

return nil
}

func (c *monitorCollector) collectMetricsForEth(name string, se *proto.Sentence, ctx *collectorContext) {
for _, prop := range c.props {
v, ok := se.Map[prop]
if !ok {
continue
}

value := float64(c.valueForProp(prop, v))

ctx.ch <- prometheus.MustNewConstMetric(c.descriptions[prop], prometheus.GaugeValue, value, ctx.device.Name, ctx.device.Address, name)

}

}

func (c *monitorCollector) valueForProp(name, value string) int {
switch {
case name == "status":
return func(v string) int {
if v == "link-ok" {
return 1
}
return 0
}(value)
case name == "rate":
return func(v string) int {
switch {
case v == "10Mbps":
return 10
case v == "100Mbps":
return 100
case v == "1Gbps":
return 1000
}
return 0
}(value)
case name == "full-duplex":
return func(v string) int {
if v == "true" {
return 1
}
return 0
}(value)
default:
return 0
}
}
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type Config struct {
Optics bool `yaml:"optics,omitempty"`
WlanSTA bool `yaml:"wlansta,omitempty"`
WlanIF bool `yaml:"wlanif,omitempty"`
Monitor bool `yaml:"monitor,omitempty"`
} `yaml:"features,omitempty"`
}

Expand Down
6 changes: 6 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ var (
withOptics = flag.Bool("with-optics", false, "retrieves optical diagnostic metrics")
withWlanSTA = flag.Bool("with-wlansta", false, "retrieves connected wlan station metrics")
withWlanIF = flag.Bool("with-wlanif", false, "retrieves wlan interface metrics")
withMonitor = flag.Bool("with-monitor", false, "retrieves ethernet interface monitor info")
timeout = flag.Duration("timeout", collector.DefaultTimeout, "timeout when connecting to devices")
tls = flag.Bool("tls", false, "use tls to connect to routers")
insecure = flag.Bool("insecure", false, "skips verification of server certificate when using TLS (not recommended)")
Expand Down Expand Up @@ -190,6 +191,11 @@ func collectorOptions() []collector.Option {
opts = append(opts, collector.WithWlanIF())
}

if *withMonitor || cfg.Features.Monitor {
opts = append(opts, collector.Monitor())

}

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

0 comments on commit beb09ce

Please sign in to comment.