Skip to content

Commit

Permalink
PoE collector (#54)
Browse files Browse the repository at this point in the history
Thanks for the PR.

* PoE collector
* Correcting mistake in comment for WithPOE function
  • Loading branch information
welbymcroberts authored and nshttpd committed Oct 25, 2019
1 parent c757fc7 commit 9fc2841
Show file tree
Hide file tree
Showing 4 changed files with 135 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 @@ -84,6 +84,13 @@ func WithDHCPv6() Option {
}
}

// WithPOE enables PoE metrics
func WithPOE() Option {
return func(c *collector) {
c.collectors = append(c.collectors, newPOECollector())
}
}

// WithPools enables IP(v6) pool metrics
func WithPools() Option {
return func(c *collector) {
Expand Down
122 changes: 122 additions & 0 deletions collector/poe_collector.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package collector

import (
"strconv"
"strings"

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

type poeCollector struct {
currentDesc *prometheus.Desc
powerDesc *prometheus.Desc
voltageDesc *prometheus.Desc
props []string
}

func newPOECollector() routerOSCollector {
const prefix = "poe"

labelNames := []string{"name", "address", "interface"}
return &poeCollector{
currentDesc: description(prefix, "current", "current in mA", labelNames),
powerDesc: description(prefix, "wattage", "Power in W", labelNames),
voltageDesc: description(prefix, "voltage", "Voltage in V", labelNames),
props: []string{"poe-out-current", "poe-out-voltage", "poe-out-power"},
}
}

func (c *poeCollector) describe(ch chan<- *prometheus.Desc) {
ch <- c.currentDesc
ch <- c.powerDesc
ch <- c.voltageDesc
}

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

ifaces := make([]string, 0)
for _, iface := range reply.Re {
n := iface.Map["name"]
ifaces = append(ifaces, n)
}

if len(ifaces) == 0 {
return nil
}

return c.collectPOEMetricsForInterfaces(ifaces, ctx)
}

func (c *poeCollector) collectPOEMetricsForInterfaces(ifaces []string, ctx *collectorContext) error {
reply, err := ctx.client.Run("/interface/ethernet/poe/monitor",
"=numbers="+strings.Join(ifaces, ","),
"=once=",
"=.proplist=name,"+strings.Join(c.props, ","))
if err != nil {
log.WithFields(log.Fields{
"device": ctx.device.Name,
"error": err,
}).Error("error fetching interface poe monitor metrics")
return err
}

for _, se := range reply.Re {
name, ok := se.Map["name"]
if !ok {
continue
}

c.collectMetricsForInterface(name, se, ctx)
}

return nil
}

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

value, err := strconv.ParseFloat(v, 64)
if err != nil {
log.WithFields(log.Fields{
"device": ctx.device.Name,
"interface": name,
"property": prop,
"error": err,
}).Error("error parsing interface poe monitor metric")
return
}

ctx.ch <- prometheus.MustNewConstMetric(c.descForKey(prop), prometheus.GaugeValue, value, ctx.device.Name, ctx.device.Address, name)
}
}

func (c *poeCollector) valueForKey(name, value string) (float64, error) {
return strconv.ParseFloat(value, 64)
}

func (c *poeCollector) descForKey(name string) *prometheus.Desc {
switch name {
case "poe-out-current":
return c.currentDesc
case "poe-out-voltage":
return c.voltageDesc
case "poe-out-power":
return c.powerDesc
}

return nil
}
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type Config struct {
DHCP bool `yaml:"dhcp,omitempty"`
DHCPv6 bool `yaml:"dhcpv6,omitempty"`
Routes bool `yaml:"routes,omitempty"`
POE bool `yaml:"poe,omitempty"`
Pools bool `yaml:"pools,omitempty"`
Optics bool `yaml:"optics,omitempty"`
WlanSTA bool `yaml:"wlansta,omitempty"`
Expand Down
5 changes: 5 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ var (
withRoutes = flag.Bool("with-routes", false, "retrieves routing table information")
withDHCP = flag.Bool("with-dhcp", false, "retrieves DHCP server metrics")
withDHCPv6 = flag.Bool("with-dhcpv6", false, "retrieves DHCPv6 server metrics")
withPOE = flag.Bool("with-poe", false, "retrieves PoE metrics")
withPools = flag.Bool("with-pools", false, "retrieves IP(v6) pool metrics")
withOptics = flag.Bool("with-optics", false, "retrieves optical diagnostic metrics")
withWlanSTA = flag.Bool("with-wlansta", false, "retrieves connected wlan station metrics")
Expand Down Expand Up @@ -188,6 +189,10 @@ func collectorOptions() []collector.Option {
opts = append(opts, collector.WithDHCPv6())
}

if *withPOE || cfg.Features.POE {
opts = append(opts, collector.WithPOE())
}

if *withPools || cfg.Features.Pools {
opts = append(opts, collector.WithPools())
}
Expand Down

0 comments on commit 9fc2841

Please sign in to comment.