nezha/model/monitor.go

121 lines
2.5 KiB
Go
Raw Normal View History

2019-12-07 05:14:40 -05:00
package model
2020-11-06 07:56:46 -05:00
import (
"fmt"
2020-11-10 21:07:45 -05:00
pb "github.com/naiba/nezha/proto"
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
// MTReportState ..
MTReportState
)
2019-12-07 05:14:40 -05:00
// State ..
type State struct {
2019-12-09 10:45:23 -05:00
CPU float64
MemUsed uint64
SwapUsed uint64
DiskUsed uint64
NetInTransfer uint64
NetOutTransfer uint64
NetInSpeed uint64
NetOutSpeed uint64
Uptime uint64
2019-12-07 05:14:40 -05:00
}
// PB ..
func (s *State) PB() *pb.State {
return &pb.State{
2019-12-09 10:45:23 -05:00
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,
2019-12-07 05:14:40 -05:00
}
}
2019-12-09 05:14:31 -05:00
// PB2State ..
func PB2State(s *pb.State) State {
return State{
2019-12-09 10:45:23 -05:00
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(),
2019-12-09 05:14:31 -05:00
}
}
2019-12-07 05:14:40 -05:00
// Host ..
type Host struct {
Platform string
PlatformVersion string
CPU []string
2020-11-06 07:56:46 -05:00
MemTotal uint64
DiskTotal uint64
SwapTotal uint64
2019-12-07 05:14:40 -05:00
Arch string
Virtualization string
2019-12-09 10:45:23 -05:00
BootTime uint64
2019-12-10 04:57:57 -05:00
IP string `json:"-"`
2019-12-09 03:02:49 -05:00
CountryCode string
2019-12-07 05:14:40 -05:00
Version string
}
// PB ..
func (h *Host) PB() *pb.Host {
return &pb.Host{
Platform: h.Platform,
PlatformVersion: h.PlatformVersion,
Cpu: h.CPU,
2020-11-06 07:56:46 -05:00
MemTotal: h.MemTotal,
DiskTotal: h.DiskTotal,
SwapTotal: h.SwapTotal,
2019-12-07 05:14:40 -05:00
Arch: h.Arch,
Virtualization: h.Virtualization,
BootTime: h.BootTime,
2019-12-09 03:02:49 -05:00
Ip: h.IP,
CountryCode: h.CountryCode,
2019-12-07 05:14:40 -05:00
Version: h.Version,
}
}
2019-12-09 05:14:31 -05:00
// PB2Host ...
func PB2Host(h *pb.Host) Host {
2020-11-06 07:56:46 -05:00
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))
}
2019-12-09 05:14:31 -05:00
return Host{
Platform: h.GetPlatform(),
PlatformVersion: h.GetPlatformVersion(),
2020-11-06 07:56:46 -05:00
CPU: distCpu,
MemTotal: h.GetMemTotal(),
DiskTotal: h.GetDiskTotal(),
SwapTotal: h.GetSwapTotal(),
2019-12-09 05:14:31 -05:00
Arch: h.GetArch(),
Virtualization: h.GetVirtualization(),
BootTime: h.GetBootTime(),
IP: h.GetIp(),
CountryCode: h.GetCountryCode(),
Version: h.GetVersion(),
}
}