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