Change service plugin to choose an alternate ip if needed.

Change-Id: Ia9f107b7993236bc4b30aa149d2765e9e465acbd
Closes-Bug: #1461307
This commit is contained in:
Tim Kuhlman 2015-06-24 09:01:29 -06:00
parent b5c441bd36
commit 3eaf9d8afe
2 changed files with 26 additions and 6 deletions

View File

@ -1,4 +1,6 @@
import logging
import psutil
from urlparse import urlparse
from plugin import Plugin
@ -21,7 +23,7 @@ class ServicePlugin(Plugin):
def __init__(self, kwargs):
self.service_name = kwargs['service_name']
self.process_names = kwargs['process_names']
self.service_api_url = kwargs['service_api_url']
self.service_api_url = kwargs.get('service_api_url')
self.search_pattern = kwargs['search_pattern']
super(ServicePlugin, self).__init__(kwargs['template_dir'], kwargs['overwrite'])
@ -49,10 +51,28 @@ class ServicePlugin(Plugin):
config.merge(watch_process([process], self.service_name, process, exact_match=False))
if self.service_api_url and self.search_pattern:
# Setup an active http_status check on the API
log.info("\tConfiguring an http_check for the {0} API.".format(self.service_name))
config.merge(service_api_check(self.service_name + '-api', self.service_api_url,
# Check if there is something listening on the host/port
parsed = urlparse(self.service_api_url)
host, port = parsed.netloc.split(':')
listening = []
for connection in psutil.net_connections():
if connection.status == psutil.CONN_LISTEN and connection.laddr[1] == int(port):
listening.append(connection.laddr[0])
if len(listening) > 0:
# If not listening on localhost or ips then use another local ip
if host == 'localhost' and len(set(['0.0.0.0', '::', '::1']) & set(listening)) == 0:
api_url = listening[0] + ':' + port
else:
api_url = self.service_api_url
# Setup an active http_status check on the API
log.info("\tConfiguring an http_check for the {0} API.".format(self.service_name))
config.merge(service_api_check(self.service_name + '-api', api_url,
self.search_pattern, self.service_name))
else:
log.info("\tNo process found listening on {0} ".format(port) +
"skipping setup of http_check for the {0} API." .format(self.service_name))
return config

View File

@ -4,11 +4,11 @@ httplib2
ntplib
pymongo
pylint
psutil>=2.0
psutil>=2.2.1
python-memcached
PyYAML
redis
simplejson
supervisor
tornado
python-monascaclient
python-monascaclient