优化流量、硬盘上报

This commit is contained in:
奶爸 2019-12-13 14:51:51 +08:00
parent b521ba371c
commit a8e02fc9bb
2 changed files with 24 additions and 32 deletions

View File

@ -6,6 +6,7 @@ import (
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"strings" "strings"
"sync/atomic"
"time" "time"
"github.com/shirou/gopsutil/cpu" "github.com/shirou/gopsutil/cpu"
@ -35,12 +36,7 @@ func GetHost() *model.Host {
} }
mv, _ := mem.VirtualMemory() mv, _ := mem.VirtualMemory()
ms, _ := mem.SwapMemory() ms, _ := mem.SwapMemory()
var diskTotal uint64 u, _ := disk.Usage("/")
dparts, _ := disk.Partitions(true)
for _, part := range dparts {
u, _ := disk.Usage(part.Mountpoint)
diskTotal += u.Total
}
var ip ipDotSbGeoIP var ip ipDotSbGeoIP
resp, err := http.Get("https://api.ip.sb/geoip") resp, err := http.Get("https://api.ip.sb/geoip")
if err == nil { if err == nil {
@ -53,7 +49,7 @@ func GetHost() *model.Host {
PlatformVersion: hi.PlatformVersion, PlatformVersion: hi.PlatformVersion,
CPU: cpus, CPU: cpus,
MemTotal: mv.Total, MemTotal: mv.Total,
DiskTotal: diskTotal, DiskTotal: u.Total,
SwapTotal: ms.Total, SwapTotal: ms.Total,
Arch: hi.KernelArch, Arch: hi.KernelArch,
Virtualization: hi.VirtualizationSystem, Virtualization: hi.VirtualizationSystem,
@ -77,22 +73,17 @@ func GetState(delay int64) *model.State {
cpuPercent = cp[0] cpuPercent = cp[0]
} }
// Disk // Disk
var diskUsed uint64 u, _ := disk.Usage("/")
dparts, _ := disk.Partitions(true)
for _, part := range dparts {
u, _ := disk.Usage(part.Mountpoint)
diskUsed += u.Used
}
return &model.State{ return &model.State{
CPU: cpuPercent, CPU: cpuPercent,
MemUsed: mv.Used, MemUsed: mv.Used,
SwapUsed: ms.Used, SwapUsed: ms.Used,
DiskUsed: diskUsed, DiskUsed: u.Used,
NetInTransfer: netInTransfer, NetInTransfer: atomic.LoadUint64(&netInTransfer),
NetOutTransfer: netOutTransfer, NetOutTransfer: atomic.LoadUint64(&netOutTransfer),
NetInSpeed: netInSpeed, NetInSpeed: atomic.LoadUint64(&netInSpeed),
NetOutSpeed: netOutSpeed, NetOutSpeed: atomic.LoadUint64(&netOutSpeed),
Uptime: hi.Uptime, Uptime: hi.Uptime,
} }
} }
@ -104,21 +95,14 @@ func TrackNetworkSpeed() {
if err == nil { if err == nil {
innerNetInTransfer += nc[0].BytesRecv innerNetInTransfer += nc[0].BytesRecv
innerNetOutTransfer += nc[0].BytesSent innerNetOutTransfer += nc[0].BytesSent
if netInTransfer == 0 { now := uint64(time.Now().Unix())
netInTransfer = innerNetInTransfer diff := now - atomic.LoadUint64(&lastUpdate)
}
if netOutTransfer == 0 {
netOutTransfer = innerNetOutTransfer
}
diff := uint64(time.Now().Unix())
if lastUpdate == 0 {
lastUpdate = diff
return
}
diff -= lastUpdate
if diff > 0 { if diff > 0 {
netInSpeed = (innerNetInTransfer - netInTransfer) / diff atomic.StoreUint64(&netInSpeed, (innerNetInTransfer-atomic.LoadUint64(&netInTransfer))/diff)
netOutSpeed = (innerNetOutTransfer - netOutTransfer) / diff atomic.StoreUint64(&netOutSpeed, (innerNetOutTransfer-atomic.LoadUint64(&netOutTransfer))/diff)
} }
atomic.StoreUint64(&netInTransfer, innerNetInTransfer)
atomic.StoreUint64(&netOutTransfer, innerNetOutTransfer)
atomic.StoreUint64(&lastUpdate, now)
} }
} }

View File

@ -35,6 +35,14 @@ func (s *NezhaHandler) Heartbeat(r *pb.Beat, stream pb.NezhaService_HeartbeatSer
if clientID, err = s.Auth.Check(stream.Context()); err != nil { if clientID, err = s.Auth.Check(stream.Context()); err != nil {
return err return err
} }
// 默认连接后先上报10分钟
err = stream.Send(&pb.Command{
Type: model.MTReportState,
})
if err != nil {
return err
}
// 放入在线服务器列表
dao.ServerLock.RLock() dao.ServerLock.RLock()
closeCh := make(chan error) closeCh := make(chan error)
dao.ServerList[clientID].StreamClose = closeCh dao.ServerList[clientID].StreamClose = closeCh