Skip to content

Commit

Permalink
fixed bgp session metrics for ros7
Browse files Browse the repository at this point in the history
  • Loading branch information
oGi4i committed Nov 16, 2022
1 parent 6c38cec commit 92ecbce
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 70 deletions.
29 changes: 27 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,33 @@ A Prometheus Exporter for Mikrotik devices. Can be configured to collect metrics
devices. Single device monitoring can be configured all on the command line. Multiple devices require a configuration
file. A user will be required that has read-only access to the device configuration via the API.

Currently the exporter collects metrics for interfaces and system resources. Others can be added as long as published
via the API.
Because of some breaking changes in the ROS API with ROS7 currently this exporter mainly aims to be compatible with
ROS7.
Some of the metrics might not work on ROS6.

Currently the exporter supports collecting these groups of metrics:

- interface
- resource
- bgp session
- dhcp lease
- dhcp ipv6 lease
- firmware
- health
- ip routes
- ethernet
- poe
- ip pool
- sfp
- wlan stations
- capsman
- wlan
- ipsec
- ospf neighbors
- lte
- netwatch
- conntrack
- bridge hosts

#### Mikrotik Config

Expand Down
43 changes: 29 additions & 14 deletions collector/bgp/bgp.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,29 @@ import (
)

var (
properties = []string{"name", "remote-as", "state", "prefix-count", "updates-sent", "updates-received", "withdrawn-sent", "withdrawn-received"}
labelNames = []string{"name", "address", "session", "asn"}
metricDescriptions = map[string]*prometheus.Desc{
"state": metrics.BuildMetricDescription(prefix, "state", "bgp session state (up = 1)", labelNames),
"prefix-count": metrics.BuildMetricDescription(prefix, "prefix_count", "number of prefixes per session", labelNames),
"updates-sent": metrics.BuildMetricDescription(prefix, "updates_sent", "number of bgp updates sent per session", labelNames),
"updates-received": metrics.BuildMetricDescription(prefix, "updates_received", "number of bgp updates received per session", labelNames),
"withdrawn-sent": metrics.BuildMetricDescription(prefix, "withdrawn_sent", "number of bgp withdrawns sent per session", labelNames),
"withdrawn-received": metrics.BuildMetricDescription(prefix, "withdrawn_received", "number of bgp withdrawns received per session", labelNames),
properties = []string{"name", "remote.as", "remote.address", "established", "remote.messages", "local.messages", "remote.bytes", "local.bytes"}
labelNames = []string{"name", "address", "session", "asn", "remote_address"}
metricDescriptions = map[string]*metrics.MetricDescription{
"established": {
Desc: metrics.BuildMetricDescription(prefix, "established", "bgp session established (up = 1)", labelNames),
ValueType: prometheus.GaugeValue,
},
"remote.messages": {
Desc: metrics.BuildMetricDescription(prefix, "remote_messages", "number of bgp messages received per session", labelNames),
ValueType: prometheus.CounterValue,
},
"local.messages": {
Desc: metrics.BuildMetricDescription(prefix, "local_messages", "number of bgp messages sent per session", labelNames),
ValueType: prometheus.CounterValue,
},
"remote.bytes": {
Desc: metrics.BuildMetricDescription(prefix, "remote_bytes", "number of bytes received per session", labelNames),
ValueType: prometheus.CounterValue,
},
"local.bytes": {
Desc: metrics.BuildMetricDescription(prefix, "local_bytes", "number of bytes sent per session", labelNames),
ValueType: prometheus.CounterValue,
},
}
)

Expand All @@ -40,7 +54,7 @@ func (c *bgpCollector) Name() string {

func (c *bgpCollector) Describe(ch chan<- *prometheus.Desc) {
for _, d := range metricDescriptions {
ch <- d
ch <- d.Desc
}
}

Expand All @@ -59,7 +73,7 @@ func (c *bgpCollector) Collect(ctx *context.Context) error {

func (c *bgpCollector) fetch(ctx *context.Context) ([]*proto.Sentence, error) {
reply, err := ctx.RouterOSClient.Run(
"/routing/bgp/peer/print",
"/routing/bgp/session/print",
"=.proplist="+strings.Join(properties, ","),
)
if err != nil {
Expand Down Expand Up @@ -88,8 +102,8 @@ func (c *bgpCollector) collectMetricForProperty(property string, re *proto.Sente
err error
)
switch property {
case "state":
if value == "established" {
case "established":
if value == "true" {
v = 1
}
default:
Expand All @@ -107,5 +121,6 @@ func (c *bgpCollector) collectMetricForProperty(property string, re *proto.Sente
return
}

ctx.MetricsChan <- prometheus.MustNewConstMetric(metricDescriptions[property], prometheus.GaugeValue, v, ctx.DeviceName, ctx.DeviceAddress, session, re.Map["remote-as"])
desc := metricDescriptions[property]
ctx.MetricsChan <- prometheus.MustNewConstMetric(desc.Desc, desc.ValueType, v, ctx.DeviceName, ctx.DeviceAddress, session, re.Map["remote.as"], re.Map["remote.address"])
}
99 changes: 45 additions & 54 deletions collector/bgp/bgp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,11 @@ func Test_bgpCollector_Describe(t *testing.T) {

<-done
r.ElementsMatch([]*prometheus.Desc{
metrics.BuildMetricDescription(prefix, "state", "bgp session state (up = 1)", labelNames),
metrics.BuildMetricDescription(prefix, "prefix_count", "number of prefixes per session", labelNames),
metrics.BuildMetricDescription(prefix, "updates_sent", "number of bgp updates sent per session", labelNames),
metrics.BuildMetricDescription(prefix, "updates_received", "number of bgp updates received per session", labelNames),
metrics.BuildMetricDescription(prefix, "withdrawn_sent", "number of bgp withdrawns sent per session", labelNames),
metrics.BuildMetricDescription(prefix, "withdrawn_received", "number of bgp withdrawns received per session", labelNames),
metrics.BuildMetricDescription(prefix, "established", "bgp session established (up = 1)", labelNames),
metrics.BuildMetricDescription(prefix, "remote_messages", "number of bgp messages received per session", labelNames),
metrics.BuildMetricDescription(prefix, "local_messages", "number of bgp messages sent per session", labelNames),
metrics.BuildMetricDescription(prefix, "remote_bytes", "number of bytes received per session", labelNames),
metrics.BuildMetricDescription(prefix, "local_bytes", "number of bytes sent per session", labelNames),
}, got)
}

Expand All @@ -72,50 +71,46 @@ func Test_bgpCollector_Collect(t *testing.T) {
setMocks: func() {
routerOSClientMock.RunMock.Inspect(func(sentence ...string) {
r.Equal([]string{
"/routing/bgp/peer/print",
"=.proplist=name,remote-as,state,prefix-count,updates-sent,updates-received,withdrawn-sent,withdrawn-received",
"/routing/bgp/session/print",
"=.proplist=name,remote.as,remote.address,established,remote.messages,local.messages,remote.bytes,local.bytes",
}, sentence)
}).Return(&routeros.Reply{
Re: []*proto.Sentence{
{
Map: map[string]string{
"name": "session",
"remote-as": "65000",
"state": "established",
"prefix-count": "111",
"updates-sent": "11",
"updates-received": "21",
"withdrawn-sent": "1",
"withdrawn-received": "2",
"name": "session",
"remote.as": "65000",
"remote.address": "1.1.1.1",
"established": "true",
"remote.messages": "111",
"local.messages": "11",
"remote.bytes": "222",
"local.bytes": "21",
},
},
},
}, nil)
},
want: []prometheus.Metric{
prometheus.MustNewConstMetric(
metrics.BuildMetricDescription(prefix, "state", "bgp session state (up = 1)", labelNames),
prometheus.GaugeValue, 1, "device", "address", "session", "65000",
metrics.BuildMetricDescription(prefix, "established", "bgp session established (up = 1)", labelNames),
prometheus.GaugeValue, 1, "device", "address", "session", "65000", "1.1.1.1",
),
prometheus.MustNewConstMetric(
metrics.BuildMetricDescription(prefix, "prefix_count", "number of prefixes per session", labelNames),
prometheus.GaugeValue, 111, "device", "address", "session", "65000",
metrics.BuildMetricDescription(prefix, "remote_messages", "number of bgp messages received per session", labelNames),
prometheus.CounterValue, 111, "device", "address", "session", "65000", "1.1.1.1",
),
prometheus.MustNewConstMetric(
metrics.BuildMetricDescription(prefix, "updates_sent", "number of bgp updates sent per session", labelNames),
prometheus.GaugeValue, 11, "device", "address", "session", "65000",
metrics.BuildMetricDescription(prefix, "local_messages", "number of bgp messages sent per session", labelNames),
prometheus.CounterValue, 11, "device", "address", "session", "65000", "1.1.1.1",
),
prometheus.MustNewConstMetric(
metrics.BuildMetricDescription(prefix, "updates_received", "number of bgp updates received per session", labelNames),
prometheus.GaugeValue, 21, "device", "address", "session", "65000",
metrics.BuildMetricDescription(prefix, "remote_bytes", "number of bytes received per session", labelNames),
prometheus.CounterValue, 222, "device", "address", "session", "65000", "1.1.1.1",
),
prometheus.MustNewConstMetric(
metrics.BuildMetricDescription(prefix, "withdrawn_sent", "number of bgp withdrawns sent per session", labelNames),
prometheus.GaugeValue, 1, "device", "address", "session", "65000",
),
prometheus.MustNewConstMetric(
metrics.BuildMetricDescription(prefix, "withdrawn_received", "number of bgp withdrawns received per session", labelNames),
prometheus.GaugeValue, 2, "device", "address", "session", "65000",
metrics.BuildMetricDescription(prefix, "local_bytes", "number of bytes sent per session", labelNames),
prometheus.CounterValue, 21, "device", "address", "session", "65000", "1.1.1.1",
),
},
},
Expand All @@ -124,8 +119,8 @@ func Test_bgpCollector_Collect(t *testing.T) {
setMocks: func() {
routerOSClientMock.RunMock.Inspect(func(sentence ...string) {
r.Equal([]string{
"/routing/bgp/peer/print",
"=.proplist=name,remote-as,state,prefix-count,updates-sent,updates-received,withdrawn-sent,withdrawn-received",
"/routing/bgp/session/print",
"=.proplist=name,remote.as,remote.address,established,remote.messages,local.messages,remote.bytes,local.bytes",
}, sentence)
}).Return(nil, errors.New("some fetch error"))
},
Expand All @@ -136,46 +131,42 @@ func Test_bgpCollector_Collect(t *testing.T) {
setMocks: func() {
routerOSClientMock.RunMock.Inspect(func(sentence ...string) {
r.Equal([]string{
"/routing/bgp/peer/print",
"=.proplist=name,remote-as,state,prefix-count,updates-sent,updates-received,withdrawn-sent,withdrawn-received",
"/routing/bgp/session/print",
"=.proplist=name,remote.as,remote.address,established,remote.messages,local.messages,remote.bytes,local.bytes",
}, sentence)
}).Return(&routeros.Reply{
Re: []*proto.Sentence{
{
Map: map[string]string{
"name": "session",
"remote-as": "65000",
"state": "other-state",
"prefix-count": "a111",
"updates-sent": "11",
"updates-received": "21",
"withdrawn-sent": "1",
"withdrawn-received": "2",
"name": "session",
"remote.as": "65000",
"remote.address": "1.1.1.1",
"established": "true",
"remote.messages": "d111",
"local.messages": "11",
"remote.bytes": "222",
"local.bytes": "21",
},
},
},
}, nil)
},
want: []prometheus.Metric{
prometheus.MustNewConstMetric(
metrics.BuildMetricDescription(prefix, "state", "bgp session state (up = 1)", labelNames),
prometheus.GaugeValue, 0, "device", "address", "session", "65000",
),
prometheus.MustNewConstMetric(
metrics.BuildMetricDescription(prefix, "updates_sent", "number of bgp updates sent per session", labelNames),
prometheus.GaugeValue, 11, "device", "address", "session", "65000",
metrics.BuildMetricDescription(prefix, "established", "bgp session established (up = 1)", labelNames),
prometheus.GaugeValue, 1, "device", "address", "session", "65000", "1.1.1.1",
),
prometheus.MustNewConstMetric(
metrics.BuildMetricDescription(prefix, "updates_received", "number of bgp updates received per session", labelNames),
prometheus.GaugeValue, 21, "device", "address", "session", "65000",
metrics.BuildMetricDescription(prefix, "local_messages", "number of bgp messages sent per session", labelNames),
prometheus.CounterValue, 11, "device", "address", "session", "65000", "1.1.1.1",
),
prometheus.MustNewConstMetric(
metrics.BuildMetricDescription(prefix, "withdrawn_sent", "number of bgp withdrawns sent per session", labelNames),
prometheus.GaugeValue, 1, "device", "address", "session", "65000",
metrics.BuildMetricDescription(prefix, "remote_bytes", "number of bytes received per session", labelNames),
prometheus.CounterValue, 222, "device", "address", "session", "65000", "1.1.1.1",
),
prometheus.MustNewConstMetric(
metrics.BuildMetricDescription(prefix, "withdrawn_received", "number of bgp withdrawns received per session", labelNames),
prometheus.GaugeValue, 2, "device", "address", "session", "65000",
metrics.BuildMetricDescription(prefix, "local_bytes", "number of bytes sent per session", labelNames),
prometheus.CounterValue, 21, "device", "address", "session", "65000", "1.1.1.1",
),
},
},
Expand Down

0 comments on commit 92ecbce

Please sign in to comment.