监控进程和网络
This commit is contained in:
62
features/monitor/psnet/psnet.go
Normal file
62
features/monitor/psnet/psnet.go
Normal file
@@ -0,0 +1,62 @@
|
||||
package psnet
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"ems.agt/lib/log"
|
||||
"ems.agt/lib/services"
|
||||
"ems.agt/lib/wsinfo"
|
||||
"ems.agt/restagent/config"
|
||||
"github.com/gorilla/websocket"
|
||||
"github.com/shirou/gopsutil/process"
|
||||
)
|
||||
|
||||
var (
|
||||
// websockte通信
|
||||
UriWs = config.UriPrefix + "/monitor/{apiVersion}/psnet/ws"
|
||||
// 停止进程
|
||||
UriStop = config.UriPrefix + "/monitor/{apiVersion}/psnet/stop"
|
||||
)
|
||||
|
||||
// 进程管理
|
||||
var wsUpgrade = websocket.Upgrader{
|
||||
CheckOrigin: func(r *http.Request) bool {
|
||||
return true
|
||||
},
|
||||
}
|
||||
|
||||
// ProcessWs
|
||||
func ProcessWs(w http.ResponseWriter, r *http.Request) {
|
||||
ws, err := wsUpgrade.Upgrade(w, r, nil)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
wsClient := wsinfo.NewWsClient("processClient", ws)
|
||||
go wsClient.Read()
|
||||
go wsClient.Write()
|
||||
}
|
||||
|
||||
// 停止进程 {"PID":30040}
|
||||
func StopProcess(w http.ResponseWriter, r *http.Request) {
|
||||
// json 請求參數獲取
|
||||
var bodyArgs struct {
|
||||
PID int32 `json:"PID" validate:"required"`
|
||||
}
|
||||
err := services.ShouldBindJSON(r, &bodyArgs)
|
||||
if err != nil {
|
||||
log.Error("io.ReadAll is failed:", err)
|
||||
services.ResponseErrorWithJson(w, 400, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
proc, err := process.NewProcess(bodyArgs.PID)
|
||||
if err != nil {
|
||||
services.ResponseErrorWithJson(w, 400, err.Error())
|
||||
return
|
||||
}
|
||||
if err := proc.Kill(); err != nil {
|
||||
services.ResponseErrorWithJson(w, 400, err.Error())
|
||||
return
|
||||
}
|
||||
services.ResponseStatusOK200Null(w)
|
||||
}
|
||||
Reference in New Issue
Block a user