Fix more.

This commit is contained in:
Hui Xiang 2014-12-12 17:57:19 +08:00
commit 6d1d866c0e
4 changed files with 72 additions and 48 deletions

View File

@ -98,8 +98,9 @@ END
MonitorNeutron_start() {
ocf_log info "MonitorNeutron_start"
su ${OCF_RESKEY_user} -s /bin/sh -c "python ${OCF_RESKEY_binary} $OCF_RESKEY_additional_parameters" \
' >> /dev/null 2>&1 & echo $!' > $OCF_RESKEY_pid
su ${OCF_RESKEY_user} -s /bin/sh -c "python ${OCF_RESKEY_binary} \
$OCF_RESKEY_additional_parameters"' \
>> /dev/null 2>&1 & echo $!' > $OCF_RESKEY_pid
if [ $? = $OCF_SUCCESS ]; then
return $OCF_SUCCESS

View File

@ -20,9 +20,7 @@ import sys
import time
from oslo.config import cfg
from neutron.openstack.common import log as logging
LOG = logging.getLogger(__name__)
import logging as LOG
class Daemon(object):
@ -58,18 +56,16 @@ class Daemon(object):
# fork second time
self._fork()
# redirect standard file descriptors
# redirect standard file descriptors
sys.stdout.flush()
sys.stderr.flush()
stdin = open(self.stdin, 'r')
stdout = open(self.stdout, 'a+')
stderr = open(self.stderr, 'a+', 0)
#os.dup2(stdin.fileno(), sys.stdin.fileno())
#os.dup2(stdout.fileno(), sys.stdout.fileno())
#os.dup2(stderr.fileno(), sys.stderr.fileno())
os.dup2(stdin.fileno(), sys.stdin.fileno())
os.dup2(stdout.fileno(), sys.stdout.fileno())
os.dup2(stderr.fileno(), sys.stderr.fileno())
#atexit.register(self.delete_pid)
signal.signal(signal.SIGTERM, self.handle_sigterm)
def handle_sigterm(self, signum, frame):
@ -93,19 +89,26 @@ class MonitorNeutronAgentsDaemon(Daemon):
super(MonitorNeutronAgentsDaemon, self).__init__()
self.check_interval = check_interval
LOG.info('Monitor Neutron Agent Loop Init')
self.env = {}
def get_env(self):
env = {}
env_data = '/etc/legacy_ha_env_data'
if os.path.isfile(env_data):
with open(env_data, 'r') as f:
line = f.readline()
data = line.split('=').strip()
if data and data[0] and data[1]:
env[data[0]] = env[data[1]]
else:
raise Exception("OpenStack env data uncomplete.")
return env
envrc_f = '/etc/legacy_ha_envrc'
envrc_f_m = False
if os.path.isfile(envrc_f):
ctime = time.ctime(os.stat(envrc_f).st_ctime)
mtime = time.ctime(os.stat(envrc_f).st_mtime)
if ctime != mtime:
envrc_f_m = True
if not self.env or envrc_f_m:
with open(envrc_f, 'r') as f:
for line in f:
data = line.strip().split('=')
if data and data[0] and data[1]:
self.env[data[0]] = data[1]
else:
raise Exception("OpenStack env data uncomplete.")
return self.env
def reassign_agent_resources(self):
''' Use agent scheduler API to detect down agents and re-schedule '''
@ -130,10 +133,6 @@ class MonitorNeutronAgentsDaemon(Daemon):
region_name=env['region'])
partner_gateways = []
#partner_gateways = [unit_private_ip().split('.')[0]]
#for partner_gateway in relations_of_type(reltype='cluster'):
# gateway_hostname = get_hostname(partner_gateway['private-address'])
# partner_gateways.append(gateway_hostname.partition('.')[0])
agents = quantum.list_agents(agent_type=DHCP_AGENT)
dhcp_agents = []
@ -147,8 +146,7 @@ class MonitorNeutronAgentsDaemon(Daemon):
agent['id'])['networks']:
networks[network['id']] = agent['id']
else:
if agent['host'].partition('.')[0] in partner_gateways:
dhcp_agents.append(agent['id'])
dhcp_agents.append(agent['id'])
agents = quantum.list_agents(agent_type=L3_AGENT)
routers = {}
@ -160,8 +158,7 @@ class MonitorNeutronAgentsDaemon(Daemon):
agent['id'])['routers']:
routers[router['id']] = agent['id']
else:
if agent['host'].split('.')[0] in partner_gateways:
l3_agents.append(agent['id'])
l3_agents.append(agent['id'])
if len(dhcp_agents) == 0 or len(l3_agents) == 0:
LOG.info('Unable to relocate resources, there are %s dhcp_agents '
@ -194,11 +191,9 @@ class MonitorNeutronAgentsDaemon(Daemon):
def run(self):
while True:
LOG.info('Monitor Neutron Agent Loop Start')
print "Monitor Neutron Agent Loop Start"
print "Start : %s" % time.ctime()
#time.sleep(self.check_interval)
time.sleep( 15 )
print "End : %s" % time.ctime()
LOG.info("Start : %s" % time.ctime())
time.sleep(self.check_interval)
LOG.info("End : %s" % time.ctime())
self.reassign_agent_resources()
@ -207,11 +202,15 @@ if __name__ == '__main__':
cfg.StrOpt('check_interval',
default=15,
help='Check Neutron Agents interval.'),
cfg.StrOpt('log_file',
default='/var/log/monitor.log',
help='log file'),
]
cfg.CONF.register_cli_opts(opts)
cfg.CONF(project='monitor_neutron_agents', default_config_files=[])
LOG.basicConfig(filename=cfg.CONF.log_file, level=LOG.INFO)
monitor_daemon = MonitorNeutronAgentsDaemon(
check_interval=cfg.CONF.check_interval)
monitor_daemon.start()

