feat: omc原始代码
This commit is contained in:
125
src/modules/trace/controller/tcpdump.go
Normal file
125
src/modules/trace/controller/tcpdump.go
Normal file
@@ -0,0 +1,125 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"nms_nbi/src/framework/i18n"
|
||||
"nms_nbi/src/framework/utils/ctx"
|
||||
"nms_nbi/src/framework/vo/result"
|
||||
traceService "nms_nbi/src/modules/trace/service"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gin-gonic/gin/binding"
|
||||
)
|
||||
|
||||
// 实例化控制层 TcpdumpController 结构体
|
||||
var NewTcpdump = &TcpdumpController{
|
||||
TcpdumpService: traceService.NewTcpdumpImpl,
|
||||
}
|
||||
|
||||
// 信令抓包请求
|
||||
//
|
||||
// PATH /tcpdump
|
||||
type TcpdumpController struct {
|
||||
// 信令抓包服务
|
||||
TcpdumpService traceService.ITcpdump
|
||||
}
|
||||
|
||||
// 网元抓包PACP 开始
|
||||
//
|
||||
// POST /start
|
||||
func (s *TcpdumpController) DumpStart(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
var body struct {
|
||||
NeType string `json:"neType" binding:"required"` // 网元类型
|
||||
NeId string `json:"neId" binding:"required"` // 网元ID
|
||||
Cmd string `json:"cmd" binding:"required"` // 命令 "-n -s 0 -v -w"
|
||||
}
|
||||
err := c.ShouldBindBodyWith(&body, binding.JSON)
|
||||
if err != nil {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
|
||||
fileName, err := s.TcpdumpService.DumpStart(body.NeType, body.NeId, body.Cmd)
|
||||
if err != nil {
|
||||
msg := err.Error()
|
||||
if msg == "noData" {
|
||||
// 找不到 %s %s 对应网元信息
|
||||
msg = i18n.TTemplate(language, "trace.tcpdump.noData", map[string]any{"type": body.NeType, "id": body.NeId})
|
||||
}
|
||||
c.JSON(200, result.ErrMsg(msg))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.OkData(map[string]any{
|
||||
"msg": "tcpdump started",
|
||||
"out": fileName,
|
||||
"log": "",
|
||||
}))
|
||||
}
|
||||
|
||||
// 网元抓包PACP 结束
|
||||
//
|
||||
// POST /stop
|
||||
func (s *TcpdumpController) DumpStop(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
var body struct {
|
||||
NeType string `json:"neType" binding:"required"` // 网元类型
|
||||
NeId string `json:"neId" binding:"required"` // 网元ID
|
||||
FileName string `json:"fileName"` // 文件名 查看日志信息
|
||||
}
|
||||
err := c.ShouldBindBodyWith(&body, binding.JSON)
|
||||
if err != nil {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
|
||||
logMsg, err := s.TcpdumpService.DumpStop(body.NeType, body.NeId, body.FileName)
|
||||
if err != nil {
|
||||
msg := err.Error()
|
||||
if msg == "noData" {
|
||||
// 找不到 %s %s 对应网元信息
|
||||
msg := i18n.TTemplate(language, "trace.tcpdump.noData", map[string]any{"type": body.NeType, "id": body.NeId})
|
||||
c.JSON(200, result.ErrMsg(msg))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.ErrMsg(msg))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.OkData(map[string]any{
|
||||
"msg": "tcpdump stopped",
|
||||
"out": body.FileName,
|
||||
"log": logMsg,
|
||||
}))
|
||||
}
|
||||
|
||||
// UPF标准版内部抓包
|
||||
//
|
||||
// POST /traceUPF
|
||||
func (s *TcpdumpController) TraceUPF(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
var body struct {
|
||||
NeType string `json:"neType" binding:"required"` // 网元类型
|
||||
NeId string `json:"neId" binding:"required"` // 网元ID
|
||||
Cmd string `json:"cmd" binding:"required"` // 命令
|
||||
}
|
||||
err := c.ShouldBindBodyWith(&body, binding.JSON)
|
||||
if err != nil {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
|
||||
fileName, logMsg, err := s.TcpdumpService.DumpUPF(body.NeType, body.NeId, body.Cmd)
|
||||
if err != nil {
|
||||
msg := err.Error()
|
||||
if msg == "noData" {
|
||||
// 找不到 %s %s 对应网元信息
|
||||
msg = i18n.TTemplate(language, "trace.tcpdump.noData", map[string]any{"type": body.NeType, "id": body.NeId})
|
||||
}
|
||||
c.JSON(200, result.ErrMsg(msg))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.OkData(map[string]any{
|
||||
"msg": "trace UPF dump pacp",
|
||||
"out": fileName,
|
||||
"log": logMsg,
|
||||
}))
|
||||
}
|
||||
13
src/modules/trace/service/tcpdump.go
Normal file
13
src/modules/trace/service/tcpdump.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package service
|
||||
|
||||
// 信令抓包 服务层接口
|
||||
type ITcpdump interface {
|
||||
// DumpStart 触发tcpdump开始抓包 filePcapName, err
|
||||
DumpStart(neType, neId, cmdStr string) (string, error)
|
||||
|
||||
// DumpStop 停止已存在抓包句柄
|
||||
DumpStop(neType, neId, fileName string) (string, error)
|
||||
|
||||
// DumpUPF UPF标准版抓包
|
||||
DumpUPF(neType, neId, cmdStr string) (string, string, error)
|
||||
}
|
||||
185
src/modules/trace/service/tcpdump.impl.go
Normal file
185
src/modules/trace/service/tcpdump.impl.go
Normal file
@@ -0,0 +1,185 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"nms_nbi/src/framework/config"
|
||||
"nms_nbi/src/framework/logger"
|
||||
"nms_nbi/src/framework/utils/cmd"
|
||||
"nms_nbi/src/framework/utils/date"
|
||||
neService "nms_nbi/src/modules/network_element/service"
|
||||
)
|
||||
|
||||
// 实例化服务层 TcpdumpImpl 结构体
|
||||
var NewTcpdumpImpl = &TcpdumpImpl{
|
||||
neInfoService: neService.NewNeInfoImpl,
|
||||
tcpdumpPIDMap: map[string]string{},
|
||||
}
|
||||
|
||||
// 信令抓包 服务层处理
|
||||
type TcpdumpImpl struct {
|
||||
// 网元信息服务
|
||||
neInfoService neService.INeInfo
|
||||
// 抓包进程PID
|
||||
tcpdumpPIDMap map[string]string
|
||||
}
|
||||
|
||||
// DumpStart 触发tcpdump开始抓包 filePcapName, err
|
||||
func (s *TcpdumpImpl) DumpStart(neType, neId, cmdStr string) (string, error) {
|
||||
// 检查网元信息
|
||||
neInfo := s.neInfoService.SelectNeInfoByNeTypeAndNeID(neType, neId)
|
||||
if neInfo.NeId != neId {
|
||||
return "", fmt.Errorf("noData")
|
||||
}
|
||||
|
||||
// SSH命令
|
||||
usernameNe := config.Get("ne.user").(string) // 网元统一用户
|
||||
sshHost := fmt.Sprintf("%s@%s", usernameNe, neInfo.IP)
|
||||
|
||||
// 是否拥有sudo权限并拼接
|
||||
withSudo := ""
|
||||
if _, err := cmd.ExecWithCheck("ssh", sshHost, "sudo -n uname"); err == nil {
|
||||
withSudo = "sudo "
|
||||
}
|
||||
|
||||
if msg, err := cmd.ExecWithCheck("ssh", sshHost, fmt.Sprintf("%s tcpdump --version", withSudo)); err != nil {
|
||||
// stderr: bash: tcpdump:未找到命令 => exit status 127
|
||||
msg = strings.TrimSpace(msg)
|
||||
logger.Warnf("DumpStart err: %s => %s", msg, err.Error())
|
||||
return "", fmt.Errorf(msg)
|
||||
}
|
||||
|
||||
// 拼装命令
|
||||
neTypeID := fmt.Sprintf("%s_%s", neInfo.NeType, neInfo.NeId)
|
||||
timeStr := date.ParseDateToStr(time.Now(), date.YYYYMMDDHHMMSS)
|
||||
fileName := fmt.Sprintf("%s_%s", timeStr, neTypeID)
|
||||
sendCmd := fmt.Sprintf("cd /tmp \n %s nohup timeout 30m tcpdump -i any %s -s0 -w %s.pcap > %s.log 2>&1 & \necho $!", withSudo, cmdStr, fileName, fileName)
|
||||
// cd /tmp
|
||||
// sudo nohup timeout 60m tcpdump -i any -n -s 0 -v -w -s0 -w 20240115140822_UDM_001.pcap > 20240115140822_UDM_001.log 2>&1 & echo $!
|
||||
msg, err := cmd.ExecWithCheck("ssh", sshHost, sendCmd)
|
||||
msg = strings.TrimSpace(msg)
|
||||
if err != nil || strings.HasPrefix(msg, "stderr:") {
|
||||
logger.Warnf("DumpStart err: %s => %s", msg, err.Error())
|
||||
return "", err
|
||||
}
|
||||
|
||||
// 检查进程 ps aux | grep tcpdump
|
||||
// 强杀 sudo pkill tcpdump
|
||||
s.tcpdumpPIDMap[neTypeID] = msg
|
||||
return fileName, err
|
||||
}
|
||||
|
||||
// DumpStop 停止已存在抓包句柄
|
||||
func (s *TcpdumpImpl) DumpStop(neType, neId, fileName string) (string, error) {
|
||||
// 检查网元信息
|
||||
neInfo := s.neInfoService.SelectNeInfoByNeTypeAndNeID(neType, neId)
|
||||
if neInfo.NeId != neId {
|
||||
return "", fmt.Errorf("noData")
|
||||
}
|
||||
|
||||
// SSH命令
|
||||
usernameNe := config.Get("ne.user").(string) // 网元统一用户
|
||||
sshHost := fmt.Sprintf("%s@%s", usernameNe, neInfo.IP)
|
||||
|
||||
// 是否拥有sudo权限并拼接
|
||||
withSudo := ""
|
||||
if _, err := cmd.ExecWithCheck("ssh", sshHost, "sudo -n uname"); err == nil {
|
||||
withSudo = "sudo "
|
||||
}
|
||||
|
||||
// 是否存在进程
|
||||
neTypeID := fmt.Sprintf("%s_%s", neInfo.NeType, neInfo.NeId)
|
||||
pid, ok := s.tcpdumpPIDMap[neTypeID]
|
||||
if !ok || pid == "" {
|
||||
return "", fmt.Errorf("tcpdump is not running")
|
||||
}
|
||||
|
||||
// 查看日志
|
||||
viewLogFile := ""
|
||||
if fileName != "" && strings.Contains(fileName, neTypeID) {
|
||||
viewLogFile = fmt.Sprintf("\n cat %s.log", fileName)
|
||||
}
|
||||
|
||||
// 拼装命令
|
||||
sendCmd := fmt.Sprintf("cd /tmp \n %s kill %s %s", withSudo, pid, viewLogFile)
|
||||
msg, err := cmd.ExecWithCheck("ssh", sshHost, sendCmd)
|
||||
delete(s.tcpdumpPIDMap, neTypeID)
|
||||
if err != nil || strings.HasPrefix(msg, "stderr:") {
|
||||
logger.Warnf("DumpStop err: %s => %s", strings.TrimSpace(msg), err.Error())
|
||||
return "", err
|
||||
}
|
||||
return msg, nil
|
||||
}
|
||||
|
||||
// DumpUPF UPF标准版抓包
|
||||
func (s *TcpdumpImpl) DumpUPF(neType, neId, cmdStr string) (string, string, error) {
|
||||
// 检查网元信息
|
||||
neInfo := s.neInfoService.SelectNeInfoByNeTypeAndNeID(neType, neId)
|
||||
if neInfo.NeId != neId {
|
||||
return "", "", fmt.Errorf("noData")
|
||||
}
|
||||
|
||||
// SSH命令
|
||||
usernameNe := config.Get("ne.user").(string) // 网元统一用户
|
||||
sshHost := fmt.Sprintf("%s@%s", usernameNe, neInfo.IP)
|
||||
|
||||
// 是否拥有sudo权限并拼接
|
||||
withSudo := ""
|
||||
if _, err := cmd.ExecWithCheck("ssh", sshHost, "sudo -n uname"); err == nil {
|
||||
withSudo = "sudo "
|
||||
}
|
||||
|
||||
if msg, err := cmd.ExecWithCheck("ssh", sshHost, fmt.Sprintf("%s expect -version", withSudo)); err != nil {
|
||||
// stderr: bash: expect:未找到命令 => exit status 127
|
||||
msg = strings.TrimSpace(msg)
|
||||
logger.Warnf("DumpUPF err: %s => %s", msg, err.Error())
|
||||
return "", "", fmt.Errorf(msg)
|
||||
}
|
||||
|
||||
// 拼装命令
|
||||
neTypeID := fmt.Sprintf("%s_%s", neInfo.NeType, neInfo.NeId)
|
||||
timeStr := date.ParseDateToStr(time.Now(), date.YYYYMMDDHHMMSS)
|
||||
fileName := fmt.Sprintf("%s_%s", timeStr, neTypeID)
|
||||
// UPF标准版本telnet脚本
|
||||
scriptStr := "set pcapCmd [lindex $argv 0]\nspawn telnet localhost 5002\nexpect \"upfd1# \"\nsend \"$pcapCmd\\n\"\nexpect \"upfd1# \"\nsend \"quit\\n\"\nexpect \"eof\""
|
||||
writePcapFile := fmt.Sprintf("echo '%s' > pcapUPF.sh\n %s chmod +x pcapUPF.sh", scriptStr, withSudo)
|
||||
writeLogFile := fmt.Sprintf("> %s.log 2>&1 \ncat %s.log", fileName, fileName)
|
||||
|
||||
// 以off结尾是停止抓包,不需要写文件
|
||||
pcapCmd := cmdStr
|
||||
if !strings.HasSuffix(pcapCmd, "off") {
|
||||
pcapCmd = fmt.Sprintf("%s file %s.pcap", cmdStr, fileName)
|
||||
}
|
||||
sendCmd := fmt.Sprintf("cd /tmp \n%s\n expect ./pcapUPF.sh '%s' %s", writePcapFile, pcapCmd, writeLogFile)
|
||||
// cd /tmp
|
||||
// echo '' >
|
||||
// expect ./cap.sh > pcapUPF.sh
|
||||
// sudo chmod +x pcapUPF.sh
|
||||
// expect ./cap.sh 'pcap dispatch trace off' > 20240115165701_UDM_001.log 2>&1
|
||||
// cat 20240115165701_UDM_001.log
|
||||
msg, err := cmd.ExecWithCheck("ssh", sshHost, sendCmd)
|
||||
msg = strings.TrimSpace(msg)
|
||||
if err != nil || strings.HasPrefix(msg, "stderr:") {
|
||||
logger.Warnf("DumpUPF err: %s => %s", msg, err.Error())
|
||||
return "", "", err
|
||||
}
|
||||
if strings.Contains(msg, "Unable to connect to remote host") {
|
||||
return "", "", fmt.Errorf("connection refused")
|
||||
}
|
||||
// 以off结尾是停止抓包,不需要写文件
|
||||
if strings.HasSuffix(pcapCmd, "off") {
|
||||
if strings.Contains(msg, "Write ") {
|
||||
lastTmpIndex := strings.LastIndex(msg, "/tmp/")
|
||||
text := msg[lastTmpIndex+5:]
|
||||
extensionIndex := strings.LastIndex(text, ".pcap")
|
||||
if extensionIndex != -1 {
|
||||
fileName = text[:extensionIndex]
|
||||
}
|
||||
} else {
|
||||
fileName = ""
|
||||
}
|
||||
}
|
||||
return fileName, msg, err
|
||||
}
|
||||
35
src/modules/trace/trace.go
Normal file
35
src/modules/trace/trace.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package trace
|
||||
|
||||
import (
|
||||
"nms_nbi/src/framework/logger"
|
||||
"nms_nbi/src/framework/middleware"
|
||||
"nms_nbi/src/framework/middleware/collectlogs"
|
||||
"nms_nbi/src/modules/trace/controller"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// 模块路由注册
|
||||
func Setup(router *gin.Engine) {
|
||||
logger.Infof("开始加载 ====> trace 模块路由")
|
||||
|
||||
// 信令抓包
|
||||
tcpdumpGroup := router.Group("/tcpdump")
|
||||
{
|
||||
tcpdumpGroup.POST("/start",
|
||||
middleware.PreAuthorize(nil),
|
||||
collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.tcpdump", collectlogs.BUSINESS_TYPE_OTHER)),
|
||||
controller.NewTcpdump.DumpStart,
|
||||
)
|
||||
tcpdumpGroup.POST("/stop",
|
||||
middleware.PreAuthorize(nil),
|
||||
collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.tcpdump", collectlogs.BUSINESS_TYPE_OTHER)),
|
||||
controller.NewTcpdump.DumpStop,
|
||||
)
|
||||
tcpdumpGroup.POST("/traceUPF",
|
||||
middleware.PreAuthorize(nil),
|
||||
collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.tcpdump", collectlogs.BUSINESS_TYPE_OTHER)),
|
||||
controller.NewTcpdump.TraceUPF,
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user