mirror of
https://github.com/Buriburizaem0n/nezha_domains.git
synced 2026-02-04 12:40:07 +00:00
添加代码注释+修正一个typo
This commit is contained in:
@@ -43,6 +43,7 @@ func (r *AlertRule) Enabled() bool {
|
||||
return r.Enable != nil && *r.Enable
|
||||
}
|
||||
|
||||
// Snapshot 对传入的Server进行该报警规则下所有type的检查 返回包含每项检查结果的空接口
|
||||
func (r *AlertRule) Snapshot(cycleTransferStats *CycleTransferStats, server *Server, db *gorm.DB) []interface{} {
|
||||
var point []interface{}
|
||||
for i := 0; i < len(r.Rules); i++ {
|
||||
@@ -51,9 +52,10 @@ func (r *AlertRule) Snapshot(cycleTransferStats *CycleTransferStats, server *Ser
|
||||
return point
|
||||
}
|
||||
|
||||
// Check 传入包含当前报警规则下所有type检查结果的空接口 返回报警持续时间与是否通过报警检查(通过则返回true)
|
||||
func (r *AlertRule) Check(points [][]interface{}) (int, bool) {
|
||||
var max int
|
||||
var count int
|
||||
var max int // 报警持续时间
|
||||
var count int // 检查未通过的个数
|
||||
for i := 0; i < len(r.Rules); i++ {
|
||||
if r.Rules[i].IsTransferDurationRule() {
|
||||
// 循环区间流量报警
|
||||
@@ -83,11 +85,13 @@ func (r *AlertRule) Check(points [][]interface{}) (int, bool) {
|
||||
fail++
|
||||
}
|
||||
}
|
||||
// 当70%以上的采样点未通过规则判断时 才认为当前检查未通过
|
||||
if fail/total > 0.7 {
|
||||
count++
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
// 仅当所有检查均未通过时 返回false
|
||||
return max, count != len(r.Rules)
|
||||
}
|
||||
|
||||
@@ -48,8 +48,9 @@ func (c *AgentConfig) Save() error {
|
||||
return ioutil.WriteFile(c.v.ConfigFileUsed(), data, os.ModePerm)
|
||||
}
|
||||
|
||||
// Config 站点配置
|
||||
type Config struct {
|
||||
Debug bool
|
||||
Debug bool // debug模式开关
|
||||
Site struct {
|
||||
Brand string // 站点名称
|
||||
CookieName string // 浏览器 Cookie 名称
|
||||
@@ -73,13 +74,14 @@ type Config struct {
|
||||
EnablePlainIPInNotification bool
|
||||
|
||||
// IP变更提醒
|
||||
Cover uint8 // 覆盖范围
|
||||
IgnoredIPNotification string // 特定服务器
|
||||
Cover uint8 // 覆盖范围(0:提醒未被 IgnoredIPNotification 包含的所有服务器; 1:仅提醒被 IgnoredIPNotification 包含的服务器;)
|
||||
IgnoredIPNotification string // 特定服务器IP(多个服务器用逗号分隔)
|
||||
|
||||
v *viper.Viper
|
||||
IgnoredIPNotificationServerIDs map[uint64]bool
|
||||
IgnoredIPNotificationServerIDs map[uint64]bool // [ServerID] -> bool(值为true代表当前ServerID在特定服务器列表内)
|
||||
}
|
||||
|
||||
// Read 读取配置文件并应用
|
||||
func (c *Config) Read(path string) error {
|
||||
c.v = viper.New()
|
||||
c.v.SetConfigFile(path)
|
||||
@@ -101,6 +103,7 @@ func (c *Config) Read(path string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// updateIgnoredIPNotificationID 更新用于判断服务器ID是否属于特定服务器的map
|
||||
func (c *Config) updateIgnoredIPNotificationID() {
|
||||
c.IgnoredIPNotificationServerIDs = make(map[uint64]bool)
|
||||
splitedIDs := strings.Split(c.IgnoredIPNotification, ",")
|
||||
@@ -112,6 +115,7 @@ func (c *Config) updateIgnoredIPNotificationID() {
|
||||
}
|
||||
}
|
||||
|
||||
// Save 保存配置文件
|
||||
func (c *Config) Save() error {
|
||||
c.updateIgnoredIPNotificationID()
|
||||
data, err := yaml.Marshal(c)
|
||||
|
||||
@@ -22,7 +22,7 @@ type Cron struct {
|
||||
PushSuccessful bool // 推送成功的通知
|
||||
LastExecutedAt time.Time // 最后一次执行时间
|
||||
LastResult bool // 最后一次执行结果
|
||||
Cover uint8
|
||||
Cover uint8 // 计划任务覆盖范围 (0:仅覆盖特定服务器 1:仅忽略特定服务器)
|
||||
|
||||
CronJobID cron.EntryID `gorm:"-"`
|
||||
ServersRaw string
|
||||
|
||||
@@ -56,6 +56,7 @@ func (m *Monitor) PB() *pb.Task {
|
||||
}
|
||||
}
|
||||
|
||||
// CronSpec 返回服务监控请求间隔对应的 cron 表达式
|
||||
func (m *Monitor) CronSpec() string {
|
||||
if m.Duration == 0 {
|
||||
// 默认间隔 30 秒
|
||||
@@ -76,6 +77,7 @@ func (m *Monitor) AfterFind(tx *gorm.DB) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// IsServiceSentinelNeeded 判断该任务类型是否需要进行服务监控 需要则返回true
|
||||
func IsServiceSentinelNeeded(t uint64) bool {
|
||||
return t != TaskTypeCommand && t != TaskTypeTerminal && t != TaskTypeUpgrade
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
pb "github.com/naiba/nezha/proto"
|
||||
)
|
||||
|
||||
// MonitorHistory 历史监控记录
|
||||
type MonitorHistory struct {
|
||||
Common
|
||||
MonitorID uint64
|
||||
|
||||
@@ -159,10 +159,12 @@ func (u *Rule) Snapshot(cycleTransferStats *CycleTransferStats, server *Server,
|
||||
return nil
|
||||
}
|
||||
|
||||
// IsTransferDurationRule 判断该规则是否属于周期流量规则 属于则返回true
|
||||
func (rule Rule) IsTransferDurationRule() bool {
|
||||
return strings.HasSuffix(rule.Type, "_cycle")
|
||||
}
|
||||
|
||||
// GetTransferDurationStart 获取周期流量的起始时间
|
||||
func (rule Rule) GetTransferDurationStart() time.Time {
|
||||
// Accept uppercase and lowercase
|
||||
unit := strings.ToLower(rule.CycleUnit)
|
||||
@@ -202,6 +204,7 @@ func (rule Rule) GetTransferDurationStart() time.Time {
|
||||
return startTime
|
||||
}
|
||||
|
||||
// GetTransferDurationEnd 获取周期流量结束时间
|
||||
func (rule Rule) GetTransferDurationEnd() time.Time {
|
||||
// Accept uppercase and lowercase
|
||||
unit := strings.ToLower(rule.CycleUnit)
|
||||
|
||||
Reference in New Issue
Block a user