| 1 |
# Copyright(c) 2021 Inspur |
| 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 |
""" |
| 16 |
Main abstraction layer for retrieving node status from consul |
| 17 |
""" |
| 18 |
|
| 19 |
import consul |
| 20 |
from oslo_utils import netutils |
| 21 |
|
| 22 |
from masakarimonitors.i18n import _ |
| 23 |
|
| 24 |
|
| 25 |
DEFAULT_CONSUL_PORT = 8500 |
| 26 |
|
| 27 |
|
| 28 |
class ConsulException(Exception): |
| 29 |
"""Base Consul Exception""" |
| 30 |
msg_fmt = _("An unknown exception occurred.") |
| 31 |
|
| 32 |
def __init__(self, message=None, **kwargs): |
| 33 |
if not message: |
| 34 |
message = self.msg_fmt % kwargs |
| 35 |
|
| 36 |
super(ConsulException, self).__init__(message) |
| 37 |
|
| 38 |
|
| 39 |
class ConsulAgentNotExist(ConsulException): |
| 40 |
msg_fmt = _("Consul agent of %(cluster)s not exist.") |
| 41 |
|
| 42 |
|
| 43 |
class ConsulGetMembersException(ConsulException): |
| 44 |
msg_fmt = _("Failed to get members of %(cluster)s: %(err)s.") |
| 45 |
|
| 46 |
|
| 47 |
class ConsulManager(object): |
| 48 |
"""Consul manager class |
| 49 |
|
| 50 |
This class helps to pull health data from all consul clusters, |
| 51 |
and return health data in sequence. |
| 52 |
""" |
| 53 |
|
| 54 |
def __init__(self, CONF): |
| 55 |
self.agents = {} |
| 56 |
self.init_agents(CONF) |
| 57 |
|
| 58 |
def init_agents(self, CONF): |
| 59 |
if CONF.consul.agent_manage: |
| 60 |
addr, port = netutils.parse_host_port( |
| 61 |
CONF.consul.agent_manage, DEFAULT_CONSUL_PORT) |
| 62 |
self.agents['manage'] = ConsulAgent('manage', addr, port) |
| 63 |
if CONF.consul.agent_tenant: |
| 64 |
addr, port = netutils.parse_host_port( |
| 65 |
CONF.consul.agent_tenant, DEFAULT_CONSUL_PORT) |
| 66 |
self.agents['tenant'] = ConsulAgent('tenant', addr, port) |
| 67 |
if CONF.consul.agent_storage: |
| 68 |
addr, port = netutils.parse_host_port( |
| 69 |
CONF.consul.agent_storage, DEFAULT_CONSUL_PORT) |
| 70 |
self.agents['storage'] = ConsulAgent('storage', addr, port) |
| 71 |
|
| 72 |
def valid_agents(self, sequence): |
| 73 |
for name in sequence: |
| 74 |
if self.agents.get(name) is None: |
| 75 |
raise ConsulAgentNotExist(cluster=name) |
| 76 |
|
| 77 |
def get_health(self, sequence): |
| 78 |
hosts_health = {} |
| 79 |
all_agents = [] |
| 80 |
for name in sequence: |
| 81 |
if name in self.agents: |
| 82 |
consul_agent = self.agents.get(name) |
| 83 |
agent_health = consul_agent.get_health() |
| 84 |
hosts_health[name] = agent_health |
| 85 |
if not all_agents: |
| 86 |
all_agents = agent_health.keys() |
| 87 |
|
| 88 |
sequence_hosts_health = {} |
| 89 |
for host in all_agents: |
| 90 |
sequence_hosts_health[host] = [] |
| 91 |
for name in sequence: |
| 92 |
if name in self.agents: |
| 93 |
state = hosts_health[name].get(host) |
| 94 |
if state: |
| 95 |
sequence_hosts_health[host].append(state) |
| 96 |
else: |
| 97 |
continue |
| 98 |
|
| 99 |
return sequence_hosts_health |
| 100 |
|
| 101 |
|
| 102 |
class ConsulAgent(object): |
| 103 |
"""Agent to consul cluster""" |
| 104 |
|
| 105 |
def __init__(self, name, addr=None, port=None): |
| 106 |
self.name = name |
| 107 |
self.addr = addr |
| 108 |
self.port = port |
| 109 |
# connection to consul cluster |
| 110 |
self.cluster = consul.Consul(host=addr, port=self.port) |
| 111 |
|
| 112 |
def get_agents(self): |
| 113 |
try: |
| 114 |
members = self.cluster.agent.members() |
| 115 |
except Exception as e: |
| 116 |
raise ConsulGetMembersException(cluster=self.name, err=str(e)) |
| 117 |
|
| 118 |
return members |
| 119 |
|
| 120 |
def get_health(self): |
| 121 |
agents_health = {} |
| 122 |
agents = self.get_agents() |
| 123 |
for agent in agents: |
| 124 |
host = agent.get('Name') |
| 125 |
status = agent.get('Status') |
| 126 |
if status == 1: |
| 127 |
agents_health[host] = 'up' |
| 128 |
else: |
| 129 |
agents_health[host] = 'down' |
| 130 |
|
| 131 |
return agents_health |