mikrotik-exporter/collector/dhcpv6_collector.go
Daniel Czerwonk d170b0a4d2 More features (#9)
* added config file implementation, refactoring
* add gitignore
* improved test
* preperations for more metrics
* added resource metrics
* added first bgp metrics
* added asn as label for bgp metrics
* added prefix and message counts to bgp metrics
* simplified
* Update README.md
* added yaml dependency
* fixed go routine call
* added timeout
* clean up
* added TLS support
* set default api port for TLS
* added routes metric
* added missing log information
* added type collectorContext to reduce the count of parameters for better readability
* added DHCP and DHCPv6 metrics
* filter for active dhcp leases only
* added pool metrics
* enable/disable feature in config file
* refactoring

* clean up

* comment fix
2018-04-11 09:21:38 -04:00

90 lines
2.1 KiB
Go

package collector
import (
"fmt"
"strconv"
"github.com/prometheus/client_golang/prometheus"
log "github.com/sirupsen/logrus"
)
type dhcpv6Collector struct {
bindingCountDesc *prometheus.Desc
}
func newDHCPv6Collector() routerOSCollector {
c := &dhcpv6Collector{}
c.init()
return c
}
func (c *dhcpv6Collector) init() {
const prefix = "dhcpv6"
labelNames := []string{"name", "address", "server"}
c.bindingCountDesc = description(prefix, "binding_count", "number of active bindings per DHCPv6 server", labelNames)
}
func (c *dhcpv6Collector) describe(ch chan<- *prometheus.Desc) {
ch <- c.bindingCountDesc
}
func (c *dhcpv6Collector) collect(ctx *collectorContext) error {
names, err := c.fetchDHCPServerNames(ctx)
if err != nil {
return err
}
for _, n := range names {
err := c.colllectForDHCPServer(ctx, n)
if err != nil {
return err
}
}
return nil
}
func (c *dhcpv6Collector) fetchDHCPServerNames(ctx *collectorContext) ([]string, error) {
reply, err := ctx.client.Run("/ipv6/dhcp-server/print", "=.proplist=name")
if err != nil {
log.WithFields(log.Fields{
"device": ctx.device.Name,
"error": err,
}).Error("error fetching DHCPv6 server names")
return nil, err
}
names := []string{}
for _, re := range reply.Re {
names = append(names, re.Map["name"])
}
return names, nil
}
func (c *dhcpv6Collector) colllectForDHCPServer(ctx *collectorContext, dhcpServer string) error {
reply, err := ctx.client.Run("/ipv6/dhcp-server/binding/print", fmt.Sprintf("?server=%s", dhcpServer), "=count-only=")
if err != nil {
log.WithFields(log.Fields{
"dhcpv6_server": dhcpServer,
"device": ctx.device.Name,
"error": err,
}).Error("error fetching DHCPv6 binding counts")
return err
}
v, err := strconv.ParseFloat(reply.Done.Map["ret"], 32)
if err != nil {
log.WithFields(log.Fields{
"dhcpv6_server": dhcpServer,
"device": ctx.device.Name,
"error": err,
}).Error("error parsing DHCPv6 binding counts")
return err
}
ctx.ch <- prometheus.MustNewConstMetric(c.bindingCountDesc, prometheus.GaugeValue, v, ctx.device.Name, ctx.device.Address, dhcpServer)
return nil
}