2 * Copyright (c) 2019 AT&T Intellectual Property.
3 * Copyright (c) 2018-2019 Nokia.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * This source code is part of the near-RT RIC (RAN Intelligent Controller)
18 * platform project (RICP).
37 const defaultReportingEntityID = "00000000-0000-0000-0000-000000000000"
38 const defaultVNFName = "Vespa"
39 const defaultNFNamingCode = "ricp"
41 func readSystemUUID() string {
42 data, err := ioutil.ReadFile("/sys/class/dmi/id/product_uuid")
44 return defaultReportingEntityID
46 return strings.TrimSpace(string(data))
49 func getVNFName() string {
50 VNFName := os.Getenv("VESMGR_VNFNAME")
57 func getNFNamingCode() string {
58 NFNamingCode := os.Getenv("VESMGR_NFNAMINGCODE")
59 if NFNamingCode == "" {
60 return defaultNFNamingCode
65 func basicVespaConf() VESAgentConfiguration {
66 var vespaconf = VESAgentConfiguration{
69 Event: EventConfiguration{
70 VNFName: getVNFName(),
71 ReportingEntityName: "Vespa",
72 ReportingEntityID: readSystemUUID(),
74 NfNamingCode: getNFNamingCode(),
75 NfcNamingCodes: []NfcNamingCode{},
76 RetryInterval: time.Second * 5,
79 Measurement: MeasurementConfiguration{
80 // Domain abbreviation has to be set to “Mvfs” for VES 5.3,
81 // and to “Measurement” for later VES interface versions.
82 DomainAbbreviation: "Mvfs",
83 MaxBufferingDuration: time.Hour,
84 Prometheus: PrometheusConfig{
85 Timeout: time.Second * 30,
86 KeepAlive: time.Second * 30,
88 DefaultValues: &MetricRule{
89 VMIDLabel: "'{{.labels.instance}}'",
98 // AppMetricsStruct contains xapplication metrics definition
99 type AppMetricsStruct struct {
105 ObjectInstance string
109 // AppMetrics contains metrics definitions for all Xapps
110 type AppMetrics map[string]AppMetricsStruct
112 // Parses the metrics data from an array of bytes, which is expected to contain a JSON
113 // array with structs of the following format:
120 // { "name": "...", "objectName": "...", "objectInstamce": "..." },
128 func parseMetricsFromDescriptor(descriptor []byte, appMetrics AppMetrics) AppMetrics {
129 var desc []map[string]interface{}
130 json.Unmarshal(descriptor, &desc)
132 for _, app := range desc {
133 config, configOk := app["config"]
135 logger.Info("No xApp config found!")
138 measurements, measurementsOk := config.(map[string]interface{})["measurements"]
140 logger.Info("No xApp metrics found!")
144 for _, m := range measurements.([]interface{}) {
145 moId, moIdOk := m.(map[string]interface{})["moId"].(string)
146 measType, measTypeOk := m.(map[string]interface{})["measType"].(string)
147 measId, measIdOk := m.(map[string]interface{})["measId"].(string)
148 measInterval, measIntervalOk := m.(map[string]interface{})["measInterval"].(string)
149 metrics, metricsOk := m.(map[string]interface{})["metrics"]
150 if !metricsOk || !measTypeOk || !measIdOk || !moIdOk || !measIntervalOk {
151 logger.Info("No metrics found for moId=%s measType=%s measId=%s measInterval=%s", moId, measId, measType, measInterval)
154 logger.Info("Parsed measurement: moId=%s type=%s id=%s interval=%s", moId, measType, measId, measInterval)
156 parseMetricsRules(metrics.([]interface{}), appMetrics, moId, measType, measId, measInterval)
162 // Parses the metrics data from an array of interfaces, which are expected to be maps
163 // of the following format:
164 // { "name": xxx, "objectName": yyy, "objectInstance": zzz }
165 // Entries, which do not have all the necessary fields, are ignored.
166 func parseMetricsRules(metricsMap []interface{}, appMetrics AppMetrics, moId, measType, measId, measInterval string) AppMetrics {
167 for _, element := range metricsMap {
168 name, nameOk := element.(map[string]interface{})["name"].(string)
170 _, alreadyFound := appMetrics[name]
171 objectName, objectNameOk := element.(map[string]interface{})["objectName"].(string)
172 objectInstance, objectInstanceOk := element.(map[string]interface{})["objectInstance"].(string)
173 counterId, counterIdOk := element.(map[string]interface{})["counterId"].(string)
174 if !alreadyFound && objectNameOk && objectInstanceOk && counterIdOk {
175 appMetrics[name] = AppMetricsStruct{moId, measType, measId, measInterval, objectName, objectInstance, counterId}
176 logger.Info("Parsed counter name=%s %s/%s M%sC%s", name, objectName, objectInstance, measId, counterId)
179 logger.Info("skipped duplicate counter %s", name)
186 func getRules(vespaconf *VESAgentConfiguration, xAppConfig []byte) bool {
187 makeRule := func(expr string, value AppMetricsStruct) MetricRule {
189 Target: "AdditionalObjects",
191 ObjectInstance: fmt.Sprintf("%s:%s", value.ObjectInstance, value.CounterId),
192 ObjectName: value.ObjectName,
194 {Name: "ricComponentName", Expr: "'{{.labels.kubernetes_name}}'"},
195 {Name: "moId", Expr: value.MoId},
196 {Name: "measType", Expr: value.MeasType},
197 {Name: "measId", Expr: value.MeasId},
198 {Name: "measInterval", Expr: value.MeasInterval},
202 appMetrics := make(AppMetrics)
203 metrics := parseMetricsFromDescriptor(xAppConfig, appMetrics)
205 if pltFile := os.Getenv("VESMGR_PLT_CFG_FILE"); pltFile != "" {
206 pltConfig, err := ioutil.ReadFile(pltFile)
208 logger.Error("Unable to read platform config file: %v", err)
210 metrics = parseMetricsFromDescriptor(pltConfig, metrics)
214 vespaconf.Measurement.Prometheus.Rules.Metrics = make([]MetricRule, 0, len(metrics))
215 for key, value := range metrics {
216 vespaconf.Measurement.Prometheus.Rules.Metrics = append(vespaconf.Measurement.Prometheus.Rules.Metrics, makeRule(key, value))
218 if len(vespaconf.Measurement.Prometheus.Rules.Metrics) == 0 {
219 logger.Info("vespa config with empty metrics")
222 return len(vespaconf.Measurement.Prometheus.Rules.Metrics) > 0
225 func getCollectorConfiguration(vespaconf *VESAgentConfiguration) {
226 vespaconf.PrimaryCollector.User = os.Getenv("VESMGR_PRICOLLECTOR_USER")
227 vespaconf.PrimaryCollector.Password = os.Getenv("VESMGR_PRICOLLECTOR_PASSWORD")
228 vespaconf.PrimaryCollector.PassPhrase = os.Getenv("VESMGR_PRICOLLECTOR_PASSPHRASE")
229 vespaconf.PrimaryCollector.FQDN = os.Getenv("VESMGR_PRICOLLECTOR_ADDR")
230 vespaconf.PrimaryCollector.ServerRoot = os.Getenv("VESMGR_PRICOLLECTOR_SERVERROOT")
231 vespaconf.PrimaryCollector.Topic = os.Getenv("VESMGR_PRICOLLECTOR_TOPIC")
232 portStr := os.Getenv("VESMGR_PRICOLLECTOR_PORT")
235 vespaconf.PrimaryCollector.Port = 8443
237 port, _ := strconv.Atoi(portStr)
238 vespaconf.PrimaryCollector.Port = port
241 secureStr := os.Getenv("VESMGR_PRICOLLECTOR_SECURE")
242 if secureStr == "true" {
243 vespaconf.PrimaryCollector.Secure = true
245 vespaconf.PrimaryCollector.Secure = false
249 func createVespaConfig(writer io.Writer, xAppStatus []byte) {
250 vespaconf := basicVespaConf()
252 getRules(&vespaconf, xAppStatus)
254 getCollectorConfiguration(&vespaconf)
256 err := yaml.NewEncoder(writer).Encode(vespaconf)
258 logger.Error("Cannot write vespa conf file: %s", err.Error())