nezha/model/monitor.go

76 lines
1.3 KiB
Go
Raw Normal View History

2019-12-07 18:14:40 +08:00
package model
2020-11-06 20:56:46 +08:00
import (
"encoding/json"
"fmt"
2020-11-11 10:07:45 +08:00
pb "github.com/naiba/nezha/proto"
"github.com/robfig/cron/v3"
"gorm.io/gorm"
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
TaskTypeHTTPGET
TaskTypeICMPPing
TaskTypeTCPPing
TaskTypeCommand
2021-08-18 11:56:54 +08:00
TaskTypeTerminal
2019-12-09 16:02:49 +08:00
)
2021-08-18 11:56:54 +08:00
type TerminalTask struct {
// websocket 主机名
Host string `json:"host,omitempty"`
// 是否启用 SSL
UseSSL bool `json:"use_ssl,omitempty"`
// 会话标识
Session string `json:"session,omitempty"`
}
const (
MonitorCoverAll = iota
MonitorCoverIgnoreAll
)
type Monitor struct {
Common
Name string
Type uint8
Target string
SkipServersRaw string
Duration uint64
Notify bool
Cover uint8
SkipServers map[uint64]bool `gorm:"-" json:"-"`
CronJobID cron.EntryID `gorm:"-" json:"-"`
2019-12-07 18:14:40 +08:00
}
func (m *Monitor) PB() *pb.Task {
return &pb.Task{
Id: m.ID,
Type: uint64(m.Type),
Data: m.Target,
2019-12-09 18:14:31 +08:00
}
}
func (m *Monitor) CronSpec() string {
if m.Duration == 0 {
// 默认间隔 30 秒
m.Duration = 30
}
return fmt.Sprintf("@every %ds", m.Duration)
}
func (m *Monitor) AfterFind(tx *gorm.DB) error {
var skipServers []uint64
if err := 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
}