mirror of
https://github.com/fankes/beszel.git
synced 2025-10-19 17:59:28 +08:00
change NIC env var to NICS to support multiple interfaces
This commit is contained in:
@@ -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,16 +528,37 @@ 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 {
|
||||
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
|
||||
a.netIoStats.BytesRecv += v.BytesRecv
|
||||
@@ -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"),
|
||||
|
Reference in New Issue
Block a user