2 changed files with 92 additions and 0 deletions
@ -0,0 +1,89 @@ |
|||||
|
package core |
||||
|
|
||||
|
import ( |
||||
|
"encoding/json" |
||||
|
"fmt" |
||||
|
"net" |
||||
|
"net/http" |
||||
|
"os" |
||||
|
"os/exec" |
||||
|
"os/user" |
||||
|
"runtime" |
||||
|
) |
||||
|
|
||||
|
// json 结构体
|
||||
|
type HdResp struct { |
||||
|
Status string `json:"status"` //状态
|
||||
|
Hip string `json:"hostip"` //主机ip
|
||||
|
Hname string `json:"hostname"` //主机名
|
||||
|
Cpu string `json:"cpu"` //cpu
|
||||
|
Mem string `json:"mem"` //内存
|
||||
|
Disks string `json:"disks"` //磁盘
|
||||
|
Net string `json:"net"` //网络
|
||||
|
|
||||
|
} |
||||
|
|
||||
|
// 服务器的硬件信息
|
||||
|
func Hdinfo(w http.ResponseWriter, r *http.Request) { |
||||
|
// 获取服务器的信息
|
||||
|
hostip, err := GetHostIP() |
||||
|
if err != nil { |
||||
|
fmt.Println("获取主机IP失败:", err) |
||||
|
return |
||||
|
} |
||||
|
hostname, err := os.Hostname() |
||||
|
if err != nil { |
||||
|
fmt.Println("获取主机名失败:", err) |
||||
|
return |
||||
|
} |
||||
|
// respone file list
|
||||
|
response := HdResp{ |
||||
|
Status: "success", |
||||
|
Hip: hostip, |
||||
|
Hname: hostname, |
||||
|
Cpu: runtime.NumCPU(), |
||||
|
Mem: fmt.Sprintf("%d", runtime.NumGoroutine()), |
||||
|
Disks: "", // 因 getDiskInfo 未定义,暂时使用空字符串占位,需实现 getDiskInfo 函数后再修改
|
||||
|
|
||||
|
} |
||||
|
// 开启跨域
|
||||
|
uCorsHadler(w, r) |
||||
|
json.NewEncoder(w).Encode(response) |
||||
|
} |
||||
|
|
||||
|
func getMachieNet() { |
||||
|
ifaces, err := net.Interfaces() |
||||
|
if err != nil { |
||||
|
fmt.Println("无法获取网络接口信息:", err) |
||||
|
} else { |
||||
|
for _, iface := range ifaces { |
||||
|
fmt.Println("网络接口名称:", iface.Name) |
||||
|
fmt.Println("硬件地址 (MAC):", iface.HardwareAddr) |
||||
|
addrs, _ := iface.Addrs() |
||||
|
for _, addr := range addrs { |
||||
|
fmt.Println("IP地址:", addr.String()) |
||||
|
} |
||||
|
fmt.Println() |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
func getCurrentUser() (*user.User, error) { |
||||
|
currentUser, err := user.Current() |
||||
|
if err != nil { |
||||
|
return nil, err |
||||
|
} |
||||
|
return currentUser, nil |
||||
|
} |
||||
|
|
||||
|
func getDiskInfo() (string, error) { |
||||
|
var diskInfo string |
||||
|
// 执行 df 命令获取磁盘信息
|
||||
|
cmd := exec.Command("df", "-h") |
||||
|
output, err := cmd.Output() |
||||
|
if err != nil { |
||||
|
return "", err |
||||
|
} |
||||
|
diskInfo = string(output) |
||||
|
return diskInfo, nil |
||||
|
} |
||||
Loading…
Reference in new issue