Update Openstack plugins
[osm/MON.git] / plugins / OpenStack / Aodh / notifier.py
index 2068f03..3487daa 100644 (file)
 import json
 import logging as log
 
+try:
+    import aodhclient
+except ImportError:
+    log.warn("Failed to import the aodhclient")
+
+
 from core.message_bus.producer import KafkaProducer
 
+from plugins.OpenStack.Aodh.alarming import Alarming
 from plugins.OpenStack.response import OpenStack_Response
-from plugins.OpenStack.singleton import Singleton
+from plugins.OpenStack.settings import Config
 
 __author__ = "Helena McGough"
 
 ALARM_NAMES = [
-    "Average_Memory_Usage_Above_Threshold",
-    "Read_Latency_Above_Threshold",
-    "Write_Latency_Above_Threshold",
-    "DISK_READ_OPS",
-    "DISK_WRITE_OPS",
-    "DISK_READ_BYTES",
-    "DISK_WRITE_BYTES",
-    "Net_Packets_Dropped",
-    "Packets_in_Above_Threshold",
-    "Packets_out_Above_Threshold",
-    "CPU_Utilization_Above_Threshold"]
-
-
-@Singleton
+    "average_memory_usage_above_threshold",
+    "disk_read_ops",
+    "disk_write_ops",
+    "disk_read_bytes",
+    "disk_write_bytes",
+    "net_packets_dropped",
+    "packets_in_above_threshold",
+    "packets_out_above_threshold",
+    "cpu_utilization_above_threshold"]
+
+
+def register_notifier():
+    """Run the notifier instance."""
+    config = Config.instance()
+    instance = Notifier(config=config)
+    instance.config()
+    instance.notify()
+
+
 class Notifier(object):
     """Alarm Notification class."""
 
-    def __init__(self):
+    def __init__(self, config):
         """Initialize alarm notifier."""
+        log.info("Initialize the notifier for the SO.")
+        self._config = config
         self._response = OpenStack_Response()
-
         self._producer = KafkaProducer("alarm_response")
+        self._alarming = Alarming()
 
-    def notify(self, alarming):
+    def config(self):
+        """Configure the alarm notifier."""
+        log.info("Configure the notifier instance.")
+        self._config.read_environ("aodh")
+
+    def notify(self):
         """Send alarm notifications responses to the SO."""
-        auth_token, endpoint = alarming.authenticate(None)
+        log.info("Checking for alarm notifications")
+        auth_token, endpoint = self._alarming.authenticate()
 
         while(1):
-            alarm_list = json.loads(alarming.list_alarms(endpoint, auth_token))
-            for alarm in alarm_list:
+            alarm_list = self._alarming.list_alarms(endpoint, auth_token)
+            for alarm in json.loads(alarm_list):
                 alarm_id = alarm['alarm_id']
                 alarm_name = alarm['name']
                 # Send a notification response to the SO on alarm trigger
                 if alarm_name in ALARM_NAMES:
-                    alarm_state = alarming.get_alarm_state(
+                    alarm_state = self._alarming.get_alarm_state(
                         endpoint, auth_token, alarm_id)
                     if alarm_state == "alarm":
                         # Generate and send an alarm notification response
@@ -82,3 +102,6 @@ class Notifier(object):
                                 'notify_alarm', resp_message, 'alarm_response')
                         except Exception as exc:
                             log.warn("Failed to send notify response:%s", exc)
+
+if aodhclient:
+    register_notifier()