Skip to content

Commit

Permalink
Merge pull request #5 from SRv6d/option-include-interfaces
Browse files Browse the repository at this point in the history
Allow explicitly including interfaces instead of only allowing excludes
  • Loading branch information
johannwagner authored Jun 14, 2023
2 parents a7f2e20 + 168de0b commit 98926cc
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 15 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)")
)

Expand Down Expand Up @@ -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,
}
Expand Down
16 changes: 14 additions & 2 deletions transceiver-collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -203,6 +204,7 @@ func NewCollector(excludeInterfaces []string, collectInterfaceFeatures bool, pow

return &TransceiverCollector{
excludeInterfaces: excludeInterfaces,
includeInterfaces: includeInterfaces,
collectInterfaceFeatures: collectInterfaceFeatures,
powerUnitdBm: powerUnitdBm,
}
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 98926cc

Please sign in to comment.