DHCP Lease Collector (#50)
* DHCP Leases Collector Add Information about DHCP Leases: * Active MAC Address * Active Address * Hostname * Status * Expire time * Modified resource collector Add Boardname and RouterOS version metrics
This commit is contained in:
69
collector/dhcp_lease_collector.go
Normal file
69
collector/dhcp_lease_collector.go
Normal file
@@ -0,0 +1,69 @@
|
||||
package collector
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"gopkg.in/routeros.v2/proto"
|
||||
)
|
||||
|
||||
type dhcpLeaseCollector struct {
|
||||
props []string
|
||||
descriptions *prometheus.Desc
|
||||
}
|
||||
|
||||
func (c *dhcpLeaseCollector) init() {
|
||||
c.props = []string{"active-mac-address", "status", "expires-after", "active-address", "host-name"}
|
||||
|
||||
labelNames := []string{"name", "address", "activemacaddress", "status", "expiresafter", "activeaddress", "hostname"}
|
||||
c.descriptions = description("dhcp", "leases_metrics", "number of metrics", labelNames)
|
||||
|
||||
}
|
||||
|
||||
func newDHCPLCollector() routerOSCollector {
|
||||
c := &dhcpLeaseCollector{}
|
||||
c.init()
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *dhcpLeaseCollector) describe(ch chan<- *prometheus.Desc) {
|
||||
ch <- c.descriptions
|
||||
}
|
||||
|
||||
func (c *dhcpLeaseCollector) collect(ctx *collectorContext) error {
|
||||
stats, err := c.fetch(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, re := range stats {
|
||||
c.collectMetric(ctx, re)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *dhcpLeaseCollector) fetch(ctx *collectorContext) ([]*proto.Sentence, error) {
|
||||
reply, err := ctx.client.Run("/ip/dhcp-server/lease/print", "=.proplist="+strings.Join(c.props, ","))
|
||||
if err != nil {
|
||||
log.WithFields(log.Fields{
|
||||
"device": ctx.device.Name,
|
||||
"error": err,
|
||||
}).Error("error fetching DHCP leases metrics")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return reply.Re, nil
|
||||
}
|
||||
|
||||
func (c *dhcpLeaseCollector) collectMetric(ctx *collectorContext, re *proto.Sentence) {
|
||||
v := 1.0
|
||||
|
||||
activemacaddress := re.Map["active-mac-address"]
|
||||
status := re.Map["status"]
|
||||
expiresafter := re.Map["expires-after"]
|
||||
activeaddress := re.Map["active-address"]
|
||||
hostname := re.Map["host-name"]
|
||||
|
||||
ctx.ch <- prometheus.MustNewConstMetric(c.descriptions, prometheus.CounterValue, v, ctx.device.Name, ctx.device.Address, activemacaddress, status, expiresafter, activeaddress, hostname)
|
||||
}
|
||||
Reference in New Issue
Block a user