Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions sysfs/net_class.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"regexp"

"github.com/prometheus/procfs/internal/util"
)
Expand Down Expand Up @@ -63,7 +64,7 @@ type NetClassIface struct {
type NetClass map[string]NetClassIface

// NetClassDevices scans /sys/class/net for devices and returns them as a list of names.
func (fs FS) NetClassDevices() ([]string, error) {
func (fs FS) NetClassDevices(ignoredDevicesPattern *regexp.Regexp) ([]string, error) {
var res []string
path := fs.sys.Path(netclassPath)

Expand All @@ -76,15 +77,18 @@ func (fs FS) NetClassDevices() ([]string, error) {
if deviceDir.Mode().IsRegular() {
continue
}
if ignoredDevicesPattern.MatchString(deviceDir.Name()) {
continue
}
res = append(res, deviceDir.Name())
}

return res, nil
}

// NetClass returns info for all net interfaces (iface) read from /sys/class/net/<iface>.
func (fs FS) NetClass() (NetClass, error) {
devices, err := fs.NetClassDevices()
func (fs FS) NetClass(ignoredDevicesPattern *regexp.Regexp) (NetClass, error) {
devices, err := fs.NetClassDevices(ignoredDevicesPattern)
if err != nil {
return nil, err
}
Expand Down
36 changes: 34 additions & 2 deletions sysfs/net_class_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package sysfs

import (
"reflect"
"regexp"
"testing"
)

Expand All @@ -26,7 +27,12 @@ func TestNewNetClassDevices(t *testing.T) {
t.Fatal(err)
}

devices, err := fs.NetClassDevices()
re, err := regexp.Compile("^$")
if err != nil {
t.Fatal(err)
}

devices, err := fs.NetClassDevices(re)
if err != nil {
t.Fatal(err)
}
Expand All @@ -39,13 +45,39 @@ func TestNewNetClassDevices(t *testing.T) {
}
}

func TestNewNetClassDevicesFilter(t *testing.T) {
fs, err := NewFS(sysTestFixtures)
if err != nil {
t.Fatal(err)
}

re, err := regexp.Compile("eth")
if err != nil {
t.Fatal(err)
}

devices, err := fs.NetClassDevices(re)
if err != nil {
t.Fatal(err)
}

if len(devices) != 0 {
t.Errorf("Unexpected number of devices, want %d, have %d", 0, len(devices))
}
}

func TestNetClass(t *testing.T) {
fs, err := NewFS(sysTestFixtures)
if err != nil {
t.Fatal(err)
}

nc, err := fs.NetClass()
re, err := regexp.Compile("^$")
if err != nil {
t.Fatal(err)
}

nc, err := fs.NetClass(re)
if err != nil {
t.Fatal(err)
}
Expand Down