|
|
|
@ -19,9 +19,9 @@ import ( |
|
|
|
|
|
|
|
// 返回json数据
|
|
|
|
type SysResp struct { |
|
|
|
Status int `json:"status"` |
|
|
|
Message string `json:"message"` |
|
|
|
Data interface{} `json:"data"` |
|
|
|
Status int `json:"status"` |
|
|
|
Message string `json:"message"` |
|
|
|
Data any `json:"data"` |
|
|
|
} |
|
|
|
|
|
|
|
// 系统监控
|
|
|
|
@ -44,7 +44,7 @@ func SysMonitor(w http.ResponseWriter, r *http.Request) { |
|
|
|
// 获取CPU占用率 (需要等待1秒来计算)
|
|
|
|
cpuPercent, err := cpu.Percent(time.Second, false) |
|
|
|
if err != nil { |
|
|
|
log.Fatal("获取CPU信息失败: %v", err) |
|
|
|
log.Fatalf("获取CPU信息失败: %v", err) |
|
|
|
return |
|
|
|
} |
|
|
|
cpuinfo := fmt.Sprintf("CPU使用率: %.2f%%\n", cpuPercent[0]) |
|
|
|
@ -52,46 +52,22 @@ func SysMonitor(w http.ResponseWriter, r *http.Request) { |
|
|
|
// 获取内存占用信息
|
|
|
|
memInfo, err := mem.VirtualMemory() |
|
|
|
if err != nil { |
|
|
|
log.Fatal("获取内存信息失败: %v", err) |
|
|
|
log.Fatalf("获取内存信息失败: %v", err) |
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
// else {
|
|
|
|
// fmt.Println("\n=== 内存信息 ===")
|
|
|
|
// fmt.Printf("总内存: %.2f GB\n", float64(memInfo.Total)/1024/1024/1024)
|
|
|
|
// fmt.Printf("已使用: %.2f GB\n", float64(memInfo.Used)/1024/1024/1024)
|
|
|
|
// fmt.Printf("可用内存: %.2f GB\n", float64(memInfo.Available)/1024/1024/1024)
|
|
|
|
// fmt.Printf("内存使用率: %.2f%%\n", memInfo.UsedPercent)
|
|
|
|
// }
|
|
|
|
// meminfo := fmt.Sprintf("总内存: %.2f GB\n", float64(memInfo.Total)/1024/1024/1024)
|
|
|
|
// meminfo += fmt.Sprintf("已使用: %.2f GB\n", float64(memInfo.Used)/1024/1024/1024)
|
|
|
|
// meminfo += fmt.Sprintf("可用内存: %.2f GB\n", float64(memInfo.Available)/1024/1024/1024)
|
|
|
|
// meminfo += fmt.Sprintf("内存使用率: %.2f%%\n", memInfo.UsedPercent)
|
|
|
|
// meminfo += fmt.Sprintf("交换内存: %.2f GB\n", float64(memInfo.SwapTotal)/1024/1024/1024)
|
|
|
|
// meminfo += fmt.Sprintf("已使用交换内存: %.2f GB\n", float64(memInfo.SwapUsed)/1024/1024/1024)
|
|
|
|
// meminfo += fmt.Sprintf("可用交换内存: %.2f GB\n", float64(memInfo.SwapFree)/1024/1024/1024)
|
|
|
|
// meminfo += fmt.Sprintf("交换内存使用率: %.2f%%\n", memInfo.SwapUsedPercent)
|
|
|
|
|
|
|
|
// 获取网络流量信息
|
|
|
|
netIO, err := net.IOCounters(false) |
|
|
|
if err != nil { |
|
|
|
log.Fatal("获取网络信息失败: %v", err) |
|
|
|
log.Fatalf("获取网络信息失败: %v", err) |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
// log.Printf("获取网络信息失败: %v", err)
|
|
|
|
// } else if len(netIO) > 0 {
|
|
|
|
// fmt.Println("\n=== 网络信息 ===")
|
|
|
|
// fmt.Printf("总接收: %.2f MB\n", float64(netIO[0].BytesRecv)/1024/1024)
|
|
|
|
// fmt.Printf("总发送: %.2f MB\n", float64(netIO[0].BytesSent)/1024/1024)
|
|
|
|
// fmt.Printf("接收包数: %d\n", netIO[0].PacketsRecv)
|
|
|
|
// fmt.Printf("发送包数: %d\n", netIO[0].PacketsSent)
|
|
|
|
// }
|
|
|
|
|
|
|
|
// 获取系统进程信息 (只获取前5个进程)
|
|
|
|
processes, err := process.Processes() |
|
|
|
if err != nil { |
|
|
|
log.Fatal("获取进程信息失败: %v", err) |
|
|
|
log.Fatalf("获取进程信息失败: %v", err) |
|
|
|
return |
|
|
|
} |
|
|
|
// 统计运行中的进程数量
|
|
|
|
|