Updated the OpenStack plugins
[osm/MON.git] / plugins / OpenStack / Aodh / notifier.py
1 # Copyright 2017 Intel Research and Development Ireland Limited
2 # *************************************************************
3
4 # This file is part of OSM Monitoring module
5 # All Rights Reserved to Intel Corporation
6
7 # Licensed under the Apache License, Version 2.0 (the "License"); you may
8 # not use this file except in compliance with the License. You may obtain
9 # a copy of the License at
10
11 # http://www.apache.org/licenses/LICENSE-2.0
12
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16 # License for the specific language governing permissions and limitations
17 # under the License.
18
19 # For those usages not covered by the Apache License, Version 2.0 please
20 # contact: helena.mcgough@intel.com or adrian.hoban@intel.com
21 ##
22 """Notifier class for alarm notification response."""
23
24 import json
25 import logging as log
26
27 from core.message_bus.producer import KafkaProducer
28
29 from plugins.OpenStack.response import OpenStack_Response
30 from plugins.OpenStack.singleton import Singleton
31
32 __author__ = "Helena McGough"
33
34 ALARM_NAMES = [
35 "Average_Memory_Usage_Above_Threshold",
36 "Read_Latency_Above_Threshold",
37 "Write_Latency_Above_Threshold",
38 "DISK_READ_OPS",
39 "DISK_WRITE_OPS",
40 "DISK_READ_BYTES",
41 "DISK_WRITE_BYTES",
42 "Net_Packets_Dropped",
43 "Packets_in_Above_Threshold",
44 "Packets_out_Above_Threshold",
45 "CPU_Utilization_Above_Threshold"]
46
47
48 @Singleton
49 class Notifier(object):
50 """Alarm Notification class."""
51
52 def __init__(self):
53 """Initialize alarm notifier."""
54 self._response = OpenStack_Response()
55
56 self._producer = KafkaProducer("alarm_response", None)
57
58 def notify(self, alarming):
59 """Send alarm notifications responses to the SO."""
60 auth_token, endpoint = alarming.authenticate(None)
61
62 while(1):
63 alarm_list = json.loads(alarming.list_alarms(endpoint, auth_token))
64 for alarm in alarm_list:
65 alarm_id = alarm['alarm_id']
66 alarm_name = alarm['name']
67 # Send a notification response to the SO on alarm trigger
68 if alarm_name in ALARM_NAMES:
69 alarm_state = alarming.get_alarm_state(
70 endpoint, auth_token, alarm_id)
71 if alarm_state == "alarm":
72 # Generate and send an alarm notification response
73 try:
74 a_date = alarm['state_timestamp'].replace("T", " ")
75 rule = alarm['gnocchi_resources_threshold_rule']
76 resp_message = self._response.generate_response(
77 'notify_alarm', a_id=alarm_id,
78 r_id=rule['resource_id'],
79 sev=alarm['severity'], date=a_date,
80 state=alarm_state, vim_type="OpenStack")
81 self._producer.notify_alarm(
82 'notify_alarm', resp_message, 'alarm_response')
83 except Exception as exc:
84 log.warn("Failed to send notify response:%s", exc)