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).
36 const defaultReportingEntityID = "00000000-0000-0000-0000-000000000000"
37 const defaultVNFName = "Vespa"
38 const defaultNFNamingCode = "ricp"
40 func readSystemUUID() string {
41 data, err := ioutil.ReadFile("/sys/class/dmi/id/product_uuid")
43 return defaultReportingEntityID
45 return strings.TrimSpace(string(data))
48 func getVNFName() string {
49 VNFName := os.Getenv("VESMGR_VNFNAME")
56 func getNFNamingCode() string {
57 NFNamingCode := os.Getenv("VESMGR_NFNAMINGCODE")
58 if NFNamingCode == "" {
59 return defaultNFNamingCode
64 func basicVespaConf() VESAgentConfiguration {
65 var vespaconf = VESAgentConfiguration{
68 Event: EventConfiguration{
69 VNFName: getVNFName(),
70 ReportingEntityName: "Vespa",
71 ReportingEntityID: readSystemUUID(),
73 NfNamingCode: getNFNamingCode(),
74 NfcNamingCodes: []NfcNamingCode{},
75 RetryInterval: time.Second * 5,
78 Measurement: MeasurementConfiguration{
79 // Domain abbreviation has to be set to “Mvfs” for VES 5.3,
80 // and to “Measurement” for later VES interface versions.
81 DomainAbbreviation: "Mvfs",
82 MaxBufferingDuration: time.Hour,
83 Prometheus: PrometheusConfig{
84 Timeout: time.Second * 30,
85 KeepAlive: time.Second * 30,
87 DefaultValues: &MetricRule{
88 VMIDLabel: "'{{.labels.instance}}'",
97 // AppMetricsStruct contains xapplication metrics definition
98 type AppMetricsStruct struct {
100 ObjectInstance string
103 // AppMetrics contains metrics definitions for all Xapps
104 type AppMetrics map[string]AppMetricsStruct
106 // Parses the metrics data from an array of bytes, which is expected to contain a JSON
107 // array with structs of the following format:
112 // { "name": "...", "objectName": "...", "objectInstamce": "..." },
117 func parseMetricsFromXAppDescriptor(descriptor []byte, appMetrics AppMetrics) AppMetrics {
118 var desc []map[string]interface{}
119 json.Unmarshal(descriptor, &desc)
121 for _, app := range desc {
122 config, configOk := app["config"]
124 metrics, metricsOk := config.(map[string]interface{})["metrics"]
126 parseMetricsRules(metrics.([]interface{}), appMetrics)
133 // Parses the metrics data from an array of interfaces, which are expected to be maps
134 // of the following format:
135 // { "name": xxx, "objectName": yyy, "objectInstance": zzz }
136 // Entries, which do not have all the necessary fields, are ignored.
137 func parseMetricsRules(metricsMap []interface{}, appMetrics AppMetrics) AppMetrics {
138 for _, element := range metricsMap {
139 name, nameOk := element.(map[string]interface{})["name"].(string)
141 _, alreadyFound := appMetrics[name]
142 objectName, objectNameOk := element.(map[string]interface{})["objectName"].(string)
143 objectInstance, objectInstanceOk := element.(map[string]interface{})["objectInstance"].(string)
144 if !alreadyFound && objectNameOk && objectInstanceOk {
145 appMetrics[name] = AppMetricsStruct{objectName, objectInstance}
146 logger.Info("parsed counter %s %s %s", name, objectName, objectInstance)
149 logger.Info("skipped duplicate counter %s", name)
156 func getRules(vespaconf *VESAgentConfiguration, xAppConfig []byte) {
157 appMetrics := make(AppMetrics)
158 parseMetricsFromXAppDescriptor(xAppConfig, appMetrics)
160 makeRule := func(expr string, objName string, objInstance string) MetricRule {
162 Target: "AdditionalObjects",
164 ObjectInstance: objInstance,
168 Name: "ricComponentName",
169 Expr: "'{{.labels.kubernetes_name}}'",
174 var metricsMap map[string][]interface{}
175 json.Unmarshal(xAppConfig, &metricsMap)
176 metrics := parseMetricsRules(metricsMap["metrics"], appMetrics)
178 vespaconf.Measurement.Prometheus.Rules.Metrics = make([]MetricRule, 0, len(metrics))
179 for key, value := range metrics {
180 vespaconf.Measurement.Prometheus.Rules.Metrics = append(vespaconf.Measurement.Prometheus.Rules.Metrics, makeRule(key, value.ObjectName, value.ObjectInstance))
182 if len(vespaconf.Measurement.Prometheus.Rules.Metrics) == 0 {
183 logger.Info("vespa config with empty metrics")
187 func getCollectorConfiguration(vespaconf *VESAgentConfiguration) {
188 vespaconf.PrimaryCollector.User = os.Getenv("VESMGR_PRICOLLECTOR_USER")
189 vespaconf.PrimaryCollector.Password = os.Getenv("VESMGR_PRICOLLECTOR_PASSWORD")
190 vespaconf.PrimaryCollector.PassPhrase = os.Getenv("VESMGR_PRICOLLECTOR_PASSPHRASE")
191 vespaconf.PrimaryCollector.FQDN = os.Getenv("VESMGR_PRICOLLECTOR_ADDR")
192 vespaconf.PrimaryCollector.ServerRoot = os.Getenv("VESMGR_PRICOLLECTOR_SERVERROOT")
193 vespaconf.PrimaryCollector.Topic = os.Getenv("VESMGR_PRICOLLECTOR_TOPIC")
194 portStr := os.Getenv("VESMGR_PRICOLLECTOR_PORT")
196 vespaconf.PrimaryCollector.Port = 8443
198 port, _ := strconv.Atoi(portStr)
199 vespaconf.PrimaryCollector.Port = port
201 secureStr := os.Getenv("VESMGR_PRICOLLECTOR_SECURE")
202 if secureStr == "true" {
203 vespaconf.PrimaryCollector.Secure = true
205 vespaconf.PrimaryCollector.Secure = false
209 func createVespaConfig(writer io.Writer, xAppStatus []byte) {
210 vespaconf := basicVespaConf()
211 getRules(&vespaconf, xAppStatus)
212 getCollectorConfiguration(&vespaconf)
213 err := yaml.NewEncoder(writer).Encode(vespaconf)
215 logger.Error("Cannot write vespa conf file: %s", err.Error())