From bacdc3c5871aef300800ef077258b22becd47a26 Mon Sep 17 00:00:00 2001 From: Marvin Vogt Date: Fri, 5 May 2023 13:11:17 +0200 Subject: [PATCH 1/2] Allow explicitly including interfaces instead of only allowing excludes --- main.go | 19 +++++++++++++++---- transceiver-collector/collector.go | 16 ++++++++++++++-- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/main.go b/main.go index 87c7802..369332d 100644 --- a/main.go +++ b/main.go @@ -21,6 +21,7 @@ var ( metricsPath = flag.String("web.telemetry-path", "/metrics", "Path under which to expose metrics") collectInterfaceFeatures = flag.Bool("collector.interface-features.enable", true, "Collect interface features") excludeInterfaces = flag.String("exclude.interfaces", "", "Comma seperated list of interfaces to exclude") + includeInterfaces = flag.String("include.interfaces", "", "Comma seperated list of interfaces to include") powerUnitdBm = flag.Bool("collector.optical-power-in-dbm", false, "Report optical powers in dBm instead of mW (default false -> mW)") ) @@ -81,13 +82,23 @@ func (t transceiverCollectorWrapper) Describe(ch chan<- *prometheus.Desc) { } func handleMetricsRequest(w http.ResponseWriter, request *http.Request) { + var excludedIfaceNames []string + var includedIfaceNames []string registry := prometheus.NewRegistry() - excludedIfaceNames := strings.Split(*excludeInterfaces, ",") - for index, excludedIfaceName := range excludedIfaceNames { - excludedIfaceNames[index] = strings.Trim(excludedIfaceName, " ") + if len(*excludeInterfaces) > 0 { + excludedIfaceNames = strings.Split(*excludeInterfaces, ",") + for index, excludedIfaceName := range excludedIfaceNames { + excludedIfaceNames[index] = strings.Trim(excludedIfaceName, " ") + } + } + if len(*includeInterfaces) > 0 { + includedIfaceNames = strings.Split(*includeInterfaces, ",") + for index, includedIfaceName := range includedIfaceNames { + includedIfaceNames[index] = strings.Trim(includedIfaceName, " ") + } } - transceiverCollector := transceivercollector.NewCollector(excludedIfaceNames, *collectInterfaceFeatures, *powerUnitdBm) + transceiverCollector := transceivercollector.NewCollector(excludedIfaceNames, includedIfaceNames, *collectInterfaceFeatures, *powerUnitdBm) wrapper := &transceiverCollectorWrapper{ collector: transceiverCollector, } diff --git a/transceiver-collector/collector.go b/transceiver-collector/collector.go index f974317..29513ab 100644 --- a/transceiver-collector/collector.go +++ b/transceiver-collector/collector.go @@ -91,6 +91,7 @@ var laserLabels = []string{"interface", "laser_index"} // TransceiverCollector implements prometheus.Collector interface and collects various interface statistics type TransceiverCollector struct { excludeInterfaces []string + includeInterfaces []string collectInterfaceFeatures bool powerUnitdBm bool } @@ -172,7 +173,7 @@ func init() { } // NewCollector initializes a new TransceiverCollector -func NewCollector(excludeInterfaces []string, collectInterfaceFeatures bool, powerUnitdBm bool) *TransceiverCollector { +func NewCollector(excludeInterfaces []string, includeInterfaces []string, collectInterfaceFeatures bool, powerUnitdBm bool) *TransceiverCollector { laserTxPowerThresholdsSupportedDesc = prometheus.NewDesc(prefix+"laser_tx_power_supports_thresholds_bool", "1 if thresholds for the laser tx power are supported", laserLabels, nil) laserRxPowerThresholdsSupportedDesc = prometheus.NewDesc(prefix+"laser_rx_power_supports_thresholds_bool", "1 if thresholds for the laser rx power are supported", laserLabels, nil) if powerUnitdBm { @@ -203,6 +204,7 @@ func NewCollector(excludeInterfaces []string, collectInterfaceFeatures bool, pow return &TransceiverCollector{ excludeInterfaces: excludeInterfaces, + includeInterfaces: includeInterfaces, collectInterfaceFeatures: collectInterfaceFeatures, powerUnitdBm: powerUnitdBm, } @@ -293,14 +295,24 @@ func (t *TransceiverCollector) getMonitoredInterfaces() ([]string, error) { return []string{}, errors.Wrapf(err, "Could not enumerate system's interfaces") } + InterfacesExcluded := len(t.excludeInterfaces) > 0 + InterfacesIncluded := len(t.includeInterfaces) > 0 + if InterfacesExcluded == true && InterfacesIncluded == true { + return []string{}, errors.New("Cannot include and exclude interfaces at the same time") + } + ifaceNames := []string{} for _, iface := range interfaces { if iface.Flags&net.FlagLoopback > 0 { continue } - if contains(t.excludeInterfaces, iface.Name) { + if InterfacesExcluded == true && contains(t.excludeInterfaces, iface.Name) { continue } + if InterfacesIncluded == true && !contains(t.includeInterfaces, iface.Name) { + continue + } + ifaceNames = append(ifaceNames, iface.Name) } return ifaceNames, nil From 168de0b358bdcd871f106523326916ee1c4fad08 Mon Sep 17 00:00:00 2001 From: Marvin Vogt Date: Fri, 5 May 2023 17:08:20 +0200 Subject: [PATCH 2/2] Mention option to explicitly include interfaces in README --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 87dce82..2819b97 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,8 @@ Usage of ./transceiver-exporter: Report optical powers in dBm instead of mW (default false -> mW) -exclude.interfaces string Comma seperated list of interfaces to exclude + -include.interfaces string + Comma seperated list of interfaces to include -version Print version and exit -web.listen-address string