|
|
|
@ -26,10 +26,6 @@ type SysResp struct { |
|
|
|
|
|
|
|
// 系统监控
|
|
|
|
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("/") |
|
|
|
@ -47,7 +43,7 @@ func SysMonitor(w http.ResponseWriter, r *http.Request) { |
|
|
|
log.Fatalf("获取CPU信息失败: %v", err) |
|
|
|
return |
|
|
|
} |
|
|
|
cpuinfo := fmt.Sprintf("CPU使用率: %.2f%%\n", cpuPercent[0]) |
|
|
|
cpuinfo := fmt.Sprintf(" %.2f%%\n", cpuPercent[0]) |
|
|
|
|
|
|
|
// 获取内存占用信息
|
|
|
|
memInfo, err := mem.VirtualMemory() |
|
|
|
@ -75,11 +71,11 @@ func SysMonitor(w http.ResponseWriter, r *http.Request) { |
|
|
|
|
|
|
|
//生成proto的内容
|
|
|
|
hdinfo, err := proto.Marshal(&pb.Hdinfo{ |
|
|
|
Hdstat: fmt.Sprintf("使用:%v /总计:%v", hduval, hdtval), |
|
|
|
Procout: fmt.Sprintf("进程数: %d", procnum), |
|
|
|
Hdstat: fmt.Sprintf("%v / %v", hduval, hdtval), |
|
|
|
Procout: fmt.Sprintf(" %d", procnum), |
|
|
|
Cpustat: cpuinfo, |
|
|
|
Memstat: fmt.Sprintf("内存使用率: %.2f%%", memInfo.UsedPercent), |
|
|
|
Netstat: fmt.Sprintf("总接收: %.2f MB", float64(netIO[0].BytesRecv)/1024/1024), |
|
|
|
Memstat: fmt.Sprintf(" %.2f%%", memInfo.UsedPercent), |
|
|
|
Netstat: fmt.Sprintf(" %.2f MB", float64(netIO[0].BytesRecv)/1024/1024), |
|
|
|
}) |
|
|
|
if err != nil { |
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError) |
|
|
|
@ -91,20 +87,16 @@ func SysMonitor(w http.ResponseWriter, r *http.Request) { |
|
|
|
// 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{ |
|
|
|
sysresp := SysResp{ |
|
|
|
Status: 0, |
|
|
|
Message: "success", |
|
|
|
Data: encdata, |
|
|
|
}) |
|
|
|
} |
|
|
|
|
|
|
|
w.Write(jsondata) |
|
|
|
// 增加跨域
|
|
|
|
uCorsHadler(w, r) |
|
|
|
json.NewEncoder(w).Encode(sysresp) |
|
|
|
} |
|
|
|
|