nezha/model/service.go

132 lines
3.3 KiB
Go
Raw 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 (
2024-10-24 12:13:45 -04:00
ServiceCoverAll = iota
ServiceCoverIgnoreAll
)
2024-10-24 12:13:45 -04:00
type Service struct {
Common
2024-10-23 11:06:11 -04:00
Name string `json:"name,omitempty"`
Type uint8 `json:"type,omitempty"`
Target string `json:"target,omitempty"`
SkipServersRaw string `json:"-"`
Duration uint64 `json:"duration,omitempty"`
Notify bool `json:"notify,omitempty"`
NotificationGroupID uint64 `json:"notification_group_id,omitempty"` // 当前服务监控所属的通知组 ID
Cover uint8 `json:"cover,omitempty"`
EnableTriggerTask bool `gorm:"default: false" json:"enable_trigger_task,omitempty"`
EnableShowInService bool `gorm:"default: false" json:"enable_show_in_service,omitempty"`
FailTriggerTasksRaw string `gorm:"default:'[]'" json:"-"`
RecoverTriggerTasksRaw string `gorm:"default:'[]'" json:"-"`
FailTriggerTasks []uint64 `gorm:"-" json:"fail_trigger_tasks"` // 失败时执行的触发任务id
RecoverTriggerTasks []uint64 `gorm:"-" json:"recover_trigger_tasks"` // 恢复时执行的触发任务id
2024-11-16 07:57:03 -05:00
MinLatency float32 `json:"min_latency"`
MaxLatency float32 `json:"max_latency"`
2024-10-23 11:06:11 -04:00
LatencyNotify bool `json:"latency_notify,omitempty"`
SkipServers map[uint64]bool `gorm:"-" json:"skip_servers"`
CronJobID cron.EntryID `gorm:"-" json:"-"`
2019-12-07 05:14:40 -05:00
}
2024-10-24 12:13:45 -04:00
func (m *Service) 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 表达式
2024-10-24 12:13:45 -04:00
func (m *Service) CronSpec() string {
if m.Duration == 0 {
// 默认间隔 30 秒
m.Duration = 30
}
return fmt.Sprintf("@every %ds", m.Duration)
}
2024-10-24 12:13:45 -04:00
func (m *Service) BeforeSave(tx *gorm.DB) error {
2024-10-23 11:06:11 -04:00
if data, err := utils.Json.Marshal(m.SkipServers); err != nil {
return err
} else {
m.SkipServersRaw = string(data)
}
2023-04-15 07:04:38 -04:00
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
}
2024-10-24 12:13:45 -04:00
func (m *Service) AfterFind(tx *gorm.DB) error {
m.SkipServers = make(map[uint64]bool)
2024-10-23 11:06:11 -04:00
if err := utils.Json.Unmarshal([]byte(m.SkipServersRaw), &m.SkipServers); err != nil {
2024-10-24 12:13:45 -04:00
log.Println("NEZHA>> Service.AfterFind:", err)
return nil
}
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
}