View File

@ -192,6 +192,9 @@ def nm_changed():
ca_crt = b64decode(relation_get('ca_cert'))
install_ca_cert(ca_crt)
if config('ha-legacy-mode'):
cache_env_data()
@hooks.hook("cluster-relation-departed")
@restart_on_change(restart_map())
@ -243,21 +246,20 @@ def ha_relation_joined():
'op monitor on-fail="restart" interval="10s"'
.format(external_agent=external_agent),
'res_MonitorHA': 'op monitor interval="5s"',
'nees_connectivity': 'location res_MonitorHA '
'rule pingd: defined pingd'
#'rule -inf: not_defined pingd or pingd lte 0'
}
clones = {
'cl_PingCheck': 'res_PingCheck',
'cl_ClusterMon': 'res_ClusterMon'
}
constraints = {'location': 'nees_connectivity res_MonitorHA '
'rule pingd: defined pingd'}
relation_set(corosync_bindiface=cluster_config['ha-bindiface'],
corosync_mcastport=cluster_config['ha-mcastport'],
resources=resources,
resource_params=resource_params,
clones=clones)
clones=clones,
constraints=constraints)
if __name__ == '__main__':

View File

@ -473,11 +473,7 @@ def reassign_agent_resources():
auth_url=auth_url,
region_name=env['region'])
partner_gateways = [unit_private_ip().split('.')[0]]
for partner_gateway in relations_of_type(reltype='cluster'):
gateway_hostname = get_hostname(partner_gateway['private-address'])
partner_gateways.append(gateway_hostname.partition('.')[0])
partner_gateways = get_quantum_gateway_cluster_nodes()
agents = quantum.list_agents(agent_type=DHCP_AGENT)
dhcp_agents = []
l3_agents = []
@ -608,6 +604,14 @@ def get_dns_host():
return ' '.join(dns_hosts)
def get_quantum_gateway_cluster_nodes():
partner_gateways = get_hostname(unit_private_ip())
for partner_gateway in relations_of_type(reltype='cluster'):
gateway_hostname = get_hostname(partner_gateway['private-address'])
partner_gateways.append(gateway_hostname.partition('.')[0])
return partner_gateways
def copy_file(source_dir, des_dir, f, f_mod=None, update=False):
if not os.path.isdir(des_dir):
mkdir(des_dir)
@ -663,7 +667,7 @@ def install_legacy_ha_files(update=False):
if config('ha-legacy-mode'):
init_ocf_MonitorNeutron_f(update=update)
init_external_agent_f(update=update)
#init_reassign_agent_services_binary()
# init_reassign_agent_services_binary()
init_monitor_daemon(update=update)
@ -679,6 +683,24 @@ def cache_env_data():
log('Unable to get NetworkServiceContext at this time', level=ERROR)
return
with open('/etc/legacy_ha_env_data', 'w') as f:
for k, v in env.items():
f.write(''.join([k, '=', v, '\n']))
no_envrc = False
envrc_f = '/etc/legacy_ha_envrc'
if os.path.isfile(envrc_f):
with open(envrc_f, 'r') as f:
data = f.read()
data = data.strip().split('\n')
diff = False
for line in data:
k = line.split('=')[0]
v = line.split('=')[1]
if k not in env or v != env[k]:
diff = True
break
else:
no_envrc = True
if no_envrc or diff:
with open(envrc_f, 'w') as f:
for k, v in env.items():
f.write(''.join([k, '=', v, '\n']))