marge: 合并代码
This commit is contained in:
59
features/cdr/cdrevent.go
Normal file
59
features/cdr/cdrevent.go
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
package cdr
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"ems.agt/lib/dborm"
|
||||||
|
"ems.agt/lib/global"
|
||||||
|
"ems.agt/lib/log"
|
||||||
|
"ems.agt/lib/services"
|
||||||
|
"ems.agt/restagent/config"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
UriCDREvent = config.DefaultUriPrefix + "/cdrManagement/v1/elementType/{elementTypeValue}/objectType/cdrEvent"
|
||||||
|
UriCDRFile = config.DefaultUriPrefix + "/cdrManagement/v1/elementType/{elementTypeValue}/objectType/cdrFile"
|
||||||
|
|
||||||
|
CustomUriCDREvent = config.UriPrefix + "/cdrManagement/v1/elementType/{elementTypeValue}/objectType/cdrEvent"
|
||||||
|
CustomUriCDRFile = config.UriPrefix + "/cdrManagement/v1/elementType/{elementTypeValue}/objectType/cdrFile"
|
||||||
|
)
|
||||||
|
|
||||||
|
type CDREvent struct {
|
||||||
|
NeType string `json:"neType" xorm:"ne_type"`
|
||||||
|
NeName string `json:"neName" xorm:"ne_name"`
|
||||||
|
RmUID string `json:"rmUID" xorm:"rm_uid"`
|
||||||
|
Timestamp int `json:"timestamp" xorm:"timestamp"`
|
||||||
|
CDR map[string]any `json:"CDR" xorm:"cdr_json"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func PostCDREventFromNF(w http.ResponseWriter, r *http.Request) {
|
||||||
|
log.Info("PostCDREventFromNF processing... ")
|
||||||
|
|
||||||
|
// body, err := io.ReadAll(io.LimitReader(r.Body, global.RequestBodyMaxLen))
|
||||||
|
body, err := io.ReadAll(io.LimitReader(r.Body, global.RequestBodyMaxLen))
|
||||||
|
if err != nil {
|
||||||
|
log.Error("Faile to io.ReadAll: ", err)
|
||||||
|
services.ResponseNotFound404UriNotExist(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
cdrEvent := new(CDREvent)
|
||||||
|
err = json.Unmarshal(body, &cdrEvent)
|
||||||
|
if err != nil {
|
||||||
|
log.Error("Failed to Unmarshal cdrEvent:", err)
|
||||||
|
services.ResponseInternalServerError500ProcessError(w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log.Trace("cdrEvent:", cdrEvent)
|
||||||
|
|
||||||
|
affected, err := dborm.XormInsertTableOne("cdr_event", cdrEvent)
|
||||||
|
if err != nil && affected <= 0 {
|
||||||
|
log.Error("Failed to insert cdr_event:", err)
|
||||||
|
services.ResponseInternalServerError500ProcessError(w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
services.ResponseStatusOK204NoContent(w)
|
||||||
|
}
|
||||||
@@ -527,7 +527,7 @@ func DistributeSoftwareToNF(w http.ResponseWriter, r *http.Request) {
|
|||||||
services.ResponseInternalServerError500ProcessError(w, err)
|
services.ResponseInternalServerError500ProcessError(w, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.Debug("neSoftware:", neSoftware)
|
log.Trace("neSoftware:", neSoftware)
|
||||||
|
|
||||||
sql = fmt.Sprintf("select * from ne_version where ne_type='%s' and ne_id='%s'", neTypeUpper, neId)
|
sql = fmt.Sprintf("select * from ne_version where ne_type='%s' and ne_id='%s'", neTypeUpper, neId)
|
||||||
neVersion, err := dborm.XormGetDataBySQL(sql)
|
neVersion, err := dborm.XormGetDataBySQL(sql)
|
||||||
@@ -536,13 +536,13 @@ func DistributeSoftwareToNF(w http.ResponseWriter, r *http.Request) {
|
|||||||
services.ResponseInternalServerError500ProcessError(w, err)
|
services.ResponseInternalServerError500ProcessError(w, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.Debug("neVersion:", neVersion)
|
log.Trace("neVersion:", neVersion)
|
||||||
|
|
||||||
sshHost := fmt.Sprintf("%s@%s", config.GetYamlConfig().NE.User, neInfo.Ip)
|
sshHost := fmt.Sprintf("%s@%s", config.GetYamlConfig().NE.User, neInfo.Ip)
|
||||||
mkdirCmd := fmt.Sprintf("sudo mkdir -p %s/software/%s", config.GetYamlConfig().NE.OmcDir, neTypeLower)
|
mkdirCmd := fmt.Sprintf("sudo mkdir -p %s/software/%s", config.GetYamlConfig().NE.OmcDir, neTypeLower)
|
||||||
cmd := exec.Command("ssh", sshHost, mkdirCmd)
|
cmd := exec.Command("ssh", sshHost, mkdirCmd)
|
||||||
out, err := cmd.CombinedOutput()
|
out, err := cmd.CombinedOutput()
|
||||||
log.Debugf("Exec output: %v", string(out))
|
log.Tracef("Exec output: %v", string(out))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Faile to mkdir:", err)
|
log.Error("Faile to mkdir:", err)
|
||||||
services.ResponseInternalServerError500ProcessError(w, err)
|
services.ResponseInternalServerError500ProcessError(w, err)
|
||||||
@@ -557,7 +557,7 @@ func DistributeSoftwareToNF(w http.ResponseWriter, r *http.Request) {
|
|||||||
neInfo.Ip, config.GetYamlConfig().NE.ScpDir)
|
neInfo.Ip, config.GetYamlConfig().NE.ScpDir)
|
||||||
cmd = exec.Command("scp", "-r", srcFile, scpDir)
|
cmd = exec.Command("scp", "-r", srcFile, scpDir)
|
||||||
out, err = cmd.CombinedOutput()
|
out, err = cmd.CombinedOutput()
|
||||||
log.Debugf("Exec output: %v", string(out))
|
log.Tracef("Exec output: %v", string(out))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("Faile to scp NF: neType=%s, neId=%s, ip=%s", neType, neId, neInfo.Ip)
|
log.Errorf("Faile to scp NF: neType=%s, neId=%s, ip=%s", neType, neId, neInfo.Ip)
|
||||||
services.ResponseInternalServerError500ProcessError(w, err)
|
services.ResponseInternalServerError500ProcessError(w, err)
|
||||||
@@ -568,7 +568,7 @@ func DistributeSoftwareToNF(w http.ResponseWriter, r *http.Request) {
|
|||||||
config.GetYamlConfig().NE.OmcDir, neTypeLower)
|
config.GetYamlConfig().NE.OmcDir, neTypeLower)
|
||||||
cmd = exec.Command("ssh", sshHost, cpCmd)
|
cmd = exec.Command("ssh", sshHost, cpCmd)
|
||||||
out, err = cmd.CombinedOutput()
|
out, err = cmd.CombinedOutput()
|
||||||
log.Debugf("Exec output: %v", string(out))
|
log.Tracef("Exec output: %v", string(out))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Faile to execute cp command:", err)
|
log.Error("Faile to execute cp command:", err)
|
||||||
services.ResponseInternalServerError500ProcessError(w, err)
|
services.ResponseInternalServerError500ProcessError(w, err)
|
||||||
@@ -667,7 +667,7 @@ func ActiveSoftwareToNF(w http.ResponseWriter, r *http.Request) {
|
|||||||
services.ResponseInternalServerError500ProcessError(w, err)
|
services.ResponseInternalServerError500ProcessError(w, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.Debug("neVersion:", neSoftware)
|
log.Trace("neSoftware:", neSoftware)
|
||||||
|
|
||||||
sql = fmt.Sprintf("select * from ne_version where ne_type='%s' and ne_id='%s' and version='%s'", neTypeUpper, neId, version)
|
sql = fmt.Sprintf("select * from ne_version where ne_type='%s' and ne_id='%s' and version='%s'", neTypeUpper, neId, version)
|
||||||
neVersion, err := dborm.XormGetDataBySQL(sql)
|
neVersion, err := dborm.XormGetDataBySQL(sql)
|
||||||
@@ -681,62 +681,104 @@ func ActiveSoftwareToNF(w http.ResponseWriter, r *http.Request) {
|
|||||||
services.ResponseInternalServerError500ProcessError(w, err)
|
services.ResponseInternalServerError500ProcessError(w, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.Debug("neVersion:", neVersion)
|
log.Trace("neVersion:", neVersion)
|
||||||
|
|
||||||
if !config.GetYamlConfig().OMC.TestMode {
|
if !config.GetYamlConfig().OMC.TestMode {
|
||||||
filePath := (*neVersion)[0]["file_path"]
|
filePath := (*neVersion)[0]["file_path"]
|
||||||
sshHost := fmt.Sprintf("%s@%s", config.GetYamlConfig().NE.User, neInfo.Ip)
|
sshHost := fmt.Sprintf("%s@%s", config.GetYamlConfig().NE.User, neInfo.Ip)
|
||||||
fileType := global.IsRpmOrDebPackage(filePath)
|
srcFile := fmt.Sprintf("%s/actpkg.sh", config.GetYamlConfig().OMC.BinDir)
|
||||||
if fileType == 1 {
|
|
||||||
rpmCmd := fmt.Sprintf("sudo rpm -Uvh '%s'", filePath)
|
scpDir := fmt.Sprintf("%s@%s:%s", config.GetYamlConfig().NE.User,
|
||||||
cmd := exec.Command("ssh", sshHost, rpmCmd)
|
neInfo.Ip, config.GetYamlConfig().NE.ScpDir)
|
||||||
out, err := cmd.CombinedOutput()
|
cmd := exec.Command("scp", "-r", srcFile, scpDir)
|
||||||
log.Debugf("Exec output: %v", string(out))
|
_, err := cmd.CombinedOutput()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Faile to execute rpm command:", err)
|
log.Errorf("Faile to scp NF: neType=%s, neId=%s, ip=%s", neType, neId, neInfo.Ip)
|
||||||
services.ResponseInternalServerError500ProcessError(w, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
} else if fileType == 2 {
|
|
||||||
dpkgCmd := fmt.Sprintf("sudo dpkg -i --force-all '%s'", filePath)
|
|
||||||
err := RunSSHCmd(sshHost, dpkgCmd)
|
|
||||||
if err != nil {
|
|
||||||
log.Error("Faile to execute dpkg command:", err)
|
|
||||||
services.ResponseInternalServerError500ProcessError(w, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
// timeout := time.Duration(config.GetYamlConfig().OMC.CmdTimeout) * time.Second
|
|
||||||
// ctx, cancel := context.WithTimeout(context.Background(), timeout) // 设置超时
|
|
||||||
// defer cancel()
|
|
||||||
// cmd := exec.CommandContext(ctx, "ssh", sshHost, dpkgCmd)
|
|
||||||
// var stdout, stderr bytes.Buffer
|
|
||||||
// cmd.Stdout = &stdout
|
|
||||||
// cmd.Stderr = &stderr
|
|
||||||
// err := cmd.Start()
|
|
||||||
// if err != nil {
|
|
||||||
// log.Error("Faile to execute dpkg command: %v, err: %s", err, stderr.String())
|
|
||||||
// services.ResponseInternalServerError500ProcessError(w, err)
|
|
||||||
// return
|
|
||||||
// }
|
|
||||||
// out, err := cmd.CombinedOutput()
|
|
||||||
// log.Debugf("Exec output: %v", string(out))
|
|
||||||
// if err != nil {
|
|
||||||
// log.Error("Faile to execute dpkg command:", err)
|
|
||||||
// services.ResponseInternalServerError500ProcessError(w, err)
|
|
||||||
// return
|
|
||||||
// }
|
|
||||||
} else {
|
|
||||||
err := global.ErrCMUnknownSoftwareFormat
|
|
||||||
log.Error(err)
|
|
||||||
services.ResponseInternalServerError500ProcessError(w, err)
|
services.ResponseInternalServerError500ProcessError(w, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
runCmd := fmt.Sprintf("sudo %s/actpkg.sh '%s' %s",
|
||||||
|
config.GetYamlConfig().NE.ScpDir, filePath, neTypeUpper)
|
||||||
|
if neTypeLower == "omc" {
|
||||||
|
idNeVersion, _ := strconv.Atoi((*neVersion)[0]["id"])
|
||||||
|
neVersionData := dborm.NeVersion{
|
||||||
|
Status: SoftwareStatusActive,
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = dborm.XormUpdateTableById(idNeVersion, "ne_version", neVersionData)
|
||||||
|
if err != nil {
|
||||||
|
log.Error("Faile to UpdateTableById:", err)
|
||||||
|
services.ResponseInternalServerError500ProcessError(w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
services.ResponseStatusOK204NoContent(w)
|
||||||
|
go RunSSHCmd(sshHost, runCmd)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
err = RunSSHCmd(sshHost, runCmd)
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("Faile to execute command: %s, error: %v", runCmd, err)
|
||||||
|
services.ResponseInternalServerError500ProcessError(w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// fileType := global.IsRpmOrDebPackage(filePath)
|
||||||
|
// if fileType == 1 {
|
||||||
|
// srcFile := fmt.Sprintf("%s/spawnrpm.sh", config.GetYamlConfig().OMC.BinDir)
|
||||||
|
|
||||||
|
// scpDir := fmt.Sprintf("%s@%s:%s", config.GetYamlConfig().NE.User,
|
||||||
|
// neInfo.Ip, config.GetYamlConfig().NE.ScpDir)
|
||||||
|
// cmd := exec.Command("scp", "-r", srcFile, scpDir)
|
||||||
|
// _, err := cmd.CombinedOutput()
|
||||||
|
// if err != nil {
|
||||||
|
// log.Errorf("Faile to scp NF: neType=%s, neId=%s, ip=%s", neType, neId, neInfo.Ip)
|
||||||
|
// services.ResponseInternalServerError500ProcessError(w, err)
|
||||||
|
// return
|
||||||
|
// }
|
||||||
|
|
||||||
|
// dpkgCmd := fmt.Sprintf("sudo %s/spawnrpm.sh '%s'",
|
||||||
|
// config.GetYamlConfig().NE.ScpDir, filePath)
|
||||||
|
// err = RunSSHCmd(sshHost, dpkgCmd)
|
||||||
|
// if err != nil {
|
||||||
|
// log.Errorf("Faile to execute dpkg command: %s, error: %v", dpkgCmd, err)
|
||||||
|
// services.ResponseInternalServerError500ProcessError(w, err)
|
||||||
|
// return
|
||||||
|
// }
|
||||||
|
// } else if fileType == 2 {
|
||||||
|
// srcFile := fmt.Sprintf("%s/spawndpkg.sh", config.GetYamlConfig().OMC.BinDir)
|
||||||
|
|
||||||
|
// scpDir := fmt.Sprintf("%s@%s:%s", config.GetYamlConfig().NE.User,
|
||||||
|
// neInfo.Ip, config.GetYamlConfig().NE.ScpDir)
|
||||||
|
// cmd := exec.Command("scp", "-r", srcFile, scpDir)
|
||||||
|
// _, err := cmd.CombinedOutput()
|
||||||
|
// if err != nil {
|
||||||
|
// log.Errorf("Faile to scp NF: neType=%s, neId=%s, ip=%s", neType, neId, neInfo.Ip)
|
||||||
|
// services.ResponseInternalServerError500ProcessError(w, err)
|
||||||
|
// return
|
||||||
|
// }
|
||||||
|
|
||||||
|
// dpkgCmd := fmt.Sprintf("sudo %s/spawndpkg.sh '%s'",
|
||||||
|
// config.GetYamlConfig().NE.ScpDir, filePath)
|
||||||
|
// err = RunSSHCmd(sshHost, dpkgCmd)
|
||||||
|
// if err != nil {
|
||||||
|
// log.Errorf("Faile to execute dpkg command: %s, error: %v", dpkgCmd, err)
|
||||||
|
// services.ResponseInternalServerError500ProcessError(w, err)
|
||||||
|
// return
|
||||||
|
// }
|
||||||
|
// } else {
|
||||||
|
// err := global.ErrCMUnknownSoftwareFormat
|
||||||
|
// log.Error(err)
|
||||||
|
// services.ResponseInternalServerError500ProcessError(w, err)
|
||||||
|
// return
|
||||||
|
// }
|
||||||
switch neTypeLower {
|
switch neTypeLower {
|
||||||
case "omc":
|
case "omc":
|
||||||
restartCmd := fmt.Sprintf("sudo %s/bin/omcsvc.sh restart", config.GetYamlConfig().NE.OmcDir)
|
restartCmd := fmt.Sprintf("sudo %s/bin/omcsvc.sh restart", config.GetYamlConfig().NE.OmcDir)
|
||||||
cmd := exec.Command("ssh", sshHost, restartCmd)
|
cmd := exec.Command("ssh", sshHost, restartCmd)
|
||||||
out, err := cmd.CombinedOutput()
|
out, err := cmd.CombinedOutput()
|
||||||
log.Debugf("Exec output: %v", string(out))
|
log.Tracef("Exec output: %v", string(out))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Faile to execute ssh restart omc:", err)
|
log.Error("Faile to execute ssh restart omc:", err)
|
||||||
services.ResponseInternalServerError500ProcessError(w, err)
|
services.ResponseInternalServerError500ProcessError(w, err)
|
||||||
@@ -746,9 +788,9 @@ func ActiveSoftwareToNF(w http.ResponseWriter, r *http.Request) {
|
|||||||
restartCmd := "sudo ims-stop && sudo ims-start"
|
restartCmd := "sudo ims-stop && sudo ims-start"
|
||||||
cmd := exec.Command("ssh", sshHost, restartCmd)
|
cmd := exec.Command("ssh", sshHost, restartCmd)
|
||||||
out, err := cmd.CombinedOutput()
|
out, err := cmd.CombinedOutput()
|
||||||
log.Debugf("Exec output: %v", string(out))
|
log.Tracef("Exec output: %v", string(out))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Faile to execute ssh sudo systemctl command:", err)
|
log.Error("Faile to execute ssh command: %s, error: %v", restartCmd, err)
|
||||||
services.ResponseInternalServerError500ProcessError(w, err)
|
services.ResponseInternalServerError500ProcessError(w, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -756,7 +798,7 @@ func ActiveSoftwareToNF(w http.ResponseWriter, r *http.Request) {
|
|||||||
restartCmd := fmt.Sprintf("sudo systemctl restart %s.service", neTypeLower)
|
restartCmd := fmt.Sprintf("sudo systemctl restart %s.service", neTypeLower)
|
||||||
cmd := exec.Command("ssh", sshHost, restartCmd)
|
cmd := exec.Command("ssh", sshHost, restartCmd)
|
||||||
out, err := cmd.CombinedOutput()
|
out, err := cmd.CombinedOutput()
|
||||||
log.Debugf("Exec output: %v", string(out))
|
log.Tracef("Exec output: %v", string(out))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Faile to execute ssh sudo systemctl command:", err)
|
log.Error("Faile to execute ssh sudo systemctl command:", err)
|
||||||
services.ResponseInternalServerError500ProcessError(w, err)
|
services.ResponseInternalServerError500ProcessError(w, err)
|
||||||
@@ -844,39 +886,94 @@ func RollBackSoftwareToNF(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
if !config.GetYamlConfig().OMC.TestMode {
|
if !config.GetYamlConfig().OMC.TestMode {
|
||||||
sshHost := fmt.Sprintf("%s@%s", config.GetYamlConfig().NE.User, neInfo.Ip)
|
sshHost := fmt.Sprintf("%s@%s", config.GetYamlConfig().NE.User, neInfo.Ip)
|
||||||
fileType := global.IsRpmOrDebPackage(filePath)
|
srcFile := fmt.Sprintf("%s/rbkpkg.sh", config.GetYamlConfig().OMC.BinDir)
|
||||||
if fileType == 1 {
|
|
||||||
rpmCmd := fmt.Sprintf("sudo rpm -Uvh --oldpackage '%s'", filePath)
|
scpDir := fmt.Sprintf("%s@%s:%s", config.GetYamlConfig().NE.User,
|
||||||
cmd := exec.Command("ssh", sshHost, rpmCmd)
|
neInfo.Ip, config.GetYamlConfig().NE.ScpDir)
|
||||||
out, err := cmd.CombinedOutput()
|
cmd := exec.Command("scp", "-r", srcFile, scpDir)
|
||||||
log.Debugf("Exec output: %v", string(out))
|
_, err := cmd.CombinedOutput()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Faile to execute rpm command:", err)
|
log.Errorf("Faile to scp NF: neType=%s, neId=%s, ip=%s", neType, neId, neInfo.Ip)
|
||||||
services.ResponseInternalServerError500ProcessError(w, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
} else if fileType == 2 {
|
|
||||||
dpkgCmd := fmt.Sprintf("sudo dpkg -i --force-all '%s'", filePath)
|
|
||||||
cmd := exec.Command("ssh", sshHost, dpkgCmd)
|
|
||||||
out, err := cmd.CombinedOutput()
|
|
||||||
log.Debugf("Exec output: %v", string(out))
|
|
||||||
if err != nil {
|
|
||||||
log.Error("Faile to execute dpkg command:", err)
|
|
||||||
services.ResponseInternalServerError500ProcessError(w, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
err := global.ErrCMUnknownSoftwareFormat
|
|
||||||
log.Error(err)
|
|
||||||
services.ResponseInternalServerError500ProcessError(w, err)
|
services.ResponseInternalServerError500ProcessError(w, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
runCmd := fmt.Sprintf("sudo %s/rbkpkg.sh '%s' %s",
|
||||||
|
config.GetYamlConfig().NE.ScpDir, filePath, neTypeUpper)
|
||||||
|
if neTypeLower == "omc" {
|
||||||
|
idNeVersion, _ := strconv.Atoi((*neVersion)[0]["id"])
|
||||||
|
neVersionData := dborm.NeVersion{
|
||||||
|
Version: (*neVersion)[0]["pre_version"],
|
||||||
|
FilePath: (*neVersion)[0]["pre_file"],
|
||||||
|
PreVersion: "-",
|
||||||
|
PreFile: "-",
|
||||||
|
NewVersion: (*neVersion)[0]["version"],
|
||||||
|
NewFile: (*neVersion)[0]["file_path"],
|
||||||
|
Status: SoftwareStatusActive,
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = dborm.XormUpdateTableById(idNeVersion, "ne_version", neVersionData)
|
||||||
|
if err != nil {
|
||||||
|
log.Error("Faile to UpdateTableById:", err)
|
||||||
|
services.ResponseInternalServerError500ProcessError(w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
services.ResponseStatusOK204NoContent(w)
|
||||||
|
RunSSHCmd(sshHost, runCmd)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
err = RunSSHCmd(sshHost, runCmd)
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("Faile to execute command: %s, error: %v", runCmd, err)
|
||||||
|
services.ResponseInternalServerError500ProcessError(w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// fileType := global.IsRpmOrDebPackage(filePath)
|
||||||
|
// if fileType == 1 {
|
||||||
|
// rpmCmd := fmt.Sprintf("sudo rpm -Uvh --oldpackage '%s'", filePath)
|
||||||
|
// cmd := exec.Command("ssh", sshHost, rpmCmd)
|
||||||
|
// _, err := cmd.CombinedOutput()
|
||||||
|
// if err != nil {
|
||||||
|
// log.Error("Faile to execute rpm command:", err)
|
||||||
|
// services.ResponseInternalServerError500ProcessError(w, err)
|
||||||
|
// return
|
||||||
|
// }
|
||||||
|
// } else if fileType == 2 {
|
||||||
|
// srcFile := fmt.Sprintf("%s/spawndpkg.sh", config.GetYamlConfig().OMC.BinDir)
|
||||||
|
|
||||||
|
// scpDir := fmt.Sprintf("%s@%s:%s", config.GetYamlConfig().NE.User,
|
||||||
|
// neInfo.Ip, config.GetYamlConfig().NE.ScpDir)
|
||||||
|
// cmd := exec.Command("scp", "-r", srcFile, scpDir)
|
||||||
|
// _, err := cmd.CombinedOutput()
|
||||||
|
// if err != nil {
|
||||||
|
// log.Errorf("Faile to scp NF: neType=%s, neId=%s, ip=%s", neType, neId, neInfo.Ip)
|
||||||
|
// services.ResponseInternalServerError500ProcessError(w, err)
|
||||||
|
// return
|
||||||
|
// }
|
||||||
|
// var inputStr string = "n"
|
||||||
|
// if config.GetYamlConfig().NE.DpkgOverwrite {
|
||||||
|
// inputStr = "y"
|
||||||
|
// }
|
||||||
|
// dpkgCmd := fmt.Sprintf("sudo %s/spawndpkg.sh %s '%s'",
|
||||||
|
// config.GetYamlConfig().NE.ScpDir, inputStr, filePath)
|
||||||
|
// err = RunSSHCmd(sshHost, dpkgCmd)
|
||||||
|
// if err != nil {
|
||||||
|
// log.Errorf("Faile to execute dpkg command: %s, error: %v", dpkgCmd, err)
|
||||||
|
// services.ResponseInternalServerError500ProcessError(w, err)
|
||||||
|
// return
|
||||||
|
// }
|
||||||
|
// } else {
|
||||||
|
// err := global.ErrCMUnknownSoftwareFormat
|
||||||
|
// log.Error(err)
|
||||||
|
// services.ResponseInternalServerError500ProcessError(w, err)
|
||||||
|
// return
|
||||||
|
// }
|
||||||
switch neTypeLower {
|
switch neTypeLower {
|
||||||
case "omc":
|
case "omc":
|
||||||
restartCmd := fmt.Sprintf("sudo %s/bin/omcsvc.sh restart", config.GetYamlConfig().NE.OmcDir)
|
restartCmd := fmt.Sprintf("sudo %s/bin/omcsvc.sh restart", config.GetYamlConfig().NE.OmcDir)
|
||||||
cmd := exec.Command("ssh", sshHost, restartCmd)
|
cmd := exec.Command("ssh", sshHost, restartCmd)
|
||||||
out, err := cmd.CombinedOutput()
|
out, err := cmd.CombinedOutput()
|
||||||
log.Debugf("Exec output: %v", string(out))
|
log.Tracef("Exec output: %v", string(out))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Faile to execute ssh restart omc:", err)
|
log.Error("Faile to execute ssh restart omc:", err)
|
||||||
services.ResponseInternalServerError500ProcessError(w, err)
|
services.ResponseInternalServerError500ProcessError(w, err)
|
||||||
@@ -886,9 +983,9 @@ func RollBackSoftwareToNF(w http.ResponseWriter, r *http.Request) {
|
|||||||
restartCmd := "sudo ims-stop && sudo ims-start"
|
restartCmd := "sudo ims-stop && sudo ims-start"
|
||||||
cmd := exec.Command("ssh", sshHost, restartCmd)
|
cmd := exec.Command("ssh", sshHost, restartCmd)
|
||||||
out, err := cmd.CombinedOutput()
|
out, err := cmd.CombinedOutput()
|
||||||
log.Debugf("Exec output: %v", string(out))
|
log.Tracef("Exec output: %v", string(out))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Faile to execute ssh sudo systemctl command:", err)
|
log.Error("Faile to execute ssh command: %s, error: %v", restartCmd, err)
|
||||||
services.ResponseInternalServerError500ProcessError(w, err)
|
services.ResponseInternalServerError500ProcessError(w, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -896,7 +993,7 @@ func RollBackSoftwareToNF(w http.ResponseWriter, r *http.Request) {
|
|||||||
restartCmd := fmt.Sprintf("sudo systemctl restart %s.service", neTypeLower)
|
restartCmd := fmt.Sprintf("sudo systemctl restart %s.service", neTypeLower)
|
||||||
cmd := exec.Command("ssh", sshHost, restartCmd)
|
cmd := exec.Command("ssh", sshHost, restartCmd)
|
||||||
out, err := cmd.CombinedOutput()
|
out, err := cmd.CombinedOutput()
|
||||||
log.Debugf("Exec output: %v", string(out))
|
log.Tracef("Exec output: %v", string(out))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Faile to execute ssh sudo systemctl command:", err)
|
log.Error("Faile to execute ssh sudo systemctl command:", err)
|
||||||
services.ResponseInternalServerError500ProcessError(w, err)
|
services.ResponseInternalServerError500ProcessError(w, err)
|
||||||
|
|||||||
@@ -528,7 +528,7 @@ func PostMMLToOMC(w http.ResponseWriter, r *http.Request) {
|
|||||||
services.ResponseNotFound404UriNotExist(w, r)
|
services.ResponseNotFound404UriNotExist(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.Debug("Body:", string(body))
|
log.Trace("Body:", string(body))
|
||||||
|
|
||||||
hostUri := fmt.Sprintf("http://%s:%s", neInfo.Ip, neInfo.Port)
|
hostUri := fmt.Sprintf("http://%s:%s", neInfo.Ip, neInfo.Port)
|
||||||
|
|
||||||
|
|||||||
@@ -169,10 +169,10 @@ func PostKPIReportFromNF(w http.ResponseWriter, r *http.Request) {
|
|||||||
services.ResponseNotFound404UriNotExist(w, r)
|
services.ResponseNotFound404UriNotExist(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.Debug("Request body:", string(body))
|
log.Trace("Request body:", string(body))
|
||||||
kpiReport := new(KpiReport)
|
kpiReport := new(KpiReport)
|
||||||
_ = json.Unmarshal(body, &kpiReport)
|
_ = json.Unmarshal(body, &kpiReport)
|
||||||
log.Debug("kpiReport:", kpiReport)
|
log.Trace("kpiReport:", kpiReport)
|
||||||
|
|
||||||
session := xEngine.NewSession()
|
session := xEngine.NewSession()
|
||||||
defer session.Close()
|
defer session.Close()
|
||||||
|
|||||||
@@ -9,6 +9,8 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/shirou/gopsutil/v3/net"
|
||||||
|
|
||||||
"github.com/go-resty/resty/v2"
|
"github.com/go-resty/resty/v2"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
|
|
||||||
@@ -726,6 +728,112 @@ func GetAllSysinfoFromNF(w http.ResponseWriter, r *http.Request) {
|
|||||||
func GetStateFromNF(w http.ResponseWriter, r *http.Request) {
|
func GetStateFromNF(w http.ResponseWriter, r *http.Request) {
|
||||||
log.Debug("GetStateFromNF processing... ")
|
log.Debug("GetStateFromNF processing... ")
|
||||||
|
|
||||||
|
vars := mux.Vars(r)
|
||||||
|
neType := vars["elementTypeValue"]
|
||||||
|
neType = strings.ToLower(neType)
|
||||||
|
|
||||||
|
if neType == "" {
|
||||||
|
services.ResponseNotFound404UriNotExist(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if neType == "all" {
|
||||||
|
var neList []dborm.NeInfo
|
||||||
|
_, err := dborm.XormGetAllNeInfo(&neList)
|
||||||
|
if err != nil {
|
||||||
|
log.Error("Failed to get all ne info:", err)
|
||||||
|
services.ResponseInternalServerError500ProcessError(w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
data := make([]map[string]interface{}, 0)
|
||||||
|
for _, ne := range neList {
|
||||||
|
result := make(map[string]interface{})
|
||||||
|
hostUri := fmt.Sprintf("http://%s:%v", ne.Ip, ne.Port)
|
||||||
|
requestURI2NF := fmt.Sprintf("%s/api/rest/systemManagement/v1/elementType/%s/objectType/systemState",
|
||||||
|
hostUri, strings.ToLower(ne.NeType))
|
||||||
|
log.Debug("requestURI2NF:", requestURI2NF)
|
||||||
|
|
||||||
|
result["ipAddress"] = ne.Ip
|
||||||
|
resp, err := client.R().
|
||||||
|
EnableTrace().
|
||||||
|
SetHeaders(map[string]string{"User-Agent": config.GetDefaultUserAgent()}).
|
||||||
|
SetHeaders(map[string]string{"Content-Type": "application/json;charset=UTF-8"}).
|
||||||
|
Get(requestURI2NF)
|
||||||
|
if err != nil {
|
||||||
|
log.Error("Get system state from NF is failed:", err)
|
||||||
|
errorMessage := services.ErrorMessage{
|
||||||
|
ErrorCode: "1", ErrorInfo: "Internal server error, NF connnect refused",
|
||||||
|
}
|
||||||
|
result["error"] = errorMessage
|
||||||
|
SN, Version, _ := dborm.XormGetNEStateInfo(ne.NeType, ne.NeId)
|
||||||
|
result["serialNum"] = SN
|
||||||
|
result["version"] = Version
|
||||||
|
} else {
|
||||||
|
systemState := make(map[string]interface{})
|
||||||
|
_ = json.Unmarshal(resp.Body(), &systemState)
|
||||||
|
result["systemState"] = systemState
|
||||||
|
}
|
||||||
|
|
||||||
|
neItem := strings.ToUpper(ne.NeType) + "/" + ne.NeId
|
||||||
|
mapState := make(map[string]interface{})
|
||||||
|
mapState[neItem] = result
|
||||||
|
data = append(data, mapState)
|
||||||
|
log.Trace("data:", data)
|
||||||
|
}
|
||||||
|
var response Response
|
||||||
|
response.Data = data
|
||||||
|
services.ResponseWithJson(w, http.StatusOK, response)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if neType == "omc" {
|
||||||
|
emsState := GetEMSState("127.0.0.1")
|
||||||
|
services.ResponseWithJson(w, http.StatusOK, emsState)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var neList []dborm.NeInfo
|
||||||
|
err := dborm.XormGetNeInfoByNeType(neType, &neList)
|
||||||
|
if err != nil {
|
||||||
|
log.Error("Failed to dborm.XormGetNeInfoByNeType:", err)
|
||||||
|
services.ResponseInternalServerError500ProcessError(w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
data := make([]map[string]interface{}, 0)
|
||||||
|
for _, ne := range neList {
|
||||||
|
hostUri := fmt.Sprintf("http://%s:%v", ne.Ip, ne.Port)
|
||||||
|
requestURI2NF := fmt.Sprintf("%s/api/rest/systemManagement/v1/elementType/%s/objectType/systemState",
|
||||||
|
hostUri, strings.ToLower(ne.NeType))
|
||||||
|
log.Debug("requestURI2NF:", requestURI2NF)
|
||||||
|
|
||||||
|
resp, err := client.R().
|
||||||
|
EnableTrace().
|
||||||
|
SetHeaders(map[string]string{"User-Agent": config.GetDefaultUserAgent()}).
|
||||||
|
SetHeaders(map[string]string{"Content-Type": "application/json;charset=UTF-8"}).
|
||||||
|
Get(requestURI2NF)
|
||||||
|
if err != nil {
|
||||||
|
log.Error("Get system state from NF is failed:", err)
|
||||||
|
} else {
|
||||||
|
systemState := make(map[string]interface{})
|
||||||
|
_ = json.Unmarshal(resp.Body(), &systemState)
|
||||||
|
data = append(data, systemState)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(data) == 1 {
|
||||||
|
services.ResponseWithJson(w, http.StatusOK, data[0])
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var response Response
|
||||||
|
response.Data = data
|
||||||
|
services.ResponseWithJson(w, http.StatusOK, response)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetStateFromNF 旧函数
|
||||||
|
// Get system state from NF/NFs
|
||||||
|
func GetStateFromNFOld(w http.ResponseWriter, r *http.Request) {
|
||||||
|
log.Debug("GetStateFromNF processing... ")
|
||||||
|
|
||||||
data := make([]map[string]interface{}, 0)
|
data := make([]map[string]interface{}, 0)
|
||||||
|
|
||||||
vars := mux.Vars(r)
|
vars := mux.Vars(r)
|
||||||
@@ -876,6 +984,35 @@ func GetEMSState(ip string) *SysState {
|
|||||||
PartitionInfo: sysInfo.PartitionInfo,
|
PartitionInfo: sysInfo.PartitionInfo,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 获取主机的 IP 地址列表
|
||||||
|
ipAddrs := []string{ip}
|
||||||
|
if ip == "" || ip == "127.0.0.1" {
|
||||||
|
ipAddrs = []string{}
|
||||||
|
interfaces, err := net.Interfaces()
|
||||||
|
if err == nil {
|
||||||
|
for _, iface := range interfaces {
|
||||||
|
for _, v := range iface.Addrs {
|
||||||
|
name := iface.Name
|
||||||
|
if name[len(name)-1] == '0' {
|
||||||
|
name = name[0 : len(name)-1]
|
||||||
|
name = strings.Trim(name, "")
|
||||||
|
}
|
||||||
|
// ignore localhost
|
||||||
|
if name == "lo" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
prefix := strings.Split(v.Addr, "/")[0]
|
||||||
|
if strings.Contains(prefix, "::") {
|
||||||
|
ipAddrs = append(ipAddrs, prefix)
|
||||||
|
}
|
||||||
|
if strings.Contains(prefix, ".") {
|
||||||
|
ipAddrs = append(ipAddrs, prefix)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
version := "16.1.1"
|
version := "16.1.1"
|
||||||
if global.Version != "" {
|
if global.Version != "" {
|
||||||
version = global.Version
|
version = global.Version
|
||||||
@@ -886,7 +1023,7 @@ func GetEMSState(ip string) *SysState {
|
|||||||
HostName: hostName,
|
HostName: hostName,
|
||||||
OsInfo: getUnameStr(),
|
OsInfo: getUnameStr(),
|
||||||
DbInfo: dbInfo,
|
DbInfo: dbInfo,
|
||||||
IpAddr: []string{ip},
|
IpAddr: ipAddrs,
|
||||||
Port: config.GetYamlConfig().Rest[0].Port,
|
Port: config.GetYamlConfig().Rest[0].Port,
|
||||||
Version: version,
|
Version: version,
|
||||||
Capability: 9999999,
|
Capability: 9999999,
|
||||||
|
|||||||
@@ -276,7 +276,7 @@ func XormGetAllNeInfo(nes *[]NeInfo) (*[]NeInfo, error) {
|
|||||||
}
|
}
|
||||||
*nes = append(*nes, *ne)
|
*nes = append(*nes, *ne)
|
||||||
}
|
}
|
||||||
log.Debug("nes:", nes)
|
log.Trace("nes:", nes)
|
||||||
return nes, nil
|
return nes, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import (
|
|||||||
// "log"
|
// "log"
|
||||||
|
|
||||||
"ems.agt/features/aaaa"
|
"ems.agt/features/aaaa"
|
||||||
|
"ems.agt/features/cdr"
|
||||||
"ems.agt/features/cm"
|
"ems.agt/features/cm"
|
||||||
"ems.agt/features/dbrest"
|
"ems.agt/features/dbrest"
|
||||||
"ems.agt/features/file"
|
"ems.agt/features/file"
|
||||||
@@ -299,6 +300,9 @@ func init() {
|
|||||||
Register("GET", ue.UriNSSFSubscriptions, ue.GetSubscriptionsFromNSSF, nil)
|
Register("GET", ue.UriNSSFSubscriptions, ue.GetSubscriptionsFromNSSF, nil)
|
||||||
Register("GET", ue.CustomUriNSSFSubscriptions, ue.GetSubscriptionsFromNSSF, nil)
|
Register("GET", ue.CustomUriNSSFSubscriptions, ue.GetSubscriptionsFromNSSF, nil)
|
||||||
|
|
||||||
|
Register("POST", cdr.UriCDREvent, cdr.PostCDREventFromNF, nil)
|
||||||
|
Register("POST", cdr.CustomUriCDREvent, cdr.PostCDREventFromNF, nil)
|
||||||
|
|
||||||
// 进程网络
|
// 进程网络
|
||||||
Register("GET", psnet.UriWs, psnet.ProcessWs, nil)
|
Register("GET", psnet.UriWs, psnet.ProcessWs, nil)
|
||||||
Register("POST", psnet.UriStop, psnet.StopProcess, nil)
|
Register("POST", psnet.UriStop, psnet.StopProcess, nil)
|
||||||
|
|||||||
@@ -123,6 +123,8 @@ type YamlConfig struct {
|
|||||||
LicenseDir string `yaml:"licensedir"`
|
LicenseDir string `yaml:"licensedir"`
|
||||||
EtcListIMS string `yaml:"etcListIMS"`
|
EtcListIMS string `yaml:"etcListIMS"`
|
||||||
EtcListDefault string `yaml:"etcListDefault"`
|
EtcListDefault string `yaml:"etcListDefault"`
|
||||||
|
DpkgOverwrite bool `yaml:"dpkgOverwrite"`
|
||||||
|
DpkgTimeout int `yaml:"dpkgTimeout"`
|
||||||
} `yaml:"ne"`
|
} `yaml:"ne"`
|
||||||
|
|
||||||
Auth struct {
|
Auth struct {
|
||||||
|
|||||||
@@ -41,23 +41,23 @@ webServer:
|
|||||||
database:
|
database:
|
||||||
type: mysql
|
type: mysql
|
||||||
user: root
|
user: root
|
||||||
# password: "1000omc@kp!"
|
# password: 1000omc@kp!
|
||||||
# host: "192.168.2.166"
|
# host: 127.0.0.1
|
||||||
# port: 33066
|
# port: 33066
|
||||||
# name: omc_db
|
name: omc_db
|
||||||
backup: d:/local.git/ems.agt/restagent/database
|
backup: d:/local.git/ems.agt/restagent/database
|
||||||
password: "root@1234"
|
password: "root@1234"
|
||||||
host: "192.168.5.57"
|
host: "192.168.5.59"
|
||||||
port: 3306
|
port: 3306
|
||||||
name: "omc_db"
|
|
||||||
# Redis 缓存数据,数据源声明全小写
|
# Redis 缓存数据,数据源声明全小写
|
||||||
redis:
|
redis:
|
||||||
dataSource:
|
dataSource:
|
||||||
# OMC系统使用库
|
# OMC系统使用库
|
||||||
default:
|
default:
|
||||||
port: 6379 # Redis port
|
port: 6379 # Redis port
|
||||||
host: "192.168.5.57" # Redis host
|
host: "192.168.5.59" # Redis host
|
||||||
password: ""
|
password: "redis@1234"
|
||||||
db: 10 # Redis db_num
|
db: 10 # Redis db_num
|
||||||
# UDM网元用户库
|
# UDM网元用户库
|
||||||
udmuser:
|
udmuser:
|
||||||
@@ -66,7 +66,7 @@ redis:
|
|||||||
password: ""
|
password: ""
|
||||||
db: 0 # Redis db_num
|
db: 0 # Redis db_num
|
||||||
# 多个数据源时可以用这个指定默认的数据源
|
# 多个数据源时可以用这个指定默认的数据源
|
||||||
defaultDataSourceName: "default"
|
defaultDataSourceName: "default"
|
||||||
|
|
||||||
# sleep: time delay for after write buffer (millisecond)
|
# sleep: time delay for after write buffer (millisecond)
|
||||||
# deadLine: timeout for io read and write (second)
|
# deadLine: timeout for io read and write (second)
|
||||||
@@ -81,7 +81,7 @@ mml:
|
|||||||
|
|
||||||
# NE config
|
# NE config
|
||||||
ne:
|
ne:
|
||||||
user: root
|
user: agtuser
|
||||||
etcdir: /usr/local/etc
|
etcdir: /usr/local/etc
|
||||||
bindir: /usr/local/bin
|
bindir: /usr/local/bin
|
||||||
omcdir: /usr/local/omc
|
omcdir: /usr/local/omc
|
||||||
@@ -90,6 +90,10 @@ ne:
|
|||||||
# backup etc list of IMS,no space
|
# backup etc list of IMS,no space
|
||||||
etcListIMS: '{*.yaml,mmtel,vars.cfg}'
|
etcListIMS: '{*.yaml,mmtel,vars.cfg}'
|
||||||
etcListDefault: '{*.yaml,*.conf,*.cfg}'
|
etcListDefault: '{*.yaml,*.conf,*.cfg}'
|
||||||
|
# true/false to overwrite config file when dpkg ne software
|
||||||
|
dpkgOverwrite: false
|
||||||
|
# dpkg timeout (second)
|
||||||
|
dpkgTimeout: 180
|
||||||
|
|
||||||
# chk2ne: true/false, if put OmcNeConfig parameters to NE
|
# chk2ne: true/false, if put OmcNeConfig parameters to NE
|
||||||
omc:
|
omc:
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
# Makefile for rest agent project
|
# Makefile for rest agent project
|
||||||
|
|
||||||
PROJECT = OMC
|
PROJECT = OMC
|
||||||
VERSION = 2.2401.2
|
VERSION = 2.2401.3
|
||||||
PLATFORM = amd64
|
PLATFORM = amd64
|
||||||
ARMPLATFORM = aarch64
|
ARMPLATFORM = aarch64
|
||||||
BUILDDIR = ../../build
|
BUILDDIR = ../../build
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ import (
|
|||||||
"crypto/x509"
|
"crypto/x509"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
_ "net/http/pprof"
|
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -187,11 +186,6 @@ func main() {
|
|||||||
uriGroup.Use(libSession.SessionHeader())
|
uriGroup.Use(libSession.SessionHeader())
|
||||||
uriGroup.Any("/*any", gin.WrapH(routes.NewRouter()))
|
uriGroup.Any("/*any", gin.WrapH(routes.NewRouter()))
|
||||||
|
|
||||||
// 注册 pprof 路由
|
|
||||||
go func() {
|
|
||||||
http.ListenAndServe("0.0.0.0:6060", nil)
|
|
||||||
}()
|
|
||||||
|
|
||||||
// 开启监控采集
|
// 开启监控采集
|
||||||
// monitor.StartMonitor(false, "")
|
// monitor.StartMonitor(false, "")
|
||||||
|
|
||||||
|
|||||||
@@ -167,6 +167,7 @@ func ExecScript(scriptPath, workDir string) (string, error) {
|
|||||||
return stdout.String(), nil
|
return stdout.String(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CheckIllegal 检查传入的字符串参数中是否包含一些特殊字符
|
||||||
func CheckIllegal(args ...string) bool {
|
func CheckIllegal(args ...string) bool {
|
||||||
if args == nil {
|
if args == nil {
|
||||||
return false
|
return false
|
||||||
@@ -181,20 +182,23 @@ func CheckIllegal(args ...string) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// HasNoPasswordSudo 检查当前用户是否拥有sudo权限
|
||||||
func HasNoPasswordSudo() bool {
|
func HasNoPasswordSudo() bool {
|
||||||
cmd2 := exec.Command("sudo", "-n", "ls")
|
cmd2 := exec.Command("sudo", "-n", "uname")
|
||||||
err2 := cmd2.Run()
|
err2 := cmd2.Run()
|
||||||
return err2 == nil
|
return err2 == nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SudoHandleCmd 是否拥有sudo权限并拼接
|
||||||
func SudoHandleCmd() string {
|
func SudoHandleCmd() string {
|
||||||
cmd := exec.Command("sudo", "-n", "ls")
|
cmd := exec.Command("sudo", "-n", "uname")
|
||||||
if err := cmd.Run(); err == nil {
|
if err := cmd.Run(); err == nil {
|
||||||
return "sudo "
|
return "sudo "
|
||||||
}
|
}
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Which 可执行文件是否在系统的PATH环境变量中
|
||||||
func Which(name string) bool {
|
func Which(name string) bool {
|
||||||
_, err := exec.LookPath(name)
|
_, err := exec.LookPath(name)
|
||||||
return err == nil
|
return err == nil
|
||||||
|
|||||||
@@ -7,23 +7,23 @@ import (
|
|||||||
|
|
||||||
// 实例化服务层 NeInfoImpl 结构体
|
// 实例化服务层 NeInfoImpl 结构体
|
||||||
var NewNeInfoImpl = &NeInfoImpl{
|
var NewNeInfoImpl = &NeInfoImpl{
|
||||||
NeInfoRepository: repository.NewNeInfoImpl,
|
neInfoRepository: repository.NewNeInfoImpl,
|
||||||
}
|
}
|
||||||
|
|
||||||
// 网元信息 服务层处理
|
// 网元信息 服务层处理
|
||||||
type NeInfoImpl struct {
|
type NeInfoImpl struct {
|
||||||
// 网元信息数据信息
|
// 网元信息数据信息
|
||||||
NeInfoRepository repository.INeInfo
|
neInfoRepository repository.INeInfo
|
||||||
}
|
}
|
||||||
|
|
||||||
// SelectNeInfoByNeTypeAndNeID 通过ne_type和ne_id查询网元信息
|
// SelectNeInfoByNeTypeAndNeID 通过ne_type和ne_id查询网元信息
|
||||||
func (r *NeInfoImpl) SelectNeInfoByNeTypeAndNeID(neType, neID string) model.NeInfo {
|
func (r *NeInfoImpl) SelectNeInfoByNeTypeAndNeID(neType, neID string) model.NeInfo {
|
||||||
return r.NeInfoRepository.SelectNeInfoByNeTypeAndNeID(neType, neID)
|
return r.neInfoRepository.SelectNeInfoByNeTypeAndNeID(neType, neID)
|
||||||
}
|
}
|
||||||
|
|
||||||
// SelectNeList 查询网元列表
|
// SelectNeList 查询网元列表
|
||||||
func (r *NeInfoImpl) SelectNeList(ne model.NeInfo, bandStatus bool) []model.NeInfo {
|
func (r *NeInfoImpl) SelectNeList(ne model.NeInfo, bandStatus bool) []model.NeInfo {
|
||||||
list := r.NeInfoRepository.SelectNeList(ne)
|
list := r.neInfoRepository.SelectNeList(ne)
|
||||||
|
|
||||||
// 网元直连读取网元服务状态
|
// 网元直连读取网元服务状态
|
||||||
if bandStatus {
|
if bandStatus {
|
||||||
@@ -32,9 +32,12 @@ func (r *NeInfoImpl) SelectNeList(ne model.NeInfo, bandStatus bool) []model.NeIn
|
|||||||
v := (*neList)[i]
|
v := (*neList)[i]
|
||||||
result, err := NeState(v)
|
result, err := NeState(v)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
(*neList)[i].ServerState = map[string]any{}
|
(*neList)[i].ServerState = map[string]any{
|
||||||
|
"online": false,
|
||||||
|
}
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
result["online"] = true
|
||||||
(*neList)[i].ServerState = result
|
(*neList)[i].ServerState = result
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -97,6 +97,8 @@ func (r *SysDictDataImpl) DeleteDictDataByCodes(dictCodes []string) (int64, erro
|
|||||||
func (r *SysDictDataImpl) InsertDictData(sysDictData model.SysDictData) string {
|
func (r *SysDictDataImpl) InsertDictData(sysDictData model.SysDictData) string {
|
||||||
insertId := r.sysDictDataRepository.InsertDictData(sysDictData)
|
insertId := r.sysDictDataRepository.InsertDictData(sysDictData)
|
||||||
if insertId != "" {
|
if insertId != "" {
|
||||||
|
// 刷新缓存
|
||||||
|
r.sysDictTypeService.ClearDictCache(sysDictData.DictType)
|
||||||
r.sysDictTypeService.LoadingDictCache(sysDictData.DictType)
|
r.sysDictTypeService.LoadingDictCache(sysDictData.DictType)
|
||||||
}
|
}
|
||||||
return insertId
|
return insertId
|
||||||
@@ -106,6 +108,8 @@ func (r *SysDictDataImpl) InsertDictData(sysDictData model.SysDictData) string {
|
|||||||
func (r *SysDictDataImpl) UpdateDictData(sysDictData model.SysDictData) int64 {
|
func (r *SysDictDataImpl) UpdateDictData(sysDictData model.SysDictData) int64 {
|
||||||
rows := r.sysDictDataRepository.UpdateDictData(sysDictData)
|
rows := r.sysDictDataRepository.UpdateDictData(sysDictData)
|
||||||
if rows > 0 {
|
if rows > 0 {
|
||||||
|
// 刷新缓存
|
||||||
|
r.sysDictTypeService.ClearDictCache(sysDictData.DictType)
|
||||||
r.sysDictTypeService.LoadingDictCache(sysDictData.DictType)
|
r.sysDictTypeService.LoadingDictCache(sysDictData.DictType)
|
||||||
}
|
}
|
||||||
return rows
|
return rows
|
||||||
|
|||||||
@@ -1,16 +1,9 @@
|
|||||||
package controller
|
package controller
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"ems.agt/src/framework/cmd"
|
|
||||||
"ems.agt/src/framework/config"
|
|
||||||
"ems.agt/src/framework/i18n"
|
"ems.agt/src/framework/i18n"
|
||||||
"ems.agt/src/framework/utils/ctx"
|
"ems.agt/src/framework/utils/ctx"
|
||||||
"ems.agt/src/framework/utils/ssh"
|
|
||||||
"ems.agt/src/framework/vo/result"
|
"ems.agt/src/framework/vo/result"
|
||||||
netElementService "ems.agt/src/modules/network_element/service"
|
|
||||||
traceService "ems.agt/src/modules/trace/service"
|
traceService "ems.agt/src/modules/trace/service"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/gin-gonic/gin/binding"
|
"github.com/gin-gonic/gin/binding"
|
||||||
@@ -18,7 +11,6 @@ import (
|
|||||||
|
|
||||||
// 实例化控制层 TcpdumpController 结构体
|
// 实例化控制层 TcpdumpController 结构体
|
||||||
var NewTcpdump = &TcpdumpController{
|
var NewTcpdump = &TcpdumpController{
|
||||||
NeInfoService: netElementService.NewNeInfoImpl,
|
|
||||||
TcpdumpService: traceService.NewTcpdumpImpl,
|
TcpdumpService: traceService.NewTcpdumpImpl,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -26,23 +18,19 @@ var NewTcpdump = &TcpdumpController{
|
|||||||
//
|
//
|
||||||
// PATH /tcpdump
|
// PATH /tcpdump
|
||||||
type TcpdumpController struct {
|
type TcpdumpController struct {
|
||||||
// 网元信息服务
|
|
||||||
NeInfoService netElementService.INeInfo
|
|
||||||
// 信令抓包服务
|
// 信令抓包服务
|
||||||
TcpdumpService traceService.ITcpdump
|
TcpdumpService traceService.ITcpdump
|
||||||
}
|
}
|
||||||
|
|
||||||
// 网元发送执行 pcap
|
// 网元抓包PACP 开始
|
||||||
//
|
//
|
||||||
// POST /ne
|
// POST /start
|
||||||
func (s *TcpdumpController) NeTask(c *gin.Context) {
|
func (s *TcpdumpController) DumpStart(c *gin.Context) {
|
||||||
language := ctx.AcceptLanguage(c)
|
language := ctx.AcceptLanguage(c)
|
||||||
var body struct {
|
var body struct {
|
||||||
NeType string `json:"neType" binding:"required"` // 网元类型
|
NeType string `json:"neType" binding:"required"` // 网元类型
|
||||||
NeId string `json:"neId" binding:"required"` // 网元ID
|
NeId string `json:"neId" binding:"required"` // 网元ID
|
||||||
Timeout int `json:"timeout" binding:"required"` // 超时时间
|
Cmd string `json:"cmd" binding:"required"` // 命令 "-n -s 0 -v -w"
|
||||||
Cmd string `json:"cmd" binding:"required"` // 命令
|
|
||||||
Timestamp string `json:"timestamp" binding:"required"` // 时间戳
|
|
||||||
}
|
}
|
||||||
err := c.ShouldBindBodyWith(&body, binding.JSON)
|
err := c.ShouldBindBodyWith(&body, binding.JSON)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -50,47 +38,32 @@ func (s *TcpdumpController) NeTask(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检查网元信息
|
fileName, err := s.TcpdumpService.DumpStart(body.NeType, body.NeId, body.Cmd)
|
||||||
neInfo := s.NeInfoService.SelectNeInfoByNeTypeAndNeID(body.NeType, body.NeId)
|
if err != nil {
|
||||||
if neInfo.NeId != body.NeId {
|
msg := err.Error()
|
||||||
// 找不到 %s %s 对应网元信息
|
if msg == "noData" {
|
||||||
msg := i18n.TTemplate(language, "trace.tcpdump.noData", map[string]any{"type": body.NeType, "id": body.NeId})
|
// 找不到 %s %s 对应网元信息
|
||||||
|
msg = i18n.TTemplate(language, "trace.tcpdump.noData", map[string]any{"type": body.NeType, "id": body.NeId})
|
||||||
|
}
|
||||||
c.JSON(200, result.ErrMsg(msg))
|
c.JSON(200, result.ErrMsg(msg))
|
||||||
return
|
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
|
|
||||||
}
|
|
||||||
if strings.Contains(msg, "command not found") {
|
|
||||||
c.JSON(200, result.ErrMsg("Command [tcpdump] Not Found"))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
c.JSON(200, result.OkData(map[string]any{
|
c.JSON(200, result.OkData(map[string]any{
|
||||||
"cmd": cmdStr,
|
"msg": "tcpdump started",
|
||||||
"msg": msg,
|
"out": fileName,
|
||||||
"fileName": filePcapName,
|
"log": "",
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
// 网元抓包pcap文件下载
|
// 网元抓包PACP 结束
|
||||||
//
|
//
|
||||||
// POST /download
|
// POST /stop
|
||||||
func (s *TcpdumpController) Download(c *gin.Context) {
|
func (s *TcpdumpController) DumpStop(c *gin.Context) {
|
||||||
language := ctx.AcceptLanguage(c)
|
language := ctx.AcceptLanguage(c)
|
||||||
var body struct {
|
var body struct {
|
||||||
NeType string `json:"neType" binding:"required"` // 网元类型
|
NeType string `json:"neType" binding:"required"` // 网元类型
|
||||||
NeId string `json:"neId" binding:"required"` // 网元ID
|
NeId string `json:"neId" binding:"required"` // 网元ID
|
||||||
FileName string `form:"fileName" ` // 文件名
|
FileName string `json:"fileName"` // 文件名 查看日志信息
|
||||||
}
|
}
|
||||||
err := c.ShouldBindBodyWith(&body, binding.JSON)
|
err := c.ShouldBindBodyWith(&body, binding.JSON)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -98,37 +71,34 @@ func (s *TcpdumpController) Download(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检查网元信息
|
logMsg, err := s.TcpdumpService.DumpStop(body.NeType, body.NeId, body.FileName)
|
||||||
neInfo := s.NeInfoService.SelectNeInfoByNeTypeAndNeID(body.NeType, body.NeId)
|
if err != nil {
|
||||||
if neInfo.NeId != body.NeId {
|
msg := err.Error()
|
||||||
// 找不到 %s %s 对应网元信息
|
if msg == "noData" {
|
||||||
msg := i18n.TTemplate(language, "trace.tcpdump.noData", map[string]any{"type": body.NeType, "id": body.NeId})
|
// 找不到 %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))
|
c.JSON(200, result.ErrMsg(msg))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
c.JSON(200, result.OkData(map[string]any{
|
||||||
nePath := fmt.Sprintf("/tmp/%s", body.FileName)
|
"msg": "tcpdump stopped",
|
||||||
localPath := fmt.Sprintf("/tmp/omc/tcpdump/%s", body.FileName)
|
"out": body.FileName,
|
||||||
err = ssh.FileSCPNeToLocal(neInfo.IP, nePath, localPath)
|
"log": logMsg,
|
||||||
if err != nil {
|
}))
|
||||||
c.JSON(200, result.ErrMsg(err.Error()))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
c.FileAttachment(localPath, body.FileName)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 网元发送执行 pcap
|
// UPF标准版内部抓包
|
||||||
//
|
//
|
||||||
// POST /neUPF
|
// POST /traceUPF
|
||||||
func (s *TcpdumpController) NeUPFTask(c *gin.Context) {
|
func (s *TcpdumpController) TraceUPF(c *gin.Context) {
|
||||||
language := ctx.AcceptLanguage(c)
|
language := ctx.AcceptLanguage(c)
|
||||||
var body struct {
|
var body struct {
|
||||||
NeType string `json:"neType" binding:"required"` // 网元类型
|
NeType string `json:"neType" binding:"required"` // 网元类型
|
||||||
NeId string `json:"neId" binding:"required"` // 网元ID
|
NeId string `json:"neId" binding:"required"` // 网元ID
|
||||||
RunType string `json:"runType" binding:"required"` // 执行开始start还是停止stop
|
Cmd string `json:"cmd" binding:"required"` // 命令
|
||||||
Cmd string `json:"cmd" binding:"required"` // 命令
|
|
||||||
Timestamp string `json:"timestamp" binding:"required"` // 时间戳
|
|
||||||
}
|
}
|
||||||
err := c.ShouldBindBodyWith(&body, binding.JSON)
|
err := c.ShouldBindBodyWith(&body, binding.JSON)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -136,154 +106,19 @@ func (s *TcpdumpController) NeUPFTask(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检查网元信息
|
fileName, logMsg, err := s.TcpdumpService.DumpUPF(body.NeType, body.NeId, body.Cmd)
|
||||||
neInfo := s.NeInfoService.SelectNeInfoByNeTypeAndNeID(body.NeType, body.NeId)
|
if err != nil {
|
||||||
if neInfo.NeId != body.NeId {
|
msg := err.Error()
|
||||||
msg := i18n.TTemplate(language, "trace.tcpdump.noData", map[string]any{"type": body.NeType, "id": body.NeId})
|
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))
|
c.JSON(200, result.ErrMsg(msg))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
c.JSON(200, result.OkData(map[string]any{
|
||||||
// 开始telnet
|
"msg": "trace UPF dump pacp",
|
||||||
if body.RunType == "start_telnet" {
|
"out": fileName,
|
||||||
filePcapName := fmt.Sprintf("%s_%s_%s.pcap", body.Timestamp, body.NeType, body.NeId)
|
"log": logMsg,
|
||||||
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
|
|
||||||
}
|
|
||||||
if strings.Contains(msg, "command not found") {
|
|
||||||
c.JSON(200, result.ErrMsg("Command [expect] Not Found"))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if strings.Contains(msg, "Unable to connect to remote host") {
|
|
||||||
c.JSON(200, result.ErrMsg("Connection Refused"))
|
|
||||||
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
|
|
||||||
}
|
|
||||||
if strings.Contains(msg, "command not found") {
|
|
||||||
c.JSON(200, result.ErrMsg("Command [expect] Not Found"))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if strings.Contains(msg, "Unable to connect to remote host") {
|
|
||||||
c.JSON(200, result.ErrMsg("Connection Refused"))
|
|
||||||
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("Please select RunType to execute: start_telnet/stop_telnet/start_str/stop_str"))
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,13 @@
|
|||||||
package service
|
package service
|
||||||
|
|
||||||
// 通用请求 服务层接口
|
// 信令抓包 服务层接口
|
||||||
type ITcpdump interface {
|
type ITcpdump interface {
|
||||||
// UPFTelnetStart UPF进行telnet抓包
|
// DumpStart 触发tcpdump开始抓包 filePcapName, err
|
||||||
UPFTelnet(neIp, cmdStr string) (string, error)
|
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)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,40 +2,184 @@ package service
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"ems.agt/src/framework/cmd"
|
||||||
|
"ems.agt/src/framework/config"
|
||||||
|
"ems.agt/src/framework/logger"
|
||||||
|
"ems.agt/src/framework/utils/date"
|
||||||
neService "ems.agt/src/modules/network_element/service"
|
neService "ems.agt/src/modules/network_element/service"
|
||||||
)
|
)
|
||||||
|
|
||||||
// 实例化服务层 TcpdumpImpl 结构体
|
// 实例化服务层 TcpdumpImpl 结构体
|
||||||
var NewTcpdumpImpl = &TcpdumpImpl{
|
var NewTcpdumpImpl = &TcpdumpImpl{
|
||||||
neInfoService: neService.NewNeInfoImpl,
|
neInfoService: neService.NewNeInfoImpl,
|
||||||
|
tcpdumpPIDMap: map[string]string{},
|
||||||
}
|
}
|
||||||
|
|
||||||
// 通用请求 服务层处理
|
// 信令抓包 服务层处理
|
||||||
type TcpdumpImpl struct {
|
type TcpdumpImpl struct {
|
||||||
// 网元信息服务
|
// 网元信息服务
|
||||||
neInfoService neService.INeInfo
|
neInfoService neService.INeInfo
|
||||||
|
// 抓包进程PID
|
||||||
|
tcpdumpPIDMap map[string]string
|
||||||
}
|
}
|
||||||
|
|
||||||
// UPFTelnetStart UPF进行telnet抓包
|
// DumpStart 触发tcpdump开始抓包 filePcapName, err
|
||||||
func (s *TcpdumpImpl) UPFTelnet(neIp, cmdStr string) (string, error) {
|
func (s *TcpdumpImpl) DumpStart(neType, neId, cmdStr string) (string, error) {
|
||||||
// 创建TCP连接
|
// 检查网元信息
|
||||||
conn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", neIp, 5002))
|
neInfo := s.neInfoService.SelectNeInfoByNeTypeAndNeID(neType, neId)
|
||||||
if err != nil {
|
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
|
return "", err
|
||||||
}
|
}
|
||||||
defer conn.Close()
|
|
||||||
|
|
||||||
fmt.Fprintln(conn, cmdStr)
|
// 检查进程 ps aux | grep tcpdump
|
||||||
|
// 强杀 sudo pkill tcpdump
|
||||||
// 读取内容
|
s.tcpdumpPIDMap[neTypeID] = msg
|
||||||
time.Sleep(time.Duration(300) * time.Millisecond)
|
return fileName, err
|
||||||
buf := make([]byte, 1024*8)
|
}
|
||||||
n, err := conn.Read(buf)
|
|
||||||
if err != nil {
|
// DumpStop 停止已存在抓包句柄
|
||||||
return "", err
|
func (s *TcpdumpImpl) DumpStop(neType, neId, fileName string) (string, error) {
|
||||||
}
|
// 检查网元信息
|
||||||
return string(buf[0:n]), nil
|
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
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,20 +16,20 @@ func Setup(router *gin.Engine) {
|
|||||||
// 信令抓包
|
// 信令抓包
|
||||||
tcpdumpGroup := router.Group("/tcpdump")
|
tcpdumpGroup := router.Group("/tcpdump")
|
||||||
{
|
{
|
||||||
tcpdumpGroup.POST("/ne",
|
tcpdumpGroup.POST("/start",
|
||||||
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)),
|
||||||
controller.NewTcpdump.NeTask,
|
controller.NewTcpdump.DumpStart,
|
||||||
)
|
)
|
||||||
tcpdumpGroup.POST("/neUPF",
|
tcpdumpGroup.POST("/stop",
|
||||||
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)),
|
||||||
controller.NewTcpdump.NeUPFTask,
|
controller.NewTcpdump.DumpStop,
|
||||||
)
|
)
|
||||||
tcpdumpGroup.POST("/download",
|
tcpdumpGroup.POST("/traceUPF",
|
||||||
middleware.PreAuthorize(nil),
|
middleware.PreAuthorize(nil),
|
||||||
collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.tcpdump", collectlogs.BUSINESS_TYPE_IMPORT)),
|
collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.tcpdump", collectlogs.BUSINESS_TYPE_OTHER)),
|
||||||
controller.NewTcpdump.Download,
|
controller.NewTcpdump.TraceUPF,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user