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)
|
||||
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)
|
||||
neVersion, err := dborm.XormGetDataBySQL(sql)
|
||||
@@ -536,13 +536,13 @@ func DistributeSoftwareToNF(w http.ResponseWriter, r *http.Request) {
|
||||
services.ResponseInternalServerError500ProcessError(w, err)
|
||||
return
|
||||
}
|
||||
log.Debug("neVersion:", neVersion)
|
||||
log.Trace("neVersion:", neVersion)
|
||||
|
||||
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)
|
||||
cmd := exec.Command("ssh", sshHost, mkdirCmd)
|
||||
out, err := cmd.CombinedOutput()
|
||||
log.Debugf("Exec output: %v", string(out))
|
||||
log.Tracef("Exec output: %v", string(out))
|
||||
if err != nil {
|
||||
log.Error("Faile to mkdir:", err)
|
||||
services.ResponseInternalServerError500ProcessError(w, err)
|
||||
@@ -557,7 +557,7 @@ func DistributeSoftwareToNF(w http.ResponseWriter, r *http.Request) {
|
||||
neInfo.Ip, config.GetYamlConfig().NE.ScpDir)
|
||||
cmd = exec.Command("scp", "-r", srcFile, scpDir)
|
||||
out, err = cmd.CombinedOutput()
|
||||
log.Debugf("Exec output: %v", string(out))
|
||||
log.Tracef("Exec output: %v", string(out))
|
||||
if err != nil {
|
||||
log.Errorf("Faile to scp NF: neType=%s, neId=%s, ip=%s", neType, neId, neInfo.Ip)
|
||||
services.ResponseInternalServerError500ProcessError(w, err)
|
||||
@@ -568,7 +568,7 @@ func DistributeSoftwareToNF(w http.ResponseWriter, r *http.Request) {
|
||||
config.GetYamlConfig().NE.OmcDir, neTypeLower)
|
||||
cmd = exec.Command("ssh", sshHost, cpCmd)
|
||||
out, err = cmd.CombinedOutput()
|
||||
log.Debugf("Exec output: %v", string(out))
|
||||
log.Tracef("Exec output: %v", string(out))
|
||||
if err != nil {
|
||||
log.Error("Faile to execute cp command:", err)
|
||||
services.ResponseInternalServerError500ProcessError(w, err)
|
||||
@@ -667,7 +667,7 @@ func ActiveSoftwareToNF(w http.ResponseWriter, r *http.Request) {
|
||||
services.ResponseInternalServerError500ProcessError(w, err)
|
||||
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)
|
||||
neVersion, err := dborm.XormGetDataBySQL(sql)
|
||||
@@ -681,62 +681,104 @@ func ActiveSoftwareToNF(w http.ResponseWriter, r *http.Request) {
|
||||
services.ResponseInternalServerError500ProcessError(w, err)
|
||||
return
|
||||
}
|
||||
log.Debug("neVersion:", neVersion)
|
||||
log.Trace("neVersion:", neVersion)
|
||||
|
||||
if !config.GetYamlConfig().OMC.TestMode {
|
||||
filePath := (*neVersion)[0]["file_path"]
|
||||
sshHost := fmt.Sprintf("%s@%s", config.GetYamlConfig().NE.User, neInfo.Ip)
|
||||
fileType := global.IsRpmOrDebPackage(filePath)
|
||||
if fileType == 1 {
|
||||
rpmCmd := fmt.Sprintf("sudo rpm -Uvh '%s'", filePath)
|
||||
cmd := exec.Command("ssh", sshHost, rpmCmd)
|
||||
out, err := cmd.CombinedOutput()
|
||||
log.Debugf("Exec output: %v", string(out))
|
||||
if err != nil {
|
||||
log.Error("Faile to execute rpm command:", err)
|
||||
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)
|
||||
srcFile := fmt.Sprintf("%s/actpkg.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
|
||||
}
|
||||
|
||||
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 {
|
||||
case "omc":
|
||||
restartCmd := fmt.Sprintf("sudo %s/bin/omcsvc.sh restart", config.GetYamlConfig().NE.OmcDir)
|
||||
cmd := exec.Command("ssh", sshHost, restartCmd)
|
||||
out, err := cmd.CombinedOutput()
|
||||
log.Debugf("Exec output: %v", string(out))
|
||||
log.Tracef("Exec output: %v", string(out))
|
||||
if err != nil {
|
||||
log.Error("Faile to execute ssh restart omc:", err)
|
||||
services.ResponseInternalServerError500ProcessError(w, err)
|
||||
@@ -746,9 +788,9 @@ func ActiveSoftwareToNF(w http.ResponseWriter, r *http.Request) {
|
||||
restartCmd := "sudo ims-stop && sudo ims-start"
|
||||
cmd := exec.Command("ssh", sshHost, restartCmd)
|
||||
out, err := cmd.CombinedOutput()
|
||||
log.Debugf("Exec output: %v", string(out))
|
||||
log.Tracef("Exec output: %v", string(out))
|
||||
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)
|
||||
return
|
||||
}
|
||||
@@ -756,7 +798,7 @@ func ActiveSoftwareToNF(w http.ResponseWriter, r *http.Request) {
|
||||
restartCmd := fmt.Sprintf("sudo systemctl restart %s.service", neTypeLower)
|
||||
cmd := exec.Command("ssh", sshHost, restartCmd)
|
||||
out, err := cmd.CombinedOutput()
|
||||
log.Debugf("Exec output: %v", string(out))
|
||||
log.Tracef("Exec output: %v", string(out))
|
||||
if err != nil {
|
||||
log.Error("Faile to execute ssh sudo systemctl command:", err)
|
||||
services.ResponseInternalServerError500ProcessError(w, err)
|
||||
@@ -844,39 +886,94 @@ func RollBackSoftwareToNF(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
if !config.GetYamlConfig().OMC.TestMode {
|
||||
sshHost := fmt.Sprintf("%s@%s", config.GetYamlConfig().NE.User, neInfo.Ip)
|
||||
fileType := global.IsRpmOrDebPackage(filePath)
|
||||
if fileType == 1 {
|
||||
rpmCmd := fmt.Sprintf("sudo rpm -Uvh --oldpackage '%s'", filePath)
|
||||
cmd := exec.Command("ssh", sshHost, rpmCmd)
|
||||
out, err := cmd.CombinedOutput()
|
||||
log.Debugf("Exec output: %v", string(out))
|
||||
if err != nil {
|
||||
log.Error("Faile to execute rpm command:", err)
|
||||
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)
|
||||
srcFile := fmt.Sprintf("%s/rbkpkg.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
|
||||
}
|
||||
|
||||
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 {
|
||||
case "omc":
|
||||
restartCmd := fmt.Sprintf("sudo %s/bin/omcsvc.sh restart", config.GetYamlConfig().NE.OmcDir)
|
||||
cmd := exec.Command("ssh", sshHost, restartCmd)
|
||||
out, err := cmd.CombinedOutput()
|
||||
log.Debugf("Exec output: %v", string(out))
|
||||
log.Tracef("Exec output: %v", string(out))
|
||||
if err != nil {
|
||||
log.Error("Faile to execute ssh restart omc:", err)
|
||||
services.ResponseInternalServerError500ProcessError(w, err)
|
||||
@@ -886,9 +983,9 @@ func RollBackSoftwareToNF(w http.ResponseWriter, r *http.Request) {
|
||||
restartCmd := "sudo ims-stop && sudo ims-start"
|
||||
cmd := exec.Command("ssh", sshHost, restartCmd)
|
||||
out, err := cmd.CombinedOutput()
|
||||
log.Debugf("Exec output: %v", string(out))
|
||||
log.Tracef("Exec output: %v", string(out))
|
||||
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)
|
||||
return
|
||||
}
|
||||
@@ -896,7 +993,7 @@ func RollBackSoftwareToNF(w http.ResponseWriter, r *http.Request) {
|
||||
restartCmd := fmt.Sprintf("sudo systemctl restart %s.service", neTypeLower)
|
||||
cmd := exec.Command("ssh", sshHost, restartCmd)
|
||||
out, err := cmd.CombinedOutput()
|
||||
log.Debugf("Exec output: %v", string(out))
|
||||
log.Tracef("Exec output: %v", string(out))
|
||||
if err != nil {
|
||||
log.Error("Faile to execute ssh sudo systemctl command:", err)
|
||||
services.ResponseInternalServerError500ProcessError(w, err)
|
||||
|
||||
@@ -528,7 +528,7 @@ func PostMMLToOMC(w http.ResponseWriter, r *http.Request) {
|
||||
services.ResponseNotFound404UriNotExist(w, r)
|
||||
return
|
||||
}
|
||||
log.Debug("Body:", string(body))
|
||||
log.Trace("Body:", string(body))
|
||||
|
||||
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)
|
||||
return
|
||||
}
|
||||
log.Debug("Request body:", string(body))
|
||||
log.Trace("Request body:", string(body))
|
||||
kpiReport := new(KpiReport)
|
||||
_ = json.Unmarshal(body, &kpiReport)
|
||||
log.Debug("kpiReport:", kpiReport)
|
||||
log.Trace("kpiReport:", kpiReport)
|
||||
|
||||
session := xEngine.NewSession()
|
||||
defer session.Close()
|
||||
|
||||
@@ -9,6 +9,8 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/shirou/gopsutil/v3/net"
|
||||
|
||||
"github.com/go-resty/resty/v2"
|
||||
"github.com/gorilla/mux"
|
||||
|
||||
@@ -726,6 +728,112 @@ func GetAllSysinfoFromNF(w http.ResponseWriter, r *http.Request) {
|
||||
func GetStateFromNF(w http.ResponseWriter, r *http.Request) {
|
||||
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)
|
||||
|
||||
vars := mux.Vars(r)
|
||||
@@ -876,6 +984,35 @@ func GetEMSState(ip string) *SysState {
|
||||
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"
|
||||
if global.Version != "" {
|
||||
version = global.Version
|
||||
@@ -886,7 +1023,7 @@ func GetEMSState(ip string) *SysState {
|
||||
HostName: hostName,
|
||||
OsInfo: getUnameStr(),
|
||||
DbInfo: dbInfo,
|
||||
IpAddr: []string{ip},
|
||||
IpAddr: ipAddrs,
|
||||
Port: config.GetYamlConfig().Rest[0].Port,
|
||||
Version: version,
|
||||
Capability: 9999999,
|
||||
|
||||
Reference in New Issue
Block a user