Skip to content

Commit 9fc2841

Browse files
welbymcrobertsnshttpd
authored andcommitted
PoE collector (#54)
Thanks for the PR. * PoE collector * Correcting mistake in comment for WithPOE function
1 parent c757fc7 commit 9fc2841

File tree

4 files changed

+135
-0
lines changed

4 files changed

+135
-0
lines changed

collector/collector.go

+7
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,13 @@ func WithDHCPv6() Option {
8484
}
8585
}
8686

87+
// WithPOE enables PoE metrics
88+
func WithPOE() Option {
89+
return func(c *collector) {
90+
c.collectors = append(c.collectors, newPOECollector())
91+
}
92+
}
93+
8794
// WithPools enables IP(v6) pool metrics
8895
func WithPools() Option {
8996
return func(c *collector) {

collector/poe_collector.go

+122
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
package collector
2+
3+
import (
4+
"strconv"
5+
"strings"
6+
7+
"github.com/prometheus/client_golang/prometheus"
8+
log "github.com/sirupsen/logrus"
9+
"gopkg.in/routeros.v2/proto"
10+
)
11+
12+
type poeCollector struct {
13+
currentDesc *prometheus.Desc
14+
powerDesc *prometheus.Desc
15+
voltageDesc *prometheus.Desc
16+
props []string
17+
}
18+
19+
func newPOECollector() routerOSCollector {
20+
const prefix = "poe"
21+
22+
labelNames := []string{"name", "address", "interface"}
23+
return &poeCollector{
24+
currentDesc: description(prefix, "current", "current in mA", labelNames),
25+
powerDesc: description(prefix, "wattage", "Power in W", labelNames),
26+
voltageDesc: description(prefix, "voltage", "Voltage in V", labelNames),
27+
props: []string{"poe-out-current", "poe-out-voltage", "poe-out-power"},
28+
}
29+
}
30+
31+
func (c *poeCollector) describe(ch chan<- *prometheus.Desc) {
32+
ch <- c.currentDesc
33+
ch <- c.powerDesc
34+
ch <- c.voltageDesc
35+
}
36+
37+
func (c *poeCollector) collect(ctx *collectorContext) error {
38+
reply, err := ctx.client.Run("/interface/ethernet/poe/print", "=.proplist=name")
39+
if err != nil {
40+
log.WithFields(log.Fields{
41+
"device": ctx.device.Name,
42+
"error": err,
43+
}).Error("error fetching interface poe metrics")
44+
return err
45+
}
46+
47+
ifaces := make([]string, 0)
48+
for _, iface := range reply.Re {
49+
n := iface.Map["name"]
50+
ifaces = append(ifaces, n)
51+
}
52+
53+
if len(ifaces) == 0 {
54+
return nil
55+
}
56+
57+
return c.collectPOEMetricsForInterfaces(ifaces, ctx)
58+
}
59+
60+
func (c *poeCollector) collectPOEMetricsForInterfaces(ifaces []string, ctx *collectorContext) error {
61+
reply, err := ctx.client.Run("/interface/ethernet/poe/monitor",
62+
"=numbers="+strings.Join(ifaces, ","),
63+
"=once=",
64+
"=.proplist=name,"+strings.Join(c.props, ","))
65+
if err != nil {
66+
log.WithFields(log.Fields{
67+
"device": ctx.device.Name,
68+
"error": err,
69+
}).Error("error fetching interface poe monitor metrics")
70+
return err
71+
}
72+
73+
for _, se := range reply.Re {
74+
name, ok := se.Map["name"]
75+
if !ok {
76+
continue
77+
}
78+
79+
c.collectMetricsForInterface(name, se, ctx)
80+
}
81+
82+
return nil
83+
}
84+
85+
func (c *poeCollector) collectMetricsForInterface(name string, se *proto.Sentence, ctx *collectorContext) {
86+
for _, prop := range c.props {
87+
v, ok := se.Map[prop]
88+
if !ok {
89+
continue
90+
}
91+
92+
value, err := strconv.ParseFloat(v, 64)
93+
if err != nil {
94+
log.WithFields(log.Fields{
95+
"device": ctx.device.Name,
96+
"interface": name,
97+
"property": prop,
98+
"error": err,
99+
}).Error("error parsing interface poe monitor metric")
100+
return
101+
}
102+
103+
ctx.ch <- prometheus.MustNewConstMetric(c.descForKey(prop), prometheus.GaugeValue, value, ctx.device.Name, ctx.device.Address, name)
104+
}
105+
}
106+
107+
func (c *poeCollector) valueForKey(name, value string) (float64, error) {
108+
return strconv.ParseFloat(value, 64)
109+
}
110+
111+
func (c *poeCollector) descForKey(name string) *prometheus.Desc {
112+
switch name {
113+
case "poe-out-current":
114+
return c.currentDesc
115+
case "poe-out-voltage":
116+
return c.voltageDesc
117+
case "poe-out-power":
118+
return c.powerDesc
119+
}
120+
121+
return nil
122+
}

config/config.go

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ type Config struct {
1515
DHCP bool `yaml:"dhcp,omitempty"`
1616
DHCPv6 bool `yaml:"dhcpv6,omitempty"`
1717
Routes bool `yaml:"routes,omitempty"`
18+
POE bool `yaml:"poe,omitempty"`
1819
Pools bool `yaml:"pools,omitempty"`
1920
Optics bool `yaml:"optics,omitempty"`
2021
WlanSTA bool `yaml:"wlansta,omitempty"`

main.go

+5
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ var (
3838
withRoutes = flag.Bool("with-routes", false, "retrieves routing table information")
3939
withDHCP = flag.Bool("with-dhcp", false, "retrieves DHCP server metrics")
4040
withDHCPv6 = flag.Bool("with-dhcpv6", false, "retrieves DHCPv6 server metrics")
41+
withPOE = flag.Bool("with-poe", false, "retrieves PoE metrics")
4142
withPools = flag.Bool("with-pools", false, "retrieves IP(v6) pool metrics")
4243
withOptics = flag.Bool("with-optics", false, "retrieves optical diagnostic metrics")
4344
withWlanSTA = flag.Bool("with-wlansta", false, "retrieves connected wlan station metrics")
@@ -188,6 +189,10 @@ func collectorOptions() []collector.Option {
188189
opts = append(opts, collector.WithDHCPv6())
189190
}
190191

192+
if *withPOE || cfg.Features.POE {
193+
opts = append(opts, collector.WithPOE())
194+
}
195+
191196
if *withPools || cfg.Features.Pools {
192197
opts = append(opts, collector.WithPools())
193198
}

0 commit comments

Comments
 (0)