2019-12-07 05:14:40 -05:00
|
|
|
package model
|
|
|
|
|
2020-11-06 07:56:46 -05:00
|
|
|
import (
|
2021-04-22 09:53:31 -04:00
|
|
|
"encoding/json"
|
|
|
|
|
2020-11-10 21:07:45 -05:00
|
|
|
pb "github.com/naiba/nezha/proto"
|
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
|
2019-12-09 03:02:49 -05: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
|
2021-04-22 09:53:31 -04:00
|
|
|
Name string
|
|
|
|
Type uint8
|
|
|
|
Target string
|
|
|
|
SkipServersRaw string
|
|
|
|
Notify bool
|
2021-06-21 09:30:42 -04:00
|
|
|
Cover uint8
|
|
|
|
SkipServers map[uint64]bool `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
|
|
|
|
|
|
|
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
|
|
|
|
}
|