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 { type Agent struct {
addr string addr string
nic string
pubKey []byte pubKey []byte
sem chan struct{} sem chan struct{}
containerStatsMap map[string]*container.PrevContainerStats containerStatsMap map[string]*container.PrevContainerStats
@@ -434,16 +433,11 @@ func (a *Agent) Run() {
} }
if extraFilesystems, exists := os.LookupEnv("EXTRA_FILESYSTEMS"); exists { if extraFilesystems, exists := os.LookupEnv("EXTRA_FILESYSTEMS"); exists {
// parse comma separated list of filesystems
for _, filesystem := range strings.Split(extraFilesystems, ",") { for _, filesystem := range strings.Split(extraFilesystems, ",") {
a.fsStats[filesystem] = &system.FsStats{} a.fsStats[filesystem] = &system.FsStats{}
} }
} }
if nic, exists := os.LookupEnv("NIC"); exists {
a.nic = nic
}
a.initializeDiskInfo(fsEnvVarExists) a.initializeDiskInfo(fsEnvVarExists)
a.initializeDiskIoStats() a.initializeDiskIoStats()
a.initializeNetIoStats() a.initializeNetIoStats()
@@ -534,16 +528,37 @@ func (a *Agent) initializeDiskIoStats() {
func (a *Agent) initializeNetIoStats() { func (a *Agent) initializeNetIoStats() {
// reset valid network interfaces // reset valid network interfaces
a.netInterfaces = make(map[string]struct{}, 0) 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 // reset network I/O stats
a.netIoStats.BytesSent = 0 a.netIoStats.BytesSent = 0
a.netIoStats.BytesRecv = 0 a.netIoStats.BytesRecv = 0
// get intial network I/O stats // get intial network I/O stats
if netIO, err := psutilNet.IOCounters(true); err == nil { if netIO, err := psutilNet.IOCounters(true); err == nil {
a.netIoStats.Time = time.Now() a.netIoStats.Time = time.Now()
for _, v := range netIO { for _, v := range netIO {
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) { if a.skipNetworkInterface(v) {
continue continue
} }
}
log.Printf("Detected network interface: %+v (%+v recv, %+v sent)\n", v.Name, v.BytesRecv, v.BytesSent) log.Printf("Detected network interface: %+v (%+v recv, %+v sent)\n", v.Name, v.BytesRecv, v.BytesSent)
a.netIoStats.BytesSent += v.BytesSent a.netIoStats.BytesSent += v.BytesSent
a.netIoStats.BytesRecv += v.BytesRecv a.netIoStats.BytesRecv += v.BytesRecv
@@ -567,8 +582,7 @@ func twoDecimals(value float64) float64 {
func (a *Agent) skipNetworkInterface(v psutilNet.IOCountersStat) bool { func (a *Agent) skipNetworkInterface(v psutilNet.IOCountersStat) bool {
switch { switch {
case a.nic != "" && v.Name != a.nic, case strings.HasPrefix(v.Name, "lo"),
strings.HasPrefix(v.Name, "lo"),
strings.HasPrefix(v.Name, "docker"), strings.HasPrefix(v.Name, "docker"),
strings.HasPrefix(v.Name, "br-"), strings.HasPrefix(v.Name, "br-"),
strings.HasPrefix(v.Name, "veth"), strings.HasPrefix(v.Name, "veth"),