Skip to content

Commit

Permalink
use time.Tick
Browse files Browse the repository at this point in the history
  • Loading branch information
uubulb committed Jan 16, 2025
1 parent 2df72c2 commit fb685b9
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 20 deletions.
8 changes: 4 additions & 4 deletions model/alertrule.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,15 @@ func (r *AlertRule) Check(points [][]bool) (maxDuration int, passed bool) {
hasPassedRule = true
continue
}
total, fail := 0.0, 0.0
for timeTick := len(points) - duration; timeTick < len(points); timeTick++ {
total, fail := 0, 0
for _, point := range points[len(points)-duration:] {
total++
if !points[timeTick][ruleId] {
if !point[ruleId] {
fail++
}
}
// 当70%以上的采样点未通过规则判断时 才认为当前检查未通过
if fail/total <= 0.7 {
if fail*100/total*100 <= 70 {
hasPassedRule = true
}
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/ddns/ddns.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ func (provider *Provider) UpdateDomain(ctx context.Context) {
for _, domain := range provider.DDNSProfile.Domains {
for retries := 0; retries < int(provider.DDNSProfile.MaxRetries); retries++ {
provider.domain = domain
log.Printf("NEZHA>> 正在尝试更新域名(%s)DDNS(%d/%d)", provider.domain, retries+1, provider.DDNSProfile.MaxRetries)
log.Printf("NEZHA>> Updating DNS Record of domain %s: %d/%d", provider.domain, retries+1, provider.DDNSProfile.MaxRetries)
if err := provider.updateDomain(); err != nil {
log.Printf("NEZHA>> 尝试更新域名(%s)DDNS失败: %v", provider.domain, err)
log.Printf("NEZHA>> Failed to update DNS record of domain %s: %v", provider.domain, err)
} else {
log.Printf("NEZHA>> 尝试更新域名(%s)DDNS成功", provider.domain)
log.Printf("NEZHA>> Update DNS record of domain %s succeed", provider.domain)
break
}
}
Expand Down
2 changes: 1 addition & 1 deletion service/rpc/nezha.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ func (s *NezhaHandler) ReportGeoIP(c context.Context, r *pb.GeoIP) (*pb.GeoIP, e
}(provider)
}
} else {
log.Printf("NEZHA>> 获取DDNS配置时发生错误: %v", err)
log.Printf("NEZHA>> Failed to retrieve DDNS configuration: %v", err)
}
}

Expand Down
8 changes: 4 additions & 4 deletions service/singleton/alertsentinel.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,20 +74,20 @@ func AlertSentinelStart() {
AlertsLock.Unlock()

time.Sleep(time.Second * 10)
var lastPrint time.Time
lastPrint := time.Now()
var checkCount uint64
ticker := time.Tick(3 * time.Second) // 3秒钟检查一次
for {
startedAt := time.Now()
startedAt := <-ticker
checkStatus()
checkCount++
if lastPrint.Before(startedAt.Add(-1 * time.Hour)) {
if Conf.Debug {
log.Println("NEZHA>> 报警规则检测每小时", checkCount, "次", startedAt, time.Now())
log.Printf("NEZHA>> Checking alert rules %d times each hour %v %v", checkCount, startedAt, time.Now())
}
checkCount = 0
lastPrint = startedAt
}
time.Sleep(time.Until(startedAt.Add(time.Second * 3))) // 3秒钟检查一次
}
}

Expand Down
8 changes: 4 additions & 4 deletions service/singleton/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ func SendNotification(notificationGroupID uint64, desc string, muteLabel *string

if !flag {
if Conf.Debug {
log.Println("NEZHA>> 静音的重复通知:", desc, muteLabel)
log.Println("NEZHA>> Muted repeated notification", desc, muteLabel)
}
return
}
Expand All @@ -256,7 +256,7 @@ func SendNotification(notificationGroupID uint64, desc string, muteLabel *string
NotificationsLock.RLock()
defer NotificationsLock.RUnlock()
for _, n := range NotificationList[notificationGroupID] {
log.Println("NEZHA>> 尝试通知", n.Name)
log.Printf("NEZHA>> Try to notify %s", n.Name)
}
for _, n := range NotificationList[notificationGroupID] {
ns := model.NotificationServerBundle{
Expand All @@ -268,9 +268,9 @@ func SendNotification(notificationGroupID uint64, desc string, muteLabel *string
ns.Server = ext[0]
}
if err := ns.Send(desc); err != nil {
log.Println("NEZHA>> ", n.Name, " 发送通知失败:", err)
log.Printf("NEZHA>> Sending notification to %s failed: %v", n.Name, err)
} else {
log.Println("NEZHA>> ", n.Name, " 发送通知成功:")
log.Printf("NEZHA>> Sending notification to %s succeed", n.Name)
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions service/singleton/servicesentinel.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ func (ss *ServiceSentinel) worker() {
// 从服务状态汇报管道获取汇报的服务数据
for r := range ss.serviceReportChannel {
if ss.Services[r.Data.GetId()] == nil || ss.Services[r.Data.GetId()].ID == 0 {
log.Printf("NEZHA>> 错误的服务监控上报 %+v", r)
log.Printf("NEZHA>> Incorrect service monitor report %+v", r)
continue
}
mh := r.Data
Expand All @@ -383,7 +383,7 @@ func (ss *ServiceSentinel) worker() {
Data: mh.Data,
ServerID: r.Reporter,
}).Error; err != nil {
log.Println("NEZHA>> 服务监控数据持久化失败:", err)
log.Printf("NEZHA>> Failed to save service monitor metrics: %v", err)
}
}
serviceTcpMap[r.Reporter] = ts
Expand Down Expand Up @@ -450,7 +450,7 @@ func (ss *ServiceSentinel) worker() {
Up: ss.serviceResponseDataStoreCurrentUp[mh.GetId()],
Down: ss.serviceResponseDataStoreCurrentDown[mh.GetId()],
}).Error; err != nil {
log.Println("NEZHA>> 服务监控数据持久化失败:", err)
log.Printf("NEZHA>> Failed to save service monitor metrics: %v", err)
}
}

Expand Down
2 changes: 1 addition & 1 deletion service/singleton/singleton.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func RecordTransferHourlyUsage() {
if len(txs) == 0 {
return
}
log.Println("NEZHA>> Cron 流量统计入库", len(txs), DB.Create(txs).Error)
log.Printf("NEZHA>> Recorded traffic metrics. Affected %d server(s), Error: %v", len(txs), DB.Create(txs).Error)
}

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

0 comments on commit fb685b9

Please sign in to comment.