Skip to content

Commit

Permalink
Merge pull request #9 from rwxd/interfaces-regex
Browse files Browse the repository at this point in the history
use regex to include/exclude interfaces
  • Loading branch information
johannwagner authored May 6, 2024
2 parents 57f9b78 + 0a5c216 commit 5dadb29
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 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
28 changes: 27 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"net/http"
"os"
"regexp"
"strings"

"github.com/prometheus/client_golang/prometheus"
Expand All @@ -22,8 +23,13 @@ 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")
includeInterfacesRegex = 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)")

excludeInterfacesRegexCompiled = &regexp.Regexp{}
includeInterfacesRegexCompiled = &regexp.Regexp{}
)

func main() {
Expand All @@ -34,6 +40,10 @@ func main() {
os.Exit(0)
}

if err := compileRegexFlags(); err != nil {
log.Fatalf(err.Error())
}

startServer()
}

Expand All @@ -44,6 +54,21 @@ func printVersion() {
fmt.Println("Metrics Exporter for pluggable transceivers on Linux based hosts / switches")
}

// compileRegexFlags compiles the cli regex flags into the global variables
// and returns an error if the regex is invalid
func compileRegexFlags() error {
var err error
excludeInterfacesRegexCompiled, err = regexp.Compile(*excludeInterfacesRegex)
if err != nil {
return fmt.Errorf("error compiling exclude.interfaces-regex: %v", err)
}
includeInterfacesRegexCompiled, err = regexp.Compile(*includeInterfacesRegex)
if err != nil {
return fmt.Errorf("error compiling include.interfaces-regex: %v", err)
}
return nil
}

func startServer() {
log.Infof("Starting transceiver-exporter (version: %s)\n", version)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -99,7 +124,8 @@ 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, excludeInterfacesRegexCompiled, includeInterfacesRegexCompiled, *excludeInterfacesDown, *collectInterfaceFeatures, *powerUnitdBm)
wrapper := &transceiverCollectorWrapper{
collector: transceiverCollector,
}
Expand Down
18 changes: 17 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 *regexp.Regexp
includeInterfacesRegex *regexp.Regexp
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 *regexp.Regexp, 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,11 @@ func (t *TransceiverCollector) getMonitoredInterfaces() ([]string, error) {
return []string{}, errors.New("Cannot include and exclude interfaces at the same time")
}

// check if the regex is a non empty string to prevent matching no interfaces
// when the default value of the cli flag is used
regexIncludeValid := t.includeInterfacesRegex.String() != ""
regexExcludeValid := t.excludeInterfacesRegex.String() != ""

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

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

0 comments on commit 5dadb29

Please sign in to comment.