2014-02-27 16:55:07 -07:00
|
|
|
#!/usr/bin/env python
|
2014-05-01 12:27:06 -06:00
|
|
|
# Copyright (c) 2014 Hewlett-Packard Development Company, L.P.
|
2014-02-27 16:55:07 -07:00
|
|
|
#
|
2014-05-01 12:27:06 -06:00
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
|
|
# implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
2014-02-27 16:55:07 -07:00
|
|
|
""" Notification Engine
|
|
|
|
This engine reads alarms from Kafka and then notifies the customer using their configured notification method.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import logging
|
2014-03-17 11:26:34 -06:00
|
|
|
import logging.config
|
2014-03-17 16:29:09 -06:00
|
|
|
import multiprocessing
|
2014-03-19 09:22:55 -06:00
|
|
|
import os
|
2014-02-27 16:55:07 -07:00
|
|
|
import signal
|
|
|
|
import sys
|
2014-03-19 09:22:55 -06:00
|
|
|
import time
|
2014-03-12 11:31:57 -06:00
|
|
|
import yaml
|
2014-02-27 16:55:07 -07:00
|
|
|
|
2015-01-28 15:34:52 -07:00
|
|
|
from notification_engine import NotificationEngine
|
2015-02-04 16:49:06 -07:00
|
|
|
from retry_engine import RetryEngine
|
2014-02-27 16:55:07 -07:00
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
processors = [] # global list to facilitate clean signal handling
|
2014-03-19 09:22:55 -06:00
|
|
|
exiting = False
|
2014-02-27 16:55:07 -07:00
|
|
|
|
|
|
|
|
2014-03-06 17:24:29 -07:00
|
|
|
def clean_exit(signum, frame=None):
|
2014-03-19 09:22:55 -06:00
|
|
|
"""Exit all processes attempting to finish uncommited active work before exit.
|
2014-03-17 16:29:09 -06:00
|
|
|
Can be called on an os signal or no zookeeper losing connection.
|
2014-02-27 16:55:07 -07:00
|
|
|
"""
|
2014-03-19 09:22:55 -06:00
|
|
|
global exiting
|
|
|
|
if exiting:
|
2015-01-28 15:34:52 -07:00
|
|
|
# Since this is set up as a handler for SIGCHLD when this kills one
|
|
|
|
# child it gets another signal, the global exiting avoids this running
|
|
|
|
# multiple times.
|
2014-03-19 09:22:55 -06:00
|
|
|
log.debug('Exit in progress clean_exit received additional signal %s' % signum)
|
|
|
|
return
|
|
|
|
|
|
|
|
log.info('Received signal %s, beginning graceful shutdown.' % signum)
|
|
|
|
exiting = True
|
|
|
|
|
2015-01-28 15:34:52 -07:00
|
|
|
for process in processors:
|
2014-03-17 15:05:17 -06:00
|
|
|
try:
|
2014-03-19 09:22:55 -06:00
|
|
|
if process.is_alive():
|
|
|
|
process.terminate() # Sends sigterm which any processes after a notification is sent attempt to handle
|
|
|
|
except Exception:
|
|
|
|
pass
|
|
|
|
|
|
|
|
# Kill everything, that didn't already die
|
|
|
|
for child in multiprocessing.active_children():
|
|
|
|
log.debug('Killing pid %s' % child.pid)
|
|
|
|
try:
|
|
|
|
os.kill(child.pid, signal.SIGKILL)
|
2014-03-17 16:29:09 -06:00
|
|
|
except Exception:
|
2014-03-17 15:05:17 -06:00
|
|
|
pass
|
2014-03-06 17:24:29 -07:00
|
|
|
|
2014-03-10 16:19:47 -06:00
|
|
|
sys.exit(0)
|
2014-02-27 16:55:07 -07:00
|
|
|
|
|
|
|
|
2015-02-04 16:49:06 -07:00
|
|
|
def start_process(proccess_type, config):
|
|
|
|
log.info("start process: {}".format(proccess_type))
|
|
|
|
p = proccess_type(config)
|
|
|
|
p.run()
|
|
|
|
|
|
|
|
|
2014-02-27 16:55:07 -07:00
|
|
|
def main(argv=None):
|
|
|
|
if argv is None:
|
|
|
|
argv = sys.argv
|
|
|
|
if len(argv) == 2:
|
|
|
|
config_file = argv[1]
|
|
|
|
elif len(argv) > 2:
|
2014-03-17 16:29:09 -06:00
|
|
|
print("Usage: " + argv[0] + " <config_file>")
|
2014-07-16 13:51:48 -06:00
|
|
|
print("Config file defaults to /etc/monasca/notification.yaml")
|
2014-02-27 16:55:07 -07:00
|
|
|
return 1
|
|
|
|
else:
|
2014-07-16 13:51:48 -06:00
|
|
|
config_file = '/etc/monasca/notification.yaml'
|
2014-02-27 16:55:07 -07:00
|
|
|
|
|
|
|
config = yaml.load(open(config_file, 'r'))
|
|
|
|
|
|
|
|
# Setup logging
|
2014-03-17 11:26:34 -06:00
|
|
|
logging.config.dictConfig(config['logging'])
|
2014-03-06 17:24:29 -07:00
|
|
|
|
2015-02-04 16:49:06 -07:00
|
|
|
for proc in range(0, config['processors']['notification']['number']):
|
|
|
|
processors.append(multiprocessing.Process(
|
|
|
|
target=start_process, args=(NotificationEngine, config)))
|
|
|
|
|
|
|
|
processors.append(multiprocessing.Process(
|
|
|
|
target=start_process, args=(RetryEngine, config)))
|
2014-02-27 16:55:07 -07:00
|
|
|
|
2014-07-16 13:51:48 -06:00
|
|
|
# Start
|
2014-02-27 16:55:07 -07:00
|
|
|
try:
|
|
|
|
log.info('Starting processes')
|
|
|
|
for process in processors:
|
|
|
|
process.start()
|
2014-03-19 09:22:55 -06:00
|
|
|
|
2015-01-28 15:34:52 -07:00
|
|
|
# The signal handlers must be added after the processes start otherwise
|
|
|
|
# they run on all processes
|
2014-03-19 09:22:55 -06:00
|
|
|
signal.signal(signal.SIGCHLD, clean_exit)
|
2014-03-19 11:53:47 -06:00
|
|
|
signal.signal(signal.SIGINT, clean_exit)
|
|
|
|
signal.signal(signal.SIGTERM, clean_exit)
|
2014-03-19 09:22:55 -06:00
|
|
|
|
|
|
|
while True:
|
2015-01-28 15:34:52 -07:00
|
|
|
time.sleep(10)
|
|
|
|
|
2014-03-17 16:29:09 -06:00
|
|
|
except Exception:
|
2014-03-20 14:55:41 -06:00
|
|
|
log.exception('Error! Exiting.')
|
2014-02-27 16:55:07 -07:00
|
|
|
for process in processors:
|
|
|
|
process.terminate()
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
sys.exit(main())
|