nezha/model/monitor.go

143 lines
3.2 KiB
Go
Raw Permalink Normal View History

2019-12-07 05:14:40 -05:00
package model
2020-11-06 07:56:46 -05:00
import (
"fmt"
"log"
"github.com/robfig/cron/v3"
"gorm.io/gorm"
"github.com/naiba/nezha/pkg/utils"
pb "github.com/naiba/nezha/proto"
2020-11-06 07:56:46 -05:00
)
2019-12-07 05:14:40 -05:00
2019-12-09 03:02:49 -05:00
const (
_ = iota
TaskTypeHTTPGet
TaskTypeICMPPing
TaskTypeTCPPing
TaskTypeCommand
2021-08-17 23:56:54 -04:00
TaskTypeTerminal
TaskTypeUpgrade
TaskTypeKeepalive
TaskTypeTerminalGRPC
2024-07-14 07:41:50 -04:00
TaskTypeNAT
TaskTypeReportHostInfo
TaskTypeFM
2019-12-09 03:02:49 -05:00
)
2021-08-17 23:56:54 -04:00
type TerminalTask struct {
StreamID string
2021-08-17 23:56:54 -04:00
}
2024-07-14 07:41:50 -04:00
type TaskNAT struct {
StreamID string
Host string
}
type TaskFM struct {
StreamID string
}
const (
MonitorCoverAll = iota
MonitorCoverIgnoreAll
)
type Monitor struct {
Common
Name string
Type uint8
Target string
SkipServersRaw string
Duration uint64
Notify bool
NotificationTag string // 当前服务监控所属的通知组
Cover uint8
2023-04-15 07:04:38 -04:00
EnableTriggerTask bool `gorm:"default: false"`
EnableShowInService bool `gorm:"default: false"`
2023-04-15 07:04:38 -04:00
FailTriggerTasksRaw string `gorm:"default:'[]'"`
RecoverTriggerTasksRaw string `gorm:"default:'[]'"`
FailTriggerTasks []uint64 `gorm:"-" json:"-"` // 失败时执行的触发任务id
RecoverTriggerTasks []uint64 `gorm:"-" json:"-"` // 恢复时执行的触发任务id
2022-09-16 22:30:32 -04:00
MinLatency float32
MaxLatency float32
LatencyNotify bool
SkipServers map[uint64]bool `gorm:"-" json:"-"`
CronJobID cron.EntryID `gorm:"-" json:"-"`
2019-12-07 05:14:40 -05:00
}
func (m *Monitor) PB() *pb.Task {
return &pb.Task{
Id: m.ID,
Type: uint64(m.Type),
Data: m.Target,
2019-12-09 05:14:31 -05:00
}
}
2022-04-11 10:51:02 -04:00
// CronSpec 返回服务监控请求间隔对应的 cron 表达式
func (m *Monitor) CronSpec() string {
if m.Duration == 0 {
// 默认间隔 30 秒
m.Duration = 30
}
return fmt.Sprintf("@every %ds", m.Duration)
}
2023-04-15 07:04:38 -04:00
func (m *Monitor) BeforeSave(tx *gorm.DB) error {
if data, err := utils.Json.Marshal(m.FailTriggerTasks); err != nil {
return err
} else {
m.FailTriggerTasksRaw = string(data)
}
if data, err := utils.Json.Marshal(m.RecoverTriggerTasks); err != nil {
return err
} else {
m.RecoverTriggerTasksRaw = string(data)
}
return nil
}
func (m *Monitor) AfterFind(tx *gorm.DB) error {
m.SkipServers = make(map[uint64]bool)
var skipServers []uint64
if err := utils.Json.Unmarshal([]byte(m.SkipServersRaw), &skipServers); err != nil {
log.Println("NEZHA>> Monitor.AfterFind:", err)
return nil
}
for i := 0; i < len(skipServers); i++ {
m.SkipServers[skipServers[i]] = true
}
2023-04-15 07:04:38 -04:00
// 加载触发任务列表
if err := utils.Json.Unmarshal([]byte(m.FailTriggerTasksRaw), &m.FailTriggerTasks); err != nil {
return err
}
if err := utils.Json.Unmarshal([]byte(m.RecoverTriggerTasksRaw), &m.RecoverTriggerTasks); err != nil {
return err
}
return nil
}
2021-09-27 09:18:09 -04:00
2022-04-11 10:51:02 -04:00
// IsServiceSentinelNeeded 判断该任务类型是否需要进行服务监控 需要则返回true
2021-09-27 09:18:09 -04:00
func IsServiceSentinelNeeded(t uint64) bool {
return t != TaskTypeCommand && t != TaskTypeTerminalGRPC && t != TaskTypeUpgrade
2021-09-27 09:18:09 -04:00
}
func (m *Monitor) InitSkipServers() error {
var skipServers []uint64
if err := utils.Json.Unmarshal([]byte(m.SkipServersRaw), &skipServers); err != nil {
return err
}
m.SkipServers = make(map[uint64]bool)
for i := 0; i < len(skipServers); i++ {
m.SkipServers[skipServers[i]] = true
}
return nil
}