71c6c1cd1cb9b69576236b3513997dcc80ef6aee
1 # Copyright 2017 Intel Research and Development Ireland Limited
2 # *************************************************************
4 # This file is part of OSM Monitoring module
5 # All Rights Reserved to Intel Corporation
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
11 # http://www.apache.org/licenses/LICENSE-2.0
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
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
22 # __author__ = Helena McGough
24 """A Webserver to send alarm notifications from Aodh to the SO."""
32 from six
.moves
.BaseHTTPServer
import BaseHTTPRequestHandler
33 from six
.moves
.BaseHTTPServer
import HTTPServer
35 # Initialise a logger for alarm notifier
36 from osm_mon
.core
.message_bus
.producer
import Producer
37 from osm_mon
.core
.settings
import Config
39 cfg
= Config
.instance()
41 logging
.basicConfig(stream
=sys
.stdout
,
42 format
='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
43 datefmt
='%m/%d/%Y %I:%M:%S %p',
44 level
=logging
.getLevelName(cfg
.OSMMON_LOG_LEVEL
))
45 log
= logging
.getLogger(__name__
)
47 kafka_logger
= logging
.getLogger('kafka')
48 kafka_logger
.setLevel(logging
.getLevelName(cfg
.OSMMON_KAFKA_LOG_LEVEL
))
49 kafka_formatter
= logging
.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
50 kafka_handler
= logging
.StreamHandler(sys
.stdout
)
51 kafka_handler
.setFormatter(kafka_formatter
)
52 kafka_logger
.addHandler(kafka_handler
)
54 sys
.path
.append(os
.path
.abspath(os
.path
.join(os
.path
.realpath(__file__
), '..', '..', '..', '..', '..')))
56 from osm_mon
.core
.database
import DatabaseManager
58 from osm_mon
.plugins
.OpenStack
.response
import OpenStackResponseBuilder
61 class NotifierHandler(BaseHTTPRequestHandler
):
62 """Handler class for alarm_actions triggered by OSM alarms."""
64 def _set_headers(self
):
65 """Set the headers for a request."""
66 self
.send_response(200)
67 self
.send_header('Content-type', 'text/html')
71 """Get request functionality."""
75 """POST request function."""
76 # Gets header and data from the post request and records info
78 # Gets the size of data
79 content_length
= int(self
.headers
['Content-Length'])
80 post_data
= self
.rfile
.read(content_length
)
81 # Python 2/3 string compatibility
83 post_data
= post_data
.decode()
84 except AttributeError:
86 log
.info("This alarm was triggered: %s", post_data
)
88 # Send alarm notification to message bus
90 self
.notify_alarm(json
.loads(post_data
))
92 log
.exception("Error notifying alarm")
94 def notify_alarm(self
, values
):
95 """Sends alarm notification message to bus."""
97 # Initialise configuration and authentication for response message
98 response
= OpenStackResponseBuilder()
100 database_manager
= DatabaseManager()
102 alarm_id
= values
['alarm_id']
103 alarm
= database_manager
.get_alarm(alarm_id
, 'openstack')
104 # Process an alarm notification if resource_id is valid
105 # Get date and time for response message
106 a_date
= time
.strftime("%d-%m-%Y") + " " + time
.strftime("%X")
107 # Generate and send response
108 resp_message
= response
.generate_response(
111 vdu_name
=alarm
.vdu_name
,
112 vnf_member_index
=alarm
.vnf_member_index
,
114 metric_name
=alarm
.metric_name
,
115 operation
=alarm
.operation
,
116 threshold_value
=alarm
.threshold
,
117 sev
=values
['severity'],
119 state
=values
['current'])
120 self
._publish
_response
('notify_alarm', json
.dumps(resp_message
))
121 log
.info("Sent alarm notification: %s", resp_message
)
123 def _publish_response(self
, key
: str, msg
: str):
124 producer
= Producer()
125 producer
.send(topic
='alarm_response', key
=key
, value
=msg
)
129 def run(server_class
=HTTPServer
, handler_class
=NotifierHandler
, port
=8662):
130 """Run the webserver application to retrieve alarm notifications."""
132 server_address
= ('', port
)
133 httpd
= server_class(server_address
, handler_class
)
134 log
.info("Starting alarm notifier server on port: %s", port
)
135 httpd
.serve_forever()
136 except Exception as exc
:
137 log
.warning("Failed to start webserver, %s", exc
)
140 if __name__
== "__main__":
141 cfg
= Config
.instance()
142 p
= re
.compile(':(\d+)', re
.IGNORECASE
)
143 m
= p
.search(cfg
.OS_NOTIFIER_URI
)