1 # -*- coding: utf-8 -*-
3 # Copyright 2018 Whitestack, LLC
4 # *************************************************************
6 # This file is part of OSM Monitoring module
7 # All Rights Reserved to Whitestack, LLC
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
13 # http://www.apache.org/licenses/LICENSE-2.0
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
20 # For those usages not covered by the Apache License, Version 2.0 please
21 # contact: bdiaz@whitestack.com or glavado@whitestack.com
24 MON component in charge of CRUD operations for vim_accounts and alarms. It uses the message bus to communicate.
31 from osm_mon
.core
.config
import Config
32 from osm_mon
.core
.message_bus_client
import MessageBusClient
33 from osm_mon
.core
.response
import ResponseBuilder
34 from osm_mon
.server
.service
import ServerService
36 log
= logging
.getLogger(__name__
)
40 def __init__(self
, config
: Config
, loop
=None):
43 loop
= asyncio
.get_event_loop()
45 self
.msg_bus
= MessageBusClient(config
)
46 self
.service
= ServerService(config
)
47 self
.service
.populate_prometheus()
50 self
.loop
.run_until_complete(self
.start())
52 async def start(self
, wait_time
=5):
53 topics
= ["alarm_request"]
56 await self
.msg_bus
.aioread(topics
, self
._process
_msg
)
58 except Exception as e
:
59 # Failed to subscribe to kafka topic
60 log
.error("Error when subscribing to topic(s) %s", str(topics
))
61 log
.exception("Exception %s", str(e
))
62 # Wait for some time for kaka to stabilize and then reattempt to subscribe again
64 log
.info("Retrying to subscribe the kafka topic(s) %s", str(topics
))
66 async def _process_msg(self
, topic
, key
, values
):
67 log
.info("Message arrived: %s", values
)
69 if topic
== "alarm_request":
70 if key
== "create_alarm_request":
71 alarm_details
= values
["alarm_create_request"]
72 cor_id
= alarm_details
["correlation_id"]
73 response_builder
= ResponseBuilder()
75 alarm
= self
.service
.create_alarm(
76 alarm_details
["alarm_name"],
77 alarm_details
["threshold_value"],
78 alarm_details
["operation"].lower(),
79 alarm_details
["severity"].lower(),
80 alarm_details
["statistic"].lower(),
81 alarm_details
["metric_name"],
82 alarm_details
["action"],
83 alarm_details
["tags"],
85 response
= response_builder
.generate_response(
86 "create_alarm_response",
92 log
.exception("Error creating alarm: ")
93 response
= response_builder
.generate_response(
94 "create_alarm_response",
99 await self
._publish
_response
(
100 "alarm_response_" + str(cor_id
),
101 "create_alarm_response",
105 if key
== "delete_alarm_request":
106 alarm_details
= values
["alarm_delete_request"]
107 alarm_uuid
= alarm_details
["alarm_uuid"]
108 response_builder
= ResponseBuilder()
109 cor_id
= alarm_details
["correlation_id"]
111 self
.service
.delete_alarm(alarm_uuid
)
112 response
= response_builder
.generate_response(
113 "delete_alarm_response",
119 log
.exception("Error deleting alarm: ")
120 response
= response_builder
.generate_response(
121 "delete_alarm_response",
126 await self
._publish
_response
(
127 "alarm_response_" + str(cor_id
),
128 "delete_alarm_response",
133 log
.exception("Exception processing message: ")
135 async def _publish_response(self
, topic
: str, key
: str, msg
: dict):
137 "Sending response %s to topic %s with key %s", json
.dumps(msg
), topic
, key
139 await self
.msg_bus
.aiowrite(topic
, key
, msg
)