|
|
|
@ -1,6 +1,8 @@ |
|
|
|
package core |
|
|
|
|
|
|
|
import ( |
|
|
|
"aufs/proto/pb" |
|
|
|
"encoding/json" |
|
|
|
"fmt" |
|
|
|
"log" |
|
|
|
"net/http" |
|
|
|
@ -11,6 +13,7 @@ import ( |
|
|
|
"github.com/shirou/gopsutil/v3/mem" |
|
|
|
"github.com/shirou/gopsutil/v3/net" |
|
|
|
"github.com/shirou/gopsutil/v3/process" |
|
|
|
"google.golang.org/protobuf/proto" |
|
|
|
) |
|
|
|
|
|
|
|
// 系统监控
|
|
|
|
@ -23,70 +26,86 @@ func SysMonitor(w http.ResponseWriter, r *http.Request) { |
|
|
|
// 磁盘容量信息
|
|
|
|
diskInfo, err := disk.Usage("/") |
|
|
|
if err != nil { |
|
|
|
fmt.Println("获取磁盘信息失败:", err) |
|
|
|
log.Fatal("获取磁盘信息失败:", err) |
|
|
|
return |
|
|
|
} else { |
|
|
|
fmt.Println("=== 磁盘信息 ===") |
|
|
|
fmt.Printf("总容量: %.2f GB\n", float64(diskInfo.Total)/1024/1024/1024) |
|
|
|
fmt.Printf("已使用: %.2f GB\n", float64(diskInfo.Used)/1024/1024/1024) |
|
|
|
fmt.Printf("可用空间: %.2f GB\n", float64(diskInfo.Free)/1024/1024/1024) |
|
|
|
fmt.Printf("使用率: %.2f%%\n", diskInfo.UsedPercent) |
|
|
|
} |
|
|
|
// 磁盘空间占用
|
|
|
|
hdtval := fmt.Sprintf("总容量: %.2f GB\n", float64(diskInfo.Total)/1024/1024/1024) |
|
|
|
hduval := fmt.Sprintf("已使用: %.2f GB\n", float64(diskInfo.Used)/1024/1024/1024) |
|
|
|
|
|
|
|
// 获取CPU占用率 (需要等待1秒来计算)
|
|
|
|
cpuPercent, err := cpu.Percent(time.Second, false) |
|
|
|
if err != nil { |
|
|
|
log.Printf("获取CPU信息失败: %v", err) |
|
|
|
} else { |
|
|
|
fmt.Println("\n=== CPU信息 ===") |
|
|
|
fmt.Printf("CPU使用率: %.2f%%\n", cpuPercent[0]) |
|
|
|
log.Fatal("获取CPU信息失败: %v", err) |
|
|
|
return |
|
|
|
} |
|
|
|
cpuinfo := fmt.Sprintf("CPU使用率: %.2f%%\n", cpuPercent[0]) |
|
|
|
|
|
|
|
// 获取内存占用信息
|
|
|
|
memInfo, err := mem.VirtualMemory() |
|
|
|
if err != nil { |
|
|
|
log.Printf("获取内存信息失败: %v", err) |
|
|
|
} 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) |
|
|
|
log.Fatal("获取内存信息失败: %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.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) |
|
|
|
log.Fatal("获取网络信息失败: %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.Printf("获取进程信息失败: %v", err) |
|
|
|
} else { |
|
|
|
fmt.Println("\n=== 进程信息 (前5个) ===") |
|
|
|
count := 0 |
|
|
|
for _, p := range processes { |
|
|
|
if count >= 5 { |
|
|
|
break |
|
|
|
} |
|
|
|
|
|
|
|
pid := p.Pid |
|
|
|
name, _ := p.Name() |
|
|
|
cpuPercent, _ := p.CPUPercent() |
|
|
|
memPercent, _ := p.MemoryPercent() |
|
|
|
log.Fatal("获取进程信息失败: %v", err) |
|
|
|
return |
|
|
|
} |
|
|
|
// 统计运行中的进程数量
|
|
|
|
procnum := int32(len(processes)) |
|
|
|
|
|
|
|
fmt.Printf("PID: %d, 名称: %s, CPU使用率: %.2f%%, 内存使用率: %.2f%%\n", |
|
|
|
pid, name, cpuPercent, memPercent) |
|
|
|
count++ |
|
|
|
} |
|
|
|
//生成proto的内容
|
|
|
|
hdinfo, err := proto.Marshal(&pb.Hdinfo{ |
|
|
|
Hdstat: fmt.Sprintf("使用:%v /总计:%v", hduval, hdtval), |
|
|
|
Procout: fmt.Sprintf("进程数: %d", procnum), |
|
|
|
Cpustat: cpuinfo, |
|
|
|
Memstat: fmt.Sprintf("内存使用率: %.2f%%\n", memInfo.UsedPercent), |
|
|
|
Netstat: fmt.Sprintf("总接收: %.2f MB\n", float64(netIO[0].BytesRecv)/1024/1024), |
|
|
|
}) |
|
|
|
if err != nil { |
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError) |
|
|
|
} |
|
|
|
|
|
|
|
// make json string
|
|
|
|
jsondata, err := json.Marshal(hdinfo) |
|
|
|
if err != nil { |
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError) |
|
|
|
} |
|
|
|
w.Header().Set("Content-Type", "application/json") |
|
|
|
w.Write(jsondata) |
|
|
|
} |
|
|
|
|