2021-04-29 18:05:29 +00:00
// 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.
// +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"
2021-07-21 18:23:11 +00:00
"strings"
2021-05-14 17:07:30 +00:00
"syscall"
2021-04-29 18:05:29 +00:00
2021-06-17 11:21:05 +00:00
"github.com/go-kit/log"
"github.com/go-kit/log/level"
2021-04-29 18:05:29 +00:00
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/procfs/sysfs"
"github.com/safchain/ethtool"
2021-05-14 17:07:30 +00:00
"golang.org/x/sys/unix"
2021-07-19 09:44:28 +00:00
"gopkg.in/alecthomas/kingpin.v2"
2021-04-29 18:05:29 +00:00
)
var (
2021-08-10 16:57:36 +00:00
ethtoolIgnoredDevices = kingpin . Flag ( "collector.ethtool.ignored-devices" , "Regexp of net devices to ignore for ethtool collector." ) . Default ( "^$" ) . String ( )
ethtoolIncludedMetrics = kingpin . Flag ( "collector.ethtool.metrics-include" , "Regexp of ethtool stats to include." ) . Default ( ".*" ) . String ( )
2021-07-21 18:23:11 +00:00
metricNameRegex = regexp . MustCompile ( ` _*[^0-9A-Za-z_]+_* ` )
receivedRegex = regexp . MustCompile ( ` (^|_)rx(_|$) ` )
transmittedRegex = regexp . MustCompile ( ` (^|_)tx(_|$) ` )
2021-04-29 18:05:29 +00:00
)
type EthtoolStats interface {
Stats ( string ) ( map [ string ] uint64 , error )
}
type ethtoolStats struct {
}
func ( e * ethtoolStats ) Stats ( intf string ) ( map [ string ] uint64 , error ) {
return ethtool . Stats ( intf )
}
type ethtoolCollector struct {
2021-07-19 09:44:28 +00:00
fs sysfs . FS
entries map [ string ] * prometheus . Desc
ignoredDevicesPattern * regexp . Regexp
2021-08-10 16:57:36 +00:00
metricsPattern * regexp . Regexp
2021-07-19 09:44:28 +00:00
logger log . Logger
stats EthtoolStats
2021-04-29 18:05:29 +00:00
}
// makeEthtoolCollector is the internal constructor for EthtoolCollector.
2021-06-11 16:02:08 +00:00
// This allows NewEthtoolTestCollector to override its .stats interface
2021-04-29 18:05:29 +00:00
// 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 )
}
// Pre-populate some common ethtool metrics.
return & ethtoolCollector {
2021-07-19 09:44:28 +00:00
fs : fs ,
ignoredDevicesPattern : regexp . MustCompile ( * ethtoolIgnoredDevices ) ,
2021-08-10 16:57:36 +00:00
metricsPattern : regexp . MustCompile ( * ethtoolIncludedMetrics ) ,
2021-07-19 09:44:28 +00:00
logger : logger ,
stats : & ethtoolStats { } ,
2021-04-29 18:05:29 +00:00
entries : map [ string ] * prometheus . Desc {
"rx_bytes" : prometheus . NewDesc (
2021-07-22 09:29:58 +00:00
prometheus . BuildFQName ( namespace , "ethtool" , "received_bytes_total" ) ,
2021-04-29 18:05:29 +00:00
"Network interface bytes received" ,
[ ] string { "device" } , nil ,
) ,
"rx_dropped" : prometheus . NewDesc (
2021-07-22 09:29:58 +00:00
prometheus . BuildFQName ( namespace , "ethtool" , "received_dropped_total" ) ,
2021-04-29 18:05:29 +00:00
"Number of received frames dropped" ,
[ ] string { "device" } , nil ,
) ,
"rx_errors" : prometheus . NewDesc (
2021-07-22 09:29:58 +00:00
prometheus . BuildFQName ( namespace , "ethtool" , "received_errors_total" ) ,
2021-04-29 18:05:29 +00:00
"Number of received frames with errors" ,
[ ] string { "device" } , nil ,
) ,
"rx_packets" : prometheus . NewDesc (
2021-07-22 09:29:58 +00:00
prometheus . BuildFQName ( namespace , "ethtool" , "received_packets_total" ) ,
2021-04-29 18:05:29 +00:00
"Network interface packets received" ,
[ ] string { "device" } , nil ,
) ,
"tx_bytes" : prometheus . NewDesc (
2021-07-22 09:29:58 +00:00
prometheus . BuildFQName ( namespace , "ethtool" , "transmitted_bytes_total" ) ,
2021-04-29 18:05:29 +00:00
"Network interface bytes sent" ,
[ ] string { "device" } , nil ,
) ,
"tx_errors" : prometheus . NewDesc (
2021-07-22 09:29:58 +00:00
prometheus . BuildFQName ( namespace , "ethtool" , "transmitted_errors_total" ) ,
2021-04-29 18:05:29 +00:00
"Number of sent frames with errors" ,
[ ] string { "device" } , nil ,
) ,
"tx_packets" : prometheus . NewDesc (
2021-07-22 09:29:58 +00:00
prometheus . BuildFQName ( namespace , "ethtool" , "transmitted_packets_total" ) ,
2021-04-29 18:05:29 +00:00
"Network interface packets sent" ,
[ ] string { "device" } , nil ,
) ,
} ,
} , nil
}
func init ( ) {
registerCollector ( "ethtool" , defaultDisabled , NewEthtoolCollector )
}
2021-07-21 18:23:11 +00:00
// Sanitize the given metric name by replacing invalid characters by underscores.
//
// OpenMetrics and the Prometheus exposition format require the metric name
// to consist only of alphanumericals and "_", ":" and they must not start
// with digits. Since colons in MetricFamily are reserved to signal that the
// MetricFamily is the result of a calculation or aggregation of a general
// purpose monitoring system, colons will be replaced as well.
//
// Note: If not subsequently prepending a namespace and/or subsystem (e.g.,
// with prometheus.BuildFQName), the caller must ensure that the supplied
// metricName does not begin with a digit.
func SanitizeMetricName ( metricName string ) string {
return metricNameRegex . ReplaceAllString ( metricName , "_" )
}
// Generate the fully-qualified metric name for the ethool metric.
func buildEthtoolFQName ( metric string ) string {
metricName := strings . TrimLeft ( strings . ToLower ( SanitizeMetricName ( metric ) ) , "_" )
metricName = receivedRegex . ReplaceAllString ( metricName , "${1}received${2}" )
metricName = transmittedRegex . ReplaceAllString ( metricName , "${1}transmitted${2}" )
return prometheus . BuildFQName ( namespace , "ethtool" , metricName )
}
2021-04-29 18:05:29 +00:00
// NewEthtoolCollector returns a new Collector exposing ethtool stats.
func NewEthtoolCollector ( logger log . Logger ) ( Collector , error ) {
return makeEthtoolCollector ( logger )
}
func ( c * ethtoolCollector ) Update ( ch chan <- prometheus . Metric ) error {
netClass , err := c . fs . NetClass ( )
if err != nil {
if errors . Is ( err , os . ErrNotExist ) || errors . Is ( err , os . ErrPermission ) {
level . Debug ( c . logger ) . Log ( "msg" , "Could not read netclass file" , "err" , err )
return ErrNoData
}
return fmt . Errorf ( "could not get net class info: %w" , err )
}
if len ( netClass ) == 0 {
return fmt . Errorf ( "no network devices found" )
}
for device := range netClass {
var stats map [ string ] uint64
var err error
2021-07-19 09:44:28 +00:00
if c . ignoredDevicesPattern . MatchString ( device ) {
continue
}
2021-04-29 18:05:29 +00:00
stats , err = c . stats . Stats ( device )
2021-05-14 17:07:30 +00:00
// If Stats() returns EOPNOTSUPP it doesn't support ethtool stats. Log that only at Debug level.
// Otherwise log it at Error level.
2021-04-29 18:05:29 +00:00
if err != nil {
2021-05-14 17:07:30 +00:00
if errno , ok := err . ( syscall . Errno ) ; ok {
if err == unix . EOPNOTSUPP {
level . Debug ( c . logger ) . Log ( "msg" , "ethtool stats error" , "err" , err , "device" , device , "errno" , uint ( errno ) )
} else if errno != 0 {
level . Error ( c . logger ) . Log ( "msg" , "ethtool stats error" , "err" , err , "device" , device , "errno" , uint ( errno ) )
}
} else {
level . Error ( c . logger ) . Log ( "msg" , "ethtool stats error" , "err" , err , "device" , device )
}
}
if stats == nil || len ( stats ) < 1 {
// No stats returned; device does not support ethtool stats.
2021-04-29 18:05:29 +00:00
continue
}
// Sort metric names so that the test fixtures will match up
keys := make ( [ ] string , 0 , len ( stats ) )
for k := range stats {
keys = append ( keys , k )
}
sort . Strings ( keys )
for _ , metric := range keys {
2021-08-10 16:57:36 +00:00
if ! c . metricsPattern . MatchString ( metric ) {
continue
}
2021-04-29 18:05:29 +00:00
val := stats [ metric ]
2021-07-21 18:23:11 +00:00
metricFQName := buildEthtoolFQName ( metric )
2021-04-29 18:05:29 +00:00
// Check to see if this metric exists; if not then create it and store it in c.entries.
entry , exists := c . entries [ metric ]
if ! exists {
entry = prometheus . NewDesc (
metricFQName ,
fmt . Sprintf ( "Network interface %s" , metric ) ,
[ ] string { "device" } , nil ,
)
c . entries [ metric ] = entry
}
ch <- prometheus . MustNewConstMetric (
entry , prometheus . UntypedValue , float64 ( val ) , device )
}
}
return nil
}