Skip to content

Commit da8f83b

Browse files
committed
use time.Tick
1 parent 2df72c2 commit da8f83b

File tree

7 files changed

+20
-20
lines changed

7 files changed

+20
-20
lines changed

model/alertrule.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,15 +102,15 @@ func (r *AlertRule) Check(points [][]bool) (maxDuration int, passed bool) {
102102
hasPassedRule = true
103103
continue
104104
}
105-
total, fail := 0.0, 0.0
106-
for timeTick := len(points) - duration; timeTick < len(points); timeTick++ {
105+
total, fail := 0, 0
106+
for _, point := range points[len(points)-duration:] {
107107
total++
108-
if !points[timeTick][ruleId] {
108+
if !point[ruleId] {
109109
fail++
110110
}
111111
}
112112
// 当70%以上的采样点未通过规则判断时 才认为当前检查未通过
113-
if fail/total <= 0.7 {
113+
if fail*100/total*100 <= 70 {
114114
hasPassedRule = true
115115
}
116116
}

pkg/ddns/ddns.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ func (provider *Provider) UpdateDomain(ctx context.Context) {
4848
for _, domain := range provider.DDNSProfile.Domains {
4949
for retries := 0; retries < int(provider.DDNSProfile.MaxRetries); retries++ {
5050
provider.domain = domain
51-
log.Printf("NEZHA>> 正在尝试更新域名(%s)DDNS(%d/%d)", provider.domain, retries+1, provider.DDNSProfile.MaxRetries)
51+
log.Printf("NEZHA>> Updating DNS Record of domain %s: %d/%d", provider.domain, retries+1, provider.DDNSProfile.MaxRetries)
5252
if err := provider.updateDomain(); err != nil {
53-
log.Printf("NEZHA>> 尝试更新域名(%s)DDNS失败: %v", provider.domain, err)
53+
log.Printf("NEZHA>> Failed to update DNS record of domain %s: %v", provider.domain, err)
5454
} else {
55-
log.Printf("NEZHA>> 尝试更新域名(%s)DDNS成功", provider.domain)
55+
log.Printf("NEZHA>> Update DNS record of domain %s succeed", provider.domain)
5656
break
5757
}
5858
}

service/rpc/nezha.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ func (s *NezhaHandler) ReportGeoIP(c context.Context, r *pb.GeoIP) (*pb.GeoIP, e
239239
}(provider)
240240
}
241241
} else {
242-
log.Printf("NEZHA>> 获取DDNS配置时发生错误: %v", err)
242+
log.Printf("NEZHA>> Failed to retrieve DDNS configuration: %v", err)
243243
}
244244
}
245245

service/singleton/alertsentinel.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,20 +74,20 @@ func AlertSentinelStart() {
7474
AlertsLock.Unlock()
7575

7676
time.Sleep(time.Second * 10)
77-
var lastPrint time.Time
77+
lastPrint := time.Now()
7878
var checkCount uint64
79+
ticker := time.Tick(3 * time.Second) // 3秒钟检查一次
7980
for {
80-
startedAt := time.Now()
81+
startedAt := <-ticker
8182
checkStatus()
8283
checkCount++
8384
if lastPrint.Before(startedAt.Add(-1 * time.Hour)) {
8485
if Conf.Debug {
85-
log.Println("NEZHA>> 报警规则检测每小时", checkCount, "次", startedAt, time.Now())
86+
log.Println("NEZHA>> Checking alert rules %d times each hour %v %v", checkCount, startedAt, time.Now())
8687
}
8788
checkCount = 0
8889
lastPrint = startedAt
8990
}
90-
time.Sleep(time.Until(startedAt.Add(time.Second * 3))) // 3秒钟检查一次
9191
}
9292
}
9393

service/singleton/notification.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ func SendNotification(notificationGroupID uint64, desc string, muteLabel *string
247247

248248
if !flag {
249249
if Conf.Debug {
250-
log.Println("NEZHA>> 静音的重复通知:", desc, muteLabel)
250+
log.Println("NEZHA>> Muted repeated notification", desc, muteLabel)
251251
}
252252
return
253253
}
@@ -256,7 +256,7 @@ func SendNotification(notificationGroupID uint64, desc string, muteLabel *string
256256
NotificationsLock.RLock()
257257
defer NotificationsLock.RUnlock()
258258
for _, n := range NotificationList[notificationGroupID] {
259-
log.Println("NEZHA>> 尝试通知", n.Name)
259+
log.Printf("NEZHA>> Try to notify %s", n.Name)
260260
}
261261
for _, n := range NotificationList[notificationGroupID] {
262262
ns := model.NotificationServerBundle{
@@ -268,9 +268,9 @@ func SendNotification(notificationGroupID uint64, desc string, muteLabel *string
268268
ns.Server = ext[0]
269269
}
270270
if err := ns.Send(desc); err != nil {
271-
log.Println("NEZHA>> ", n.Name, " 发送通知失败:", err)
271+
log.Printf("NEZHA>> Sending notification to %s failed: %v", n.Name, err)
272272
} else {
273-
log.Println("NEZHA>> ", n.Name, " 发送通知成功:")
273+
log.Printf("NEZHA>> Sending notification to %s succeed", n.Name)
274274
}
275275
}
276276
}

service/singleton/servicesentinel.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ func (ss *ServiceSentinel) worker() {
359359
// 从服务状态汇报管道获取汇报的服务数据
360360
for r := range ss.serviceReportChannel {
361361
if ss.Services[r.Data.GetId()] == nil || ss.Services[r.Data.GetId()].ID == 0 {
362-
log.Printf("NEZHA>> 错误的服务监控上报 %+v", r)
362+
log.Printf("NEZHA>> Incorrect service monitor report %+v", r)
363363
continue
364364
}
365365
mh := r.Data
@@ -383,7 +383,7 @@ func (ss *ServiceSentinel) worker() {
383383
Data: mh.Data,
384384
ServerID: r.Reporter,
385385
}).Error; err != nil {
386-
log.Println("NEZHA>> 服务监控数据持久化失败:", err)
386+
log.Printf("NEZHA>> Failed to save service monitor metrics: %v", err)
387387
}
388388
}
389389
serviceTcpMap[r.Reporter] = ts
@@ -450,7 +450,7 @@ func (ss *ServiceSentinel) worker() {
450450
Up: ss.serviceResponseDataStoreCurrentUp[mh.GetId()],
451451
Down: ss.serviceResponseDataStoreCurrentDown[mh.GetId()],
452452
}).Error; err != nil {
453-
log.Println("NEZHA>> 服务监控数据持久化失败:", err)
453+
log.Printf("NEZHA>> Failed to save service monitor metrics: %v", err)
454454
}
455455
}
456456

service/singleton/singleton.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ func RecordTransferHourlyUsage() {
112112
if len(txs) == 0 {
113113
return
114114
}
115-
log.Println("NEZHA>> Cron 流量统计入库", len(txs), DB.Create(txs).Error)
115+
log.Printf("NEZHA>> Recorded traffic metrics. Affected %d server(s), Error: %v", len(txs), DB.Create(txs).Error)
116116
}
117117

118118
// CleanServiceHistory 清理无效或过时的 监控记录 和 流量记录

0 commit comments

Comments
 (0)