| 1 |
# Copyright(c) 2016 Nippon Telegraph and Telephone Corporation |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
# you may not use this file except in compliance with the License. |
| 5 |
# You may obtain a copy of the License at |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
# See the License for the specific language governing permissions and |
| 13 |
# limitations under the License. |
| 14 |
|
| 15 |
import eventlet |
| 16 |
from keystoneauth1 import loading as ks_loading |
| 17 |
from openstack import connection |
| 18 |
from openstack import exceptions |
| 19 |
from oslo_log import log as oslo_logging |
| 20 |
|
| 21 |
import masakarimonitors.conf |
| 22 |
|
| 23 |
LOG = oslo_logging.getLogger(__name__) |
| 24 |
CONF = masakarimonitors.conf.CONF |
| 25 |
|
| 26 |
|
| 27 |
class SendNotification(object): |
| 28 |
|
| 29 |
def __init__(self): |
| 30 |
self._masakari_client = None |
| 31 |
|
| 32 |
@property |
| 33 |
def masakari_client(self): |
| 34 |
if not self._masakari_client: |
| 35 |
self._masakari_client = self._make_client() |
| 36 |
return self._masakari_client |
| 37 |
|
| 38 |
def _make_client(self): |
| 39 |
auth = ks_loading.load_auth_from_conf_options(CONF, 'api') |
| 40 |
session = ks_loading.load_session_from_conf_options(CONF, 'api', |
| 41 |
auth=auth) |
| 42 |
conn = connection.Connection(session=session, |
| 43 |
interface=CONF.api.api_interface, |
| 44 |
region_name=CONF.api.region) |
| 45 |
|
| 46 |
return conn.instance_ha |
| 47 |
|
| 48 |
def send_notification(self, api_retry_max, api_retry_interval, event): |
| 49 |
"""Send a notification. |
| 50 |
|
| 51 |
This method sends a notification to the masakari-api. |
| 52 |
|
| 53 |
:param api_retry_max: Number of retries when the notification |
| 54 |
processing is error. |
| 55 |
:param api_retry_interval: Trial interval of time of the notification |
| 56 |
processing is error. |
| 57 |
:param event: dictionary of event that included in notification. |
| 58 |
""" |
| 59 |
|
| 60 |
LOG.info("Send a notification. %s", event) |
| 61 |
|
| 62 |
# Send a notification. |
| 63 |
retry_count = 0 |
| 64 |
while True: |
| 65 |
try: |
| 66 |
response = self.masakari_client.create_notification( |
| 67 |
type=event['notification']['type'], |
| 68 |
hostname=event['notification']['hostname'], |
| 69 |
generated_time=event['notification']['generated_time'], |
| 70 |
payload=event['notification']['payload']) |
| 71 |
|
| 72 |
LOG.info("Response: %s", response) |
| 73 |
break |
| 74 |
|
| 75 |
except Exception as e: |
| 76 |
if isinstance(e, exceptions.HttpException): |
| 77 |
if e.status_code in [400, 409]: |
| 78 |
LOG.info(e) |
| 79 |
break |
| 80 |
|
| 81 |
if retry_count < api_retry_max: |
| 82 |
LOG.warning("Retry sending a notification. (%s)", e) |
| 83 |
retry_count = retry_count + 1 |
| 84 |
eventlet.greenthread.sleep(api_retry_interval) |
| 85 |
else: |
| 86 |
LOG.exception("Exception caught: %s", e) |
| 87 |
self._masakari_client = None |
| 88 |
break |