feat: 跟踪模块
This commit is contained in:
262
src/modules/trace/controller/tcpdump.go
Normal file
262
src/modules/trace/controller/tcpdump.go
Normal file
@@ -0,0 +1,262 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"ems.agt/src/framework/cmd"
|
||||
"ems.agt/src/framework/config"
|
||||
"ems.agt/src/framework/utils/ssh"
|
||||
"ems.agt/src/framework/vo/result"
|
||||
netElementService "ems.agt/src/modules/net_element/service"
|
||||
traceService "ems.agt/src/modules/trace/service"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gin-gonic/gin/binding"
|
||||
)
|
||||
|
||||
// 实例化控制层 TcpdumpController 结构体
|
||||
var NewTcpdump = &TcpdumpController{
|
||||
NeInfoService: netElementService.NewNeInfoImpl,
|
||||
TcpdumpService: traceService.NewTcpdumpImpl,
|
||||
}
|
||||
|
||||
// 信令抓包请求
|
||||
//
|
||||
// PATH /tcpdump
|
||||
type TcpdumpController struct {
|
||||
// 网元信息服务
|
||||
NeInfoService netElementService.INeInfo
|
||||
// 信令抓包服务
|
||||
TcpdumpService traceService.ITcpdump
|
||||
}
|
||||
|
||||
// 网元发送执行 pcap
|
||||
//
|
||||
// POST /ne
|
||||
func (s *TcpdumpController) NeTask(c *gin.Context) {
|
||||
var body struct {
|
||||
NeType string `json:"neType" binding:"required"` // 网元类型
|
||||
NeId string `json:"neId" binding:"required"` // 网元ID
|
||||
Timeout int `json:"timeout" binding:"required"` // 超时时间
|
||||
Cmd string `json:"cmd" binding:"required"` // 命令
|
||||
Timestamp string `json:"timestamp" binding:"required"` // 时间戳
|
||||
}
|
||||
err := c.ShouldBindBodyWith(&body, binding.JSON)
|
||||
if err != nil {
|
||||
c.JSON(400, result.CodeMsg(400, "参数错误"))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查网元信息
|
||||
neInfo := s.NeInfoService.SelectNeInfoByNeTypeAndNeID(body.NeType, body.NeId)
|
||||
if neInfo.NeId != body.NeId {
|
||||
msg := fmt.Sprintf("找不到 %s %s 对应网元信息", body.NeType, body.NeId)
|
||||
c.JSON(200, result.ErrMsg(msg))
|
||||
return
|
||||
}
|
||||
|
||||
filePcapName := fmt.Sprintf("%s_%s_%s.pcap", body.Timestamp, body.NeType, body.NeId)
|
||||
fileLogName := fmt.Sprintf("%s_%s_%s.log", body.Timestamp, body.NeType, body.NeId)
|
||||
writeLog := fmt.Sprintf(" > %s 2>&1 \ncat %s", fileLogName, fileLogName) // 执行信息写入日志文件,放置弹出code 127
|
||||
cmdStr := fmt.Sprintf("cd /tmp \nsudo timeout %d tcpdump -i any %s -s0 -w %s", body.Timeout, body.Cmd, filePcapName)
|
||||
usernameNe := config.Get("ne.user").(string) // 网元统一用户
|
||||
sshHost := fmt.Sprintf("%s@%s", usernameNe, neInfo.IP)
|
||||
msg, err := cmd.ExecWithCheck("ssh", sshHost, cmdStr+writeLog)
|
||||
if err != nil {
|
||||
c.JSON(200, result.ErrMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(200, result.OkData(map[string]any{
|
||||
"cmd": cmdStr,
|
||||
"msg": msg,
|
||||
"fileName": filePcapName,
|
||||
}))
|
||||
}
|
||||
|
||||
// 网元抓包pcap文件下载
|
||||
//
|
||||
// POST /download
|
||||
func (s *TcpdumpController) Download(c *gin.Context) {
|
||||
var body struct {
|
||||
NeType string `json:"neType" binding:"required"` // 网元类型
|
||||
NeId string `json:"neId" binding:"required"` // 网元ID
|
||||
FileName string `form:"fileName" ` // 文件名
|
||||
}
|
||||
err := c.ShouldBindBodyWith(&body, binding.JSON)
|
||||
if err != nil {
|
||||
c.JSON(400, result.CodeMsg(400, "参数错误"))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查网元信息
|
||||
neInfo := s.NeInfoService.SelectNeInfoByNeTypeAndNeID(body.NeType, body.NeId)
|
||||
if neInfo.NeId != body.NeId {
|
||||
msg := fmt.Sprintf("找不到 %s %s 对应网元信息", body.NeType, body.NeId)
|
||||
c.JSON(200, result.ErrMsg(msg))
|
||||
return
|
||||
}
|
||||
|
||||
nePath := fmt.Sprintf("/tmp/%s", body.FileName)
|
||||
localPath := fmt.Sprintf("%s/tcpdump/%s", config.Get("ne.scpdir"), body.FileName)
|
||||
err = ssh.FileSCPNeToLocal(neInfo.IP, nePath, localPath)
|
||||
if err != nil {
|
||||
c.JSON(200, result.ErrMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
c.FileAttachment(localPath, body.FileName)
|
||||
}
|
||||
|
||||
// 网元发送执行 pcap
|
||||
//
|
||||
// POST /neUPF
|
||||
func (s *TcpdumpController) NeUPFTask(c *gin.Context) {
|
||||
var body struct {
|
||||
NeType string `json:"neType" binding:"required"` // 网元类型
|
||||
NeId string `json:"neId" binding:"required"` // 网元ID
|
||||
RunType string `json:"runType" binding:"required"` // 执行开始start还是停止stop
|
||||
Cmd string `json:"cmd" binding:"required"` // 命令
|
||||
Timestamp string `json:"timestamp" binding:"required"` // 时间戳
|
||||
}
|
||||
err := c.ShouldBindBodyWith(&body, binding.JSON)
|
||||
if err != nil {
|
||||
c.JSON(400, result.CodeMsg(400, "参数错误"))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查网元信息
|
||||
neInfo := s.NeInfoService.SelectNeInfoByNeTypeAndNeID(body.NeType, body.NeId)
|
||||
if neInfo.NeId != body.NeId {
|
||||
msg := fmt.Sprintf("找不到 %s %s 对应网元信息", body.NeType, body.NeId)
|
||||
c.JSON(200, result.ErrMsg(msg))
|
||||
return
|
||||
}
|
||||
|
||||
// 开始telnet
|
||||
if body.RunType == "start_telnet" {
|
||||
filePcapName := fmt.Sprintf("%s_%s_%s.pcap", body.Timestamp, body.NeType, body.NeId)
|
||||
cmdStr := fmt.Sprintf("%s file %s", body.Cmd, filePcapName)
|
||||
// 进行连接telnet
|
||||
resultStr, err := s.TcpdumpService.UPFTelnet(neInfo.IP, cmdStr)
|
||||
if err != nil {
|
||||
c.JSON(200, result.ErrMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
// 处理结果
|
||||
s := strings.Index(resultStr, "pcap dispatch trace:")
|
||||
if s == -1 {
|
||||
s = strings.Index(resultStr, "Write ")
|
||||
}
|
||||
if s != -1 {
|
||||
e := strings.Index(resultStr, "\r\nupfd1#")
|
||||
resultStr = resultStr[s:e]
|
||||
} else {
|
||||
resultStr = "No stoppable found"
|
||||
}
|
||||
c.JSON(200, result.OkData(map[string]any{
|
||||
"cmd": cmdStr,
|
||||
"msg": resultStr,
|
||||
"fileName": filePcapName,
|
||||
}))
|
||||
return
|
||||
}
|
||||
// 停止telnet
|
||||
if body.RunType == "stop_telnet" {
|
||||
filePcapName := fmt.Sprintf("%s_%s_%s.pcap", body.Timestamp, body.NeType, body.NeId)
|
||||
cmdStr := "pcap dispatch trace off"
|
||||
// 进行连接telnet
|
||||
resultStr, err := s.TcpdumpService.UPFTelnet(neInfo.IP, cmdStr)
|
||||
if err != nil {
|
||||
c.JSON(200, result.ErrMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
// 处理结果
|
||||
s := strings.Index(resultStr, "pcap dispatch trace:")
|
||||
if s != -1 {
|
||||
e := strings.Index(resultStr, "\r\nupfd1#")
|
||||
resultStr = resultStr[s:e]
|
||||
} else {
|
||||
resultStr = "Executed, please stop before proceeding"
|
||||
}
|
||||
c.JSON(200, result.OkData(map[string]any{
|
||||
"cmd": cmdStr,
|
||||
"msg": resultStr,
|
||||
"fileName": filePcapName,
|
||||
}))
|
||||
return
|
||||
}
|
||||
|
||||
// 开始-脚本字符串
|
||||
if body.RunType == "start_str" {
|
||||
fileLogName := fmt.Sprintf("%s_%s_%s.log", body.Timestamp, body.NeType, body.NeId)
|
||||
filePcapName := fmt.Sprintf("%s_%s_%s.pcap", body.Timestamp, body.NeType, body.NeId)
|
||||
scriptStr := "set capcmd [lindex $argv 0]\nspawn telnet localhost 5002\nexpect \"upfd1# \"\nsend \"$capcmd\\n\"\nexpect \"upfd1# \"\nsend \"quit\\n\"\nexpect \"eof\""
|
||||
writeLog := fmt.Sprintf(" > %s 2>&1 \ncat %s", fileLogName, fileLogName) // 执行信息写入日志文件输出,避免弹出code 127
|
||||
|
||||
capCmdStr := fmt.Sprintf("%s file %s", body.Cmd, filePcapName)
|
||||
|
||||
cmdStr := fmt.Sprintf("cd /tmp\n\necho '%s' > cap.sh\n\nchmod +x cap.sh\n\nexpect ./cap.sh '%s'%s", scriptStr, capCmdStr, writeLog)
|
||||
usernameNe := config.Get("ne.user").(string) // 网元统一用户
|
||||
sshHost := fmt.Sprintf("%s@%s", usernameNe, neInfo.IP)
|
||||
msg, err := cmd.ExecWithCheck("ssh", sshHost, cmdStr)
|
||||
if err != nil {
|
||||
c.JSON(200, result.ErrMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
s := strings.Index(msg, "pcap dispatch trace:")
|
||||
if s != -1 {
|
||||
e := strings.Index(msg, "\r\nupfd1#")
|
||||
msg = msg[s:e]
|
||||
} else {
|
||||
msg = "Executed, please stop before proceeding"
|
||||
}
|
||||
c.JSON(200, result.OkData(map[string]any{
|
||||
"cmd": capCmdStr,
|
||||
"msg": msg,
|
||||
"fileName": filePcapName,
|
||||
}))
|
||||
return
|
||||
}
|
||||
// 停止-脚本字符串
|
||||
if body.RunType == "stop_str" {
|
||||
fileLogName := fmt.Sprintf("%s_%s_%s.log", body.Timestamp, body.NeType, body.NeId)
|
||||
filePcapName := fmt.Sprintf("%s_%s_%s.pcap", body.Timestamp, body.NeType, body.NeId)
|
||||
scriptStr := "set capcmd [lindex $argv 0]\nspawn telnet localhost 5002\nexpect \"upfd1# \"\nsend \"$capcmd\\n\"\nexpect \"upfd1# \"\nsend \"quit\\n\"\nexpect \"eof\""
|
||||
writeLog := fmt.Sprintf(" > %s 2>&1 \ncat %s", fileLogName, fileLogName) // 执行信息写入日志文件输出,避免弹出code 127
|
||||
|
||||
capCmdStr := body.Cmd
|
||||
|
||||
cmdStr := fmt.Sprintf("cd /tmp\n\necho '%s' > cap.sh\n\nchmod +x cap.sh\n\nexpect ./cap.sh '%s'%s", scriptStr, capCmdStr, writeLog)
|
||||
|
||||
usernameNe := config.Get("ne.user").(string) // 网元统一用户
|
||||
sshHost := fmt.Sprintf("%s@%s", usernameNe, neInfo.IP)
|
||||
msg, err := cmd.ExecWithCheck("ssh", sshHost, cmdStr)
|
||||
if err != nil {
|
||||
c.JSON(200, result.ErrMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
s := strings.Index(msg, "pcap dispatch trace:")
|
||||
if s == -1 {
|
||||
s = strings.Index(msg, "Write ")
|
||||
// 停止写入的文件名
|
||||
startIndex := strings.LastIndex(msg, "/") + 1
|
||||
endIndex := strings.LastIndex(msg, ",")
|
||||
filePcapName = msg[startIndex:endIndex]
|
||||
}
|
||||
if s != -1 {
|
||||
e := strings.Index(msg, "\r\nupfd1#")
|
||||
msg = msg[s:e]
|
||||
} else {
|
||||
msg = "No stoppable found"
|
||||
}
|
||||
c.JSON(200, result.OkData(map[string]any{
|
||||
"cmd": capCmdStr,
|
||||
"msg": msg,
|
||||
"fileName": filePcapName,
|
||||
}))
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(200, result.ErrMsg("请选择RunType执行start_telnet/stop_telnet/start_str/stop_str"))
|
||||
}
|
||||
7
src/modules/trace/service/tcpdump.go
Normal file
7
src/modules/trace/service/tcpdump.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package service
|
||||
|
||||
// 通用请求 服务层接口
|
||||
type ITcpdump interface {
|
||||
// UPFTelnetStart UPF进行telnet抓包
|
||||
UPFTelnet(neIp, cmdStr string) (string, error)
|
||||
}
|
||||
41
src/modules/trace/service/tcpdump.impl.go
Normal file
41
src/modules/trace/service/tcpdump.impl.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"time"
|
||||
|
||||
netElementService "ems.agt/src/modules/net_element/service"
|
||||
)
|
||||
|
||||
// 实例化服务层 TcpdumpImpl 结构体
|
||||
var NewTcpdumpImpl = &TcpdumpImpl{
|
||||
neInfoService: netElementService.NewNeInfoImpl,
|
||||
}
|
||||
|
||||
// 通用请求 服务层处理
|
||||
type TcpdumpImpl struct {
|
||||
// 网元信息服务
|
||||
neInfoService netElementService.INeInfo
|
||||
}
|
||||
|
||||
// UPFTelnetStart UPF进行telnet抓包
|
||||
func (s *TcpdumpImpl) UPFTelnet(neIp, cmdStr string) (string, error) {
|
||||
// 创建TCP连接
|
||||
conn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", neIp, 5002))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
fmt.Fprintln(conn, cmdStr)
|
||||
|
||||
// 读取内容
|
||||
time.Sleep(time.Duration(300) * time.Millisecond)
|
||||
buf := make([]byte, 1024*8)
|
||||
n, err := conn.Read(buf)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return string(buf[0:n]), nil
|
||||
}
|
||||
35
src/modules/trace/trace.go
Normal file
35
src/modules/trace/trace.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package trace
|
||||
|
||||
import (
|
||||
"ems.agt/src/framework/logger"
|
||||
"ems.agt/src/framework/middleware"
|
||||
"ems.agt/src/framework/middleware/collectlogs"
|
||||
"ems.agt/src/modules/trace/controller"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// 模块路由注册
|
||||
func Setup(router *gin.Engine) {
|
||||
logger.Infof("开始加载 ====> trace 模块路由")
|
||||
|
||||
// 信令抓包
|
||||
tcpdumpGroup := router.Group("/tcpdump")
|
||||
{
|
||||
tcpdumpGroup.POST("/ne",
|
||||
middleware.PreAuthorize(nil),
|
||||
collectlogs.OperateLog(collectlogs.OptionNew("信令抓包", collectlogs.BUSINESS_TYPE_OTHER)),
|
||||
controller.NewTcpdump.NeTask,
|
||||
)
|
||||
tcpdumpGroup.POST("/neUPF",
|
||||
middleware.PreAuthorize(nil),
|
||||
collectlogs.OperateLog(collectlogs.OptionNew("信令抓包", collectlogs.BUSINESS_TYPE_OTHER)),
|
||||
controller.NewTcpdump.NeUPFTask,
|
||||
)
|
||||
tcpdumpGroup.POST("/download",
|
||||
middleware.PreAuthorize(nil),
|
||||
collectlogs.OperateLog(collectlogs.OptionNew("信令抓包", collectlogs.BUSINESS_TYPE_IMPORT)),
|
||||
controller.NewTcpdump.Download,
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user