2023-03-01 14:10:50 +08:00
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Node 节点信息
|
|
|
|
type Node struct {
|
|
|
|
NodeId int64 `xorm:"not null pk autoincr INT(11)" json:"-"`
|
|
|
|
NodeGuid string `xorm:"varchar(100) index" json:"nodeGuid"`
|
|
|
|
NodeName string `xorm:"varchar(100)" json:"nodeName"`
|
|
|
|
NodeType int `xorm:"not null SMALLINT default 0" json:"nodeType"`
|
|
|
|
NodeStatus int `xorm:"not null SMALLINT default 0" json:"nodeStatus"`
|
|
|
|
Platform string `xorm:"varchar(100)" json:"platform,omitempty"`
|
|
|
|
PlatformVersion string `xorm:"varchar(100)" json:"platformVersion,omitempty"`
|
|
|
|
CPU []string `xorm:"varchar(1000)" json:"cpu,omitempty"`
|
|
|
|
MemTotal uint64 `xorm:"BIGINT" json:"memTotal,omitempty"`
|
|
|
|
DiskTotal uint64 `xorm:"BIGINT" json:"diskTotal,omitempty"`
|
|
|
|
SwapTotal uint64 `xorm:"BIGINT" json:"swapTotal,omitempty"`
|
|
|
|
Arch string `xorm:"varchar(1000)" json:"arch,omitempty"`
|
|
|
|
Virtualization string `xorm:"varchar(1000)" json:"virtualization,omitempty"`
|
|
|
|
BootTime uint64 `xorm:"BIGINT" json:"bootTime,omitempty"`
|
2023-06-17 09:41:02 +08:00
|
|
|
IP string `xorm:"varchar(100)" json:"ip"` //互联网显示地址
|
|
|
|
LocalIP string `xorm:"VARCHAR(100)" json:"localIP"` //本地局域网IP地址
|
2023-03-01 14:10:50 +08:00
|
|
|
CountryCode string `xorm:"varchar(100)" json:"countryCode,omitempty"`
|
|
|
|
Version string `xorm:"varchar(100)" json:"version,omitempty"`
|
|
|
|
CreateAt int64 `xorm:"created" json:"createAt"`
|
|
|
|
UpdateAt int64 `xorm:"updated" json:"updateAt"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n Node) ToByte() []byte {
|
|
|
|
data, err := json.Marshal(n)
|
|
|
|
if err != nil {
|
|
|
|
return []byte("")
|
|
|
|
}
|
|
|
|
return data
|
|
|
|
}
|
|
|
|
func (n Node) ToString() string {
|
|
|
|
data, err := json.Marshal(n)
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return string(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n Node) Insert() {
|
|
|
|
node := new(Node)
|
2023-05-18 11:02:04 +08:00
|
|
|
b, err := DB.Where("node_guid = ?", n.NodeGuid).Get(node)
|
2023-03-01 14:10:50 +08:00
|
|
|
if err != nil {
|
|
|
|
Logger.With(zap.String("Node", n.ToString())).Error("查询节点失败", zap.Error(err))
|
|
|
|
return
|
|
|
|
}
|
2023-05-18 11:02:04 +08:00
|
|
|
node.NodeGuid = n.NodeGuid
|
|
|
|
node.NodeName = n.NodeName
|
|
|
|
node.NodeType = n.NodeType
|
2023-03-01 14:10:50 +08:00
|
|
|
node.Platform = n.Platform
|
|
|
|
node.PlatformVersion = n.PlatformVersion
|
|
|
|
node.CPU = n.CPU
|
|
|
|
node.MemTotal = n.MemTotal
|
|
|
|
node.DiskTotal = n.DiskTotal
|
|
|
|
node.SwapTotal = n.SwapTotal
|
|
|
|
node.Arch = n.Arch
|
|
|
|
node.Virtualization = n.Virtualization
|
|
|
|
node.BootTime = n.BootTime
|
|
|
|
node.IP = n.IP
|
2023-06-17 09:41:02 +08:00
|
|
|
node.LocalIP = n.LocalIP
|
2023-03-01 14:10:50 +08:00
|
|
|
node.CountryCode = n.CountryCode
|
|
|
|
node.Version = n.Version
|
|
|
|
if b {
|
2023-05-18 11:02:04 +08:00
|
|
|
if node.NodeStatus != 2 {
|
|
|
|
node.NodeStatus = 1
|
|
|
|
}
|
2023-03-01 14:10:50 +08:00
|
|
|
_, err = DB.ID(node.NodeId).Update(node)
|
|
|
|
if err != nil {
|
|
|
|
Logger.With(zap.String("Node", node.ToString())).Error("更新节点失败", zap.Error(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
2023-05-18 11:02:04 +08:00
|
|
|
node.NodeStatus = 1
|
2023-03-01 14:10:50 +08:00
|
|
|
_, err := DB.Insert(node)
|
|
|
|
if err != nil {
|
|
|
|
Logger.With(zap.String("Node", node.ToString())).Error("入库失败", zap.Error(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NodeState 节点状态信息
|
|
|
|
type NodeState struct {
|
2023-05-18 11:02:04 +08:00
|
|
|
NodeGuid string `xorm:"VARCHAR(100) pk" json:"nodeGuid,omitempty"`
|
2023-03-01 14:10:50 +08:00
|
|
|
Uptime uint64 `xorm:"BIGINT pk" json:"uptime,omitempty"`
|
2023-05-18 11:02:04 +08:00
|
|
|
NodeName string `xorm:"varchar(100) " json:"nodeName"`
|
2023-03-01 14:10:50 +08:00
|
|
|
CPU float64 `xorm:"DECIMAL(18,4)" json:"cpu,omitempty"`
|
|
|
|
MemUsed uint64 `xorm:"BIGINT" json:"memUsed,omitempty"`
|
|
|
|
SwapUsed uint64 `xorm:"BIGINT" json:"swapUsed,omitempty"`
|
|
|
|
DiskUsed uint64 `xorm:"BIGINT" json:"diskUsed,omitempty"`
|
|
|
|
NetInTransfer uint64 `xorm:"BIGINT" json:"netInTransfer,omitempty"`
|
|
|
|
NetOutTransfer uint64 `xorm:"BIGINT" json:"netOutTransfer,omitempty"`
|
|
|
|
NetInSpeed uint64 `xorm:"BIGINT" json:"netInSpeed,omitempty"`
|
|
|
|
NetOutSpeed uint64 `xorm:"BIGINT" json:"netOutSpeed,omitempty"`
|
|
|
|
Load1 float64 `xorm:"DECIMAL(18,4)" json:"load1,omitempty"`
|
|
|
|
Load5 float64 `xorm:"DECIMAL(18,4)" json:"load5,omitempty"`
|
|
|
|
Load15 float64 `xorm:"DECIMAL(18,4)" json:"load15,omitempty"`
|
|
|
|
TcpConnCount uint64 `xorm:"BIGINT" json:"tcpConnCount,omitempty"`
|
|
|
|
UdpConnCount uint64 `xorm:"BIGINT" json:"udpConnCount,omitempty"`
|
|
|
|
ProcessCount uint64 `xorm:"BIGINT" json:"processCount,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ns NodeState) ToByte() []byte {
|
|
|
|
data, err := json.Marshal(ns)
|
|
|
|
if err != nil {
|
|
|
|
return []byte("")
|
|
|
|
}
|
|
|
|
return data
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ns NodeState) ToString() string {
|
|
|
|
data, err := json.Marshal(ns)
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return string(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ns NodeState) Insert() {
|
|
|
|
_, err := DB.Insert(ns)
|
|
|
|
if err != nil {
|
|
|
|
Logger.With(zap.String("NodeState", ns.ToString())).Error("入库失败", zap.Error(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|