diff --git a/internal/linux/net/common.go b/internal/linux/net/common.go new file mode 100644 index 00000000..8021ce76 --- /dev/null +++ b/internal/linux/net/common.go @@ -0,0 +1,16 @@ +// Copyright 2024 Joshua Rich . +// SPDX-License-Identifier: MIT + +package net + +const ( + preferencesID = "network_sensors" + loopbackDeviceName = "lo" +) + +var defaultIgnoredDevices = []string{} + +//nolint:lll +type WorkerPrefs struct { + IgnoredDevices []string `toml:"ignored_devices" comment:"list of prefixes to match for devices to ignore, for e.g., 'eth' will ignore all devices starting with eth"` +} diff --git a/internal/linux/net/linkSensors.go b/internal/linux/net/linkSensors.go index e144720a..157add11 100644 --- a/internal/linux/net/linkSensors.go +++ b/internal/linux/net/linkSensors.go @@ -1,8 +1,7 @@ -// Copyright (c) 2024 Joshua Rich -// -// This software is released under the MIT License. -// https://opensource.org/licenses/MIT +// Copyright 2024 Joshua Rich . +// SPDX-License-Identifier: MIT +//revive:disable:unused-receiver package net import ( @@ -142,6 +141,7 @@ type AddressWorker struct { nlconn *rtnetlink.Conn donech chan struct{} linux.EventSensorWorker + prefs WorkerPrefs } func (w *AddressWorker) Sensors(ctx context.Context) ([]sensor.Entity, error) { @@ -227,6 +227,16 @@ func (w *AddressWorker) Events(ctx context.Context) (<-chan sensor.Entity, error return sensorCh, nil } +func (w *AddressWorker) ID() string { + return preferencesID +} + +func (w *AddressWorker) DefaultPreferences() WorkerPrefs { + return WorkerPrefs{ + IgnoredDevices: defaultIgnoredDevices, + } +} + func NewAddressWorker(_ context.Context) (*linux.EventSensorWorker, error) { worker := linux.NewEventSensorWorker(addressWorkerID) @@ -279,8 +289,15 @@ func (w *AddressWorker) filterAddress(msg rtnetlink.AddressMessage) *sensor.Enti if err != nil { return nil } - // Ignore addresses from unwanted links. - if slices.Contains(ifaceFilters, link.Attributes.Name) { + + if link.Attributes.Name == loopbackDeviceName { + return nil + } + + // Skip ignored devices. + if slices.ContainsFunc(w.prefs.IgnoredDevices, func(e string) bool { + return strings.HasPrefix(link.Attributes.Name, e) + }) { return nil } diff --git a/internal/linux/net/networkRates.go b/internal/linux/net/networkRates.go index ef37f6f4..a9ff659f 100644 --- a/internal/linux/net/networkRates.go +++ b/internal/linux/net/networkRates.go @@ -1,7 +1,5 @@ -// Copyright (c) 2024 Joshua Rich -// -// This software is released under the MIT License. -// https://opensource.org/licenses/MIT +// Copyright 2024 Joshua Rich . +// SPDX-License-Identifier: MIT // Parts of the code for collecting stats was adapted from Prometheus: // https://github.com/prometheus/node_exporter//collector/netdev_linux.go @@ -47,10 +45,7 @@ const ( totalsName = "Total" ) -var ( - sensorList = []netStatsType{bytesRecv, bytesSent, bytesRecvRate, bytesSentRate} - defaultIgnoredDevices = []string{"lo"} -) +var sensorList = []netStatsType{bytesRecv, bytesSent, bytesRecvRate, bytesSentRate} // linkStats represents a link and its stats. type linkStats struct { @@ -198,16 +193,11 @@ func getTXAttributes(stats *rtnetlink.LinkStats64) map[string]any { type netStatsWorker struct { statsSensors map[string]map[netStatsType]*netStatsSensor nlconn *rtnetlink.Conn - prefs netStatsWorkerPrefs + prefs WorkerPrefs delta time.Duration mu sync.Mutex } -//nolint:lll -type netStatsWorkerPrefs struct { - IgnoredDevices []string `toml:"ignored_devices" comment:"list of prefixes to match for devices to ignore, for e.g., 'eth' will ignore all devices starting with eth"` -} - // updateTotals takes the total Rx/Tx bytes and updates the total sensors. func (w *netStatsWorker) updateTotals(totalBytesRx, totalBytesTx uint64) { stats := &rtnetlink.LinkStats64{ @@ -281,11 +271,11 @@ func (w *netStatsWorker) Sensors(_ context.Context) ([]sensor.Entity, error) { } func (w *netStatsWorker) ID() string { - return netRatesWorkerID + return preferencesID } -func (w *netStatsWorker) DefaultPreferences() netStatsWorkerPrefs { - return netStatsWorkerPrefs{ +func (w *netStatsWorker) DefaultPreferences() WorkerPrefs { + return WorkerPrefs{ IgnoredDevices: defaultIgnoredDevices, } } @@ -302,7 +292,7 @@ func NewNetStatsWorker(ctx context.Context) (*linux.PollingSensorWorker, error) go func() { <-ctx.Done() - if err := conn.Close(); err != nil { + if err = conn.Close(); err != nil { logging.FromContext(ctx).Debug("Could not close netlink connection.", slog.String("worker", netRatesWorkerID), slog.Any("error", err)) @@ -347,7 +337,7 @@ func (w *netStatsWorker) getLinkStats(links []rtnetlink.LinkMessage) []linkStats } // Ignore loopback. - if msg.Attributes.Name == "lo" { + if msg.Attributes.Name == loopbackDeviceName { continue }