diff --git a/cmd/agent/main.go b/cmd/agent/main.go index 51110d5..9d0c87e 100644 --- a/cmd/agent/main.go +++ b/cmd/agent/main.go @@ -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()) diff --git a/cmd/dashboard/main.go b/cmd/dashboard/main.go index 79c50d8..7328fa8 100644 --- a/cmd/dashboard/main.go +++ b/cmd/dashboard/main.go @@ -162,6 +162,7 @@ func loadCrons() { } func main() { + cleanMonitorHistory() go rpc.ServeRPC(dao.Conf.GRPCPort) go rpc.DispatchTask(time.Second * 30) go dao.AlertSentinelStart() diff --git a/model/rule.go b/model/rule.go index 312ad15..9e049bc 100644 --- a/model/rule.go +++ b/model/rule.go @@ -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 } } diff --git a/service/rpc/nezha.go b/service/rpc/nezha.go index 7308b30..8dcc02f 100644 --- a/service/rpc/nezha.go +++ b/service/rpc/nezha.go @@ -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 }