2019-12-07 05:14:40 -05:00
|
|
|
package model
|
|
|
|
|
2020-11-06 07:56:46 -05:00
|
|
|
import (
|
2021-09-02 11:45:21 -04:00
|
|
|
"fmt"
|
2022-05-02 01:36:49 -04:00
|
|
|
"log"
|
2021-04-22 09:53:31 -04:00
|
|
|
|
2021-09-02 11:45:21 -04:00
|
|
|
"github.com/robfig/cron/v3"
|
2021-04-22 09:53:31 -04:00
|
|
|
"gorm.io/gorm"
|
2024-02-12 01:16:04 -05:00
|
|
|
|
2024-11-28 06:38:54 -05:00
|
|
|
"github.com/nezhahq/nezha/pkg/utils"
|
|
|
|
pb "github.com/nezhahq/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
|
2024-08-02 07:41:39 -04:00
|
|
|
TaskTypeHTTPGet
|
2021-01-18 20:59:04 -05:00
|
|
|
TaskTypeICMPPing
|
|
|
|
TaskTypeTCPPing
|
|
|
|
TaskTypeCommand
|
2021-08-17 23:56:54 -04:00
|
|
|
TaskTypeTerminal
|
2021-09-27 09:24:04 -04:00
|
|
|
TaskTypeUpgrade
|
2024-12-04 09:46:02 -05:00
|
|
|
TaskTypeKeepalive
|
2024-07-14 00:47:36 -04:00
|
|
|
TaskTypeTerminalGRPC
|
2024-07-14 07:41:50 -04:00
|
|
|
TaskTypeNAT
|
2024-12-06 22:06:42 -05:00
|
|
|
TaskTypeReportHostInfoDeprecated
|
2024-08-20 10:25:29 -04:00
|
|
|
TaskTypeFM
|
2019-12-09 03:02:49 -05:00
|
|
|
)
|
|
|
|
|
2021-08-17 23:56:54 -04:00
|
|
|
type TerminalTask struct {
|
2024-07-14 00:47:36 -04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-08-20 10:25:29 -04:00
|
|
|
type TaskFM struct {
|
|
|
|
StreamID string
|
|
|
|
}
|
|
|
|
|
2021-06-21 09:30:42 -04:00
|
|
|
const (
|
2024-10-24 12:13:45 -04:00
|
|
|
ServiceCoverAll = iota
|
|
|
|
ServiceCoverIgnoreAll
|
2021-06-21 09:30:42 -04:00
|
|
|
)
|
|
|
|
|
2024-10-24 12:13:45 -04:00
|
|
|
type Service struct {
|
2021-01-15 11:45:49 -05:00
|
|
|
Common
|
2024-11-16 08:27:39 -05:00
|
|
|
Name string `json:"name"`
|
|
|
|
Type uint8 `json:"type"`
|
|
|
|
Target string `json:"target"`
|
2024-10-23 11:06:11 -04:00
|
|
|
SkipServersRaw string `json:"-"`
|
2024-11-16 08:27:39 -05:00
|
|
|
Duration uint64 `json:"duration"`
|
2024-10-23 11:06:11 -04:00
|
|
|
Notify bool `json:"notify,omitempty"`
|
2024-11-16 08:27:39 -05:00
|
|
|
NotificationGroupID uint64 `json:"notification_group_id"` // 当前服务监控所属的通知组 ID
|
|
|
|
Cover uint8 `json:"cover"`
|
2024-10-23 11:06:11 -04: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 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"`
|
2021-09-02 11:45:21 -04:00
|
|
|
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 {
|
2021-01-15 11:45:49 -05:00
|
|
|
return &pb.Task{
|
|
|
|
Id: m.ID,
|
|
|
|
Type: uint64(m.Type),
|
|
|
|
Data: m.Target,
|
2019-12-09 05:14:31 -05:00
|
|
|
}
|
|
|
|
}
|
2021-04-22 09:53:31 -04:00
|
|
|
|
2022-04-11 10:51:02 -04:00
|
|
|
// CronSpec 返回服务监控请求间隔对应的 cron 表达式
|
2024-10-24 12:13:45 -04:00
|
|
|
func (m *Service) CronSpec() string {
|
2021-09-02 11:45:21 -04:00
|
|
|
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 {
|
2022-05-02 01:36:49 -04:00
|
|
|
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)
|
2022-05-02 01:36:49 -04:00
|
|
|
return nil
|
2021-04-22 09:53:31 -04:00
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
2021-04-22 09:53:31 -04:00
|
|
|
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 {
|
2024-12-04 11:11:34 -05:00
|
|
|
return t != TaskTypeCommand && t != TaskTypeTerminalGRPC && t != TaskTypeUpgrade && t != TaskTypeKeepalive
|
2021-09-27 09:18:09 -04:00
|
|
|
}
|