Skip to content

Commit 8345689

Browse files
committed
Update.
1 parent 5f2a3ad commit 8345689

3 files changed

Lines changed: 65 additions & 32 deletions

File tree

model/alertrule.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,15 @@ const (
1313
)
1414

1515
type CycleTransferStats struct {
16-
Name string
17-
From time.Time
18-
To time.Time
19-
Max uint64
20-
Min uint64
21-
ServerName map[uint64]string
22-
Transfer map[uint64]uint64
23-
NextUpdate map[uint64]time.Time
16+
Name string
17+
From time.Time
18+
To time.Time
19+
Max uint64
20+
Min uint64
21+
ServerName map[uint64]string
22+
Transfer map[uint64]uint64
23+
NextUpdate map[uint64]time.Time // 下次检查时间
24+
LastResetTime map[uint64]time.Time // 上次重置时间(用于判断是否需要重置)
2425
}
2526

2627
type AlertRule struct {

service/rpc/server.go

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -972,16 +972,17 @@ func checkAndResetCycleTraffic(clientID uint64) {
972972
currentCycleStart := transferRule.GetTransferDurationStart()
973973
currentCycleEnd := transferRule.GetTransferDurationEnd()
974974

975-
// 读取上次重置参考时间(仍在读锁下,随后立即释放)
975+
// 读取上次重置时间(仍在读锁下,随后立即释放)
976976
lastResetTime := time.Time{}
977-
hasNextUpdate := false
977+
hasResetRecord := false
978978
if stats, exists := singleton.AlertsCycleTransferStatsStore[matchingAlert.ID]; exists && stats != nil {
979-
if nu, has := stats.NextUpdate[clientID]; has {
980-
hasNextUpdate = true
981-
// 如果NextUpdate在当前周期开始之前,说明是上个周期的记录,可以作为重置参考
982-
if nu.Before(currentCycleStart) {
983-
lastResetTime = nu
984-
}
979+
// 确保LastResetTime map已初始化
980+
if stats.LastResetTime == nil {
981+
stats.LastResetTime = make(map[uint64]time.Time)
982+
}
983+
if resetTime, has := stats.LastResetTime[clientID]; has {
984+
hasResetRecord = true
985+
lastResetTime = resetTime
985986
}
986987
}
987988
singleton.AlertsLock.RUnlock()
@@ -990,9 +991,35 @@ func checkAndResetCycleTraffic(clientID uint64) {
990991
needReset := false
991992
now := time.Now()
992993

993-
// 重置条件:有NextUpdate记录且lastResetTime在当前周期之前
994-
if hasNextUpdate && now.After(currentCycleStart) && !lastResetTime.IsZero() && lastResetTime.Before(currentCycleStart) {
995-
needReset = true
994+
// 重置条件:
995+
// - 如果从未重置过(!hasResetRecord),且当前时间在周期内,则首次初始化,不重置
996+
// - 如果有重置记录,检查lastResetTime是否在当前周期之前
997+
if hasResetRecord {
998+
// 有重置记录,检查是否需要重置
999+
if lastResetTime.Before(currentCycleStart) && now.After(currentCycleStart) {
1000+
needReset = true
1001+
log.Printf("[周期流量重置] 服务器ID=%d 触发重置: 上次重置时间=%s 在当前周期 %s 之前",
1002+
clientID, lastResetTime.Format("2006-01-02 15:04:05"), currentCycleStart.Format("2006-01-02 15:04:05"))
1003+
}
1004+
} else {
1005+
if now.After(currentCycleStart) {
1006+
// 没有重置记录但当前时间已进入周期,说明此前未按时重置,需要立即补偿
1007+
needReset = true
1008+
log.Printf("[周期流量重置] 服务器ID=%d 首次检测发现缺失记录,立即补偿重置。当前周期开始=%s",
1009+
clientID, currentCycleStart.Format("2006-01-02 15:04:05"))
1010+
} else {
1011+
// 周期尚未开始,记录初始化时间,等待周期开始后再重置
1012+
singleton.AlertsLock.Lock()
1013+
if stats, exists := singleton.AlertsCycleTransferStatsStore[matchingAlert.ID]; exists && stats != nil {
1014+
if stats.LastResetTime == nil {
1015+
stats.LastResetTime = make(map[uint64]time.Time)
1016+
}
1017+
stats.LastResetTime[clientID] = now
1018+
log.Printf("[周期流量初始化] 服务器ID=%d 首次记录重置时间: %s", clientID, now.Format("2006-01-02 15:04:05"))
1019+
}
1020+
singleton.AlertsLock.Unlock()
1021+
return
1022+
}
9961023
}
9971024

9981025
if !needReset {
@@ -1045,8 +1072,12 @@ func checkAndResetCycleTraffic(clientID uint64) {
10451072
if stats.Transfer == nil {
10461073
stats.Transfer = make(map[uint64]uint64)
10471074
}
1075+
if stats.LastResetTime == nil {
1076+
stats.LastResetTime = make(map[uint64]time.Time)
1077+
}
10481078
stats.NextUpdate[clientID] = now
10491079
stats.Transfer[clientID] = 0
1080+
stats.LastResetTime[clientID] = now // 更新上次重置时间
10501081
stats.From = currentCycleStart
10511082
stats.To = currentCycleEnd
10521083
}

service/singleton/alertsentinel.go

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,15 @@ func addCycleTransferStatsInfo(alert *model.AlertRule) {
4646
from := alert.Rules[j].GetTransferDurationStart()
4747
to := alert.Rules[j].GetTransferDurationEnd()
4848
AlertsCycleTransferStatsStore[alert.ID] = &model.CycleTransferStats{
49-
Name: alert.Name,
50-
From: from,
51-
To: to,
52-
Max: uint64(alert.Rules[j].Max),
53-
Min: uint64(alert.Rules[j].Min),
54-
ServerName: make(map[uint64]string),
55-
Transfer: make(map[uint64]uint64),
56-
NextUpdate: make(map[uint64]time.Time),
49+
Name: alert.Name,
50+
From: from,
51+
To: to,
52+
Max: uint64(alert.Rules[j].Max),
53+
Min: uint64(alert.Rules[j].Min),
54+
ServerName: make(map[uint64]string),
55+
Transfer: make(map[uint64]uint64),
56+
NextUpdate: make(map[uint64]time.Time),
57+
LastResetTime: make(map[uint64]time.Time),
5758
}
5859
}
5960
}
@@ -350,7 +351,7 @@ func checkStatus() {
350351
}
351352
continue
352353
}
353-
354+
354355
// 初始化每个Rule的字段,避免nil指针
355356
for i := range alert.Rules {
356357
if alert.Rules[i].NextTransferAt == nil {
@@ -372,7 +373,7 @@ func checkStatus() {
372373
}
373374
continue
374375
}
375-
376+
376377
// 安全检查:确保alert不为nil
377378
if alert == nil {
378379
if Conf.Debug {
@@ -409,7 +410,7 @@ func checkStatus() {
409410
// SQLite模式下,传入DB参数
410411
snapshot = alert.Snapshot(cycleStats, server, DB)
411412
}
412-
413+
413414
// 安全检查:确保snapshot不为nil
414415
if snapshot == nil {
415416
snapshot = make([]interface{}, 0)
@@ -1197,7 +1198,7 @@ func generateThresholdAlertMessage(alert *model.AlertRule, server *model.Server,
11971198
}
11981199

11991200
usagePercent := float64(currentUsage) / rule.Max * 100
1200-
1201+
12011202
// 根据实际使用百分比生成动态标题
12021203
var dynamicTitle string
12031204
if usagePercent >= 100 {
@@ -1207,7 +1208,7 @@ func generateThresholdAlertMessage(alert *model.AlertRule, server *model.Server,
12071208
} else {
12081209
dynamicTitle = fmt.Sprintf("%.1f%%流量使用提醒", usagePercent)
12091210
}
1210-
1211+
12111212
message := fmt.Sprintf("%s %s\n", icon, dynamicTitle)
12121213
message += fmt.Sprintf("时间: %s\n", now.Format("2006-01-02 15:04:05"))
12131214
message += fmt.Sprintf("服务器: %s\n", server.Name)

0 commit comments

Comments
 (0)