监控进程和网络
This commit is contained in:
@@ -1,51 +0,0 @@
|
|||||||
package monitor
|
|
||||||
|
|
||||||
import (
|
|
||||||
"net/http"
|
|
||||||
|
|
||||||
websocket2 "ems.agt/lib/websocket"
|
|
||||||
"ems.agt/restagent/config"
|
|
||||||
"github.com/gorilla/websocket"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
// 進程
|
|
||||||
UriWs = config.UriPrefix + "/monitor/{apiVersion}/process/ws"
|
|
||||||
)
|
|
||||||
|
|
||||||
var wsUpgrade = websocket.Upgrader{
|
|
||||||
CheckOrigin: func(r *http.Request) bool {
|
|
||||||
return true
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
func ProcessWs(w http.ResponseWriter, r *http.Request) {
|
|
||||||
ws, err := wsUpgrade.Upgrade(w, r, nil)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
wsClient := websocket2.NewWsClient("processClient", ws)
|
|
||||||
go wsClient.Read()
|
|
||||||
go wsClient.Write()
|
|
||||||
}
|
|
||||||
|
|
||||||
// @Tags Process
|
|
||||||
// @Summary Stop Process
|
|
||||||
// @Description 停止进程
|
|
||||||
// @Param request body request.ProcessReq true "request"
|
|
||||||
// @Success 200
|
|
||||||
// @Security ApiKeyAuth
|
|
||||||
// @Router /process/stop [post]
|
|
||||||
// @x-panel-log {"bodyKeys":["PID"],"paramKeys":[],"BeforeFuntions":[],"formatZH":"结束进程 [PID]","formatEN":"结束进程 [PID]"}
|
|
||||||
func StopProcess(w http.ResponseWriter, r *http.Request) {
|
|
||||||
// var req request.ProcessReq
|
|
||||||
// if err := c.ShouldBindJSON(&req); err != nil {
|
|
||||||
// helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
|
||||||
// return
|
|
||||||
// }
|
|
||||||
// if err := processService.StopProcess(req); err != nil {
|
|
||||||
// helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
|
||||||
// return
|
|
||||||
// }
|
|
||||||
// helper.SuccessWithOutData(c)
|
|
||||||
}
|
|
||||||
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)
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package websocket
|
package wsinfo
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package websocket
|
package wsinfo
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
Reference in New Issue
Block a user