diff --git a/README.md b/README.md index 43d9a72..7d92ecb 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # 哪吒监控 -![GitHub Workflow Status](https://img.shields.io/github/workflow/status/naiba/nezha/Dashboard%20image?label=管理面板%20v0.4.9&logo=github&style=for-the-badge) ![Agent release](https://img.shields.io/github/v/release/naiba/nezha?color=brightgreen&label=Agent&style=for-the-badge&logo=github) ![GitHub Workflow Status](https://img.shields.io/github/workflow/status/naiba/nezha/Agent%20release?label=Agent%20CI&logo=github&style=for-the-badge) ![shell](https://img.shields.io/badge/安装脚本-v0.4.7-brightgreen?style=for-the-badge&logo=linux) +![GitHub Workflow Status](https://img.shields.io/github/workflow/status/naiba/nezha/Dashboard%20image?label=管理面板%20v0.4.10&logo=github&style=for-the-badge) ![Agent release](https://img.shields.io/github/v/release/naiba/nezha?color=brightgreen&label=Agent&style=for-the-badge&logo=github) ![GitHub Workflow Status](https://img.shields.io/github/workflow/status/naiba/nezha/Agent%20release?label=Agent%20CI&logo=github&style=for-the-badge) ![shell](https://img.shields.io/badge/安装脚本-v0.4.7-brightgreen?style=for-the-badge&logo=linux) :trollface: 哪吒监控 一站式轻监控轻运维系统。支持系统状态、HTTP(SSL 证书变更、即将到期、到期)、TCP、Ping 监控报警,命令批量执行和计划任务。 diff --git a/cmd/playground/main.go b/cmd/playground/main.go index 388e53c..2baeaaa 100644 --- a/cmd/playground/main.go +++ b/cmd/playground/main.go @@ -12,15 +12,17 @@ import ( "github.com/go-ping/ping" "github.com/naiba/nezha/pkg/utils" + "github.com/shirou/gopsutil/v3/cpu" "github.com/shirou/gopsutil/v3/disk" + "github.com/shirou/gopsutil/v3/host" ) func main() { // icmp() // tcpping() // httpWithSSLInfo() - // diskinfo() - cmdExec() + sysinfo() + // cmdExec() } func tcpping() { @@ -34,8 +36,26 @@ func tcpping() { fmt.Println(time.Now().Sub(start).Microseconds(), float32(time.Now().Sub(start).Microseconds())/1000.0) } -func diskinfo() { - // 硬盘信息 +func sysinfo() { + hi, _ := host.Info() + var cpuType string + if hi.VirtualizationSystem != "" { + cpuType = "Virtual" + } else { + cpuType = "Physical" + } + cpuModelCount := make(map[string]int) + ci, _ := cpu.Info() + for i := 0; i < len(ci); i++ { + cpuModelCount[ci[i].ModelName]++ + } + var cpus []string + for model, count := range cpuModelCount { + cpus = append(cpus, fmt.Sprintf("%s %d %s Core", model, count, cpuType)) + } + log.Println(cpus) + os.Exit(0) + // 硬盘信息,不使用的原因是会重复统计 Linux、Mac dparts, _ := disk.Partitions(false) for _, part := range dparts { u, _ := disk.Usage(part.Mountpoint) diff --git a/model/host.go b/model/host.go index 3e1ebcb..6e4431e 100644 --- a/model/host.go +++ b/model/host.go @@ -1,8 +1,6 @@ package model import ( - "fmt" - pb "github.com/naiba/nezha/proto" ) @@ -85,22 +83,10 @@ func (h *Host) PB() *pb.Host { } 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, + CPU: h.GetCpu(), MemTotal: h.GetMemTotal(), DiskTotal: h.GetDiskTotal(), SwapTotal: h.GetSwapTotal(), diff --git a/service/dao/dao.go b/service/dao/dao.go index 2a64c3f..9ae7b39 100644 --- a/service/dao/dao.go +++ b/service/dao/dao.go @@ -13,7 +13,7 @@ import ( pb "github.com/naiba/nezha/proto" ) -var Version = "v0.4.9" // !!记得修改 README 重的 badge 版本!! +var Version = "v0.4.10" // !!记得修改 README 重的 badge 版本!! const ( SnapshotDelay = 3 diff --git a/service/monitor/monitor.go b/service/monitor/monitor.go index 16ad4d0..11a7bd9 100644 --- a/service/monitor/monitor.go +++ b/service/monitor/monitor.go @@ -28,10 +28,20 @@ var netInSpeed, netOutSpeed, netInTransfer, netOutTransfer, lastUpdate uint64 func GetHost() *model.Host { hi, _ := host.Info() - var cpus []string + var cpuType string + if hi.VirtualizationSystem != "" { + cpuType = "Virtual" + } else { + cpuType = "Physical" + } + cpuModelCount := make(map[string]int) ci, _ := cpu.Info() for i := 0; i < len(ci); i++ { - cpus = append(cpus, fmt.Sprintf("%v-%vC%vT", ci[i].ModelName, ci[i].Cores, ci[i].Stepping)) + cpuModelCount[ci[i].ModelName]++ + } + var cpus []string + for model, count := range cpuModelCount { + cpus = append(cpus, fmt.Sprintf("%s %d %s Core", model, count, cpuType)) } mv, _ := mem.VirtualMemory() ms, _ := mem.SwapMemory() @@ -47,7 +57,11 @@ func GetHost() *model.Host { if err == nil { defer resp.Body.Close() body, _ := ioutil.ReadAll(resp.Body) - ip.IP = fmt.Sprintf("ip(v4: %s, v6: %s)", ip.IP, body) + if ip.IP == "" { + ip.IP = string(body) + } else { + ip.IP = fmt.Sprintf("ip(v4: %s, v6: %s)", ip.IP, body) + } } return &model.Host{ Platform: hi.OS,