mirror of
https://github.com/Buriburizaem0n/nezha_domains.git
synced 2026-02-06 05:30:05 +00:00
🚸 release: v0.2.1
This commit is contained in:
@@ -133,33 +133,8 @@ func checkStatus() {
|
||||
// 发送通知
|
||||
max, desc := alert.Check(alertsStore[alert.ID][server.ID])
|
||||
if desc != "" {
|
||||
nID := getNotificationHash(server, desc)
|
||||
var flag bool
|
||||
if cacheN, has := dao.Cache.Get(nID); has {
|
||||
nHistory := cacheN.(NotificationHistory)
|
||||
// 每次提醒都增加一倍等待时间,最后每天最多提醒一次
|
||||
if time.Now().After(nHistory.Until) {
|
||||
flag = true
|
||||
nHistory.Duration *= 2
|
||||
if nHistory.Duration > time.Hour*24 {
|
||||
nHistory.Duration = time.Hour * 24
|
||||
}
|
||||
nHistory.Until = time.Now().Add(nHistory.Duration)
|
||||
// 缓存有效期加 10 分钟
|
||||
dao.Cache.Set(nID, nHistory, nHistory.Duration+time.Minute*10)
|
||||
}
|
||||
} else {
|
||||
// 新提醒直接通知
|
||||
flag = true
|
||||
dao.Cache.Set(nID, NotificationHistory{
|
||||
Duration: firstNotificationDelay,
|
||||
Until: time.Now().Add(firstNotificationDelay),
|
||||
}, firstNotificationDelay+time.Minute*10)
|
||||
}
|
||||
if flag {
|
||||
message := fmt.Sprintf("报警规则:%s,服务器:%s(%s),%s,逮到咯,快去看看!", alert.Name, server.Name, server.Host.IP, desc)
|
||||
go SendNotification(message)
|
||||
}
|
||||
message := fmt.Sprintf("报警规则:%s,服务器:%s(%s),%s,逮到咯,快去看看!", alert.Name, server.Name, server.Host.IP, desc)
|
||||
go SendNotification(message)
|
||||
}
|
||||
// 清理旧数据
|
||||
if max > 0 && max < len(alertsStore[alert.ID][server.ID]) {
|
||||
@@ -170,13 +145,39 @@ func checkStatus() {
|
||||
}
|
||||
|
||||
func SendNotification(desc string) {
|
||||
// 通知防骚扰策略
|
||||
nID := hex.EncodeToString(md5.New().Sum([]byte(desc)))
|
||||
var flag bool
|
||||
if cacheN, has := dao.Cache.Get(nID); has {
|
||||
nHistory := cacheN.(NotificationHistory)
|
||||
// 每次提醒都增加一倍等待时间,最后每天最多提醒一次
|
||||
if time.Now().After(nHistory.Until) {
|
||||
flag = true
|
||||
nHistory.Duration *= 2
|
||||
if nHistory.Duration > time.Hour*24 {
|
||||
nHistory.Duration = time.Hour * 24
|
||||
}
|
||||
nHistory.Until = time.Now().Add(nHistory.Duration)
|
||||
// 缓存有效期加 10 分钟
|
||||
dao.Cache.Set(nID, nHistory, nHistory.Duration+time.Minute*10)
|
||||
}
|
||||
} else {
|
||||
// 新提醒直接通知
|
||||
flag = true
|
||||
dao.Cache.Set(nID, NotificationHistory{
|
||||
Duration: firstNotificationDelay,
|
||||
Until: time.Now().Add(firstNotificationDelay),
|
||||
}, firstNotificationDelay+time.Minute*10)
|
||||
}
|
||||
|
||||
if !flag {
|
||||
return
|
||||
}
|
||||
|
||||
// 发出通知
|
||||
notificationsLock.RLock()
|
||||
defer notificationsLock.RUnlock()
|
||||
for i := 0; i < len(notifications); i++ {
|
||||
notifications[i].Send(desc)
|
||||
}
|
||||
}
|
||||
|
||||
func getNotificationHash(server *model.Server, desc string) string {
|
||||
return hex.EncodeToString(md5.New().Sum([]byte(fmt.Sprintf("%d::%s", server.ID, desc))))
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package rpc
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/naiba/nezha/model"
|
||||
@@ -21,15 +22,33 @@ func (s *NezhaHandler) ReportTask(c context.Context, r *pb.TaskResult) (*pb.Rece
|
||||
return nil, err
|
||||
}
|
||||
if r.GetType() == model.MonitorTypeHTTPGET {
|
||||
// SSL 证书变更报警
|
||||
// SSL 证书报警
|
||||
var last model.MonitorHistory
|
||||
if err := dao.DB.Where("monitor_id = ?", r.GetId()).Order("id DESC").First(&last).Error; err == nil {
|
||||
if last.Data != "" && last.Data != r.GetData() {
|
||||
var errMsg string
|
||||
if strings.HasPrefix(r.GetData(), "SSL证书错误:") {
|
||||
// 证书错误提醒
|
||||
errMsg = r.GetData()
|
||||
} else {
|
||||
var splits = strings.Split(r.GetData(), "|")
|
||||
// 证书变更提醒
|
||||
if last.Data != "" && last.Data != splits[0] {
|
||||
errMsg = fmt.Sprintf(
|
||||
"SSL证书变更,旧:%s,新:%s。",
|
||||
last.Data, splits[0])
|
||||
}
|
||||
expires, err := time.Parse("2006-01-02 15:04:05 -0700 MST", splits[1])
|
||||
// 证书过期提醒
|
||||
if err == nil && expires.Before(time.Now().AddDate(0, 0, 7)) {
|
||||
errMsg = fmt.Sprintf(
|
||||
"SSL证书将在七天内过期,过期时间:%s。",
|
||||
expires.Format("2006-01-02 15:04:05"))
|
||||
}
|
||||
}
|
||||
if errMsg != "" {
|
||||
var monitor model.Monitor
|
||||
dao.DB.First(&monitor, "id = ?", last.MonitorID)
|
||||
alertmanager.SendNotification(fmt.Sprintf(
|
||||
"监控:%s SSL证书变更,旧:%s,新:%s。",
|
||||
monitor.Name, last.Data, r.GetData()))
|
||||
alertmanager.SendNotification(fmt.Sprintf("服务监控:%s %s", monitor.Name, errMsg))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -38,12 +57,6 @@ func (s *NezhaHandler) ReportTask(c context.Context, r *pb.TaskResult) (*pb.Rece
|
||||
if err := dao.DB.Create(&mh).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// 更新最后检测时间
|
||||
var m model.Monitor
|
||||
m.ID = r.GetId()
|
||||
if err := dao.DB.Model(&m).Update("last_check", time.Now()).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &pb.Receipt{Proced: true}, nil
|
||||
}
|
||||
|
||||
@@ -93,7 +106,7 @@ func (s *NezhaHandler) ReportSystemInfo(c context.Context, r *pb.Host) (*pb.Rece
|
||||
host.IP != "" &&
|
||||
dao.ServerList[clientID].Host.IP != host.IP {
|
||||
alertmanager.SendNotification(fmt.Sprintf(
|
||||
"服务器:%s IP变更提醒,旧IP:%s,新IP:%s。",
|
||||
"IP变更提醒 服务器:%s ,旧IP:%s,新IP:%s。",
|
||||
dao.ServerList[clientID].Name, dao.ServerList[clientID].Host.IP, host.IP))
|
||||
}
|
||||
dao.ServerList[clientID].Host = &host
|
||||
|
||||
Reference in New Issue
Block a user