blob: e4f75336aaf02ace3a5463e36b876bfc965b25a4 [file] [log] [blame]
Benjamin Diaz7f11ecf2018-09-14 12:03:38 -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
21# For those usages not covered by the Apache License, Version 2.0 please
22# contact: bdiaz@whitestack.com or glavado@whitestack.com
23##
24import json
25import logging
k4.rahulbfe69882023-04-27 12:25:59 +053026from random import SystemRandom
Benjamin Diaz127bbba2018-11-26 18:05:53 -030027from json import JSONDecodeError
Benjamin Diaz7f11ecf2018-09-14 12:03:38 -030028
Benjamin Diaz127bbba2018-11-26 18:05:53 -030029import yaml
Benjamin Diaz16256cb2018-10-11 12:34:20 -030030from aiokafka import AIOKafkaProducer, AIOKafkaConsumer
Benjamin Diaz7f11ecf2018-09-14 12:03:38 -030031
32from osm_policy_module.core.config import Config
33
34log = logging.getLogger(__name__)
35
36
37class MonClient:
Mark Beierld37c54c2023-05-10 11:15:10 -040038 def __init__(self, config: Config):
garciadeblas4584f8e2021-05-14 16:50:06 +020039 self.kafka_server = "{}:{}".format(
40 config.get("message", "host"), config.get("message", "port")
41 )
Benjamin Diaz7f11ecf2018-09-14 12:03:38 -030042
garciadeblas4584f8e2021-05-14 16:50:06 +020043 async def create_alarm(
44 self,
45 metric_name: str,
46 ns_id: str,
47 vdu_name: str,
48 vnf_member_index: str,
49 threshold: int,
50 operation: str,
51 statistic: str = "AVERAGE",
garciadeblasbe42d542022-11-14 00:29:47 +010052 action: str = "",
Rahul Kumarbb6bdfa2023-11-09 08:45:38 +000053 vnfr: object = None,
54 vnfd: object = None,
garciadeblas4584f8e2021-05-14 16:50:06 +020055 ):
k4.rahulbfe69882023-04-27 12:25:59 +053056 cor_id = SystemRandom().randint(1, 10e7)
garciadeblas4584f8e2021-05-14 16:50:06 +020057 msg = self._build_create_alarm_payload(
58 cor_id,
59 metric_name,
60 ns_id,
61 vdu_name,
62 vnf_member_index,
63 threshold,
64 statistic,
65 operation,
Atul Agarwale9228cf2021-03-19 10:11:38 +000066 action,
Rahul Kumarbb6bdfa2023-11-09 08:45:38 +000067 vnfr,
68 vnfd,
garciadeblas4584f8e2021-05-14 16:50:06 +020069 )
Benjamin Diazf7451f82019-04-01 14:56:26 -030070 log.debug("Sending create_alarm_request %s", msg)
garciadeblas4584f8e2021-05-14 16:50:06 +020071 producer = AIOKafkaProducer(
garciadeblas4584f8e2021-05-14 16:50:06 +020072 bootstrap_servers=self.kafka_server,
73 key_serializer=str.encode,
74 value_serializer=str.encode,
75 )
Benjamin Diaz16256cb2018-10-11 12:34:20 -030076 await producer.start()
Benjamin Diaz16256cb2018-10-11 12:34:20 -030077 try:
garciadeblas4584f8e2021-05-14 16:50:06 +020078 await producer.send_and_wait(
79 "alarm_request", key="create_alarm_request", value=json.dumps(msg)
80 )
Benjamin Diaz16256cb2018-10-11 12:34:20 -030081 finally:
82 await producer.stop()
Benjamin Diazf7451f82019-04-01 14:56:26 -030083 log.debug("Waiting for create_alarm_response...")
Benjamin Diaz127bbba2018-11-26 18:05:53 -030084 consumer = AIOKafkaConsumer(
85 "alarm_response_" + str(cor_id),
Benjamin Diaz127bbba2018-11-26 18:05:53 -030086 bootstrap_servers=self.kafka_server,
87 key_deserializer=bytes.decode,
88 value_deserializer=bytes.decode,
garciadeblas4584f8e2021-05-14 16:50:06 +020089 auto_offset_reset="earliest",
90 )
Benjamin Diaz127bbba2018-11-26 18:05:53 -030091 await consumer.start()
92 alarm_uuid = None
Benjamin Diaz16256cb2018-10-11 12:34:20 -030093 try:
94 async for message in consumer:
Benjamin Diaz127bbba2018-11-26 18:05:53 -030095 try:
Benjamin Diaz16256cb2018-10-11 12:34:20 -030096 content = json.loads(message.value)
Benjamin Diaz127bbba2018-11-26 18:05:53 -030097 except JSONDecodeError:
98 content = yaml.safe_load(message.value)
Benjamin Diazf7451f82019-04-01 14:56:26 -030099 log.debug("Received create_alarm_response %s", content)
garciadeblas4584f8e2021-05-14 16:50:06 +0200100 if content["alarm_create_response"]["correlation_id"] == cor_id:
101 if not content["alarm_create_response"]["status"]:
Benjamin Diaz127bbba2018-11-26 18:05:53 -0300102 raise ValueError("Error creating alarm in MON")
garciadeblas4584f8e2021-05-14 16:50:06 +0200103 alarm_uuid = content["alarm_create_response"]["alarm_uuid"]
Benjamin Diaz127bbba2018-11-26 18:05:53 -0300104 break
Benjamin Diaz16256cb2018-10-11 12:34:20 -0300105 finally:
106 await consumer.stop()
Benjamin Diaz127bbba2018-11-26 18:05:53 -0300107 if not alarm_uuid:
garciadeblas4584f8e2021-05-14 16:50:06 +0200108 raise ValueError("No alarm deletion response from MON. Is MON up?")
Benjamin Diaz127bbba2018-11-26 18:05:53 -0300109 return alarm_uuid
Benjamin Diaz7f11ecf2018-09-14 12:03:38 -0300110
garciadeblas4584f8e2021-05-14 16:50:06 +0200111 async def delete_alarm(
112 self, ns_id: str, vnf_member_index: str, vdu_name: str, alarm_uuid: str
113 ):
k4.rahulbfe69882023-04-27 12:25:59 +0530114 cor_id = SystemRandom().randint(1, 10e7)
garciadeblas4584f8e2021-05-14 16:50:06 +0200115 msg = self._build_delete_alarm_payload(
116 cor_id, ns_id, vdu_name, vnf_member_index, alarm_uuid
117 )
Benjamin Diazf7451f82019-04-01 14:56:26 -0300118 log.debug("Sending delete_alarm_request %s", msg)
garciadeblas4584f8e2021-05-14 16:50:06 +0200119 producer = AIOKafkaProducer(
garciadeblas4584f8e2021-05-14 16:50:06 +0200120 bootstrap_servers=self.kafka_server,
121 key_serializer=str.encode,
122 value_serializer=str.encode,
123 )
Benjamin Diaz16256cb2018-10-11 12:34:20 -0300124 await producer.start()
Benjamin Diaz16256cb2018-10-11 12:34:20 -0300125 try:
garciadeblas4584f8e2021-05-14 16:50:06 +0200126 await producer.send_and_wait(
127 "alarm_request", key="delete_alarm_request", value=json.dumps(msg)
128 )
Benjamin Diaz16256cb2018-10-11 12:34:20 -0300129 finally:
130 await producer.stop()
Benjamin Diazf7451f82019-04-01 14:56:26 -0300131 log.debug("Waiting for delete_alarm_response...")
Benjamin Diaz127bbba2018-11-26 18:05:53 -0300132 consumer = AIOKafkaConsumer(
133 "alarm_response_" + str(cor_id),
Benjamin Diaz127bbba2018-11-26 18:05:53 -0300134 bootstrap_servers=self.kafka_server,
135 key_deserializer=bytes.decode,
136 value_deserializer=bytes.decode,
garciadeblas4584f8e2021-05-14 16:50:06 +0200137 auto_offset_reset="earliest",
138 )
Benjamin Diaz127bbba2018-11-26 18:05:53 -0300139 await consumer.start()
140 alarm_uuid = None
Benjamin Diaz16256cb2018-10-11 12:34:20 -0300141 try:
142 async for message in consumer:
Benjamin Diaz127bbba2018-11-26 18:05:53 -0300143 try:
Benjamin Diaz16256cb2018-10-11 12:34:20 -0300144 content = json.loads(message.value)
Benjamin Diaz127bbba2018-11-26 18:05:53 -0300145 except JSONDecodeError:
146 content = yaml.safe_load(message.value)
garciadeblas4584f8e2021-05-14 16:50:06 +0200147 if content["alarm_delete_response"]["correlation_id"] == cor_id:
Benjamin Diazf7451f82019-04-01 14:56:26 -0300148 log.debug("Received delete_alarm_response %s", content)
garciadeblas4584f8e2021-05-14 16:50:06 +0200149 if not content["alarm_delete_response"]["status"]:
150 raise ValueError(
151 "Error deleting alarm in MON. Response status is False."
152 )
153 alarm_uuid = content["alarm_delete_response"]["alarm_uuid"]
Benjamin Diaz127bbba2018-11-26 18:05:53 -0300154 break
Benjamin Diaz16256cb2018-10-11 12:34:20 -0300155 finally:
156 await consumer.stop()
Benjamin Diaz127bbba2018-11-26 18:05:53 -0300157 if not alarm_uuid:
garciadeblas4584f8e2021-05-14 16:50:06 +0200158 raise ValueError("No alarm deletion response from MON. Is MON up?")
Benjamin Diaz127bbba2018-11-26 18:05:53 -0300159 return alarm_uuid
Benjamin Diazf35f9142018-10-08 16:25:36 -0300160
garciadeblas4584f8e2021-05-14 16:50:06 +0200161 def _build_create_alarm_payload(
162 self,
163 cor_id: int,
164 metric_name: str,
165 ns_id: str,
166 vdu_name: str,
167 vnf_member_index: str,
168 threshold: int,
169 statistic: str,
170 operation: str,
Atul Agarwale9228cf2021-03-19 10:11:38 +0000171 action: str,
Rahul Kumarbb6bdfa2023-11-09 08:45:38 +0000172 vnfr=None,
173 vnfd=None,
garciadeblas4584f8e2021-05-14 16:50:06 +0200174 ):
Rahul Kumarbb6bdfa2023-11-09 08:45:38 +0000175 tags = {
176 "ns_id": ns_id,
177 "vdu_name": vdu_name,
178 "vnf_member_index": vnf_member_index,
179 }
180 if vnfr and vnfd:
181 # TODO: Change for multiple DF support
182 df = vnfd.get("df", [{}])[0]
garciadeblasecf3f132024-05-22 23:34:07 +0200183 metric_port = 9100
Rahul Kumarbb6bdfa2023-11-09 08:45:38 +0000184 if "exporters-endpoints" in df:
185 metric_port = df["exporters-endpoints"].get("metric-port", 9100)
186 if metric_name.startswith("kpi_"):
187 metric_name = metric_name.replace("kpi_", "")
188 metric_name.strip()
189 for vdu in vnfr["vdur"]:
190 if vdu["name"] == vdu_name:
191 vdu_ip = vdu["ip-address"]
192 tags = {"instance": vdu_ip + ":" + str(metric_port)}
Benjamin Diaz7f11ecf2018-09-14 12:03:38 -0300193 alarm_create_request = {
garciadeblas4584f8e2021-05-14 16:50:06 +0200194 "correlation_id": cor_id,
195 "alarm_name": "osm_alarm_{}_{}_{}_{}".format(
196 ns_id, vnf_member_index, vdu_name, metric_name
197 ),
198 "metric_name": metric_name,
199 "operation": operation,
200 "severity": "critical",
201 "threshold_value": threshold,
202 "statistic": statistic,
Atul Agarwale9228cf2021-03-19 10:11:38 +0000203 "action": action,
Rahul Kumarbb6bdfa2023-11-09 08:45:38 +0000204 "tags": tags,
Benjamin Diaz7f11ecf2018-09-14 12:03:38 -0300205 }
206 msg = {
garciadeblas4584f8e2021-05-14 16:50:06 +0200207 "alarm_create_request": alarm_create_request,
Benjamin Diaz7f11ecf2018-09-14 12:03:38 -0300208 }
209 return msg
210
garciadeblas4584f8e2021-05-14 16:50:06 +0200211 def _build_delete_alarm_payload(
212 self,
213 cor_id: int,
214 ns_id: str,
215 vdu_name: str,
216 vnf_member_index: str,
217 alarm_uuid: str,
218 ):
Benjamin Diazf35f9142018-10-08 16:25:36 -0300219 alarm_delete_request = {
garciadeblas4584f8e2021-05-14 16:50:06 +0200220 "correlation_id": cor_id,
221 "alarm_uuid": alarm_uuid,
222 "tags": {
223 "ns_id": ns_id,
224 "vdu_name": vdu_name,
225 "vnf_member_index": vnf_member_index,
226 },
Benjamin Diazf35f9142018-10-08 16:25:36 -0300227 }
228 msg = {
garciadeblas4584f8e2021-05-14 16:50:06 +0200229 "alarm_delete_request": alarm_delete_request,
Benjamin Diazf35f9142018-10-08 16:25:36 -0300230 }
231 return msg