mirror of
https://github.com/nezhahq/nezha.git
synced 2025-01-22 12:48:14 -05:00
small improvements (#958)
* small improvements * fix: return empty iterator if no json present * use time.Tick * changes
This commit is contained in:
parent
844865e2c1
commit
4b1af369e3
@ -102,7 +102,7 @@ func (r *AlertRule) Check(points [][]bool) (maxDuration int, passed bool) {
|
|||||||
hasPassedRule = true
|
hasPassedRule = true
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
total, fail := 0.0, 0.0
|
total, fail := 0, 0
|
||||||
for timeTick := len(points) - duration; timeTick < len(points); timeTick++ {
|
for timeTick := len(points) - duration; timeTick < len(points); timeTick++ {
|
||||||
total++
|
total++
|
||||||
if !points[timeTick][ruleId] {
|
if !points[timeTick][ruleId] {
|
||||||
@ -110,7 +110,7 @@ func (r *AlertRule) Check(points [][]bool) (maxDuration int, passed bool) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 当70%以上的采样点未通过规则判断时 才认为当前检查未通过
|
// 当70%以上的采样点未通过规则判断时 才认为当前检查未通过
|
||||||
if fail/total <= 0.7 {
|
if fail*100/total <= 70 {
|
||||||
hasPassedRule = true
|
hasPassedRule = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -70,7 +70,7 @@ func (ns *NotificationServerBundle) reqBody(message string) (string, error) {
|
|||||||
return string(msgBytes)[1 : len(msgBytes)-1]
|
return string(msgBytes)[1 : len(msgBytes)-1]
|
||||||
}), nil
|
}), nil
|
||||||
case NotificationRequestTypeForm:
|
case NotificationRequestTypeForm:
|
||||||
data, err := utils.GjsonParseStringMap(n.RequestBody)
|
data, err := utils.GjsonIter(n.RequestBody)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
@ -98,7 +98,7 @@ func (n *Notification) setRequestHeader(req *http.Request) error {
|
|||||||
if n.RequestHeader == "" {
|
if n.RequestHeader == "" {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
m, err := utils.GjsonParseStringMap(n.RequestHeader)
|
m, err := utils.GjsonIter(n.RequestHeader)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -48,11 +48,11 @@ func (provider *Provider) UpdateDomain(ctx context.Context) {
|
|||||||
for _, domain := range provider.DDNSProfile.Domains {
|
for _, domain := range provider.DDNSProfile.Domains {
|
||||||
for retries := 0; retries < int(provider.DDNSProfile.MaxRetries); retries++ {
|
for retries := 0; retries < int(provider.DDNSProfile.MaxRetries); retries++ {
|
||||||
provider.domain = domain
|
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 {
|
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 {
|
} else {
|
||||||
log.Printf("NEZHA>> 尝试更新域名(%s)DDNS成功", provider.domain)
|
log.Printf("NEZHA>> Update DNS record of domain %s succeed", provider.domain)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -77,7 +77,7 @@ func (provider *Provider) prepareRequest(ctx context.Context) (*http.Request, er
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
headers, err := utils.GjsonParseStringMap(
|
headers, err := utils.GjsonIter(
|
||||||
provider.formatWebhookString(provider.DDNSProfile.WebhookHeaders))
|
provider.formatWebhookString(provider.DDNSProfile.WebhookHeaders))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -139,7 +139,7 @@ func (provider *Provider) reqBody() (string, error) {
|
|||||||
case requestTypeJSON:
|
case requestTypeJSON:
|
||||||
return provider.formatWebhookString(provider.DDNSProfile.WebhookRequestBody), nil
|
return provider.formatWebhookString(provider.DDNSProfile.WebhookRequestBody), nil
|
||||||
case requestTypeForm:
|
case requestTypeForm:
|
||||||
data, err := utils.GjsonParseStringMap(provider.DDNSProfile.WebhookRequestBody)
|
data, err := utils.GjsonIter(provider.DDNSProfile.WebhookRequestBody)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@ package utils
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"iter"
|
||||||
|
|
||||||
"github.com/tidwall/gjson"
|
"github.com/tidwall/gjson"
|
||||||
)
|
)
|
||||||
@ -11,6 +12,8 @@ var (
|
|||||||
ErrGjsonWrongType = errors.New("wrong type")
|
ErrGjsonWrongType = errors.New("wrong type")
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var emptyIterator = func(yield func(string, string) bool) {}
|
||||||
|
|
||||||
func GjsonGet(json []byte, path string) (gjson.Result, error) {
|
func GjsonGet(json []byte, path string) (gjson.Result, error) {
|
||||||
result := gjson.GetBytes(json, path)
|
result := gjson.GetBytes(json, path)
|
||||||
if !result.Exists() {
|
if !result.Exists() {
|
||||||
@ -20,21 +23,19 @@ func GjsonGet(json []byte, path string) (gjson.Result, error) {
|
|||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func GjsonParseStringMap(jsonObject string) (map[string]string, error) {
|
func GjsonIter(json string) (iter.Seq2[string, string], error) {
|
||||||
if jsonObject == "" {
|
if json == "" {
|
||||||
return nil, nil
|
return emptyIterator, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
result := gjson.Parse(jsonObject)
|
result := gjson.Parse(json)
|
||||||
if !result.IsObject() {
|
if !result.IsObject() {
|
||||||
return nil, ErrGjsonWrongType
|
return nil, ErrGjsonWrongType
|
||||||
}
|
}
|
||||||
|
|
||||||
ret := make(map[string]string)
|
return func(yield func(string, string) bool) {
|
||||||
result.ForEach(func(key, value gjson.Result) bool {
|
result.ForEach(func(k, v gjson.Result) bool {
|
||||||
ret[key.String()] = value.String()
|
return yield(k.String(), v.String())
|
||||||
return true
|
})
|
||||||
})
|
}, nil
|
||||||
|
|
||||||
return ret, nil
|
|
||||||
}
|
}
|
||||||
|
@ -239,7 +239,7 @@ func (s *NezhaHandler) ReportGeoIP(c context.Context, r *pb.GeoIP) (*pb.GeoIP, e
|
|||||||
}(provider)
|
}(provider)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
log.Printf("NEZHA>> 获取DDNS配置时发生错误: %v", err)
|
log.Printf("NEZHA>> Failed to retrieve DDNS configuration: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -74,20 +74,19 @@ func AlertSentinelStart() {
|
|||||||
AlertsLock.Unlock()
|
AlertsLock.Unlock()
|
||||||
|
|
||||||
time.Sleep(time.Second * 10)
|
time.Sleep(time.Second * 10)
|
||||||
var lastPrint time.Time
|
lastPrint := time.Now()
|
||||||
var checkCount uint64
|
var checkCount uint64
|
||||||
for {
|
ticker := time.Tick(3 * time.Second) // 3秒钟检查一次
|
||||||
startedAt := time.Now()
|
for startedAt := range ticker {
|
||||||
checkStatus()
|
checkStatus()
|
||||||
checkCount++
|
checkCount++
|
||||||
if lastPrint.Before(startedAt.Add(-1 * time.Hour)) {
|
if lastPrint.Before(startedAt.Add(-1 * time.Hour)) {
|
||||||
if Conf.Debug {
|
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
|
checkCount = 0
|
||||||
lastPrint = startedAt
|
lastPrint = startedAt
|
||||||
}
|
}
|
||||||
time.Sleep(time.Until(startedAt.Add(time.Second * 3))) // 3秒钟检查一次
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -247,7 +247,7 @@ func SendNotification(notificationGroupID uint64, desc string, muteLabel *string
|
|||||||
|
|
||||||
if !flag {
|
if !flag {
|
||||||
if Conf.Debug {
|
if Conf.Debug {
|
||||||
log.Println("NEZHA>> 静音的重复通知:", desc, muteLabel)
|
log.Println("NEZHA>> Muted repeated notification", desc, muteLabel)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -256,7 +256,7 @@ func SendNotification(notificationGroupID uint64, desc string, muteLabel *string
|
|||||||
NotificationsLock.RLock()
|
NotificationsLock.RLock()
|
||||||
defer NotificationsLock.RUnlock()
|
defer NotificationsLock.RUnlock()
|
||||||
for _, n := range NotificationList[notificationGroupID] {
|
for _, n := range NotificationList[notificationGroupID] {
|
||||||
log.Println("NEZHA>> 尝试通知", n.Name)
|
log.Printf("NEZHA>> Try to notify %s", n.Name)
|
||||||
}
|
}
|
||||||
for _, n := range NotificationList[notificationGroupID] {
|
for _, n := range NotificationList[notificationGroupID] {
|
||||||
ns := model.NotificationServerBundle{
|
ns := model.NotificationServerBundle{
|
||||||
@ -268,9 +268,9 @@ func SendNotification(notificationGroupID uint64, desc string, muteLabel *string
|
|||||||
ns.Server = ext[0]
|
ns.Server = ext[0]
|
||||||
}
|
}
|
||||||
if err := ns.Send(desc); err != nil {
|
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 {
|
} else {
|
||||||
log.Println("NEZHA>> 向 ", n.Name, " 发送通知成功:")
|
log.Printf("NEZHA>> Sending notification to %s succeed", n.Name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -359,7 +359,7 @@ func (ss *ServiceSentinel) worker() {
|
|||||||
// 从服务状态汇报管道获取汇报的服务数据
|
// 从服务状态汇报管道获取汇报的服务数据
|
||||||
for r := range ss.serviceReportChannel {
|
for r := range ss.serviceReportChannel {
|
||||||
if ss.Services[r.Data.GetId()] == nil || ss.Services[r.Data.GetId()].ID == 0 {
|
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
|
continue
|
||||||
}
|
}
|
||||||
mh := r.Data
|
mh := r.Data
|
||||||
@ -383,7 +383,7 @@ func (ss *ServiceSentinel) worker() {
|
|||||||
Data: mh.Data,
|
Data: mh.Data,
|
||||||
ServerID: r.Reporter,
|
ServerID: r.Reporter,
|
||||||
}).Error; err != nil {
|
}).Error; err != nil {
|
||||||
log.Println("NEZHA>> 服务监控数据持久化失败:", err)
|
log.Printf("NEZHA>> Failed to save service monitor metrics: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
serviceTcpMap[r.Reporter] = ts
|
serviceTcpMap[r.Reporter] = ts
|
||||||
@ -450,7 +450,7 @@ func (ss *ServiceSentinel) worker() {
|
|||||||
Up: ss.serviceResponseDataStoreCurrentUp[mh.GetId()],
|
Up: ss.serviceResponseDataStoreCurrentUp[mh.GetId()],
|
||||||
Down: ss.serviceResponseDataStoreCurrentDown[mh.GetId()],
|
Down: ss.serviceResponseDataStoreCurrentDown[mh.GetId()],
|
||||||
}).Error; err != nil {
|
}).Error; err != nil {
|
||||||
log.Println("NEZHA>> 服务监控数据持久化失败:", err)
|
log.Printf("NEZHA>> Failed to save service monitor metrics: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -112,7 +112,7 @@ func RecordTransferHourlyUsage() {
|
|||||||
if len(txs) == 0 {
|
if len(txs) == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.Println("NEZHA>> Cron 流量统计入库", len(txs), DB.Create(txs).Error)
|
log.Printf("NEZHA>> Saved traffic metrics to database. Affected %d row(s), Error: %v", len(txs), DB.Create(txs).Error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// CleanServiceHistory 清理无效或过时的 监控记录 和 流量记录
|
// CleanServiceHistory 清理无效或过时的 监控记录 和 流量记录
|
||||||
|
Loading…
Reference in New Issue
Block a user