From 5f33249c5d7bd8a57ff24e6c7a2c75ce9d28e0e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=A5=A5=E8=B6=85?= Date: Wed, 17 May 2023 10:48:59 +0800 Subject: [PATCH] =?UTF-8?q?=E7=9B=91=E6=8E=A7=E6=95=B0=E6=8D=AE=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E6=95=B4=E4=BD=93=E6=B5=81=E9=87=8F=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/nodes/node_status_executor.go | 58 +++++++++++++++++++++++--- 1 file changed, 53 insertions(+), 5 deletions(-) diff --git a/internal/nodes/node_status_executor.go b/internal/nodes/node_status_executor.go index d8699b8..431e356 100644 --- a/internal/nodes/node_status_executor.go +++ b/internal/nodes/node_status_executor.go @@ -17,7 +17,9 @@ import ( "github.com/iwind/TeaGo/maps" "github.com/shirou/gopsutil/v3/cpu" "github.com/shirou/gopsutil/v3/disk" + "github.com/shirou/gopsutil/v3/net" "golang.org/x/sys/unix" + "math" "os" "runtime" "strings" @@ -25,12 +27,14 @@ import ( ) type NodeStatusExecutor struct { - isFirstTime bool + isFirstTime bool + lastUpdatedTime time.Time - cpuUpdatedTime time.Time cpuLogicalCount int cpuPhysicalCount int + lastIOCounterStat net.IOCountersStat + apiCallStat *rpc.CallStat ticker *time.Ticker @@ -45,7 +49,7 @@ func NewNodeStatusExecutor() *NodeStatusExecutor { func (this *NodeStatusExecutor) Listen() { this.isFirstTime = true - this.cpuUpdatedTime = time.Now() + this.lastUpdatedTime = time.Now() this.update() events.OnKey(events.EventQuit, this, func() { @@ -119,6 +123,11 @@ func (this *NodeStatusExecutor) update() { this.updateCacheSpace(status) cacheSpaceTR.End() + this.updateAllTraffic(status) + + // 修改更新时间 + this.lastUpdatedTime = time.Now() + status.UpdatedAt = time.Now().Unix() status.Timestamp = status.UpdatedAt @@ -173,8 +182,6 @@ func (this *NodeStatusExecutor) updateCPU(status *nodeconfigs.NodeStatus) { }) if this.cpuLogicalCount == 0 && this.cpuPhysicalCount == 0 { - this.cpuUpdatedTime = time.Now() - status.CPULogicalCount, err = cpu.Counts(true) if err != nil { status.Error = "cpu.Counts(): " + err.Error() @@ -282,3 +289,44 @@ func (this *NodeStatusExecutor) updateCacheSpace(status *nodeconfigs.NodeStatus) "dirs": result, }) } + +// 流量 +func (this *NodeStatusExecutor) updateAllTraffic(status *nodeconfigs.NodeStatus) { + counters, err := net.IOCounters(true) + if err != nil { + remotelogs.Warn("NODE_STATUS_EXECUTOR", err.Error()) + return + } + + var allCounter = net.IOCountersStat{} + for _, counter := range counters { + // 跳过lo + if counter.Name == "lo" { + continue + } + allCounter.BytesRecv += counter.BytesRecv + allCounter.BytesSent += counter.BytesSent + } + if allCounter.BytesSent == 0 && allCounter.BytesRecv == 0 { + return + } + + if this.lastIOCounterStat.BytesSent > 0 { + // 记录监控数据 + if allCounter.BytesSent >= this.lastIOCounterStat.BytesSent && allCounter.BytesRecv >= this.lastIOCounterStat.BytesRecv { + var costSeconds = int(math.Ceil(time.Since(this.lastUpdatedTime).Seconds())) + if costSeconds > 0 { + var bytesSent = allCounter.BytesSent - this.lastIOCounterStat.BytesSent + var bytesRecv = allCounter.BytesRecv - this.lastIOCounterStat.BytesRecv + + monitor.SharedValueQueue.Add(nodeconfigs.NodeValueItemAllTraffic, maps.Map{ + "inBytes": bytesRecv, + "outBytes": bytesSent, + "avgInBytes": bytesRecv / uint64(costSeconds), + "avgOutBytes": bytesSent / uint64(costSeconds), + }) + } + } + } + this.lastIOCounterStat = allCounter +}