🐛 修复区间流量统计
This commit is contained in:
parent
f9cd5ef59f
commit
c11e3e2e49
@ -41,6 +41,7 @@ var (
|
|||||||
|
|
||||||
var (
|
var (
|
||||||
client pb.NezhaServiceClient
|
client pb.NezhaServiceClient
|
||||||
|
inited bool
|
||||||
updateCh = make(chan struct{}) // Agent 自动更新间隔
|
updateCh = make(chan struct{}) // Agent 自动更新间隔
|
||||||
httpClient = &http.Client{
|
httpClient = &http.Client{
|
||||||
Transport: &http.Transport{
|
Transport: &http.Transport{
|
||||||
@ -99,6 +100,7 @@ func run() {
|
|||||||
var conn *grpc.ClientConn
|
var conn *grpc.ClientConn
|
||||||
|
|
||||||
retry := func() {
|
retry := func() {
|
||||||
|
inited = false
|
||||||
println("Error to close connection ...")
|
println("Error to close connection ...")
|
||||||
if conn != nil {
|
if conn != nil {
|
||||||
conn.Close()
|
conn.Close()
|
||||||
@ -128,6 +130,7 @@ func run() {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
cancel()
|
cancel()
|
||||||
|
inited = true
|
||||||
// 执行 Task
|
// 执行 Task
|
||||||
tasks, err := client.RequestTask(context.Background(), monitor.GetHost().PB())
|
tasks, err := client.RequestTask(context.Background(), monitor.GetHost().PB())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -266,7 +269,8 @@ func reportState() {
|
|||||||
defer println("reportState exit", time.Now(), "=>", err)
|
defer println("reportState exit", time.Now(), "=>", err)
|
||||||
for {
|
for {
|
||||||
now = time.Now()
|
now = time.Now()
|
||||||
if client != nil {
|
// 为了更准确的记录时段流量,inited 后再上传状态信息
|
||||||
|
if client != nil && inited {
|
||||||
monitor.TrackNetworkSpeed()
|
monitor.TrackNetworkSpeed()
|
||||||
timeOutCtx, cancel := context.WithTimeout(context.Background(), networkTimeOut)
|
timeOutCtx, cancel := context.WithTimeout(context.Background(), networkTimeOut)
|
||||||
_, err = client.ReportSystemState(timeOutCtx, monitor.GetState().PB())
|
_, err = client.ReportSystemState(timeOutCtx, monitor.GetState().PB())
|
||||||
|
@ -162,6 +162,7 @@ func loadCrons() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
cleanMonitorHistory()
|
||||||
go rpc.ServeRPC(dao.Conf.GRPCPort)
|
go rpc.ServeRPC(dao.Conf.GRPCPort)
|
||||||
go rpc.DispatchTask(time.Second * 30)
|
go rpc.DispatchTask(time.Second * 30)
|
||||||
go dao.AlertSentinelStart()
|
go dao.AlertSentinelStart()
|
||||||
|
@ -90,21 +90,21 @@ func (u *Rule) Snapshot(server *Server, db *gorm.DB) interface{} {
|
|||||||
src = server.State.NetInTransfer - uint64(server.PrevHourlyTransferIn)
|
src = server.State.NetInTransfer - uint64(server.PrevHourlyTransferIn)
|
||||||
if u.CycleInterval != 1 {
|
if u.CycleInterval != 1 {
|
||||||
var res NResult
|
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
|
src += res.N
|
||||||
}
|
}
|
||||||
case "transfer_out_cycle":
|
case "transfer_out_cycle":
|
||||||
src = server.State.NetOutTransfer - uint64(server.PrevHourlyTransferOut)
|
src = server.State.NetOutTransfer - uint64(server.PrevHourlyTransferOut)
|
||||||
if u.CycleInterval != 1 {
|
if u.CycleInterval != 1 {
|
||||||
var res NResult
|
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
|
src += res.N
|
||||||
}
|
}
|
||||||
case "transfer_all_cycle":
|
case "transfer_all_cycle":
|
||||||
src = server.State.NetOutTransfer - uint64(server.PrevHourlyTransferOut) + server.State.NetInTransfer - uint64(server.PrevHourlyTransferIn)
|
src = server.State.NetOutTransfer - uint64(server.PrevHourlyTransferOut) + server.State.NetInTransfer - uint64(server.PrevHourlyTransferIn)
|
||||||
if u.CycleInterval != 1 {
|
if u.CycleInterval != 1 {
|
||||||
var res NResult
|
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
|
src += res.N
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -73,13 +73,6 @@ func (s *NezhaHandler) ReportSystemState(c context.Context, r *pb.State) (*pb.Re
|
|||||||
dao.ServerLock.RLock()
|
dao.ServerLock.RLock()
|
||||||
defer dao.ServerLock.RUnlock()
|
defer dao.ServerLock.RUnlock()
|
||||||
dao.ServerList[clientID].LastActive = time.Now()
|
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
|
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。",
|
"IP变更提醒 服务器:%s ,旧IP:%s,新IP:%s。",
|
||||||
dao.ServerList[clientID].Name, utils.IPDesensitize(dao.ServerList[clientID].Host.IP), utils.IPDesensitize(host.IP)), true)
|
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
|
dao.ServerList[clientID].Host = &host
|
||||||
return &pb.Receipt{Proced: true}, nil
|
return &pb.Receipt{Proced: true}, nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user