diff --git a/cmd/playground/main.go b/cmd/playground/main.go index b00df4c..fec56ab 100644 --- a/cmd/playground/main.go +++ b/cmd/playground/main.go @@ -3,10 +3,17 @@ package main import ( "log" "os/exec" + + "github.com/shirou/gopsutil/disk" ) func main() { - + dparts, _ := disk.Partitions(false) + for _, part := range dparts { + u, _ := disk.Usage(part.Mountpoint) + log.Printf("Part:%v", part) + log.Printf("Usage:%v", u) + } } func cmdExec() { diff --git a/model/monitor.go b/model/monitor.go index fb0dd24..f2e7a94 100644 --- a/model/monitor.go +++ b/model/monitor.go @@ -11,11 +11,8 @@ const ( // State .. type State struct { CPU float64 - MemTotal uint64 MemUsed uint64 - SwapTotal uint64 SwapUsed uint64 - DiskTotal uint64 DiskUsed uint64 NetInTransfer uint64 NetOutTransfer uint64 @@ -28,11 +25,8 @@ type State struct { func (s *State) PB() *pb.State { return &pb.State{ Cpu: s.CPU, - MemTotal: s.MemTotal, MemUsed: s.MemUsed, - SwapTotal: s.SwapTotal, SwapUsed: s.SwapUsed, - DiskTotal: s.DiskTotal, DiskUsed: s.DiskUsed, NetInTransfer: s.NetInTransfer, NetOutTransfer: s.NetOutTransfer, @@ -46,11 +40,8 @@ func (s *State) PB() *pb.State { func PB2State(s *pb.State) State { return State{ CPU: s.GetCpu(), - MemTotal: s.GetMemTotal(), MemUsed: s.GetMemUsed(), - SwapTotal: s.GetSwapTotal(), SwapUsed: s.GetSwapUsed(), - DiskTotal: s.GetDiskTotal(), DiskUsed: s.GetDiskUsed(), NetInTransfer: s.GetNetInTransfer(), NetOutTransfer: s.GetNetOutTransfer(), @@ -65,6 +56,9 @@ type Host struct { Platform string PlatformVersion string CPU []string + MemTotal uint64 + DiskTotal uint64 + SwapTotal uint64 Arch string Virtualization string BootTime uint64 @@ -79,6 +73,9 @@ func (h *Host) PB() *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, @@ -88,12 +85,16 @@ func (h *Host) PB() *pb.Host { } } + // PB2Host ... func PB2Host(h *pb.Host) Host { return Host{ Platform: h.GetPlatform(), PlatformVersion: h.GetPlatformVersion(), CPU: h.GetCpu(), + MemTotal: h.GetMemTotal(), + DiskTotal: h.GetDiskTotal(), + SwapTotal: h.GetSwapTotal(), Arch: h.GetArch(), Virtualization: h.GetVirtualization(), BootTime: h.GetBootTime(), @@ -102,3 +103,6 @@ func PB2Host(h *pb.Host) Host { Version: h.GetVersion(), } } + + + diff --git a/proto/nezha.proto b/proto/nezha.proto index 72fbd73..feb37d4 100644 --- a/proto/nezha.proto +++ b/proto/nezha.proto @@ -12,27 +12,27 @@ message Host { string platform = 1; string platform_version = 2; repeated string cpu = 3; - string arch = 4; - string virtualization = 5; - uint64 boot_time = 6; - string ip = 7; - string country_code = 8; - string version = 9; + uint64 mem_total = 4; + uint64 disk_total = 5; + uint64 swap_total = 6; + string arch = 7; + string virtualization = 8; + uint64 boot_time = 9; + string ip = 10; + string country_code = 11; + string version = 12; } message State { double cpu = 1; - uint64 mem_total = 2; uint64 mem_used = 3; - uint64 swap_total = 4; - uint64 swap_used = 5; - uint64 disk_total = 6; - uint64 disk_used = 7; - uint64 net_in_transfer = 8; - uint64 net_out_transfer = 9; - uint64 net_in_speed = 10; - uint64 net_out_speed = 11; - uint64 uptime = 12; + uint64 swap_used = 4; + uint64 disk_used = 5; + uint64 net_in_transfer = 6; + uint64 net_out_transfer = 7; + uint64 net_in_speed = 8; + uint64 net_out_speed = 9; + uint64 uptime = 10; } message Receipt{ diff --git a/resource/template/page/home.html b/resource/template/page/home.html index ae68a05..5d412d7 100644 --- a/resource/template/page/home.html +++ b/resource/template/page/home.html @@ -16,9 +16,9 @@ 系统:@#server.Host.Platform#@-@#server.Host.PlatformVersion#@ [@#server.Host.Virtualization#@:@#server.Host.Arch#@]
CPU:@#server.Host.CPU#@
- 硬盘:@#formatByteSize(server.State.DiskUsed)#@/@#formatByteSize(server.State.DiskTotal)#@
- 内存:@#formatByteSize(server.State.MemUsed)#@/@#formatByteSize(server.State.MemTotal)#@
- 交换:@#formatByteSize(server.State.SwapUsed)#@/@#formatByteSize(server.State.SwapTotal)#@
+ 硬盘:@#formatByteSize(server.State.DiskUsed)#@/@#formatByteSize(server.Host.DiskTotal)#@
+ 内存:@#formatByteSize(server.State.MemUsed)#@/@#formatByteSize(server.Host.MemTotal)#@
+ 交换:@#formatByteSize(server.State.SwapUsed)#@/@#formatByteSize(server.Host.SwapTotal)#@
流量:@#formatByteSize(server.State.NetInTransfer)#@@#formatByteSize(server.State.NetOutTransfer)#@
@@ -39,7 +39,7 @@
内存
+ :data-total="server.Host.MemTotal">
@@ -48,7 +48,7 @@
交换
+ :data-total="server.Host.SwapTotal">
@@ -64,7 +64,7 @@
硬盘
+ :data-total="server.Host.DiskTotal">
diff --git a/service/monitor/monitor.go b/service/monitor/monitor.go index 24b4238..ad57309 100644 --- a/service/monitor/monitor.go +++ b/service/monitor/monitor.go @@ -33,6 +33,14 @@ func GetHost() *model.Host { for i := 0; i < len(ci); i++ { cpus = append(cpus, fmt.Sprintf("%v-%vC%vT", ci[i].ModelName, ci[i].Cores, ci[i].Stepping)) } + mv, _ := mem.VirtualMemory() + ms, _ := mem.SwapMemory() + var diskTotal uint64 + dparts, _ := disk.Partitions(true) + for _, part := range dparts { + u, _ := disk.Usage(part.Mountpoint) + diskTotal += u.Total + } var ip ipDotSbGeoIP resp, err := http.Get("https://api.ip.sb/geoip") if err == nil { @@ -44,6 +52,9 @@ func GetHost() *model.Host { Platform: hi.OS, PlatformVersion: hi.PlatformVersion, CPU: cpus, + MemTotal: mv.Total, + DiskTotal: diskTotal, + SwapTotal: ms.Total, Arch: hi.KernelArch, Virtualization: hi.VirtualizationSystem, BootTime: hi.BootTime, @@ -66,25 +77,17 @@ func GetState(delay int64) *model.State { cpuPercent = cp[0] } // Disk - var diskTotal, diskUsed uint64 + var diskUsed uint64 dparts, _ := disk.Partitions(true) - var lastDevice string for _, part := range dparts { u, _ := disk.Usage(part.Mountpoint) - if lastDevice != part.Device { - diskTotal += u.Total - lastDevice = part.Device - } diskUsed += u.Used } return &model.State{ CPU: cpuPercent, - MemTotal: mv.Total, MemUsed: mv.Used, - SwapTotal: ms.Total, SwapUsed: ms.Used, - DiskTotal: diskTotal, DiskUsed: diskUsed, NetInTransfer: netInTransfer, NetOutTransfer: netOutTransfer, @@ -97,14 +100,10 @@ func GetState(delay int64) *model.State { // TrackNetworkSpeed .. func TrackNetworkSpeed() { var innerNetInTransfer, innerNetOutTransfer uint64 - nc, err := net.IOCounters(true) + nc, err := net.IOCounters(false) if err == nil { - for i := 0; i < len(nc); i++ { - if strings.HasPrefix(nc[i].Name, "e") { - innerNetInTransfer += nc[i].BytesRecv - innerNetOutTransfer += nc[i].BytesSent - } - } + innerNetInTransfer += nc[0].BytesRecv + innerNetOutTransfer += nc[0].BytesSent if netInTransfer == 0 { netInTransfer = innerNetInTransfer }