blob: c5b935ee3dd2cf99034bb7ba2b2cef0569cae9b0 [file] [log] [blame]
Benjamin Diaza97bdb32019-04-10 15:22:22 -03001# -*- coding: utf-8 -*-
2
3# Copyright 2018 Whitestack, LLC
4# *************************************************************
5
6# This file is part of OSM Monitoring module
7# All Rights Reserved to Whitestack, LLC
8
9# Licensed under the Apache License, Version 2.0 (the "License"); you may
10# not use this file except in compliance with the License. You may obtain
11# a copy of the License at
12
13# http://www.apache.org/licenses/LICENSE-2.0
14
15# Unless required by applicable law or agreed to in writing, software
16# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18# License for the specific language governing permissions and limitations
19# under the License.
20# For those usages not covered by the Apache License, Version 2.0 please
21# contact: bdiaz@whitestack.com or glavado@whitestack.com
22##
David Garcia52e60372021-10-11 15:07:33 +020023import base64
Benjamin Diaza97bdb32019-04-10 15:22:22 -030024import logging
Daniel Gomesb3173222023-02-10 17:39:56 +000025from typing import Dict, List
Benjamin Diaza97bdb32019-04-10 15:22:22 -030026
27import requests
28
29from osm_mon.core.config import Config
30from osm_mon.evaluator.backends.base import BaseBackend
31
32log = logging.getLogger(__name__)
33
garciadeblas8e4179f2021-05-14 16:47:03 +020034OSM_METRIC_PREFIX = "osm_"
Benjamin Diaza97bdb32019-04-10 15:22:22 -030035
Daniel Gomesb3173222023-02-10 17:39:56 +000036DEFAULT_QUERY_METRICS = ["ns_id", "vnf_member_index", "vdu_name"]
37
Benjamin Diaza97bdb32019-04-10 15:22:22 -030038
39class PrometheusBackend(BaseBackend):
Benjamin Diaza97bdb32019-04-10 15:22:22 -030040 def __init__(self, config: Config):
41 super().__init__(config)
42 self.conf = config
43
Daniel Gomesb3173222023-02-10 17:39:56 +000044 def get_metric_data(self, metric_name: str, tags: dict):
Benjamin Diazd5ac6e12019-09-19 11:59:06 -030045 query = self._build_query(metric_name, tags)
46 request_url = self._build_url(query)
David Garcia52e60372021-10-11 15:07:33 +020047 request_headers = self._build_headers()
48 return self._query_metric(request_url, request_headers)
49
50 def _query_metric(self, request_url: str, headers: Dict[str, str]):
Benjamin Diaza97bdb32019-04-10 15:22:22 -030051 log.info("Querying Prometheus: %s", request_url)
garciadeblas8e4179f2021-05-14 16:47:03 +020052 r = requests.get(
David Garcia52e60372021-10-11 15:07:33 +020053 request_url,
54 timeout=int(self.conf.get("global", "request_timeout")),
55 headers=headers,
garciadeblas8e4179f2021-05-14 16:47:03 +020056 )
Benjamin Diaza97bdb32019-04-10 15:22:22 -030057 if r.status_code == 200:
58 json_response = r.json()
garciadeblas8e4179f2021-05-14 16:47:03 +020059 if json_response["status"] == "success":
Daniel Gomesb3173222023-02-10 17:39:56 +000060 return self._get_metric_data_from_response(json_response)
Benjamin Diaza97bdb32019-04-10 15:22:22 -030061 else:
garciadeblas8e4179f2021-05-14 16:47:03 +020062 log.warning(
63 "Prometheus response is not success. Got status %s",
64 json_response["status"],
65 )
Benjamin Diaza97bdb32019-04-10 15:22:22 -030066 else:
garciadeblas8e4179f2021-05-14 16:47:03 +020067 log.warning(
68 "Error contacting Prometheus. Got status code %s: %s",
69 r.status_code,
70 r.text,
71 )
Benjamin Diaza97bdb32019-04-10 15:22:22 -030072 return None
Benjamin Diazd5ac6e12019-09-19 11:59:06 -030073
74 def _build_query(self, metric_name: str, tags: dict) -> str:
75 query_section_tags = []
76 for k, v in tags.items():
garciadeblas8e4179f2021-05-14 16:47:03 +020077 query_section_tags.append(k + '="' + v + '"')
78 query_section = "query={0}{{{1}}}".format(
79 OSM_METRIC_PREFIX + metric_name, ",".join(query_section_tags)
80 )
Benjamin Diazd5ac6e12019-09-19 11:59:06 -030081 return query_section
82
83 def _build_url(self, query: str):
garciadeblas8e4179f2021-05-14 16:47:03 +020084 return self.conf.get("prometheus", "url") + "/api/v1/query?" + query
Benjamin Diazd5ac6e12019-09-19 11:59:06 -030085
David Garcia52e60372021-10-11 15:07:33 +020086 def _build_headers(self) -> Dict[str, str]:
87 headers = {}
88 user = self.conf.get("prometheus", "user")
89 password = self.conf.get("prometheus", "password")
90 if user and password:
91 _phrase = f"{user}:{password}".encode("utf-8")
92 token = base64.b64encode(_phrase).decode("utf-8")
93 headers["Authorization"] = f"Basic {token}"
94 return headers
95
Daniel Gomesb3173222023-02-10 17:39:56 +000096 def _get_metric_data_from_response(self, json_response) -> List[Dict[str, str]]:
garciadeblas8e4179f2021-05-14 16:47:03 +020097 result = json_response["data"]["result"]
Daniel Gomesb3173222023-02-10 17:39:56 +000098 metrics_data = []
Benjamin Diazd5ac6e12019-09-19 11:59:06 -030099 if len(result):
Daniel Gomesb3173222023-02-10 17:39:56 +0000100 for metric in result:
101 metrics_labels = metric["metric"]
102 metric_value = float(metric["value"][1])
103 log.info("Metric value: %s", metric_value)
104 metrics_data.append({"labels": metrics_labels, "value": metric_value})
105 return metrics_data
Benjamin Diazd5ac6e12019-09-19 11:59:06 -0300106 else:
107 return None