Skip to content

Commit f1f09b4

Browse files
bubnovdnshttpd
authored andcommitted
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
1 parent 0cf5d19 commit f1f09b4

File tree

5 files changed

+89
-8
lines changed

5 files changed

+89
-8
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ features:
5757
bgp: true
5858
dhcp: true
5959
dhcpv6: true
60+
dhcpl: true
6061
routes: true
6162
pools: true
6263
optics: true

collector/collector.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,13 @@ func WithDHCP() Option {
7070
}
7171
}
7272

73+
// WithDHCPL enables DHCP server leases
74+
func WithDHCPL() Option {
75+
return func(c *collector) {
76+
c.collectors = append(c.collectors, newDHCPLCollector())
77+
}
78+
}
79+
7380
// WithDHCPv6 enables DHCPv6 serrver metrics
7481
func WithDHCPv6() Option {
7582
return func(c *collector) {

collector/dhcp_lease_collector.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package collector
2+
3+
import (
4+
"strings"
5+
"github.com/prometheus/client_golang/prometheus"
6+
log "github.com/sirupsen/logrus"
7+
"gopkg.in/routeros.v2/proto"
8+
)
9+
10+
type dhcpLeaseCollector struct {
11+
props []string
12+
descriptions *prometheus.Desc
13+
}
14+
15+
func (c *dhcpLeaseCollector) init() {
16+
c.props = []string{"active-mac-address", "status", "expires-after", "active-address", "host-name"}
17+
18+
labelNames := []string{"name", "address", "activemacaddress", "status", "expiresafter", "activeaddress", "hostname"}
19+
c.descriptions = description("dhcp", "leases_metrics", "number of metrics", labelNames)
20+
21+
}
22+
23+
func newDHCPLCollector() routerOSCollector {
24+
c := &dhcpLeaseCollector{}
25+
c.init()
26+
return c
27+
}
28+
29+
func (c *dhcpLeaseCollector) describe(ch chan<- *prometheus.Desc) {
30+
ch <- c.descriptions
31+
}
32+
33+
func (c *dhcpLeaseCollector) collect(ctx *collectorContext) error {
34+
stats, err := c.fetch(ctx)
35+
if err != nil {
36+
return err
37+
}
38+
39+
for _, re := range stats {
40+
c.collectMetric(ctx, re)
41+
}
42+
43+
return nil
44+
}
45+
46+
func (c *dhcpLeaseCollector) fetch(ctx *collectorContext) ([]*proto.Sentence, error) {
47+
reply, err := ctx.client.Run("/ip/dhcp-server/lease/print", "=.proplist="+strings.Join(c.props, ","))
48+
if err != nil {
49+
log.WithFields(log.Fields{
50+
"device": ctx.device.Name,
51+
"error": err,
52+
}).Error("error fetching DHCP leases metrics")
53+
return nil, err
54+
}
55+
56+
return reply.Re, nil
57+
}
58+
59+
func (c *dhcpLeaseCollector) collectMetric(ctx *collectorContext, re *proto.Sentence) {
60+
v := 1.0
61+
62+
activemacaddress := re.Map["active-mac-address"]
63+
status := re.Map["status"]
64+
expiresafter := re.Map["expires-after"]
65+
activeaddress := re.Map["active-address"]
66+
hostname := re.Map["host-name"]
67+
68+
ctx.ch <- prometheus.MustNewConstMetric(c.descriptions, prometheus.CounterValue, v, ctx.device.Name, ctx.device.Address, activemacaddress, status, expiresafter, activeaddress, hostname)
69+
}

collector/resource_collector.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ func newResourceCollector() routerOSCollector {
3232
}
3333

3434
func (c *resourceCollector) init() {
35-
c.props = []string{"free-memory", "total-memory", "cpu-load", "free-hdd-space", "total-hdd-space", "uptime"}
35+
c.props = []string{"free-memory", "total-memory", "cpu-load", "free-hdd-space", "total-hdd-space", "uptime", "board-name", "version"}
3636

37-
labelNames := []string{"name", "address"}
37+
labelNames := []string{"name", "address", "boardname", "version"}
3838
c.descriptions = make(map[string]*prometheus.Desc)
3939
for _, p := range c.props {
4040
c.descriptions[p] = descriptionForPropertyName("system", p, labelNames)
@@ -54,7 +54,7 @@ func (c *resourceCollector) collect(ctx *collectorContext) error {
5454
}
5555

5656
for _, re := range stats {
57-
c.collectForStat(re, ctx)
57+
c.collectForStat(re, ctx)
5858
}
5959

6060
return nil
@@ -74,14 +74,19 @@ func (c *resourceCollector) fetch(ctx *collectorContext) ([]*proto.Sentence, err
7474
}
7575

7676
func (c *resourceCollector) collectForStat(re *proto.Sentence, ctx *collectorContext) {
77-
for _, p := range c.props {
77+
for _, p := range c.props[:6] {
7878
c.collectMetricForProperty(p, re, ctx)
7979
}
8080
}
8181

8282
func (c *resourceCollector) collectMetricForProperty(property string, re *proto.Sentence, ctx *collectorContext) {
8383
var v float64
8484
var err error
85+
// const boardname = "BOARD"
86+
// const version = "3.33.3"
87+
88+
boardname := re.Map["board-name"]
89+
version := re.Map["version"]
8590

8691
if property == "uptime" {
8792
v, err = parseUptime(re.Map[property])
@@ -100,7 +105,7 @@ func (c *resourceCollector) collectMetricForProperty(property string, re *proto.
100105
}
101106

102107
desc := c.descriptions[property]
103-
ctx.ch <- prometheus.MustNewConstMetric(desc, prometheus.CounterValue, v, ctx.device.Name, ctx.device.Address)
108+
ctx.ch <- prometheus.MustNewConstMetric(desc, prometheus.CounterValue, v, ctx.device.Name, ctx.device.Address, boardname, version)
104109
}
105110

106111
func parseUptime(uptime string) (float64, error) {

config/test/config.test.yml renamed to config/config.test.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
---
2-
31
devices:
42
- name: test1
53
address: 192.168.1.1
@@ -14,8 +12,9 @@ features:
1412
bgp: true
1513
dhcp: true
1614
dhcpv6: true
15+
dhcpl: true
1716
routes: true
1817
pools: true
1918
optics: true
2019
wlansta: true
21-
wlanif: true
20+
wlanif: true

0 commit comments

Comments
 (0)