| Helena McGough | cda5f2f | 2017-09-12 08:30:02 +0100 | [diff] [blame] | 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 | ## |
| Helena McGough | 94f93f7 | 2017-11-23 17:29:54 +0000 | [diff] [blame] | 22 | # __author__ = Helena McGough |
| 23 | # |
| 24 | """A Webserver to send alarm notifications from Aodh to the SO.""" |
| Helena McGough | cda5f2f | 2017-09-12 08:30:02 +0100 | [diff] [blame] | 25 | import json |
| Helena McGough | 94f93f7 | 2017-11-23 17:29:54 +0000 | [diff] [blame] | 26 | import logging |
| Benjamin Diaz | 7551247 | 2018-04-27 14:32:31 -0300 | [diff] [blame] | 27 | import os |
| Helena McGough | 94f93f7 | 2017-11-23 17:29:54 +0000 | [diff] [blame] | 28 | import sys |
| Helena McGough | 94f93f7 | 2017-11-23 17:29:54 +0000 | [diff] [blame] | 29 | import time |
| 30 | |
| Benjamin Diaz | 181cce8 | 2018-03-28 21:12:11 -0300 | [diff] [blame] | 31 | from six.moves.BaseHTTPServer import BaseHTTPRequestHandler |
| 32 | from six.moves.BaseHTTPServer import HTTPServer |
| Helena McGough | 94f93f7 | 2017-11-23 17:29:54 +0000 | [diff] [blame] | 33 | |
| 34 | # Initialise a logger for alarm notifier |
| Benjamin Diaz | 181cce8 | 2018-03-28 21:12:11 -0300 | [diff] [blame] | 35 | |
| 36 | logging.basicConfig(stream=sys.stdout, |
| Helena McGough | 94f93f7 | 2017-11-23 17:29:54 +0000 | [diff] [blame] | 37 | format='%(asctime)s %(message)s', |
| Benjamin Diaz | 181cce8 | 2018-03-28 21:12:11 -0300 | [diff] [blame] | 38 | datefmt='%m/%d/%Y %I:%M:%S %p', |
| Helena McGough | 94f93f7 | 2017-11-23 17:29:54 +0000 | [diff] [blame] | 39 | level=logging.INFO) |
| 40 | log = logging.getLogger(__name__) |
| 41 | |
| Benjamin Diaz | 181cce8 | 2018-03-28 21:12:11 -0300 | [diff] [blame] | 42 | sys.path.append(os.path.abspath(os.path.join(os.path.realpath(__file__), '..', '..', '..', '..', '..'))) |
| Helena McGough | d13405d | 2017-09-21 17:09:24 +0100 | [diff] [blame] | 43 | |
| Benjamin Diaz | 181cce8 | 2018-03-28 21:12:11 -0300 | [diff] [blame] | 44 | from osm_mon.core.database import DatabaseManager |
| Helena McGough | effeb7c | 2017-11-23 15:54:08 +0000 | [diff] [blame] | 45 | from osm_mon.core.message_bus.producer import KafkaProducer |
| Helena McGough | cda5f2f | 2017-09-12 08:30:02 +0100 | [diff] [blame] | 46 | |
| Helena McGough | effeb7c | 2017-11-23 15:54:08 +0000 | [diff] [blame] | 47 | from osm_mon.plugins.OpenStack.response import OpenStack_Response |
| Helena McGough | cda5f2f | 2017-09-12 08:30:02 +0100 | [diff] [blame] | 48 | |
| Helena McGough | cda5f2f | 2017-09-12 08:30:02 +0100 | [diff] [blame] | 49 | |
| Helena McGough | 94f93f7 | 2017-11-23 17:29:54 +0000 | [diff] [blame] | 50 | class NotifierHandler(BaseHTTPRequestHandler): |
| 51 | """Handler class for alarm_actions triggered by OSM alarms.""" |
| Helena McGough | cda5f2f | 2017-09-12 08:30:02 +0100 | [diff] [blame] | 52 | |
| Helena McGough | 94f93f7 | 2017-11-23 17:29:54 +0000 | [diff] [blame] | 53 | def _set_headers(self): |
| 54 | """Set the headers for a request.""" |
| 55 | self.send_response(200) |
| 56 | self.send_header('Content-type', 'text/html') |
| 57 | self.end_headers() |
| Helena McGough | cda5f2f | 2017-09-12 08:30:02 +0100 | [diff] [blame] | 58 | |
| Helena McGough | 94f93f7 | 2017-11-23 17:29:54 +0000 | [diff] [blame] | 59 | def do_GET(self): |
| 60 | """Get request functionality.""" |
| 61 | self._set_headers() |
| Helena McGough | d00ff82 | 2017-09-20 17:42:22 +0100 | [diff] [blame] | 62 | |
| Helena McGough | 94f93f7 | 2017-11-23 17:29:54 +0000 | [diff] [blame] | 63 | def do_POST(self): |
| 64 | """POST request function.""" |
| 65 | # Gets header and data from the post request and records info |
| 66 | self._set_headers() |
| 67 | # Gets the size of data |
| 68 | content_length = int(self.headers['Content-Length']) |
| 69 | post_data = self.rfile.read(content_length) |
| Benjamin Diaz | e2fe458 | 2018-06-13 18:14:12 -0300 | [diff] [blame] | 70 | # Python 2/3 string compatibility |
| Benjamin Diaz | b85fc8c | 2018-05-03 13:22:11 -0300 | [diff] [blame] | 71 | try: |
| 72 | post_data = post_data.decode() |
| 73 | except AttributeError: |
| 74 | pass |
| Benjamin Diaz | e2fe458 | 2018-06-13 18:14:12 -0300 | [diff] [blame] | 75 | log.info("This alarm was triggered: %s", json.dumps(post_data)) |
| Helena McGough | d00ff82 | 2017-09-20 17:42:22 +0100 | [diff] [blame] | 76 | |
| Benjamin Diaz | e2fe458 | 2018-06-13 18:14:12 -0300 | [diff] [blame] | 77 | # Send alarm notification to message bus |
| 78 | try: |
| 79 | self.notify_alarm(json.dumps(post_data)) |
| 80 | except Exception: |
| 81 | log.exception("Error notifying alarm") |
| Helena McGough | cda5f2f | 2017-09-12 08:30:02 +0100 | [diff] [blame] | 82 | |
| Helena McGough | 94f93f7 | 2017-11-23 17:29:54 +0000 | [diff] [blame] | 83 | def notify_alarm(self, values): |
| Benjamin Diaz | e2fe458 | 2018-06-13 18:14:12 -0300 | [diff] [blame] | 84 | """Sends alarm notification message to bus.""" |
| Helena McGough | cda5f2f | 2017-09-12 08:30:02 +0100 | [diff] [blame] | 85 | |
| Benjamin Diaz | e2fe458 | 2018-06-13 18:14:12 -0300 | [diff] [blame] | 86 | # Initialise configuration and authentication for response message |
| 87 | response = OpenStack_Response() |
| 88 | producer = KafkaProducer('alarm_response') |
| Helena McGough | d00ff82 | 2017-09-20 17:42:22 +0100 | [diff] [blame] | 89 | |
| Benjamin Diaz | e2fe458 | 2018-06-13 18:14:12 -0300 | [diff] [blame] | 90 | database_manager = DatabaseManager() |
| Helena McGough | cda5f2f | 2017-09-12 08:30:02 +0100 | [diff] [blame] | 91 | |
| Benjamin Diaz | e2fe458 | 2018-06-13 18:14:12 -0300 | [diff] [blame] | 92 | alarm_id = values['alarm_id'] |
| 93 | alarm = database_manager.get_alarm(alarm_id, 'openstack') |
| 94 | # Process an alarm notification if resource_id is valid |
| 95 | # Get date and time for response message |
| 96 | a_date = time.strftime("%d-%m-%Y") + " " + time.strftime("%X") |
| 97 | # Generate and send response |
| 98 | resp_message = response.generate_response( |
| 99 | 'notify_alarm', |
| 100 | a_id=alarm_id, |
| 101 | vdu_name=alarm.vdu_name, |
| 102 | vnf_member_index=alarm.vnf_member_index, |
| 103 | ns_id=alarm.ns_id, |
| 104 | metric_name=alarm.metric_name, |
| 105 | operation=alarm.operation, |
| 106 | threshold_value=alarm.threshold, |
| 107 | sev=values['severity'], |
| 108 | date=a_date, |
| 109 | state=values['current']) |
| Benjamin Diaz | 326907a | 2018-06-18 14:21:46 -0300 | [diff] [blame] | 110 | producer.publish_alarm_response( |
| Benjamin Diaz | e2fe458 | 2018-06-13 18:14:12 -0300 | [diff] [blame] | 111 | 'notify_alarm', resp_message) |
| 112 | log.info("Sent alarm notification: %s", resp_message) |
| Helena McGough | 94f93f7 | 2017-11-23 17:29:54 +0000 | [diff] [blame] | 113 | |
| 114 | |
| 115 | def run(server_class=HTTPServer, handler_class=NotifierHandler, port=8662): |
| Benjamin Diaz | 181cce8 | 2018-03-28 21:12:11 -0300 | [diff] [blame] | 116 | """Run the webserver application to retrieve alarm notifications.""" |
| Helena McGough | 94f93f7 | 2017-11-23 17:29:54 +0000 | [diff] [blame] | 117 | try: |
| 118 | server_address = ('', port) |
| 119 | httpd = server_class(server_address, handler_class) |
| 120 | print('Starting alarm notifier...') |
| 121 | log.info("Starting alarm notifier server on port: %s", port) |
| 122 | httpd.serve_forever() |
| 123 | except Exception as exc: |
| Benjamin Diaz | b85fc8c | 2018-05-03 13:22:11 -0300 | [diff] [blame] | 124 | log.warning("Failed to start webserver, %s", exc) |
| Helena McGough | 94f93f7 | 2017-11-23 17:29:54 +0000 | [diff] [blame] | 125 | |
| Benjamin Diaz | 181cce8 | 2018-03-28 21:12:11 -0300 | [diff] [blame] | 126 | |
| Helena McGough | 94f93f7 | 2017-11-23 17:29:54 +0000 | [diff] [blame] | 127 | if __name__ == "__main__": |
| 128 | from sys import argv |
| 129 | |
| 130 | # Runs the webserver |
| 131 | if len(argv) == 2: |
| 132 | run(port=int(argv[1])) |
| 133 | else: |
| 134 | run() |