From 0b573da31bf4eb4b05c9bc38ba77d1300df5cda4 Mon Sep 17 00:00:00 2001 From: kakkotetsu Date: Wed, 6 Oct 2021 17:02:07 +0900 Subject: [PATCH] add metrics for MAC address table --- collectors.go | 2 ++ config/config.go | 2 ++ mac/collector.go | 62 ++++++++++++++++++++++++++++++++++++++++++++++++ mac/rpc.go | 21 ++++++++++++++++ main.go | 2 ++ 5 files changed, 89 insertions(+) create mode 100644 mac/collector.go create mode 100644 mac/rpc.go diff --git a/collectors.go b/collectors.go index 446743ee..7978e9f7 100644 --- a/collectors.go +++ b/collectors.go @@ -18,6 +18,7 @@ import ( "github.com/czerwonk/junos_exporter/isis" "github.com/czerwonk/junos_exporter/l2circuit" "github.com/czerwonk/junos_exporter/ldp" + "github.com/czerwonk/junos_exporter/mac" "github.com/czerwonk/junos_exporter/nat" "github.com/czerwonk/junos_exporter/ospf" "github.com/czerwonk/junos_exporter/power" @@ -92,6 +93,7 @@ func (c *collectors) initCollectorsForDevices(device *connector.Device) { c.addCollectorIfEnabledForDevice(device, "storage", f.Storage, storage.NewCollector) c.addCollectorIfEnabledForDevice(device, "system", f.System, system.NewCollector) c.addCollectorIfEnabledForDevice(device, "power", f.Power, power.NewCollector) + c.addCollectorIfEnabledForDevice(device, "mac", f.MAC, mac.NewCollector) } func (c *collectors) addCollectorIfEnabledForDevice(device *connector.Device, key string, enabled bool, newCollector func() collector.RPCCollector) { diff --git a/config/config.go b/config/config.go index 7bab132e..ad6db3a1 100644 --- a/config/config.go +++ b/config/config.go @@ -50,6 +50,7 @@ type FeatureConfig struct { Satellite bool `yaml:"satellite,omitempty"` System bool `yaml:"system,omitempty"` Power bool `yaml:"power,omitempty"` + MAC bool `yaml:"mac,omitempty"` } // New creates a new config @@ -103,6 +104,7 @@ func setDefaultValues(c *Config) { f.RPM = false f.Satellite = false f.Power = false + f.MAC = false } // FeaturesForDevice gets the feature set configured for a device diff --git a/mac/collector.go b/mac/collector.go new file mode 100644 index 00000000..64d25ea7 --- /dev/null +++ b/mac/collector.go @@ -0,0 +1,62 @@ +package mac + +import ( + "github.com/czerwonk/junos_exporter/collector" + "github.com/czerwonk/junos_exporter/rpc" + "github.com/prometheus/client_golang/prometheus" +) + +const prefix string = "junos_mac_table_" + +var ( + totalCount *prometheus.Desc + recieveCount *prometheus.Desc + dynamicCount *prometheus.Desc + floodCount *prometheus.Desc +) + +func init() { + l := []string{"target"} + totalCount = prometheus.NewDesc(prefix+"total_count", "Number of entries in table", l, nil) + recieveCount = prometheus.NewDesc(prefix+"recieve_count", "Number of L3 recieve route entries in table", l, nil) + dynamicCount = prometheus.NewDesc(prefix+"dynamic_count", "Number of dynamic entries in table", l, nil) + floodCount = prometheus.NewDesc(prefix+"flood_count", "Number of flood entries in table", l, nil) +} + +type macCollector struct { +} + +// Name returns the name of the collector +func (*macCollector) Name() string { + return "Mac" +} + +// NewCollector creates a new collector +func NewCollector() collector.RPCCollector { + return &macCollector{} +} + +// Describe describes the metrics +func (*macCollector) Describe(ch chan<- *prometheus.Desc) { + ch <- totalCount + ch <- recieveCount + ch <- dynamicCount + ch <- floodCount +} + +// Collect collects metrics from JunOS +func (c *macCollector) Collect(client *rpc.Client, ch chan<- prometheus.Metric, labelValues []string) error { + var x = MacRpc{} + err := client.RunCommandAndParse("show ethernet-switching table summary", &x) + if err != nil { + return err + } + + entry := x.Information.Table.Entry + ch <- prometheus.MustNewConstMetric(totalCount, prometheus.GaugeValue, float64(entry.TotalCount), labelValues...) + ch <- prometheus.MustNewConstMetric(recieveCount, prometheus.GaugeValue, float64(entry.ReceiveCount), labelValues...) + ch <- prometheus.MustNewConstMetric(dynamicCount, prometheus.GaugeValue, float64(entry.DynamicCount), labelValues...) + ch <- prometheus.MustNewConstMetric(floodCount, prometheus.GaugeValue, float64(entry.FloodCount), labelValues...) + + return nil +} diff --git a/mac/rpc.go b/mac/rpc.go new file mode 100644 index 00000000..d98b1382 --- /dev/null +++ b/mac/rpc.go @@ -0,0 +1,21 @@ +package mac + +type MacRpc struct { + Information EthernetSwitchingTableInformation `xml:"ethernet-switching-table-information"` +} + +type EthernetSwitchingTableInformation struct { + Table EthernetSwitchingTable `xml:"ethernet-switching-table"` +} + +type EthernetSwitchingTable struct { + Entry MacTableEntry `xml:"mac-table-entry"` +} + +type MacTableEntry struct { + TotalCount int64 `xml:"mac-table-total-count"` + ReceiveCount int64 `xml:"mac-table-recieve-count"` + DynamicCount int64 `xml:"mac-table-dynamic-count"` + FloodCount int64 `xml:"mac-table-flood-count"` +} + diff --git a/main.go b/main.go index 7fc652a6..b0b46659 100644 --- a/main.go +++ b/main.go @@ -57,6 +57,7 @@ var ( rpkiEnabled = flag.Bool("rpki.enabled", false, "Scrape rpki metrics") satelliteEnabled = flag.Bool("satellite.enabled", false, "Scrape metrics from satellite devices") systemEnabled = flag.Bool("system.enabled", false, "Scrape system metrics") + macEnabled = flag.Bool("mac.enabled", false, "Scrape MAC address table metrics") alarmFilter = flag.String("alarms.filter", "", "Regex to filter for alerts to ignore") configFile = flag.String("config.file", "", "Path to config file") dynamicIfaceLabels = flag.Bool("dynamic-interface-labels", true, "Parse interface descriptions to get labels dynamicly") @@ -206,6 +207,7 @@ func loadConfigFromFlags() *config.Config { f.Satellite = *satelliteEnabled f.System = *systemEnabled f.Power = *powerEnabled + f.MAC = *macEnabled return c }