131 lines
4.3 KiB
Go
131 lines
4.3 KiB
Go
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"`
|
|
IP string `xorm:"varchar(100)" json:"ip"`
|
|
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)
|
|
b, err := DB.Where("node_guid = ?", n.NodeGuid).Get(node)
|
|
if err != nil {
|
|
Logger.With(zap.String("Node", n.ToString())).Error("查询节点失败", zap.Error(err))
|
|
return
|
|
}
|
|
node.NodeGuid = n.NodeGuid
|
|
node.NodeName = n.NodeName
|
|
node.NodeType = n.NodeType
|
|
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
|
|
node.CountryCode = n.CountryCode
|
|
node.Version = n.Version
|
|
if b {
|
|
if node.NodeStatus != 2 {
|
|
node.NodeStatus = 1
|
|
}
|
|
_, err = DB.ID(node.NodeId).Update(node)
|
|
if err != nil {
|
|
Logger.With(zap.String("Node", node.ToString())).Error("更新节点失败", zap.Error(err))
|
|
return
|
|
}
|
|
} else {
|
|
node.NodeStatus = 1
|
|
_, err := DB.Insert(node)
|
|
if err != nil {
|
|
Logger.With(zap.String("Node", node.ToString())).Error("入库失败", zap.Error(err))
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
// NodeState 节点状态信息
|
|
type NodeState struct {
|
|
NodeGuid string `xorm:"VARCHAR(100) pk" json:"nodeGuid,omitempty"`
|
|
Uptime uint64 `xorm:"BIGINT pk" json:"uptime,omitempty"`
|
|
NodeName string `xorm:"varchar(100) " json:"nodeName"`
|
|
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
|
|
}
|
|
}
|