2019-12-07 18:14:40 +08:00
|
|
|
package model
|
|
|
|
|
2020-11-06 20:56:46 +08:00
|
|
|
import (
|
2021-09-02 23:45:21 +08:00
|
|
|
"fmt"
|
2022-05-02 13:36:49 +08:00
|
|
|
"log"
|
2021-04-22 21:53:31 +08:00
|
|
|
|
2021-09-02 23:45:21 +08:00
|
|
|
"github.com/robfig/cron/v3"
|
2021-04-22 21:53:31 +08:00
|
|
|
"gorm.io/gorm"
|
2024-02-12 14:16:04 +08:00
|
|
|
|
|
|
|
"github.com/naiba/nezha/pkg/utils"
|
|
|
|
pb "github.com/naiba/nezha/proto"
|
2020-11-06 20:56:46 +08:00
|
|
|
)
|
2019-12-07 18:14:40 +08:00
|
|
|
|
2019-12-09 16:02:49 +08:00
|
|
|
const (
|
|
|
|
_ = iota
|
2024-08-02 19:41:39 +08:00
|
|
|
TaskTypeHTTPGet
|
2021-01-19 09:59:04 +08:00
|
|
|
TaskTypeICMPPing
|
|
|
|
TaskTypeTCPPing
|
|
|
|
TaskTypeCommand
|
2021-08-18 11:56:54 +08:00
|
|
|
TaskTypeTerminal
|
2021-09-27 21:24:04 +08:00
|
|
|
TaskTypeUpgrade
|
2021-11-11 12:49:54 +08:00
|
|
|
TaskTypeKeepalive
|
2024-07-14 12:47:36 +08:00
|
|
|
TaskTypeTerminalGRPC
|
2024-07-14 19:41:50 +08:00
|
|
|
TaskTypeNAT
|
2024-08-02 19:41:39 +08:00
|
|
|
TaskTypeReportHostInfo
|
2024-08-20 22:25:29 +08:00
|
|
|
TaskTypeFM
|
2019-12-09 16:02:49 +08:00
|
|
|
)
|
|
|
|
|
2021-08-18 11:56:54 +08:00
|
|
|
type TerminalTask struct {
|
2024-07-14 12:47:36 +08:00
|
|
|
StreamID string
|
2021-08-18 11:56:54 +08:00
|
|
|
}
|
|
|
|
|
2024-07-14 19:41:50 +08:00
|
|
|
type TaskNAT struct {
|
|
|
|
StreamID string
|
|
|
|
Host string
|
|
|
|
}
|
|
|
|
|
2024-08-20 22:25:29 +08:00
|
|
|
type TaskFM struct {
|
|
|
|
StreamID string
|
|
|
|
}
|
|
|
|
|
2021-06-21 21:30:42 +08:00
|
|
|
const (
|
2024-10-25 00:13:45 +08:00
|
|
|
ServiceCoverAll = iota
|
|
|
|
ServiceCoverIgnoreAll
|
2021-06-21 21:30:42 +08:00
|
|
|
)
|
|
|
|
|
2024-10-25 00:13:45 +08:00
|
|
|
type Service struct {
|
2021-01-16 00:45:49 +08:00
|
|
|
Common
|
2024-11-16 21:27:39 +08:00
|
|
|
Name string `json:"name"`
|
|
|
|
Type uint8 `json:"type"`
|
|
|
|
Target string `json:"target"`
|
2024-10-23 23:06:11 +08:00
|
|
|
SkipServersRaw string `json:"-"`
|
2024-11-16 21:27:39 +08:00
|
|
|
Duration uint64 `json:"duration"`
|
2024-10-23 23:06:11 +08:00
|
|
|
Notify bool `json:"notify,omitempty"`
|
2024-11-16 21:27:39 +08:00
|
|
|
NotificationGroupID uint64 `json:"notification_group_id"` // 当前服务监控所属的通知组 ID
|
|
|
|
Cover uint8 `json:"cover"`
|
2024-10-23 23:06:11 +08:00
|
|
|
|
|
|
|
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 20:57:03 +08:00
|
|
|
MinLatency float32 `json:"min_latency"`
|
|
|
|
MaxLatency float32 `json:"max_latency"`
|
2024-10-23 23:06:11 +08:00
|
|
|
LatencyNotify bool `json:"latency_notify,omitempty"`
|
|
|
|
|
|
|
|
SkipServers map[uint64]bool `gorm:"-" json:"skip_servers"`
|
2021-09-02 23:45:21 +08:00
|
|
|
CronJobID cron.EntryID `gorm:"-" json:"-"`
|
2019-12-07 18:14:40 +08:00
|
|
|
}
|
|
|
|
|
2024-10-25 00:13:45 +08:00
|
|
|
func (m *Service) PB() *pb.Task {
|
2021-01-16 00:45:49 +08:00
|
|
|
return &pb.Task{
|
|
|
|
Id: m.ID,
|
|
|
|
Type: uint64(m.Type),
|
|
|
|
Data: m.Target,
|
2019-12-09 18:14:31 +08:00
|
|
|
}
|
|
|
|
}
|
2021-04-22 21:53:31 +08:00
|
|
|
|
2022-04-11 22:51:02 +08:00
|
|
|
// CronSpec 返回服务监控请求间隔对应的 cron 表达式
|
2024-10-25 00:13:45 +08:00
|
|
|
func (m *Service) CronSpec() string {
|
2021-09-02 23:45:21 +08:00
|
|
|
if m.Duration == 0 {
|
|
|
|
// 默认间隔 30 秒
|
|
|
|
m.Duration = 30
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("@every %ds", m.Duration)
|
|
|
|
}
|
|
|
|
|
2024-10-25 00:13:45 +08:00
|
|
|
func (m *Service) BeforeSave(tx *gorm.DB) error {
|
2024-10-23 23:06:11 +08:00
|
|
|
if data, err := utils.Json.Marshal(m.SkipServers); err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
m.SkipServersRaw = string(data)
|
|
|
|
}
|
2023-04-15 19:04:38 +08: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-25 00:13:45 +08:00
|
|
|
func (m *Service) AfterFind(tx *gorm.DB) error {
|
2022-05-02 13:36:49 +08:00
|
|
|
m.SkipServers = make(map[uint64]bool)
|
2024-10-23 23:06:11 +08:00
|
|
|
if err := utils.Json.Unmarshal([]byte(m.SkipServersRaw), &m.SkipServers); err != nil {
|
2024-10-25 00:13:45 +08:00
|
|
|
log.Println("NEZHA>> Service.AfterFind:", err)
|
2022-05-02 13:36:49 +08:00
|
|
|
return nil
|
2021-04-22 21:53:31 +08:00
|
|
|
}
|
2023-04-15 19:04:38 +08: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
|
|
|
|
}
|
|
|
|
|
2021-04-22 21:53:31 +08:00
|
|
|
return nil
|
|
|
|
}
|
2021-09-27 21:18:09 +08:00
|
|
|
|
2022-04-11 22:51:02 +08:00
|
|
|
// IsServiceSentinelNeeded 判断该任务类型是否需要进行服务监控 需要则返回true
|
2021-09-27 21:18:09 +08:00
|
|
|
func IsServiceSentinelNeeded(t uint64) bool {
|
2024-07-17 10:06:23 +08:00
|
|
|
return t != TaskTypeCommand && t != TaskTypeTerminalGRPC && t != TaskTypeUpgrade
|
2021-09-27 21:18:09 +08:00
|
|
|
}
|