// Copyright 2021 The Prometheus Authors // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. //go:build !noethtool // +build !noethtool // The hard work of collecting data from the kernel via the ethtool interfaces is done by // https://github.com/safchain/ethtool/ // by Sylvain Afchain. Used under the Apache license. package collector import ( "errors" "fmt" "os" "regexp" "sort" "strings" "sync" "syscall" "github.com/alecthomas/kingpin/v2" "github.com/go-kit/log" "github.com/go-kit/log/level" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/procfs/sysfs" "github.com/safchain/ethtool" "golang.org/x/sys/unix" ) var ( ethtoolDeviceInclude = kingpin.Flag("collector.ethtool.device-include", "Regexp of ethtool devices to include (mutually exclusive to device-exclude).").String() ethtoolDeviceExclude = kingpin.Flag("collector.ethtool.device-exclude", "Regexp of ethtool devices to exclude (mutually exclusive to device-include).").String() ethtoolIncludedMetrics = kingpin.Flag("collector.ethtool.metrics-include", "Regexp of ethtool stats to include.").Default(".*").String() ethtoolReceivedRegex = regexp.MustCompile(`(^|_)rx(_|$)`) ethtoolTransmitRegex = regexp.MustCompile(`(^|_)tx(_|$)`) ) type Ethtool interface { DriverInfo(string) (ethtool.DrvInfo, error) Stats(string) (map[string]uint64, error) LinkInfo(string) (ethtool.EthtoolCmd, error) } type ethtoolLibrary struct { ethtool *ethtool.Ethtool } func (e *ethtoolLibrary) DriverInfo(intf string) (ethtool.DrvInfo, error) { return e.ethtool.DriverInfo(intf) } func (e *ethtoolLibrary) Stats(intf string) (map[string]uint64, error) { return e.ethtool.Stats(intf) } func (e *ethtoolLibrary) LinkInfo(intf string) (ethtool.EthtoolCmd, error) { var ethtoolCmd ethtool.EthtoolCmd _, err := ethtoolCmd.CmdGet(intf) return ethtoolCmd, err } type ethtoolCollector struct { fs sysfs.FS entries map[string]*prometheus.Desc entriesMutex sync.Mutex ethtool Ethtool deviceFilter deviceFilter infoDesc *prometheus.Desc metricsPattern *regexp.Regexp logger log.Logger } // makeEthtoolCollector is the internal constructor for EthtoolCollector. // This allows NewEthtoolTestCollector to override its .ethtool interface // for testing. func makeEthtoolCollector(logger log.Logger) (*ethtoolCollector, error) { fs, err := sysfs.NewFS(*sysPath) if err != nil { return nil, fmt.Errorf("failed to open sysfs: %w", err) } e, err := ethtool.NewEthtool() if err != nil { return nil, fmt.Errorf("failed to initialize ethtool library: %w", err) } // Pre-populate some common ethtool metrics. return ðtoolCollector{ fs: fs, ethtool: ðtoolLibrary{e}, deviceFilter: newDeviceFilter(*ethtoolDeviceExclude, *ethtoolDeviceInclude), metricsPattern: regexp.MustCompile(*ethtoolIncludedMetrics), logger: logger, entries: map[string]*prometheus.Desc{ "rx_bytes": prometheus.NewDesc( prometheus.BuildFQName(namespace, "ethtool", "received_bytes_total"), "Network interface bytes received", []string{"device"}, nil, ), "rx_dropped": prometheus.NewDesc( prometheus.BuildFQName(namespace, "ethtool", "received_dropped_total"), "Number of received frames dropped", []string{"device"}, nil, ), "rx_errors": prometheus.NewDesc( prometheus.BuildFQName(namespace, "ethtool", "received_errors_total"), "Number of received frames with errors", []string{"device"}, nil, ), "rx_packets": prometheus.NewDesc( prometheus.BuildFQName(namespace, "ethtool", "received_packets_total"), "Network interface packets received", []string{"device"}, nil, ), "tx_bytes": prometheus.NewDesc( prometheus.BuildFQName(namespace, "ethtool", "transmitted_bytes_total"), "Network interface bytes sent", []string{"device"}, nil, ), "tx_errors": prometheus.NewDesc( prometheus.BuildFQName(namespace, "ethtool", "transmitted_errors_total"), "Number of sent frames with errors", []string{"device"}, nil, ), "tx_packets": prometheus.NewDesc( prometheus.BuildFQName(namespace, "ethtool", "transmitted_packets_total"), "Network interface packets sent", []string{"device"}, nil, ), // link info "supported_port": prometheus.NewDesc( prometheus.BuildFQName(namespace, "network", "supported_port_info"), "Type of ports or PHYs supported by network device", []string{"device", "type"}, nil, ), "supported_speed": prometheus.NewDesc( prometheus.BuildFQName(namespace, "network", "supported_speed_bytes"), "Combination of speeds and features supported by network device", []string{"device", "duplex", "mode"}, nil, ), "supported_autonegotiate": prometheus.NewDesc( prometheus.BuildFQName(namespace, "network", "autonegotiate_supported"), "If this port device supports autonegotiate", []string{"device"}, nil, ), "supported_pause": prometheus.NewDesc( prometheus.BuildFQName(namespace, "network", "pause_supported"), "If this port device supports pause frames", []string{"device"}, nil, ), "supported_asymmetricpause": prometheus.NewDesc( prometheus.BuildFQName(namespace, "network", "asymmetricpause_supported"), "If this port device supports asymmetric pause frames", []string{"device"}, nil, ), "advertised_speed": prometheus.NewDesc( prometheus.BuildFQName(namespace, "network", "advertised_speed_bytes"), "Combination of speeds and features offered by network device", []string{"device", "duplex", "mode"}, nil, ), "advertised_autonegotiate": prometheus.NewDesc( prometheus.BuildFQName(namespace, "network", "autonegotiate_advertised"), "If this port device offers autonegotiate", []string{"device"}, nil, ), "advertised_pause": prometheus.NewDesc( prometheus.BuildFQName(namespace, "network", "pause_advertised"), "If this port device offers pause capability", []string{"device"}, nil, ), "advertised_asymmetricpause": prometheus.NewDesc( prometheus.BuildFQName(namespace, "network", "asymmetricpause_advertised"), "If this port device offers asymmetric pause capability", []string{"device"}, nil, ), "autonegotiate": prometheus.NewDesc( prometheus.BuildFQName(namespace, "network", "autonegotiate"), "If this port is using autonegotiate", []string{"device"}, nil, ), }, infoDesc: prometheus.NewDesc( prometheus.BuildFQName(namespace, "ethtool", "info"), "A metric with a constant '1' value labeled by bus_info, device, driver, expansion_rom_version, firmware_version, version.", []string{"bus_info", "device", "driver", "expansion_rom_version", "firmware_version", "version"}, nil, ), }, nil } func init() { registerCollector("ethtool", defaultDisabled, NewEthtoolCollector) } // Generate the fully-qualified metric name for the ethool metric. func buildEthtoolFQName(metric string) string { metricName := strings.TrimLeft(strings.ToLower(SanitizeMetricName(metric)), "_") metricName = ethtoolReceivedRegex.ReplaceAllString(metricName, "${1}received${2}") metricName = ethtoolTransmitRegex.ReplaceAllString(metricName, "${1}transmitted${2}") return prometheus.BuildFQName(namespace, "ethtool", metricName) } // NewEthtoolCollector returns a new Collector exposing ethtool stats. func NewEthtoolCollector(logger log.Logger) (Collector, error) { return makeEthtoolCollector(logger) } // updatePortCapabilities generates metrics for autonegotiate, pause and asymmetricpause. // The bit offsets here correspond to ethtool_link_mode_bit_indices in linux/include/uapi/linux/ethtool.h // https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/uapi/linux/ethtool.h func (c *ethtoolCollector) updatePortCapabilities(ch chan<- prometheus.Metric, prefix string, device string, linkModes uint32) { var ( autonegotiate = 0.0 pause = 0.0 asymmetricPause = 0.0 ) if linkModes&(1<