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"
|
2021-04-22 09:53:31 -04:00
|
|
|
|
2022-03-18 11:13:22 -04:00
|
|
|
"github.com/naiba/nezha/pkg/utils"
|
2020-11-10 21:07:45 -05:00
|
|
|
pb "github.com/naiba/nezha/proto"
|
2021-09-02 11:45:21 -04:00
|
|
|
"github.com/robfig/cron/v3"
|
2021-04-22 09:53:31 -04:00
|
|
|
"gorm.io/gorm"
|
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
|
2021-01-18 20:59:04 -05:00
|
|
|
TaskTypeHTTPGET
|
|
|
|
TaskTypeICMPPing
|
|
|
|
TaskTypeTCPPing
|
|
|
|
TaskTypeCommand
|
2021-08-17 23:56:54 -04:00
|
|
|
TaskTypeTerminal
|
2021-09-27 09:24:04 -04:00
|
|
|
TaskTypeUpgrade
|
2021-11-10 23:49:54 -05:00
|
|
|
TaskTypeKeepalive
|
2019-12-09 03:02:49 -05:00
|
|
|
)
|
|
|
|
|
2021-08-17 23:56:54 -04:00
|
|
|
type TerminalTask struct {
|
|
|
|
// websocket 主机名
|
|
|
|
Host string `json:"host,omitempty"`
|
|
|
|
// 是否启用 SSL
|
|
|
|
UseSSL bool `json:"use_ssl,omitempty"`
|
|
|
|
// 会话标识
|
|
|
|
Session string `json:"session,omitempty"`
|
2022-04-13 00:27:40 -04:00
|
|
|
// Agent在连接Server时需要的额外Cookie信息
|
|
|
|
Cookie string `json:"cookie,omitempty"`
|
2021-08-17 23:56:54 -04:00
|
|
|
}
|
|
|
|
|
2021-06-21 09:30:42 -04:00
|
|
|
const (
|
|
|
|
MonitorCoverAll = iota
|
|
|
|
MonitorCoverIgnoreAll
|
|
|
|
)
|
|
|
|
|
2021-01-15 11:45:49 -05:00
|
|
|
type Monitor struct {
|
|
|
|
Common
|
2022-04-14 15:13:53 -04:00
|
|
|
Name string
|
|
|
|
Type uint8
|
|
|
|
Target string
|
|
|
|
SkipServersRaw string
|
|
|
|
Duration uint64
|
|
|
|
Notify bool
|
|
|
|
NotificationTag string // 当前服务监控所属的通知组
|
|
|
|
Cover uint8
|
2021-09-02 11:45:21 -04:00
|
|
|
|
|
|
|
SkipServers map[uint64]bool `gorm:"-" json:"-"`
|
|
|
|
CronJobID cron.EntryID `gorm:"-" json:"-"`
|
2019-12-07 05:14:40 -05:00
|
|
|
}
|
|
|
|
|
2021-01-15 11:45:49 -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
|
|
|
}
|
|
|
|
}
|
2021-04-22 09:53:31 -04:00
|
|
|
|
2022-04-11 10:51:02 -04:00
|
|
|
// CronSpec 返回服务监控请求间隔对应的 cron 表达式
|
2021-09-02 11:45:21 -04:00
|
|
|
func (m *Monitor) CronSpec() string {
|
|
|
|
if m.Duration == 0 {
|
|
|
|
// 默认间隔 30 秒
|
|
|
|
m.Duration = 30
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("@every %ds", m.Duration)
|
|
|
|
}
|
|
|
|
|
2021-04-22 09:53:31 -04:00
|
|
|
func (m *Monitor) AfterFind(tx *gorm.DB) error {
|
|
|
|
var skipServers []uint64
|
2022-03-18 11:13:22 -04:00
|
|
|
if err := utils.Json.Unmarshal([]byte(m.SkipServersRaw), &skipServers); err != nil {
|
2021-04-22 09:53:31 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
m.SkipServers = make(map[uint64]bool)
|
|
|
|
for i := 0; i < len(skipServers); i++ {
|
|
|
|
m.SkipServers[skipServers[i]] = true
|
|
|
|
}
|
|
|
|
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 {
|
2021-09-27 11:07:05 -04:00
|
|
|
return t != TaskTypeCommand && t != TaskTypeTerminal && t != TaskTypeUpgrade
|
2021-09-27 09:18:09 -04:00
|
|
|
}
|
2022-01-14 04:34:49 -05:00
|
|
|
|
|
|
|
func (m *Monitor) InitSkipServers() error {
|
|
|
|
var skipServers []uint64
|
2022-03-18 11:13:22 -04:00
|
|
|
if err := utils.Json.Unmarshal([]byte(m.SkipServersRaw), &skipServers); err != nil {
|
2022-01-14 04:34:49 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
m.SkipServers = make(map[uint64]bool)
|
|
|
|
for i := 0; i < len(skipServers); i++ {
|
|
|
|
m.SkipServers[skipServers[i]] = true
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|