💥 v2.0 必须更新面板,新增服务监控

This commit is contained in:
naiba
2021-01-16 00:45:49 +08:00
parent 0ce8017875
commit a41c792577
38 changed files with 1015 additions and 453 deletions

View File

@@ -118,7 +118,7 @@ func (r *AlertRule) Check(points [][]interface{}) (int, string) {
fail++
}
}
if fail/total > 0.3 {
if fail/total > 0.5 {
count++
dist.WriteString(fmt.Sprintf("%+v\n", r.Rules[i]))
}

View File

@@ -2,13 +2,10 @@ package model
import "time"
// CtxKeyAuthorizedUser ..
const CtxKeyAuthorizedUser = "ckau"
// CtxKeyOauth2State ..
const CtxKeyOauth2State = "cko2s"
// Common ..
type Common struct {
ID uint64 `gorm:"primary_key"`
CreatedAt time.Time
@@ -16,7 +13,6 @@ type Common struct {
DeletedAt *time.Time `sql:"index"`
}
// Response ..
type Response struct {
Code uint64 `json:"code,omitempty"`
Message string `json:"message,omitempty"`

View File

@@ -29,7 +29,6 @@ type Config struct {
v *viper.Viper
}
// ReadInConfig ..
func (c *Config) Read(path string) error {
c.v = viper.New()
c.v.SetConfigFile(path)

114
model/host.go Normal file
View File

@@ -0,0 +1,114 @@
package model
import (
"fmt"
pb "github.com/naiba/nezha/proto"
)
const (
_ = iota
MTReportHostState
)
type HostState struct {
CPU float64
MemUsed uint64
SwapUsed uint64
DiskUsed uint64
NetInTransfer uint64
NetOutTransfer uint64
NetInSpeed uint64
NetOutSpeed uint64
Uptime uint64
}
func (s *HostState) PB() *pb.State {
return &pb.State{
Cpu: s.CPU,
MemUsed: s.MemUsed,
SwapUsed: s.SwapUsed,
DiskUsed: s.DiskUsed,
NetInTransfer: s.NetInTransfer,
NetOutTransfer: s.NetOutTransfer,
NetInSpeed: s.NetInSpeed,
NetOutSpeed: s.NetOutSpeed,
Uptime: s.Uptime,
}
}
func PB2State(s *pb.State) HostState {
return HostState{
CPU: s.GetCpu(),
MemUsed: s.GetMemUsed(),
SwapUsed: s.GetSwapUsed(),
DiskUsed: s.GetDiskUsed(),
NetInTransfer: s.GetNetInTransfer(),
NetOutTransfer: s.GetNetOutTransfer(),
NetInSpeed: s.GetNetInSpeed(),
NetOutSpeed: s.GetNetOutSpeed(),
Uptime: s.GetUptime(),
}
}
type Host struct {
Platform string
PlatformVersion string
CPU []string
MemTotal uint64
DiskTotal uint64
SwapTotal uint64
Arch string
Virtualization string
BootTime uint64
IP string `json:"-"`
CountryCode string
Version string
}
func (h *Host) PB() *pb.Host {
return &pb.Host{
Platform: h.Platform,
PlatformVersion: h.PlatformVersion,
Cpu: h.CPU,
MemTotal: h.MemTotal,
DiskTotal: h.DiskTotal,
SwapTotal: h.SwapTotal,
Arch: h.Arch,
Virtualization: h.Virtualization,
BootTime: h.BootTime,
Ip: h.IP,
CountryCode: h.CountryCode,
Version: h.Version,
}
}
func PB2Host(h *pb.Host) Host {
cpuCount := make(map[string]int, 0)
cpus := h.GetCpu()
for _, u := range cpus {
cpuCount[u]++
}
var distCpu []string
for u, num := range cpuCount {
distCpu = append(distCpu, fmt.Sprintf("%sx%d", u, num))
}
return Host{
Platform: h.GetPlatform(),
PlatformVersion: h.GetPlatformVersion(),
CPU: distCpu,
MemTotal: h.GetMemTotal(),
DiskTotal: h.GetDiskTotal(),
SwapTotal: h.GetSwapTotal(),
Arch: h.GetArch(),
Virtualization: h.GetVirtualization(),
BootTime: h.GetBootTime(),
IP: h.GetIp(),
CountryCode: h.GetCountryCode(),
Version: h.GetVersion(),
}
}

View File

@@ -1,120 +1,27 @@
package model
import (
"fmt"
pb "github.com/naiba/nezha/proto"
)
const (
_ = iota
// MTReportState ..
MTReportState
MonitorTypeHTTPGET
MonitorTypeICMPPing
MonitorTypeTCPPing
)
// State ..
type State struct {
CPU float64
MemUsed uint64
SwapUsed uint64
DiskUsed uint64
NetInTransfer uint64
NetOutTransfer uint64
NetInSpeed uint64
NetOutSpeed uint64
Uptime uint64
type Monitor struct {
Common
Name string
Type uint8
Target string
}
// PB ..
func (s *State) PB() *pb.State {
return &pb.State{
Cpu: s.CPU,
MemUsed: s.MemUsed,
SwapUsed: s.SwapUsed,
DiskUsed: s.DiskUsed,
NetInTransfer: s.NetInTransfer,
NetOutTransfer: s.NetOutTransfer,
NetInSpeed: s.NetInSpeed,
NetOutSpeed: s.NetOutSpeed,
Uptime: s.Uptime,
}
}
// PB2State ..
func PB2State(s *pb.State) State {
return State{
CPU: s.GetCpu(),
MemUsed: s.GetMemUsed(),
SwapUsed: s.GetSwapUsed(),
DiskUsed: s.GetDiskUsed(),
NetInTransfer: s.GetNetInTransfer(),
NetOutTransfer: s.GetNetOutTransfer(),
NetInSpeed: s.GetNetInSpeed(),
NetOutSpeed: s.GetNetOutSpeed(),
Uptime: s.GetUptime(),
}
}
// Host ..
type Host struct {
Platform string
PlatformVersion string
CPU []string
MemTotal uint64
DiskTotal uint64
SwapTotal uint64
Arch string
Virtualization string
BootTime uint64
IP string `json:"-"`
CountryCode string
Version string
}
// PB ..
func (h *Host) PB() *pb.Host {
return &pb.Host{
Platform: h.Platform,
PlatformVersion: h.PlatformVersion,
Cpu: h.CPU,
MemTotal: h.MemTotal,
DiskTotal: h.DiskTotal,
SwapTotal: h.SwapTotal,
Arch: h.Arch,
Virtualization: h.Virtualization,
BootTime: h.BootTime,
Ip: h.IP,
CountryCode: h.CountryCode,
Version: h.Version,
}
}
// PB2Host ...
func PB2Host(h *pb.Host) Host {
cpuCount := make(map[string]int, 0)
cpus := h.GetCpu()
for _, u := range cpus {
cpuCount[u]++
}
var distCpu []string
for u, num := range cpuCount {
distCpu = append(distCpu, fmt.Sprintf("%sx%d", u, num))
}
return Host{
Platform: h.GetPlatform(),
PlatformVersion: h.GetPlatformVersion(),
CPU: distCpu,
MemTotal: h.GetMemTotal(),
DiskTotal: h.GetDiskTotal(),
SwapTotal: h.GetSwapTotal(),
Arch: h.GetArch(),
Virtualization: h.GetVirtualization(),
BootTime: h.GetBootTime(),
IP: h.GetIp(),
CountryCode: h.GetCountryCode(),
Version: h.GetVersion(),
func (m *Monitor) PB() *pb.Task {
return &pb.Task{
Id: m.ID,
Type: uint64(m.Type),
Data: m.Target,
}
}

22
model/monitor_history.go Normal file
View File

@@ -0,0 +1,22 @@
package model
import (
pb "github.com/naiba/nezha/proto"
)
type MonitorHistory struct {
Common
MonitorID uint64
Delay float32 // 延迟,毫秒
Data string
Successful bool // 是否成功
}
func PB2MonitorHistory(r *pb.TaskResult) MonitorHistory {
return MonitorHistory{
Delay: r.GetDelay(),
Successful: r.GetSuccessful(),
MonitorID: r.GetId(),
Data: r.GetData(),
}
}

View File

@@ -8,19 +8,18 @@ import (
pb "github.com/naiba/nezha/proto"
)
// Server ..
type Server struct {
Common
Name string
DisplayIndex int // 展示权重,越大越靠前
Secret string `json:"-"`
Tag string
Host *Host `gorm:"-"`
State *State `gorm:"-"`
Host *Host `gorm:"-"`
State *HostState `gorm:"-"`
LastActive time.Time
Stream pb.NezhaService_HeartbeatServer `gorm:"-" json:"-"`
StreamClose chan<- error `gorm:"-" json:"-"`
TaskClose chan error `gorm:"-" json:"-"`
TaskStream pb.NezhaService_RequestTaskServer `gorm:"-" json:"-"`
}
func (s Server) Marshal() template.JS {

View File

@@ -8,7 +8,6 @@ import (
"github.com/naiba/com"
)
// User ...
type User struct {
Common
Login string `gorm:"UNIQUE_INDEX" json:"login,omitempty"` // 登录名
@@ -26,7 +25,6 @@ type User struct {
TeamsID []uint64 `gorm:"-"`
}
// NewUserFromGitHub ..
func NewUserFromGitHub(gu *github.User) User {
var u User
u.ID = uint64(gu.GetID())
@@ -45,7 +43,6 @@ func NewUserFromGitHub(gu *github.User) User {
return u
}
// IssueNewToken ...
func (u *User) IssueNewToken() {
u.Token = com.MD5(fmt.Sprintf("%d%d%s", time.Now().UnixNano(), u.ID, u.Login))
u.TokenExpired = time.Now().AddDate(0, 2, 0)