diff --git a/config/etc/default/crontask.yaml b/config/etc/default/crontask.yaml index f98d5f88..60064c17 100644 --- a/config/etc/default/crontask.yaml +++ b/config/etc/default/crontask.yaml @@ -8,9 +8,9 @@ logger: count: 90 omc: - name: RJ_OMC_S001 + name: BJ_OMC01 hosturi: http://127.0.0.1:3030 - hostno: S001 + hostno: R001 province: BJ netabbr: HX vendor: RJ @@ -30,15 +30,15 @@ database: # northbound interface, cm/pm # duration(day): saved days # zipPeriods(day): periods of zip pm data file -northbound: +nbi: cm: cfgfiledir: /usr/local/omc/etc/cm - xmlfiledir: /opt/omc/ftp/cm + xmlfiledir: /opt/omc/ftp version: V1.0.1 duration: 30 pm: cfgfiledir: /usr/local/omc/etc/pm - xmlfiledir: /opt/omc/ftp/pm + xmlfiledir: /opt/omc/ftp version: V1.0.1 duration: 30 zipPeriods: 1 diff --git a/crontask/etc/crontask.yaml b/crontask/etc/crontask.yaml index e545caf8..6ada995b 100644 --- a/crontask/etc/crontask.yaml +++ b/crontask/etc/crontask.yaml @@ -10,13 +10,11 @@ logger: omc: name: BJ_OMC01 hosturi: http://127.0.0.1:3040 - hostno: S001 + hostno: R001 province: BJ netabbr: HX vendor: RJ -process: - tasks: file: ./etc/tasks.yaml diff --git a/crontask/etc/tasks.yaml b/crontask/etc/tasks.yaml index 4ef08234..dffaf195 100644 --- a/crontask/etc/tasks.yaml +++ b/crontask/etc/tasks.yaml @@ -97,7 +97,7 @@ tasks: params: interval: 1 unit: Day - at: 00:00,06:00,12:00,18:00,22:27 + at: 00:00,06:00,12:00,18:00,23:19 do: GenCmXmlFile - name: Task of Generate measure threshold alarm status: Inactive @@ -160,7 +160,7 @@ crontab: # params: - name: Generate PM xml file status: Active - tab: 5,20,35,50 * * * * + tab: 5,22,34,50 * * * * do: GenPmXmlFile uri: this is uri params: Generating PM xml file diff --git a/crontask/genpmxml.go b/crontask/genpmxml.go index c295c14c..1a4a77f5 100644 --- a/crontask/genpmxml.go +++ b/crontask/genpmxml.go @@ -3,7 +3,6 @@ package main import ( "encoding/xml" "fmt" - "io/ioutil" "os" "regexp" "strconv" @@ -30,7 +29,7 @@ func (t *TaskFunc) GenPmXmlFile(uri, params, body string) { func (t *TaskFunc) ReadPmYaml(cmfile string) (map[string]interface{}, error) { log.Debug("cmfile:", cmfile) - file, err := ioutil.ReadFile(cmfile) + file, err := os.ReadFile(cmfile) if err != nil { log.Error(err) return nil, err @@ -52,6 +51,8 @@ func (t *TaskFunc) IsPseudoSubPmName(pmName string) bool { } func (t *TaskFunc) GenNFPMXmlFile(neType string) error { + log.Info("GenNFPMXmlFile processing...") + var nes []NeInfo _, err := XormGetNeInfoByType(neType, &nes) if err != nil { @@ -59,17 +60,14 @@ func (t *TaskFunc) GenNFPMXmlFile(neType string) error { return err } - // cfile := fmt.Sprintf("%s/pm-%s.yaml", yamlConfig.NBI.PM.CfgFileDir, strings.ToLower(neType)) - - // resultMap, _ := t.ReadPmYaml(cfile) - ti := time.Now() var startTime string timestamp := ti.Format("2006-01-02 15:04:05") index := global.GetCurrentTimeSliceIndexByPeriod(ti, 15) - date := time.Now().Format("2006-01-02") - log.Debugf("date: %s index:%d", date, index) + date := ti.Format("2006-01-02") + dateHour := ti.Format("2006010215") + log.Debugf("date: %s index:%d dateHour:%s", date, index, dateHour) _, offset := ti.Zone() var tzOffset string @@ -81,56 +79,45 @@ func (t *TaskFunc) GenNFPMXmlFile(neType string) error { pmFile := new(pmschema.PmFile) - var pms []NorthboundPm - _, err = XormGetNorthboundPm(date, index, neType, &pms) + var pmResults []NorthboundPm + _, err = XormGetNorthboundPm(date, index, neType, &pmResults) if err != nil { log.Error("Failed to get northbound_pm:", err) return err } - if len(pms) == 0 { + if len(pmResults) == 0 { log.Errorf("%s:%v", neType, global.ErrPMNotFoundData) return global.ErrPMNotFoundData } pmFile.XsAttr = "http://www.w3.org/2001/XMLSchema" pmFile.XsiAttr = "http://www.w3.org/2001/XMLSchema-instance" - /* - objectTypes := make([]string, 0) - sortValues := make(map[string]interface{}) - keys := make([]string, 0) - if len(pms) > 0 { - for _, data := range pms[0].Datas { - for _, pmData := range data.PmDatas { - sortValues[pmData.PmName] = pmData.SubDatas - keys = append(keys, pmData.PmName) - } - objectTypes = append(objectTypes, data.ObjectType) - } - sort.Strings(keys) - sort.Strings(objectTypes) - } - log.Debug("objectTypes:", objectTypes) - log.Debug("keys:", keys) - */ + + //创建目录 + path := fmt.Sprintf("%s/HX/%s/%s/PM/%s", yamlConfig.OMC.Province, yamlConfig.OMC.Vendor, yamlConfig.OMC.Name, dateHour) + folderPath := global.CreateDir(path, yamlConfig.NBI.PM.XmlFileDir) + + var objectType string var measurement pmschema.Measurements - for _, pm := range pms { - for _, pdata := range pm.Datas { - measurement = pmschema.Measurements{ObjectType: pdata.ObjectType} - measurement.PmData.Object.RmUIDAttr = pm.RmUID - measurement.PmData.Object.DnAttr = pm.Dn - measurement.PmData.Object.UserLabelAttr = pm.NeName - startTime = pm.StartTime + for _, pmResult := range pmResults { + for _, pmData := range pmResult.Datas { + objectType = pmData.ObjectType + measurement = pmschema.Measurements{ObjectType: objectType} + measurement.PmData.Object.RmUIDAttr = pmResult.RmUID + measurement.PmData.Object.DnAttr = pmResult.Dn + measurement.PmData.Object.UserLabelAttr = pmResult.NeName + startTime = pmResult.StartTime i := 1 - for _, pmData := range pdata.KPIs { - measurement.PmName.N = append(measurement.PmName.N, pmschema.N{IAttr: i, Value: pmData.KPIID}) + for _, pmKPI := range pmData.KPIs { + measurement.PmName.N = append(measurement.PmName.N, pmschema.N{IAttr: i, Value: pmKPI.KPIID}) cv := pmschema.CV{IAttr: i} isPseudo := true value := "0" reg := regexp.MustCompile(`_\w+`) //sns := strings.Split(pmData.KPIID, "_") - for _, v := range pmData.KPIValues { + for _, v := range pmKPI.KPIValues { if fmt.Sprintf("%v", v.Name) == "Total" { isPseudo = false value = fmt.Sprintf("%v", v.Value) @@ -140,7 +127,7 @@ func (t *TaskFunc) GenNFPMXmlFile(neType string) error { //if len(sns) > 1 { // sns := strings.Split(sns[1], ".") //} - sn := reg.ReplaceAllString(pmData.KPIID, v.Name) + sn := reg.ReplaceAllString(pmKPI.KPIID, v.Name) //sn := sns[0] + v.Name // cv.NV = append(cv.NV, pmschema.NV{SN: sn, SV: fmt.Sprintf("%v", v.Value)}) cv.SN = append(cv.SN, sn) @@ -159,109 +146,52 @@ func (t *TaskFunc) GenNFPMXmlFile(neType string) error { } pmFile.Measurements = append(pmFile.Measurements, measurement) - } - } - - pmFile.FileHeader = pmschema.FileHeader{ - TimeStamp: timestamp, - TimeZone: tzOffset, - Period: 15, - VendorName: "Ruijie Network", - ElementType: neType, - PmVersion: global.Version, - StartTime: startTime, - } - /* - for _, obj := range objectTypes { - measurement := pmschema.Measurements{ObjectType: obj} - - for _, pm := range pms { - measurement.PmData.Object.RmUIDAttr = pm.RmUID - measurement.PmData.Object.DnAttr = pm.Dn - measurement.PmData.Object.UserLabelAttr = pm.NeName - for _, pdata := range pm.Datas { - measurement := pmschema.Measurements{ObjectType: pdata.ObjectType} - if pdata.ObjectType == obj { - i := 1 - for _, pmName := range keys { - measurement.PmName.N = append(measurement.PmName.N, pmschema.N{IAttr: i, Value: pmName}) - measurement.PmData.Object.V = append(measurement.PmData.Object.V, pmschema.V{IAttr: i, Value: "1"}) - i++ - //measurement.PmData.Object.V = append(measurement.PmData.Object.V, pmschema.V{IAttr: i, Value: sortValues[pmName].Value}) - //measurement.PmData.Object.CV = sortValues[pmName].Value - } - } - } + pmFile.FileHeader = pmschema.FileHeader{ + TimeStamp: timestamp, + TimeZone: tzOffset, + Period: 15, + VendorName: yamlConfig.OMC.Vendor, + ElementType: neType, + PmVersion: yamlConfig.NBI.PM.Version, + StartTime: startTime, } - pmFile.Measurements = append(pmFile.Measurements, measurement) - } - */ - /* - for k, pm := range pms { - measurement := pmschema.Measurements{ObjectType: "gold_kpi"} + x, _ := xml.MarshalIndent(pmFile, "", " ") + x = append([]byte(xml.Header), x...) - for s, ne := range nes { - rmUID := fmt.Sprintf("01000HX%sBJ0D0%d", neType, s+1) - vmId := fmt.Sprintf("kylin10.0-00%d-%s", s+1, neType) - measurement.PmName. - vnfInstanceID := fmt.Sprintf("2%xd55b4-%d018-41f4-af%d5-28b%d828788", s+10, s+6, s+4, s+3) - object := pmschema.Object{RmUIDAttr: rmUID, - DnAttr: "DC=www.ruijie.com.cn,SubNetwork=10001,SubNetwork=114214,ManagedElement=325", - UserLabelAttr: ne.NeName, PVFlagAttr: ne.PvFlag, VMIDAttr: vmId, VNFInstanceIDAttr: vnfInstanceID} + //folderName := global.GetFmtTimeString(global.DateTime, startTime, global.DateHour) + //folderPath := global.CreateDir(folderName, yamlConfig.NBI.PM.XmlFileDir) - i := 1 - for _, p := range keys { - if s == 0 { - objects.FieldName.N = append(objects.FieldName.N, pmschema.N{IAttr: i, Value: p}) - } - var v string - switch p { - case "Id": - v = ne.NeId - case "UserLabel": - v = ne.NeName - case "VendorName": - v = "Ruijie Network" - case "SwVersion": - v = global.Version - case "PatchInfo": - v = "-" - case "AdministrativeState": - v = AdminStateUnlocked - case "OperationalState": - v = OperationStateEnabled - case "VnfInstanceId": - v = vnfInstanceID - default: - if sortResult[p] == "" { - v = "-" - } else { - v = fmt.Sprintf("%s", sortResult[p]) - } - } - object.V = append(object.V, cmschema.V{IAttr: i, Value: v}) - i++ - } - objects.FieldValue.Object = append(objects.FieldValue.Object, object) + startTime = startTime[:len("2006-01-02 15:04:05")] + log.Debug("startTime:", startTime) + timefile := global.GetFmtTimeString(time.DateTime, startTime, global.DateData) + + xmlfile := fmt.Sprintf("%s/%s-PM-%s-%s-%s-%s-%s-15-001.xml", + folderPath, yamlConfig.OMC.Province, neType, objectType, yamlConfig.OMC.HostNo, yamlConfig.NBI.PM.Version, timefile) + tmpXmlfile := xmlfile + ".tmp" + err = os.WriteFile(tmpXmlfile, x, 0664) + if err != nil { + log.Error("Failed to WriteFile xml file:", err) + continue + } + err = os.Rename(tmpXmlfile, xmlfile) + if err != nil { + log.Error("Failed to Rename xml file:", err) + continue + } + zipFile := xmlfile + ".zip" + err = global.ZipOneFile(xmlfile, zipFile, false) + if err != nil { + log.Error("Failed to ZipOneFile:", err) + continue + } + err = os.Remove(xmlfile) + if err != nil { + log.Error("Failed to delete file:", err) + continue } - pmFile.Measurements = append(pmFile.Measurements, measurement) } - */ - - x, _ := xml.MarshalIndent(pmFile, "", " ") - x = append([]byte(xml.Header), x...) - - folderName := global.GetFmtTimeString(global.DateTime, startTime, global.DateHour) - folderPath := global.CreateDir(folderName, yamlConfig.NBI.PM.XmlFileDir) - timefile := global.GetFmtTimeString(global.DateTime, startTime, global.DateData) - xmlfile := fmt.Sprintf("%s/PM-%s-%s-%s-15-001.xml", folderPath, neType, global.Version, timefile) - log.Debugf("folderPath:%s, xmlfile:%s", folderPath, xmlfile) - err = os.WriteFile(xmlfile, x, 0664) - if err != nil { - log.Error("Failed to write xml file:", err) - - return err } + return nil } diff --git a/lib/global/global.go b/lib/global/global.go index 17e2e182..444ca57f 100644 --- a/lib/global/global.go +++ b/lib/global/global.go @@ -14,6 +14,7 @@ const ( DateTime = "2006-01-02 15:04:05" DateData = "20060102150405" DateHour = "2006010215" + DateZone = "2006-01-02 15:04:05 +0000 UTC" ) const (