🐛 修复区间流量统计

This commit is contained in:
naiba 2021-07-16 18:09:50 +08:00
parent f9cd5ef59f
commit c11e3e2e49
4 changed files with 16 additions and 11 deletions

View File

@ -41,6 +41,7 @@ var (
var (
client pb.NezhaServiceClient
inited bool
updateCh = make(chan struct{}) // Agent 自动更新间隔
httpClient = &http.Client{
Transport: &http.Transport{
@ -99,6 +100,7 @@ func run() {
var conn *grpc.ClientConn
retry := func() {
inited = false
println("Error to close connection ...")
if conn != nil {
conn.Close()
@ -128,6 +130,7 @@ func run() {
continue
}
cancel()
inited = true
// 执行 Task
tasks, err := client.RequestTask(context.Background(), monitor.GetHost().PB())
if err != nil {
@ -266,7 +269,8 @@ func reportState() {
defer println("reportState exit", time.Now(), "=>", err)
for {
now = time.Now()
if client != nil {
// 为了更准确的记录时段流量inited 后再上传状态信息
if client != nil && inited {
monitor.TrackNetworkSpeed()
timeOutCtx, cancel := context.WithTimeout(context.Background(), networkTimeOut)
_, err = client.ReportSystemState(timeOutCtx, monitor.GetState().PB())

View File

@ -162,6 +162,7 @@ func loadCrons() {
}
func main() {
cleanMonitorHistory()
go rpc.ServeRPC(dao.Conf.GRPCPort)
go rpc.DispatchTask(time.Second * 30)
go dao.AlertSentinelStart()

View File

@ -90,21 +90,21 @@ func (u *Rule) Snapshot(server *Server, db *gorm.DB) interface{} {
src = server.State.NetInTransfer - uint64(server.PrevHourlyTransferIn)
if u.CycleInterval != 1 {
var res NResult
db.Model(&Transfer{}).Select("SUM('in') AS n").Where("created_at > ? AND server_id = ?", u.GetTransferDurationStart(), server.ID).Scan(&res)
db.Model(&Transfer{}).Select("SUM(`in`) AS n").Where("created_at > ? AND server_id = ?", u.GetTransferDurationStart(), server.ID).Scan(&res)
src += res.N
}
case "transfer_out_cycle":
src = server.State.NetOutTransfer - uint64(server.PrevHourlyTransferOut)
if u.CycleInterval != 1 {
var res NResult
db.Model(&Transfer{}).Select("SUM('out') AS n").Where("created_at > ? AND server_id = ?", u.GetTransferDurationStart(), server.ID).Scan(&res)
db.Model(&Transfer{}).Select("SUM(`out`) AS n").Where("created_at > ? AND server_id = ?", u.GetTransferDurationStart(), server.ID).Scan(&res)
src += res.N
}
case "transfer_all_cycle":
src = server.State.NetOutTransfer - uint64(server.PrevHourlyTransferOut) + server.State.NetInTransfer - uint64(server.PrevHourlyTransferIn)
if u.CycleInterval != 1 {
var res NResult
db.Model(&Transfer{}).Select("SUM('in'+'out') AS n").Where("created_at > ? AND server_id = ?", u.GetTransferDurationStart(), server.ID).Scan(&res)
db.Model(&Transfer{}).Select("SUM(`in`+`out`) AS n").Where("created_at > ? AND server_id = ?", u.GetTransferDurationStart(), server.ID).Scan(&res)
src += res.N
}
}

View File

@ -73,13 +73,6 @@ func (s *NezhaHandler) ReportSystemState(c context.Context, r *pb.State) (*pb.Re
dao.ServerLock.RLock()
defer dao.ServerLock.RUnlock()
dao.ServerList[clientID].LastActive = time.Now()
// 判断是否是机器重启,如果是机器重启要录入最后记录的流量里面
if state.Uptime < dao.ServerList[clientID].State.Uptime {
dao.ServerList[clientID].PrevHourlyTransferIn = dao.ServerList[clientID].PrevHourlyTransferIn - int64(dao.ServerList[clientID].State.NetInTransfer)
dao.ServerList[clientID].PrevHourlyTransferOut = dao.ServerList[clientID].PrevHourlyTransferOut - int64(dao.ServerList[clientID].State.NetOutTransfer)
}
dao.ServerList[clientID].State = &state
// 如果从未记录过,先打点,等到小时时间点时入库
@ -111,6 +104,13 @@ func (s *NezhaHandler) ReportSystemInfo(c context.Context, r *pb.Host) (*pb.Rece
"IP变更提醒 服务器:%s 旧IP%s新IP%s。",
dao.ServerList[clientID].Name, utils.IPDesensitize(dao.ServerList[clientID].Host.IP), utils.IPDesensitize(host.IP)), true)
}
// 判断是否是机器重启,如果是机器重启要录入最后记录的流量里面
if dao.ServerList[clientID].Host.BootTime < host.BootTime {
dao.ServerList[clientID].PrevHourlyTransferIn = dao.ServerList[clientID].PrevHourlyTransferIn - int64(dao.ServerList[clientID].State.NetInTransfer)
dao.ServerList[clientID].PrevHourlyTransferOut = dao.ServerList[clientID].PrevHourlyTransferOut - int64(dao.ServerList[clientID].State.NetOutTransfer)
}
dao.ServerList[clientID].Host = &host
return &pb.Receipt{Proced: true}, nil
}