| 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 | cda5f2f | 2017-09-12 08:30:02 +0100 | [diff] [blame] | 26 | |
| Helena McGough | 94f93f7 | 2017-11-23 17:29:54 +0000 | [diff] [blame^] | 27 | import logging |
| Helena McGough | d13405d | 2017-09-21 17:09:24 +0100 | [diff] [blame] | 28 | |
| Helena McGough | 94f93f7 | 2017-11-23 17:29:54 +0000 | [diff] [blame^] | 29 | import sys |
| 30 | |
| 31 | import time |
| 32 | |
| 33 | from BaseHTTPServer import BaseHTTPRequestHandler |
| 34 | from BaseHTTPServer import HTTPServer |
| 35 | |
| 36 | # Initialise a logger for alarm notifier |
| 37 | logging.basicConfig(filename='aodh_notify.log', |
| 38 | format='%(asctime)s %(message)s', |
| 39 | datefmt='%m/%d/%Y %I:%M:%S %p', filemode='a', |
| 40 | level=logging.INFO) |
| 41 | log = logging.getLogger(__name__) |
| 42 | |
| 43 | sys.path.append("/root/MON") |
| Helena McGough | d13405d | 2017-09-21 17:09:24 +0100 | [diff] [blame] | 44 | |
| Helena McGough | cda5f2f | 2017-09-12 08:30:02 +0100 | [diff] [blame] | 45 | from core.message_bus.producer import KafkaProducer |
| 46 | |
| Helena McGough | d00ff82 | 2017-09-20 17:42:22 +0100 | [diff] [blame] | 47 | from plugins.OpenStack.Aodh.alarming import Alarming |
| Helena McGough | 94f93f7 | 2017-11-23 17:29:54 +0000 | [diff] [blame^] | 48 | from plugins.OpenStack.common import Common |
| Helena McGough | cda5f2f | 2017-09-12 08:30:02 +0100 | [diff] [blame] | 49 | from plugins.OpenStack.response import OpenStack_Response |
| Helena McGough | d00ff82 | 2017-09-20 17:42:22 +0100 | [diff] [blame] | 50 | from plugins.OpenStack.settings import Config |
| Helena McGough | cda5f2f | 2017-09-12 08:30:02 +0100 | [diff] [blame] | 51 | |
| 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 | class NotifierHandler(BaseHTTPRequestHandler): |
| 54 | """Handler class for alarm_actions triggered by OSM alarms.""" |
| Helena McGough | cda5f2f | 2017-09-12 08:30:02 +0100 | [diff] [blame] | 55 | |
| Helena McGough | 94f93f7 | 2017-11-23 17:29:54 +0000 | [diff] [blame^] | 56 | def _set_headers(self): |
| 57 | """Set the headers for a request.""" |
| 58 | self.send_response(200) |
| 59 | self.send_header('Content-type', 'text/html') |
| 60 | self.end_headers() |
| Helena McGough | cda5f2f | 2017-09-12 08:30:02 +0100 | [diff] [blame] | 61 | |
| Helena McGough | 94f93f7 | 2017-11-23 17:29:54 +0000 | [diff] [blame^] | 62 | def do_GET(self): |
| 63 | """Get request functionality.""" |
| 64 | self._set_headers() |
| 65 | self.wfile.write("<html><body><h1>hi!</h1></body></html>") |
| Helena McGough | d00ff82 | 2017-09-20 17:42:22 +0100 | [diff] [blame] | 66 | |
| Helena McGough | 94f93f7 | 2017-11-23 17:29:54 +0000 | [diff] [blame^] | 67 | def do_POST(self): |
| 68 | """POST request function.""" |
| 69 | # Gets header and data from the post request and records info |
| 70 | self._set_headers() |
| 71 | # Gets the size of data |
| 72 | content_length = int(self.headers['Content-Length']) |
| 73 | post_data = self.rfile.read(content_length) |
| 74 | self.wfile.write("<html><body><h1>POST!</h1></body></tml>") |
| 75 | log.info("This alarm was triggered: %s", json.loads(post_data)) |
| Helena McGough | d00ff82 | 2017-09-20 17:42:22 +0100 | [diff] [blame] | 76 | |
| Helena McGough | 94f93f7 | 2017-11-23 17:29:54 +0000 | [diff] [blame^] | 77 | # Generate a notify_alarm response for the SO |
| 78 | self.notify_alarm(json.loads(post_data)) |
| Helena McGough | cda5f2f | 2017-09-12 08:30:02 +0100 | [diff] [blame] | 79 | |
| Helena McGough | 94f93f7 | 2017-11-23 17:29:54 +0000 | [diff] [blame^] | 80 | def notify_alarm(self, values): |
| 81 | """Send a notifcation repsonse message to the SO.""" |
| 82 | # Initialiase configuration and authentication for response message |
| 83 | config = Config.instance() |
| 84 | config.read_environ("aodh") |
| Helena McGough | d00ff82 | 2017-09-20 17:42:22 +0100 | [diff] [blame] | 85 | self._alarming = Alarming() |
| Helena McGough | 94f93f7 | 2017-11-23 17:29:54 +0000 | [diff] [blame^] | 86 | self._common = Common() |
| 87 | self._response = OpenStack_Response() |
| 88 | self._producer = KafkaProducer('alarm_response') |
| Helena McGough | cda5f2f | 2017-09-12 08:30:02 +0100 | [diff] [blame] | 89 | |
| Helena McGough | 94f93f7 | 2017-11-23 17:29:54 +0000 | [diff] [blame^] | 90 | alarm_id = values['alarm_id'] |
| 91 | auth_token = self._common._authenticate() |
| 92 | endpoint = self._common.get_endpoint("alarming") |
| Helena McGough | d00ff82 | 2017-09-20 17:42:22 +0100 | [diff] [blame] | 93 | |
| Helena McGough | 94f93f7 | 2017-11-23 17:29:54 +0000 | [diff] [blame^] | 94 | # If authenticated generate and send response message |
| 95 | if (auth_token is not None and endpoint is not None): |
| 96 | url = "{}/v2/alarms/%s".format(endpoint) % alarm_id |
| Helena McGough | cda5f2f | 2017-09-12 08:30:02 +0100 | [diff] [blame] | 97 | |
| Helena McGough | 94f93f7 | 2017-11-23 17:29:54 +0000 | [diff] [blame^] | 98 | # Get the resource_id of the triggered alarm |
| 99 | result = self._common._perform_request( |
| 100 | url, auth_token, req_type="get") |
| 101 | alarm_details = json.loads(result.text) |
| 102 | gnocchi_rule = alarm_details['gnocchi_resources_threshold_rule'] |
| 103 | resource_id = gnocchi_rule['resource_id'] |
| Helena McGough | d00ff82 | 2017-09-20 17:42:22 +0100 | [diff] [blame] | 104 | |
| Helena McGough | 94f93f7 | 2017-11-23 17:29:54 +0000 | [diff] [blame^] | 105 | # Process an alarm notification if resource_id is valid |
| 106 | if resource_id is not None: |
| 107 | # Get date and time for response message |
| 108 | a_date = time.strftime("%d-%m-%Y") + " " + time.strftime("%X") |
| 109 | # Try generate and send response |
| 110 | try: |
| 111 | resp_message = self._response.generate_response( |
| 112 | 'notify_alarm', a_id=alarm_id, |
| 113 | r_id=resource_id, |
| 114 | sev=values['severity'], date=a_date, |
| 115 | state=values['current'], vim_type="OpenStack") |
| 116 | self._producer.notify_alarm( |
| 117 | 'notify_alarm', resp_message, 'alarm_response') |
| 118 | log.info("Sent an alarm response to SO: %s", resp_message) |
| 119 | except Exception as exc: |
| 120 | log.warn("Couldn't notify SO of the alarm: %s", exc) |
| 121 | else: |
| 122 | log.warn("No resource_id for alarm; no SO response sent.") |
| 123 | else: |
| 124 | log.warn("Authentication failure; SO notification not sent.") |
| 125 | |
| 126 | |
| 127 | def run(server_class=HTTPServer, handler_class=NotifierHandler, port=8662): |
| 128 | """Run the webserver application to retreive alarm notifications.""" |
| 129 | try: |
| 130 | server_address = ('', port) |
| 131 | httpd = server_class(server_address, handler_class) |
| 132 | print('Starting alarm notifier...') |
| 133 | log.info("Starting alarm notifier server on port: %s", port) |
| 134 | httpd.serve_forever() |
| 135 | except Exception as exc: |
| 136 | log.warn("Failed to start webserver, %s", exc) |
| 137 | |
| 138 | if __name__ == "__main__": |
| 139 | from sys import argv |
| 140 | |
| 141 | # Runs the webserver |
| 142 | if len(argv) == 2: |
| 143 | run(port=int(argv[1])) |
| 144 | else: |
| 145 | run() |