fix: 抓包结束返回日志文件名用于查看内容

This commit is contained in:
TsMask
2024-10-15 14:56:54 +08:00
parent 318fc6cc20
commit 5c0a49b820
3 changed files with 17 additions and 102 deletions

View File

@@ -2,16 +2,13 @@ package service
import (
"fmt"
"os"
"path/filepath"
"regexp"
"runtime"
"strings"
"sync"
"time"
"be.ems/src/framework/logger"
"be.ems/src/framework/utils/file"
"be.ems/src/framework/utils/ssh"
neService "be.ems/src/modules/network_element/service"
)
@@ -97,16 +94,16 @@ func (s *TCPdump) DumpStart(neType, neId, cmdStr string) (string, error) {
}
// DumpStop 停止已存在抓包句柄
func (s *TCPdump) DumpStop(neType, neId, taskCode string) (string, error) {
func (s *TCPdump) DumpStop(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")
return []string{}, fmt.Errorf("app.common.noNEInfo")
}
// 网元主机的SSH客户端
sshClient, err := s.neInfoService.NeRunSSHClient(neInfo.NeType, neInfo.NeId)
if err != nil {
return "", err
return []string{}, err
}
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)
PIDMap, ok := dumpPIDMap.Load(pidKey)
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"]
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
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)
// pids=$(pgrep -P 1914341) && [ -n "$pids" ] && sudo kill $pids;sudo timeout 2s cat tcpdump.log
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 ls /tmp/omc/tcpdump/udm/001/20240817104241
output, err := sshClient.RunCMD(sendCmd)
if err != nil || strings.HasPrefix(output, "stderr:") {
logger.Errorf("DumpStop err: %s => %s", strings.TrimSpace(output), err.Error())
return "", err
output = strings.TrimSpace(output)
if err != nil || strings.HasPrefix(output, "ls: ") {
logger.Errorf("DumpStop err: %s => %s", output, err.Error())
return []string{}, err
}
s.logFileLastLineToFile(PIDMap.(map[string]string), sshClient)
files := strings.Split(output, "\n")
dumpPIDMap.Delete(pidKey)
return output, nil
return files, nil
}
// logFileLastLine 日志文件最后行号
@@ -207,53 +206,6 @@ func (s *TCPdump) logFileLastLineToFile(PIDMap map[string]string, sshClient *ssh
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标准版内部抓包
func (s *TCPdump) UPFTrace(neType, neId, cmdStr string) (string, error) {
// 命令检查