change NIC env var to NICS to support multiple interfaces

This commit is contained in:
Henry Dollman
2024-09-14 17:30:42 -04:00
parent 1cb4a711c3
commit 43ba9d5c6a

View File

@@ -33,7 +33,6 @@ import (
type Agent struct {
addr string
nic string
pubKey []byte
sem chan struct{}
containerStatsMap map[string]*container.PrevContainerStats
@@ -434,16 +433,11 @@ func (a *Agent) Run() {
}
if extraFilesystems, exists := os.LookupEnv("EXTRA_FILESYSTEMS"); exists {
// parse comma separated list of filesystems
for _, filesystem := range strings.Split(extraFilesystems, ",") {
a.fsStats[filesystem] = &system.FsStats{}
}
}
if nic, exists := os.LookupEnv("NIC"); exists {
a.nic = nic
}
a.initializeDiskInfo(fsEnvVarExists)
a.initializeDiskIoStats()
a.initializeNetIoStats()
@@ -534,15 +528,36 @@ func (a *Agent) initializeDiskIoStats() {
func (a *Agent) initializeNetIoStats() {
// reset valid network interfaces
a.netInterfaces = make(map[string]struct{}, 0)
// map of network interface names passed in via NICS env var
var nicsMap map[string]struct{}
nics, nicsEnvExists := os.LookupEnv("NICS")
if nicsEnvExists {
nicsMap = make(map[string]struct{}, 0)
for _, nic := range strings.Split(nics, ",") {
nicsMap[nic] = struct{}{}
}
}
// reset network I/O stats
a.netIoStats.BytesSent = 0
a.netIoStats.BytesRecv = 0
// get intial network I/O stats
if netIO, err := psutilNet.IOCounters(true); err == nil {
a.netIoStats.Time = time.Now()
for _, v := range netIO {
if a.skipNetworkInterface(v) {
continue
switch {
// skip if nics exists and the interface is not in the list
case nicsEnvExists:
if _, nameInNics := nicsMap[v.Name]; !nameInNics {
continue
}
// otherwise run the interface name through the skipNetworkInterface function
default:
if a.skipNetworkInterface(v) {
continue
}
}
log.Printf("Detected network interface: %+v (%+v recv, %+v sent)\n", v.Name, v.BytesRecv, v.BytesSent)
a.netIoStats.BytesSent += v.BytesSent
@@ -567,8 +582,7 @@ func twoDecimals(value float64) float64 {
func (a *Agent) skipNetworkInterface(v psutilNet.IOCountersStat) bool {
switch {
case a.nic != "" && v.Name != a.nic,
strings.HasPrefix(v.Name, "lo"),
case strings.HasPrefix(v.Name, "lo"),
strings.HasPrefix(v.Name, "docker"),
strings.HasPrefix(v.Name, "br-"),
strings.HasPrefix(v.Name, "veth"),