diff --git a/collector/ethtool_linux.go b/collector/ethtool_linux.go index 912e31e5..3e01c29c 100644 --- a/collector/ethtool_linux.go +++ b/collector/ethtool_linux.go @@ -20,29 +20,22 @@ package collector import ( - "bufio" "errors" "fmt" "os" - "path/filepath" "regexp" "sort" - "strconv" - "strings" - "syscall" "github.com/go-kit/kit/log" "github.com/go-kit/kit/log/level" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/procfs/sysfs" "github.com/safchain/ethtool" - "gopkg.in/alecthomas/kingpin.v2" ) var ( receivedRegex = regexp.MustCompile(`_rx_`) transmittedRegex = regexp.MustCompile(`_tx_`) - ethtoolFixtures = kingpin.Flag("collector.ethtool.fixtures", "test fixtures to use for ethtool collector end-to-end testing").Default("").String() ) type EthtoolStats interface { @@ -63,56 +56,6 @@ type ethtoolCollector struct { stats EthtoolStats } -type EthtoolFixture struct { - fixturePath string -} - -func (e *EthtoolFixture) Stats(intf string) (map[string]uint64, error) { - res := make(map[string]uint64) - - fixtureFile, err := os.Open(filepath.Join(e.fixturePath, intf)) - if e, ok := err.(*os.PathError); ok && e.Err == syscall.ENOENT { - // The fixture for this interface doesn't exist. That's OK because it replicates - // an interface that doesn't support ethtool. - return res, nil - } - if err != nil { - return res, err - } - defer fixtureFile.Close() - - scanner := bufio.NewScanner(fixtureFile) - for scanner.Scan() { - line := scanner.Text() - if strings.HasPrefix(line, "#") { - continue - } - if strings.HasPrefix(line, "NIC statistics:") { - continue - } - line = strings.Trim(line, " ") - items := strings.Split(line, ": ") - val, err := strconv.ParseUint(items[1], 10, 64) - if err != nil { - return res, err - } - res[items[0]] = val - } - - return res, err -} - -func NewEthtoolTestCollector(logger log.Logger) (Collector, error) { - collector, err := makeEthtoolCollector(logger) - collector.stats = &EthtoolFixture{ - fixturePath: *ethtoolFixtures, - } - if err != nil { - return nil, err - } - return collector, nil -} - // makeEthtoolCollector is the internal constructor for EthtoolCollector. // This allows NewEthtoolTestCollector to override it's .stats interface // for testing. @@ -173,11 +116,6 @@ func init() { // NewEthtoolCollector returns a new Collector exposing ethtool stats. func NewEthtoolCollector(logger log.Logger) (Collector, error) { - // Specifying --collector.ethtool.fixtures on the command line activates - // the test fixtures. This is for `end-to-end-test.sh` - if *ethtoolFixtures != "" { - return NewEthtoolTestCollector(logger) - } return makeEthtoolCollector(logger) } diff --git a/collector/ethtool_linux_test.go b/collector/ethtool_linux_test.go index 28f1b70e..8aaeed3a 100644 --- a/collector/ethtool_linux_test.go +++ b/collector/ethtool_linux_test.go @@ -14,13 +14,69 @@ package collector import ( + "bufio" "fmt" + "os" + "path/filepath" + "strconv" + "strings" + "syscall" "testing" "github.com/go-kit/kit/log" "github.com/prometheus/client_golang/prometheus" ) +type EthtoolFixture struct { + fixturePath string +} + +func (e *EthtoolFixture) Stats(intf string) (map[string]uint64, error) { + res := make(map[string]uint64) + + fixtureFile, err := os.Open(filepath.Join(e.fixturePath, intf)) + if e, ok := err.(*os.PathError); ok && e.Err == syscall.ENOENT { + // The fixture for this interface doesn't exist. That's OK because it replicates + // an interface that doesn't support ethtool. + return res, nil + } + if err != nil { + return res, err + } + defer fixtureFile.Close() + + scanner := bufio.NewScanner(fixtureFile) + for scanner.Scan() { + line := scanner.Text() + if strings.HasPrefix(line, "#") { + continue + } + if strings.HasPrefix(line, "NIC statistics:") { + continue + } + line = strings.Trim(line, " ") + items := strings.Split(line, ": ") + val, err := strconv.ParseUint(items[1], 10, 64) + if err != nil { + return res, err + } + res[items[0]] = val + } + + return res, err +} + +func NewEthtoolTestCollector(logger log.Logger) (Collector, error) { + collector, err := makeEthtoolCollector(logger) + collector.stats = &EthtoolFixture{ + fixturePath: "fixtures/ethtool/", + } + if err != nil { + return nil, err + } + return collector, nil +} + func TestEthtoolCollector(t *testing.T) { testcases := []string{ prometheus.NewDesc("node_ethtool_align_errors", "Network interface align_errors", []string{"device"}, nil).String(), @@ -37,7 +93,6 @@ func TestEthtoolCollector(t *testing.T) { } *sysPath = "fixtures/sys" - *ethtoolFixtures = "fixtures/ethtool/" collector, err := NewEthtoolTestCollector(log.NewNopLogger()) if err != nil { diff --git a/collector/fixtures/e2e-output.txt b/collector/fixtures/e2e-output.txt index 5f3c3e0e..e04cb0cc 100644 --- a/collector/fixtures/e2e-output.txt +++ b/collector/fixtures/e2e-output.txt @@ -657,45 +657,6 @@ node_entropy_available_bits 1337 # HELP node_entropy_pool_size_bits Bits of entropy pool. # TYPE node_entropy_pool_size_bits gauge node_entropy_pool_size_bits 4096 -# HELP node_ethtool_align_errors Network interface align_errors -# TYPE node_ethtool_align_errors untyped -node_ethtool_align_errors{device="eth0"} 0 -# HELP node_ethtool_received_broadcast Network interface rx_broadcast -# TYPE node_ethtool_received_broadcast untyped -node_ethtool_received_broadcast{device="eth0"} 5792 -# HELP node_ethtool_received_errors_total Number of received frames with errors -# TYPE node_ethtool_received_errors_total untyped -node_ethtool_received_errors_total{device="eth0"} 0 -# HELP node_ethtool_received_missed Network interface rx_missed -# TYPE node_ethtool_received_missed untyped -node_ethtool_received_missed{device="eth0"} 401 -# HELP node_ethtool_received_multicast Network interface rx_multicast -# TYPE node_ethtool_received_multicast untyped -node_ethtool_received_multicast{device="eth0"} 23973 -# HELP node_ethtool_received_packets_total Network interface packets received -# TYPE node_ethtool_received_packets_total untyped -node_ethtool_received_packets_total{device="eth0"} 1.260062e+06 -# HELP node_ethtool_received_unicast Network interface rx_unicast -# TYPE node_ethtool_received_unicast untyped -node_ethtool_received_unicast{device="eth0"} 1.230297e+06 -# HELP node_ethtool_transmitted_aborted Network interface tx_aborted -# TYPE node_ethtool_transmitted_aborted untyped -node_ethtool_transmitted_aborted{device="eth0"} 0 -# HELP node_ethtool_transmitted_errors_total Number of sent frames with errors -# TYPE node_ethtool_transmitted_errors_total untyped -node_ethtool_transmitted_errors_total{device="eth0"} 0 -# HELP node_ethtool_transmitted_multi_collisions Network interface tx_multi_collisions -# TYPE node_ethtool_transmitted_multi_collisions untyped -node_ethtool_transmitted_multi_collisions{device="eth0"} 0 -# HELP node_ethtool_transmitted_packets_total Network interface packets sent -# TYPE node_ethtool_transmitted_packets_total untyped -node_ethtool_transmitted_packets_total{device="eth0"} 961500 -# HELP node_ethtool_transmitted_single_collisions Network interface tx_single_collisions -# TYPE node_ethtool_transmitted_single_collisions untyped -node_ethtool_transmitted_single_collisions{device="eth0"} 0 -# HELP node_ethtool_transmitted_underrun Network interface tx_underrun -# TYPE node_ethtool_transmitted_underrun untyped -node_ethtool_transmitted_underrun{device="eth0"} 0 # HELP node_exporter_build_info A metric with a constant '1' value labeled by version, revision, branch, and goversion from which node_exporter was built. # TYPE node_exporter_build_info gauge # HELP node_fibrechannel_error_frames_total Number of errors in frames @@ -2784,7 +2745,6 @@ node_scrape_collector_success{collector="diskstats"} 1 node_scrape_collector_success{collector="drbd"} 1 node_scrape_collector_success{collector="edac"} 1 node_scrape_collector_success{collector="entropy"} 1 -node_scrape_collector_success{collector="ethtool"} 1 node_scrape_collector_success{collector="fibrechannel"} 1 node_scrape_collector_success{collector="filefd"} 1 node_scrape_collector_success{collector="hwmon"} 1 diff --git a/end-to-end-test.sh b/end-to-end-test.sh index 9de1e980..d87162fe 100755 --- a/end-to-end-test.sh +++ b/end-to-end-test.sh @@ -14,7 +14,6 @@ enabled_collectors=$(cat << COLLECTORS drbd edac entropy - ethtool fibrechannel filefd hwmon @@ -110,7 +109,6 @@ fi --collector.qdisc.fixtures="collector/fixtures/qdisc/" \ --collector.netclass.ignored-devices="(dmz|int)" \ --collector.netclass.ignore-invalid-speed \ - --collector.ethtool.fixtures="collector/fixtures/ethtool/" \ --collector.bcache.priorityStats \ --collector.cpu.info \ --collector.cpu.info.flags-include="^(aes|avx.?|constant_tsc)$" \