This commit is contained in:
2023-09-16 19:23:11 +08:00
parent 9c8b4d8dc8
commit de9189bd32
6 changed files with 119 additions and 84 deletions

View File

@@ -74,6 +74,7 @@ ne:
etcdir: /usr/local/etc
bindir: /usr/local/bin
omcdir: /usr/local/omc
scpdir: /tmp
licensedir: /usr/local/etc/{neType}/license
# chk2ne: true/false, if put OmcNeConfig parameters to NE
@@ -88,7 +89,7 @@ omc:
dn: 4600
chk2ne: false
sn: 13750650
checksign: true
checksign: false
backup: /usr/local/omc/backup
upload: /usr/local/omc/upload
frontUpload: /usr/local/omc/htdocs/front/upload
@@ -97,7 +98,7 @@ omc:
license: /usr/local/omc/license
gtpUri: gtp:192.168.2.119:2152
checkContentType: false
testMode: true
testMode: false
rbacMode: true
runDir: /usr/local/omc/run

View File

@@ -1,48 +1,51 @@
package main
import (
"flag"
"fmt"
//"os"
"encoding/binary"
"encoding/hex"
"io/ioutil"
"os/exec"
"flag"
"fmt"
"os"
//"os"
"encoding/binary"
"encoding/hex"
"os/exec"
)
const magicMicroseconds = 0xa1b2c3d4
const versionMajor = 2
const versionMinor = 4
func WriteEmptyPcap(filename string, timestamp int64, length int, data []byte) error {
var cooked = [...]byte{0x00,0x00,0x03,0x04,0x00,0x06,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00}
var buf []byte
func WriteEmptyPcap(filename string, timestamp int64, length int, data []byte) error {
var cooked = [...]byte{0x00, 0x00, 0x03, 0x04, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00}
var buf []byte
//24+16+16 = 56
buf = make([]byte, 56+length)
binary.LittleEndian.PutUint32(buf[0:4], magicMicroseconds)
binary.LittleEndian.PutUint16(buf[4:6], versionMajor)
binary.LittleEndian.PutUint16(buf[6:8], versionMinor)
// bytes 8:12 stay 0 (timezone = UTC)
// bytes 12:16 stay 0 (sigfigs is always set to zero, according to
// http://wiki.wireshark.org/Development/LibpcapFileFormat
binary.LittleEndian.PutUint32(buf[16:20], 0x00040000)
binary.LittleEndian.PutUint32(buf[20:24], 0x00000071)
binary.LittleEndian.PutUint32(buf[0:4], magicMicroseconds)
binary.LittleEndian.PutUint16(buf[4:6], versionMajor)
binary.LittleEndian.PutUint16(buf[6:8], versionMinor)
// bytes 8:12 stay 0 (timezone = UTC)
// bytes 12:16 stay 0 (sigfigs is always set to zero, according to
// http://wiki.wireshark.org/Development/LibpcapFileFormat
binary.LittleEndian.PutUint32(buf[16:20], 0x00040000)
binary.LittleEndian.PutUint32(buf[20:24], 0x00000071)
// Packet Header
binary.LittleEndian.PutUint64(buf[24:32], uint64(timestamp))
binary.LittleEndian.PutUint32(buf[32:36], uint32(length+16))
binary.LittleEndian.PutUint32(buf[36:40], uint32(length+16))
copy(buf[40:], cooked[:])
copy(buf[56:], data[:])
err := ioutil.WriteFile(filename, buf[:], 0644)
err := os.WriteFile(filename, buf[:], 0644)
//fmt.Printf("CAP: %v\n", buf)
return err
return err
}
//tshark -r gtp.pcap -T json -d tcp.port==8080,http2 -Y "http2"
// tshark -r gtp.pcap -T json -d tcp.port==8080,http2 -Y "http2"
func execTshark(html string, filename string, proto string, port int) {
var tshark *exec.Cmd
var sharkCmd string
@@ -51,10 +54,10 @@ func execTshark(html string, filename string, proto string, port int) {
dataPort := fmt.Sprintf("tcp.port==%d,http2", port)
if proto == "http2" {
//tshark = exec.Command("tshark", "-r"+pcapPath,
// "-Y"+proto,
// "-Y"+proto,
// "-d"+dataPort,
// "-T", "pdml")
sharkCmd = fmt.Sprintf("tshark -r %s -T pdml -d tcp.port==%d,http2 -Y \"%s\" > %s.pdml", pcapPath, dataPort, proto, pcapPath)
// "-T", "pdml")
sharkCmd = fmt.Sprintf("tshark -r %s -T pdml -d tcp.port==%s,http2 -Y \"%s\" > %s.pdml", pcapPath, dataPort, proto, pcapPath)
tshark = exec.Command("sh", "-c", sharkCmd)
} else {
//tshark = exec.Command("tshark", "-r"+pcapPath,
@@ -71,30 +74,30 @@ func execTshark(html string, filename string, proto string, port int) {
pdmlFile := fmt.Sprintf("%s.pdml", filename)
//err1 := os.WriteFile(pdmlFile, []byte(out), 0666)
//if err1 != nil {
// fmt.Println("write html failed")
//}else {
//xsltproc pdml2html.xsl ngap.pdml > /home/agtuser/ngap.html
command := fmt.Sprintf("xsltproc /usr/lib64/pdml2html.xsl %s > %s", pdmlFile, html)
dest := exec.Command("sh", "-c", command)
_, err2 := dest.Output()
if err2 != nil {
fmt.Println("Error:", err2, command)
}
//if err1 != nil {
// fmt.Println("write html failed")
//}else {
//xsltproc pdml2html.xsl ngap.pdml > /home/agtuser/ngap.html
command := fmt.Sprintf("xsltproc /usr/lib64/pdml2html.xsl %s > %s", pdmlFile, html)
dest := exec.Command("sh", "-c", command)
_, err2 := dest.Output()
if err2 != nil {
fmt.Println("Error:", err2, command)
}
//}
}
}
func ipDataHandle(html string, iftype string, port int, timestamp int64, data []byte) int {
var filePath, proto string
if iftype == "N2" || iftype == "N1" {
filePath = fmt.Sprintf("/tmp/ng%d.pcap", timestamp)
proto = "ngap"
}else if iftype == "N4" {
} else if iftype == "N4" {
filePath = fmt.Sprintf("/tmp/pf%d.pcap", timestamp)
proto = "pfcp"
}else {
} else {
filePath = fmt.Sprintf("/tmp/hp%d.pcap", timestamp)
proto = "http2"
}
@@ -102,7 +105,7 @@ func ipDataHandle(html string, iftype string, port int, timestamp int64, data []
err := WriteEmptyPcap(filePath, timestamp, len(data), data)
if err != nil {
fmt.Printf("tshark failed with %s\n", err)
} else {
execTshark(html, filePath, proto, port)
}
@@ -114,14 +117,14 @@ func main() {
var timestamp int64
var port int
flag.Int64Var(&timestamp,"t",0,"timestamp")
flag.StringVar(&iftype,"i","","interface type")
flag.IntVar(&port,"p",0,"data port")
flag.StringVar(&ipdata,"d","","ip packet data")
flag.StringVar(&html,"f","","html file path")
flag.Int64Var(&timestamp, "t", 0, "timestamp")
flag.StringVar(&iftype, "i", "", "interface type")
flag.IntVar(&port, "p", 0, "data port")
flag.StringVar(&ipdata, "d", "", "ip packet data")
flag.StringVar(&html, "f", "", "html file path")
flag.Parse()
ds, err := hex.DecodeString(ipdata)
if err != nil {

View File

@@ -457,7 +457,6 @@ func ExportCmFromNF(w http.ResponseWriter, r *http.Request) {
//services.ResponseFileWithNameAndMD5(w, http.StatusOK, zipFile, path, md5Sum)
services.ResponseStatusOK204NoContent(w)
return
}
type ImportCMJson struct {
@@ -481,7 +480,7 @@ func ImportCmToNF(w http.ResponseWriter, r *http.Request) {
return
}
neTypeUpper := strings.ToUpper(neType)
neTypeLower := strings.ToLower(neType)
//neTypeLower := strings.ToLower(neType)
neId := services.GetUriParamString(r, "ne_id", ",", false, false)
@@ -534,41 +533,75 @@ func ImportCmToNF(w http.ResponseWriter, r *http.Request) {
log.Error("Faile to XormInsertTableOne:", err)
services.ResponseInternalServerError500ProcessError(w, err)
return
} else if has == false {
} else if !has {
err = global.ErrCMInvalidBackupFile
log.Error(err)
services.ResponseInternalServerError500ProcessError(w, err)
return
}
nePath := fmt.Sprintf("%s/etc/%s", config.GetYamlConfig().OMC.Upload, neTypeLower)
isExist, err := global.PathExists(nePath)
if err != nil {
log.Errorf("Failed to stat:", err)
services.ResponseInternalServerError500ProcessError(w, err)
return
}
if isExist {
err = os.RemoveAll(nePath)
if err != nil {
log.Errorf("Failed to remove:", err)
services.ResponseInternalServerError500ProcessError(w, err)
return
}
}
unzipCmd := fmt.Sprintf("unzip -o %s -d %s", filePath, config.GetYamlConfig().OMC.Upload)
// nePath := fmt.Sprintf("%s/etc/%s", config.GetYamlConfig().OMC.Upload, neTypeLower)
// isExist, err := global.PathExists(nePath)
// if err != nil {
// log.Errorf("Failed to stat:", err)
// services.ResponseInternalServerError500ProcessError(w, err)
// return
// }
// if isExist {
// err = os.RemoveAll(nePath)
// if err != nil {
// log.Errorf("Failed to remove:", err)
// services.ResponseInternalServerError500ProcessError(w, err)
// return
// }
// }
// unzipCmd := fmt.Sprintf("unzip -o %s -d %s", filePath, config.GetYamlConfig().OMC.Upload)
var scpCmd string
// var scpCmd string
// ipType := global.ParseIPAddr(neInfo.Ip)
// if ipType == global.IsIPv4 {
// scpCmd = fmt.Sprintf("scp -r %s/etc/%s %s@%s:%s", config.GetYamlConfig().OMC.Upload,
// neTypeLower, config.GetYamlConfig().NE.User, neInfo.Ip, config.GetYamlConfig().NE.EtcDir)
// } else {
// scpCmd = fmt.Sprintf("scp -r %s/etc/%s %s@[%s]:%s", config.GetYamlConfig().OMC.Upload,
// neTypeLower, config.GetYamlConfig().NE.User, neInfo.Ip, config.GetYamlConfig().NE.EtcDir)
// }
// err = ExecCmd(fmt.Sprintf("%s && %s", unzipCmd, scpCmd))
// if err != nil {
// log.Errorf("Faile to scp NF: neType=%s, neId=%s, ip=%s", neType, neId, neInfo.Ip)
// services.ResponseInternalServerError500ProcessError(w, err)
// return
// }
// nePath := fmt.Sprintf("%s/etc/%s", config.GetYamlConfig().OMC.Upload, neTypeLower)
// isExist, err := global.PathExists(nePath)
// if err != nil {
// log.Errorf("Failed to stat:", err)
// services.ResponseInternalServerError500ProcessError(w, err)
// return
// }
// if isExist {
// err = os.RemoveAll(nePath)
// if err != nil {
// log.Errorf("Failed to remove:", err)
// services.ResponseInternalServerError500ProcessError(w, err)
// return
// }
// }
var scpZipCmd string
ipType := global.ParseIPAddr(neInfo.Ip)
if ipType == global.IsIPv4 {
scpCmd = fmt.Sprintf("scp -r %s/etc/%s %s@%s:%s", config.GetYamlConfig().OMC.Upload,
neTypeLower, config.GetYamlConfig().NE.User, neInfo.Ip, config.GetYamlConfig().NE.EtcDir)
scpZipCmd = fmt.Sprintf("scp -r %s -d %s@%s:%s", filePath,
config.GetYamlConfig().NE.User, neInfo.Ip, config.GetYamlConfig().NE.ScpDir)
} else {
scpCmd = fmt.Sprintf("scp -r %s/etc/%s %s@[%s]:%s", config.GetYamlConfig().OMC.Upload,
neTypeLower, config.GetYamlConfig().NE.User, neInfo.Ip, config.GetYamlConfig().NE.EtcDir)
scpZipCmd = fmt.Sprintf("scp -r %s -d %s@[%s]:%s", filePath,
config.GetYamlConfig().NE.User, neInfo.Ip, config.GetYamlConfig().NE.ScpDir)
}
neFilePath := config.GetYamlConfig().NE.ScpDir + "/" + fileName
unzipCmd := fmt.Sprintf("sudo unzip -o %s -d %s", neFilePath, config.GetYamlConfig().NE.EtcDir)
err = ExecCmd(fmt.Sprintf("%s && %s", unzipCmd, scpCmd))
err = ExecCmd(fmt.Sprintf("%s && %s", scpZipCmd, unzipCmd))
if err != nil {
log.Errorf("Faile to scp NF: neType=%s, neId=%s, ip=%s", neType, neId, neInfo.Ip)
services.ResponseInternalServerError500ProcessError(w, err)
@@ -621,7 +654,6 @@ func DownloadNeBackupFile(w http.ResponseWriter, r *http.Request) {
md5Sum := (*neBackup)[0]["md5_sum"]
services.ResponseFileWithNameAndMD5(w, http.StatusOK, fileName, path, md5Sum)
return
}
func DeleteNeBackupFile(w http.ResponseWriter, r *http.Request) {
@@ -681,5 +713,4 @@ func DeleteNeBackupFile(w http.ResponseWriter, r *http.Request) {
}
services.ResponseStatusOK204NoContent(w)
return
}

View File

@@ -666,7 +666,7 @@ func ActiveSoftwareToNF(w http.ResponseWriter, r *http.Request) {
}
log.Debug("neVersion:", neVersion)
if config.GetYamlConfig().OMC.TestMode == false {
if !config.GetYamlConfig().OMC.TestMode {
filePath := (*neVersion)[0]["file_path"]
sshHost := fmt.Sprintf("%s@%s", config.GetYamlConfig().NE.User, neInfo.Ip)
rpmCmd := fmt.Sprintf("sudo rpm -Uvh '%s'", filePath)
@@ -680,7 +680,7 @@ func ActiveSoftwareToNF(w http.ResponseWriter, r *http.Request) {
}
}
idNeVersion, err := strconv.Atoi((*neVersion)[0]["id"])
idNeVersion, _ := strconv.Atoi((*neVersion)[0]["id"])
neVersionData := dborm.NeVersion{
Status: SoftwareStatusActive,
}
@@ -693,7 +693,6 @@ func ActiveSoftwareToNF(w http.ResponseWriter, r *http.Request) {
}
services.ResponseStatusOK204NoContent(w)
return
}
func RollBackSoftwareToNF(w http.ResponseWriter, r *http.Request) {
@@ -758,7 +757,7 @@ func RollBackSoftwareToNF(w http.ResponseWriter, r *http.Request) {
return
}
if config.GetYamlConfig().OMC.TestMode == false {
if !config.GetYamlConfig().OMC.TestMode {
sshHost := fmt.Sprintf("%s@%s", config.GetYamlConfig().NE.User, neInfo.Ip)
rpmCmd := fmt.Sprintf("rpm -Uvh --oldpackage '%s'", filePath)
cmd := exec.Command("ssh", sshHost, rpmCmd)
@@ -771,7 +770,7 @@ func RollBackSoftwareToNF(w http.ResponseWriter, r *http.Request) {
}
}
idNeVersion, err := strconv.Atoi((*neVersion)[0]["id"])
idNeVersion, _ := strconv.Atoi((*neVersion)[0]["id"])
neVersionData := dborm.NeVersion{
Version: (*neVersion)[0]["pre_version"],
FilePath: (*neVersion)[0]["pre_file"],
@@ -790,5 +789,4 @@ func RollBackSoftwareToNF(w http.ResponseWriter, r *http.Request) {
}
services.ResponseStatusOK204NoContent(w)
return
}

View File

@@ -114,6 +114,7 @@ type YamlConfig struct {
EtcDir string `yaml:"etcdir"`
BinDir string `yaml:"bindir"`
OmcDir string `yaml:"omcdir"`
ScpDir string `yaml:"scpdir"`
LicenseDir string `yaml:"licensedir"`
} `yaml:"ne"`

View File

@@ -74,6 +74,7 @@ ne:
etcdir: /usr/local/etc
bindir: /usr/local/bin
omcdir: /usr/local/omc
scpdir: /tmp
licensedir: /usr/local/etc/{neType}/license
# chk2ne: true/false, if put OmcNeConfig parameters to NE
@@ -97,7 +98,7 @@ omc:
license: ./license
gtpUri: gtp:192.168.2.119:2152
checkContentType: false
testMode: true
testMode: false
rbacMode: true
runDir: