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.
90 lines
1.9 KiB
90 lines
1.9 KiB
package core
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"net/rpc/jsonrpc"
|
|
"runtime"
|
|
)
|
|
|
|
type Dtm struct {
|
|
TotalAlloc string `json:"total_alloc"`
|
|
Alloc string `json:"alloc"`
|
|
}
|
|
|
|
// for rpc
|
|
type Args struct {
|
|
FilePath, Scope string
|
|
}
|
|
|
|
// 返回的参数
|
|
type Reply struct {
|
|
FilePath string `json:"file_path"`
|
|
}
|
|
|
|
// 获取内存信息
|
|
func Dtmem(w http.ResponseWriter, r *http.Request) {
|
|
var m runtime.MemStats
|
|
runtime.ReadMemStats(&m)
|
|
|
|
// 测试JSONRPC
|
|
service := "192.168.66.16:9098"
|
|
client, err := jsonrpc.Dial("tcp", service)
|
|
if err != nil {
|
|
fmt.Fprintf(w, "jsonrpc dial faild %v", err)
|
|
return
|
|
}
|
|
// 调用远程方法
|
|
var reply string
|
|
var args = Args{"/www/xt", "all"}
|
|
err = client.Call("FileRpcService.GetFilePath", args, &reply)
|
|
|
|
if err != nil {
|
|
fmt.Fprintf(w, "jsonrpc call faild %v", err)
|
|
return
|
|
}
|
|
|
|
// 服务的状态
|
|
// var scs string
|
|
// err = client.Call("SysmonitorService.GetSysInfo", args, &scs)
|
|
// if err != nil {
|
|
// fmt.Fprintf(w, "jsonrpc call faild %v", err)
|
|
// return
|
|
// }
|
|
// fmt.Printf("scs: %v\n", scs)
|
|
|
|
// fmt.Printf("reply: %v", reply)
|
|
//replyJson, err := json.Marshal(reply)
|
|
// if err != nil {
|
|
// fmt.Fprintf(w, "json marshal faild %v", err)
|
|
// return
|
|
// }
|
|
|
|
// 打印返回值
|
|
// fmt.Fprintf(w, "jsonrpc call success, reply: %v", replyJson)
|
|
|
|
// 内存动态
|
|
// dtm := Dtm{
|
|
// TotalAlloc: fmt.Sprintf("%d", m.TotalAlloc),
|
|
// Alloc: fmt.Sprintf("%v MB", m.Alloc/1024),
|
|
// }
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
// json.NewEncoder(w).Encode(dtm)
|
|
w.Write([]byte(reply))
|
|
//
|
|
// jsonobj, err := json.Marshal(m)
|
|
// if err != nil {
|
|
// fmt.Fprintf(w, "json marshal faild %v", err)
|
|
// return
|
|
// }
|
|
|
|
// fmt.Fprintf(w, "%s", jsonobj)
|
|
|
|
// usage:=m.TotalAlloc/1024/1024
|
|
// free:=m.Free/1024/1024
|
|
|
|
// //
|
|
// fmt.Fprintf("总分配内存: %v MB\n", m.TotalAlloc/1024/1024)
|
|
// fmt.Fprintf("已分配内存: %v MB\n", m.Alloc/1024/1024)
|
|
}
|
|
|