Skip to content

Commit

Permalink
feat: use regex to include/exclude interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
rwxd committed Nov 24, 2023
1 parent 57f9b78 commit 003b14d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@ 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
-exclude.interfaces-regex string
Regexp of interfaces to exclude
-exclude.interfaces-down
Don't report on interfaces being management DOWN
-include.interfaces string
Comma seperated list of interfaces to include
-include.interfaces-regex string
Regexp of interfaces to include
-version
Print version and exit
-web.listen-address string
Expand Down
6 changes: 4 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
transceivercollector "github.com/wobcom/transceiver-exporter/transceiver-collector"
)

const version string = "1.4.1"
const version string = "1.5.0"

var (
showVersion = flag.Bool("version", false, "Print version and exit")
Expand All @@ -22,6 +22,8 @@ var (
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")
excludeInterfacesRegex = flag.String("exclude.interfaces-regex", "", "Regex of interfaces to exclude")
includeInterfaceRegex = flag.String("include.interfaces-regex", "", "Regex of interfaces to include")
excludeInterfacesDown = flag.Bool("exclude.interfaces-down", false, "Don't report on interfaces being management DOWN")
powerUnitdBm = flag.Bool("collector.optical-power-in-dbm", false, "Report optical powers in dBm instead of mW (default false -> mW)")
)
Expand Down Expand Up @@ -99,7 +101,7 @@ func handleMetricsRequest(w http.ResponseWriter, request *http.Request) {
includedIfaceNames[index] = strings.Trim(includedIfaceName, " ")
}
}
transceiverCollector := transceivercollector.NewCollector(excludedIfaceNames, includedIfaceNames, *excludeInterfacesDown, *collectInterfaceFeatures, *powerUnitdBm)
transceiverCollector := transceivercollector.NewCollector(excludedIfaceNames, includedIfaceNames, *excludeInterfacesRegex, *includeInterfaceRegex, *excludeInterfacesDown, *collectInterfaceFeatures, *powerUnitdBm)
wrapper := &transceiverCollectorWrapper{
collector: transceiverCollector,
}
Expand Down
23 changes: 22 additions & 1 deletion transceiver-collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package transceivercollector
import (
"fmt"
"net"
"regexp"
"strconv"

"github.com/pkg/errors"
Expand Down Expand Up @@ -92,6 +93,8 @@ var laserLabels = []string{"interface", "laser_index"}
type TransceiverCollector struct {
excludeInterfaces []string
includeInterfaces []string
excludeInterfacesRegex string
includeInterfacesRegex string
excludeInterfacesDown bool
collectInterfaceFeatures bool
powerUnitdBm bool
Expand Down Expand Up @@ -174,7 +177,7 @@ func init() {
}

// NewCollector initializes a new TransceiverCollector
func NewCollector(excludeInterfaces []string, includeInterfaces []string, excludeInterfacesDown bool, collectInterfaceFeatures bool, powerUnitdBm bool) *TransceiverCollector {
func NewCollector(excludeInterfaces []string, includeInterfaces []string, excludeInterfacesRegex, includeInterfacesRegex string, excludeInterfacesDown bool, 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 {
Expand Down Expand Up @@ -206,6 +209,8 @@ func NewCollector(excludeInterfaces []string, includeInterfaces []string, exclud
return &TransceiverCollector{
excludeInterfaces: excludeInterfaces,
includeInterfaces: includeInterfaces,
excludeInterfacesRegex: excludeInterfacesRegex,
includeInterfacesRegex: includeInterfacesRegex,
excludeInterfacesDown: excludeInterfacesDown,
collectInterfaceFeatures: collectInterfaceFeatures,
powerUnitdBm: powerUnitdBm,
Expand Down Expand Up @@ -303,6 +308,16 @@ func (t *TransceiverCollector) getMonitoredInterfaces() ([]string, error) {
return []string{}, errors.New("Cannot include and exclude interfaces at the same time")
}

interfacesExcludeRegexCompiled, err := regexp.Compile(t.excludeInterfacesRegex)
if err != nil {
return []string{}, errors.Wrapf(err, "Could not compile regex %s", t.excludeInterfacesRegex)
}

interfacesIncludeRegexCompiled, err := regexp.Compile(t.includeInterfacesRegex)
if err != nil {
return []string{}, errors.Wrapf(err, "Could not compile regex %s", t.includeInterfacesRegex)
}

ifaceNames := []string{}
for _, iface := range interfaces {
if iface.Flags&net.FlagLoopback > 0 {
Expand All @@ -317,6 +332,12 @@ func (t *TransceiverCollector) getMonitoredInterfaces() ([]string, error) {
if InterfacesIncluded && !contains(t.includeInterfaces, iface.Name) {
continue
}
if len(t.excludeInterfacesRegex) > 0 && interfacesExcludeRegexCompiled.MatchString(iface.Name) {
continue
}
if len(t.includeInterfacesRegex) > 0 && !interfacesIncludeRegexCompiled.MatchString(iface.Name) {
continue
}

ifaceNames = append(ifaceNames, iface.Name)
}
Expand Down

0 comments on commit 003b14d

Please sign in to comment.