You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
134 lines
4.3 KiB
134 lines
4.3 KiB
package core
|
|
|
|
import (
|
|
"aufs/proto/pb"
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/shirou/gopsutil/v3/cpu"
|
|
"github.com/shirou/gopsutil/v3/disk"
|
|
"github.com/shirou/gopsutil/v3/mem"
|
|
"github.com/shirou/gopsutil/v3/net"
|
|
"github.com/shirou/gopsutil/v3/process"
|
|
"google.golang.org/protobuf/proto"
|
|
)
|
|
|
|
// 返回json数据
|
|
type SysResp struct {
|
|
Status int `json:"status"`
|
|
Message string `json:"message"`
|
|
Data interface{} `json:"data"`
|
|
}
|
|
|
|
// 系统监控
|
|
func SysMonitor(w http.ResponseWriter, r *http.Request) {
|
|
// 设置 CORS 头部
|
|
w.Header().Set("Access-Control-Allow-Origin", "*")
|
|
w.Header().Set("Access-Control-Allow-Methods", "GET")
|
|
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
|
|
|
|
// 磁盘容量信息
|
|
diskInfo, err := disk.Usage("/")
|
|
if err != nil {
|
|
log.Fatal("获取磁盘信息失败:", err)
|
|
return
|
|
}
|
|
// 磁盘空间占用
|
|
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.Fatal("获取CPU信息失败: %v", err)
|
|
return
|
|
}
|
|
cpuinfo := fmt.Sprintf("CPU使用率: %.2f%%\n", cpuPercent[0])
|
|
|
|
// 获取内存占用信息
|
|
memInfo, err := mem.VirtualMemory()
|
|
if err != nil {
|
|
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.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.Fatal("获取进程信息失败: %v", err)
|
|
return
|
|
}
|
|
// 统计运行中的进程数量
|
|
procnum := int32(len(processes))
|
|
|
|
//生成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)
|
|
}
|
|
|
|
// ver:0821
|
|
// 输出proto
|
|
encdata := base64.StdEncoding.EncodeToString(hdinfo)
|
|
// w.Header().Set("content-type", "application/x-protobuf")
|
|
// w.Write([]byte(encdata))
|
|
|
|
// 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")
|
|
|
|
// 生成json数据
|
|
jsondata, err = json.Marshal(SysResp{
|
|
Status: 0,
|
|
Message: "success",
|
|
Data: encdata,
|
|
})
|
|
|
|
w.Write(jsondata)
|
|
}
|
|
|