fix: 抓包结束返回日志文件名用于查看内容
This commit is contained in:
@@ -1,9 +1,6 @@
|
|||||||
package controller
|
package controller
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
|
|
||||||
"be.ems/src/framework/i18n"
|
"be.ems/src/framework/i18n"
|
||||||
"be.ems/src/framework/utils/ctx"
|
"be.ems/src/framework/utils/ctx"
|
||||||
"be.ems/src/framework/vo/result"
|
"be.ems/src/framework/vo/result"
|
||||||
@@ -67,42 +64,12 @@ func (s *TCPdumpController) DumpStop(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
taskLog, err := s.tcpdumpService.DumpStop(body.NeType, body.NeId, body.TaskCode)
|
logFiles, err := s.tcpdumpService.DumpStop(body.NeType, body.NeId, body.TaskCode)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, err.Error())))
|
c.JSON(200, result.ErrMsg(i18n.TKey(language, err.Error())))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c.JSON(200, result.OkMsg(taskLog))
|
c.JSON(200, result.OkData(logFiles))
|
||||||
}
|
|
||||||
|
|
||||||
// 网元抓包PACP 下载
|
|
||||||
//
|
|
||||||
// GET /download
|
|
||||||
func (s *TCPdumpController) DumpDownload(c *gin.Context) {
|
|
||||||
language := ctx.AcceptLanguage(c)
|
|
||||||
var query struct {
|
|
||||||
NeType string `form:"neType" binding:"required"` // 网元类型
|
|
||||||
NeID string `form:"neId" binding:"required"` // 网元ID
|
|
||||||
TaskCode string `form:"taskCode" binding:"required"` // 任务码,停止任务并查看日志信息
|
|
||||||
DelTemp bool `form:"delTemp"` // 完成后是否删除本地临时zip文件
|
|
||||||
}
|
|
||||||
if err := c.ShouldBindQuery(&query); err != nil {
|
|
||||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
zipFilePath, err := s.tcpdumpService.DumpDownload(query.NeType, query.NeID, query.TaskCode)
|
|
||||||
if err != nil {
|
|
||||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, err.Error())))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
defer func() {
|
|
||||||
if query.DelTemp {
|
|
||||||
_ = os.Remove(zipFilePath)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
c.FileAttachment(zipFilePath, filepath.Base(zipFilePath))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// UPF标准版内部抓包
|
// UPF标准版内部抓包
|
||||||
|
|||||||
@@ -2,16 +2,13 @@ package service
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
"runtime"
|
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"be.ems/src/framework/logger"
|
"be.ems/src/framework/logger"
|
||||||
"be.ems/src/framework/utils/file"
|
|
||||||
"be.ems/src/framework/utils/ssh"
|
"be.ems/src/framework/utils/ssh"
|
||||||
neService "be.ems/src/modules/network_element/service"
|
neService "be.ems/src/modules/network_element/service"
|
||||||
)
|
)
|
||||||
@@ -97,16 +94,16 @@ func (s *TCPdump) DumpStart(neType, neId, cmdStr string) (string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// DumpStop 停止已存在抓包句柄
|
// DumpStop 停止已存在抓包句柄
|
||||||
func (s *TCPdump) DumpStop(neType, neId, taskCode string) (string, error) {
|
func (s *TCPdump) DumpStop(neType, neId, taskCode string) ([]string, error) {
|
||||||
// 查询网元获取IP
|
// 查询网元获取IP
|
||||||
neInfo := s.neInfoService.SelectNeInfoByNeTypeAndNeID(neType, neId)
|
neInfo := s.neInfoService.SelectNeInfoByNeTypeAndNeID(neType, neId)
|
||||||
if neInfo.NeId != neId || neInfo.IP == "" {
|
if neInfo.NeId != neId || neInfo.IP == "" {
|
||||||
return "", fmt.Errorf("app.common.noNEInfo")
|
return []string{}, fmt.Errorf("app.common.noNEInfo")
|
||||||
}
|
}
|
||||||
// 网元主机的SSH客户端
|
// 网元主机的SSH客户端
|
||||||
sshClient, err := s.neInfoService.NeRunSSHClient(neInfo.NeType, neInfo.NeId)
|
sshClient, err := s.neInfoService.NeRunSSHClient(neInfo.NeType, neInfo.NeId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return []string{}, err
|
||||||
}
|
}
|
||||||
defer sshClient.Close()
|
defer sshClient.Close()
|
||||||
|
|
||||||
@@ -114,26 +111,28 @@ func (s *TCPdump) DumpStop(neType, neId, taskCode string) (string, error) {
|
|||||||
pidKey := fmt.Sprintf("%s_%s_%s", strings.ToLower(neInfo.NeType), neInfo.NeId, taskCode)
|
pidKey := fmt.Sprintf("%s_%s_%s", strings.ToLower(neInfo.NeType), neInfo.NeId, taskCode)
|
||||||
PIDMap, ok := dumpPIDMap.Load(pidKey)
|
PIDMap, ok := dumpPIDMap.Load(pidKey)
|
||||||
if !ok || PIDMap == nil {
|
if !ok || PIDMap == nil {
|
||||||
return "", fmt.Errorf("tcpdump is not running")
|
return []string{}, fmt.Errorf("tcpdump is not running")
|
||||||
}
|
}
|
||||||
pid, ok := PIDMap.(map[string]string)["pid"]
|
pid, ok := PIDMap.(map[string]string)["pid"]
|
||||||
if !ok || pid == "" {
|
if !ok || pid == "" {
|
||||||
return "", fmt.Errorf("tcpdump is not running")
|
return []string{}, fmt.Errorf("tcpdump is not running")
|
||||||
}
|
}
|
||||||
|
s.logFileLastLineToFile(PIDMap.(map[string]string), sshClient)
|
||||||
|
|
||||||
// 存放文件目录 /tmp/omc/tcpdump/udm/001/20240817104241
|
// 存放文件目录 /tmp/omc/tcpdump/udm/001/20240817104241
|
||||||
neDirTemp := fmt.Sprintf("/tmp/omc/tcpdump/%s/%s/%s", strings.ToLower(neInfo.NeType), neInfo.NeId, taskCode)
|
neDirTemp := fmt.Sprintf("/tmp/omc/tcpdump/%s/%s/%s", strings.ToLower(neInfo.NeType), neInfo.NeId, taskCode)
|
||||||
// 命令拼装
|
// 命令拼装
|
||||||
sendCmd := fmt.Sprintf("pids=$(pgrep -P %s) && [ -n \"$pids\" ] && sudo kill $pids;sudo timeout 2s cat %s/tcpdump.log", pid, neDirTemp)
|
sendCmd := fmt.Sprintf("pids=$(pgrep -P %s) && [ -n \"$pids\" ] && sudo kill $pids;sudo timeout 2s ls %s", pid, neDirTemp)
|
||||||
// pids=$(pgrep -P 1914341) && [ -n "$pids" ] && sudo kill $pids;sudo timeout 2s cat tcpdump.log
|
// pids=$(pgrep -P 1914341) && [ -n "$pids" ] && sudo kill $pids;sudo timeout 2s ls /tmp/omc/tcpdump/udm/001/20240817104241
|
||||||
output, err := sshClient.RunCMD(sendCmd)
|
output, err := sshClient.RunCMD(sendCmd)
|
||||||
if err != nil || strings.HasPrefix(output, "stderr:") {
|
output = strings.TrimSpace(output)
|
||||||
logger.Errorf("DumpStop err: %s => %s", strings.TrimSpace(output), err.Error())
|
if err != nil || strings.HasPrefix(output, "ls: ") {
|
||||||
return "", err
|
logger.Errorf("DumpStop err: %s => %s", output, err.Error())
|
||||||
|
return []string{}, err
|
||||||
}
|
}
|
||||||
|
files := strings.Split(output, "\n")
|
||||||
s.logFileLastLineToFile(PIDMap.(map[string]string), sshClient)
|
|
||||||
dumpPIDMap.Delete(pidKey)
|
dumpPIDMap.Delete(pidKey)
|
||||||
return output, nil
|
return files, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// logFileLastLine 日志文件最后行号
|
// logFileLastLine 日志文件最后行号
|
||||||
@@ -207,53 +206,6 @@ func (s *TCPdump) logFileLastLineToFile(PIDMap map[string]string, sshClient *ssh
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// DumpDownload 抓包文件网元端复制到本地输出zip文件
|
|
||||||
func (s *TCPdump) DumpDownload(neType, neId, taskCode string) (string, error) {
|
|
||||||
// 查询网元获取IP
|
|
||||||
neInfo := s.neInfoService.SelectNeInfoByNeTypeAndNeID(neType, neId)
|
|
||||||
if neInfo.NeId != neId || neInfo.IP == "" {
|
|
||||||
return "", fmt.Errorf("app.common.noNEInfo")
|
|
||||||
}
|
|
||||||
// 网元主机的SSH客户端
|
|
||||||
sshClient, err := s.neInfoService.NeRunSSHClient(neInfo.NeType, neInfo.NeId)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
defer sshClient.Close()
|
|
||||||
// 网元主机的SSH客户端进行文件传输
|
|
||||||
sftpClient, err := sshClient.NewClientSFTP()
|
|
||||||
if err != nil {
|
|
||||||
return "", fmt.Errorf("ne info sftp client err")
|
|
||||||
}
|
|
||||||
defer sftpClient.Close()
|
|
||||||
|
|
||||||
neTypeLower := strings.ToLower(neInfo.NeType)
|
|
||||||
// 网管本地路径
|
|
||||||
localDirPath := fmt.Sprintf("/tmp/omc/tcpdump/zip/%s/%s", neTypeLower, neInfo.NeId)
|
|
||||||
if runtime.GOOS == "windows" {
|
|
||||||
localDirPath = fmt.Sprintf("C:%s", localDirPath)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 网元pcap目录 /tmp/omc/tcpdump/udm/001/20240817104241
|
|
||||||
sshClient.RunCMD("mkdir -p /tmp/omc && sudo chmod 777 -R /tmp/omc")
|
|
||||||
neDirTemp := fmt.Sprintf("/tmp/omc/tcpdump/%s/%s/%s", neTypeLower, neInfo.NeId, taskCode)
|
|
||||||
// 网元端复制到本地
|
|
||||||
localDirFilePath := filepath.Join(localDirPath, taskCode)
|
|
||||||
if err = sftpClient.CopyDirRemoteToLocal(neDirTemp, localDirFilePath); err != nil {
|
|
||||||
return "", fmt.Errorf("copy tcpdump file err")
|
|
||||||
}
|
|
||||||
|
|
||||||
// 压缩zip文件名
|
|
||||||
zipFileName := fmt.Sprintf("%s-%s-pcap-%s.zip", neTypeLower, neInfo.NeId, taskCode)
|
|
||||||
zipFilePath := filepath.Join(localDirPath, zipFileName)
|
|
||||||
if err := file.CompressZipByDir(zipFilePath, localDirFilePath); err != nil {
|
|
||||||
return "", fmt.Errorf("compress zip err")
|
|
||||||
}
|
|
||||||
|
|
||||||
_ = os.RemoveAll(localDirFilePath) // 删除本地临时目录
|
|
||||||
return zipFilePath, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// UPFTrace UPF标准版内部抓包
|
// UPFTrace UPF标准版内部抓包
|
||||||
func (s *TCPdump) UPFTrace(neType, neId, cmdStr string) (string, error) {
|
func (s *TCPdump) UPFTrace(neType, neId, cmdStr string) (string, error) {
|
||||||
// 命令检查
|
// 命令检查
|
||||||
|
|||||||
@@ -30,10 +30,6 @@ func Setup(router *gin.Engine) {
|
|||||||
collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.tcpdump", collectlogs.BUSINESS_TYPE_OTHER)),
|
collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.tcpdump", collectlogs.BUSINESS_TYPE_OTHER)),
|
||||||
controller.NewTCPdump.DumpStop,
|
controller.NewTCPdump.DumpStop,
|
||||||
)
|
)
|
||||||
tcpdumpGroup.GET("/download",
|
|
||||||
middleware.PreAuthorize(nil),
|
|
||||||
controller.NewTCPdump.DumpDownload,
|
|
||||||
)
|
|
||||||
tcpdumpGroup.POST("/upf",
|
tcpdumpGroup.POST("/upf",
|
||||||
middleware.PreAuthorize(nil),
|
middleware.PreAuthorize(nil),
|
||||||
collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.tcpdump", collectlogs.BUSINESS_TYPE_OTHER)),
|
collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.tcpdump", collectlogs.BUSINESS_TYPE_OTHER)),
|
||||||
|
|||||||
Reference in New Issue
Block a user