Renaming folder structure
[osm/MON.git] / osm_mon / 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 try:
28 import aodhclient
29 except ImportError:
30 log.warn("Failed to import the aodhclient")
31
32
33 from core.message_bus.producer import KafkaProducer
34
35 from plugins.OpenStack.Aodh.alarming import Alarming
36 from plugins.OpenStack.response import OpenStack_Response
37 from plugins.OpenStack.settings import Config
38
39 __author__ = "Helena McGough"
40
41 ALARM_NAMES = [
42 "average_memory_usage_above_threshold",
43 "disk_read_ops",
44 "disk_write_ops",
45 "disk_read_bytes",
46 "disk_write_bytes",
47 "net_packets_dropped",
48 "packets_in_above_threshold",
49 "packets_out_above_threshold",
50 "cpu_utilization_above_threshold"]
51
52
53 def register_notifier():
54 """Run the notifier instance."""
55 config = Config.instance()
56 instance = Notifier(config=config)
57 instance.config()
58 instance.notify()
59
60
61 class Notifier(object):
62 """Alarm Notification class."""
63
64 def __init__(self, config):
65 """Initialize alarm notifier."""
66 log.info("Initialize the notifier for the SO.")
67 self._config = config
68 self._response = OpenStack_Response()
69 self._producer = KafkaProducer("alarm_response")
70 self._alarming = Alarming()
71
72 def config(self):
73 """Configure the alarm notifier."""
74 log.info("Configure the notifier instance.")
75 self._config.read_environ("aodh")
76
77 def notify(self):
78 """Send alarm notifications responses to the SO."""
79 log.info("Checking for alarm notifications")
80 auth_token, endpoint = self._alarming.authenticate()
81
82 while(1):
83 alarm_list = self._alarming.list_alarms(endpoint, auth_token)
84 for alarm in json.loads(alarm_list):
85 alarm_id = alarm['alarm_id']
86 alarm_name = alarm['name']
87 # Send a notification response to the SO on alarm trigger
88 if alarm_name in ALARM_NAMES:
89 alarm_state = self._alarming.get_alarm_state(
90 endpoint, auth_token, alarm_id)
91 if alarm_state == "alarm":
92 # Generate and send an alarm notification response
93 try:
94 a_date = alarm['state_timestamp'].replace("T", " ")
95 rule = alarm['gnocchi_resources_threshold_rule']
96 resp_message = self._response.generate_response(
97 'notify_alarm', a_id=alarm_id,
98 r_id=rule['resource_id'],
99 sev=alarm['severity'], date=a_date,
100 state=alarm_state, vim_type="OpenStack")
101 self._producer.notify_alarm(
102 'notify_alarm', resp_message, 'alarm_response')
103 except Exception as exc:
104 log.warn("Failed to send notify response:%s", exc)
105
106 if aodhclient:
107 register_notifier()