From 92ecbce485810dc1452cf411a8bbbeeca96792b5 Mon Sep 17 00:00:00 2001 From: oGi4i Date: Thu, 17 Nov 2022 01:41:58 +0400 Subject: [PATCH] fixed bgp session metrics for ros7 --- README.md | 29 +++++++++++- collector/bgp/bgp.go | 43 +++++++++++------ collector/bgp/bgp_test.go | 99 ++++++++++++++++++--------------------- 3 files changed, 101 insertions(+), 70 deletions(-) diff --git a/README.md b/README.md index b646f5a2..5c1bff4f 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/collector/bgp/bgp.go b/collector/bgp/bgp.go index 313f372e..de1fc604 100644 --- a/collector/bgp/bgp.go +++ b/collector/bgp/bgp.go @@ -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, + }, } ) @@ -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 } } @@ -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 { @@ -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: @@ -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"]) } diff --git a/collector/bgp/bgp_test.go b/collector/bgp/bgp_test.go index 10053bd9..9645f5b6 100644 --- a/collector/bgp/bgp_test.go +++ b/collector/bgp/bgp_test.go @@ -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) } @@ -72,21 +71,21 @@ 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", }, }, }, @@ -94,28 +93,24 @@ func Test_bgpCollector_Collect(t *testing.T) { }, 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", ), }, }, @@ -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")) }, @@ -136,21 +131,21 @@ 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", }, }, }, @@ -158,24 +153,20 @@ func Test_bgpCollector_Collect(t *testing.T) { }, 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", ), }, },