|
| 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 | +} |
0 commit comments