def __init__(self): self.keystoneclient = KeystoneClient() self.endpoint = self.keystoneclient.get_endpoint( service_type='network', endpoint_type=None) self.novaclient = NovaClient() self.neutronclient = NeutronClient(self.endpoint, self.keystoneclient.get_token())
import logging import os from clients.nova import Client __author__ = 'lto' PATH = os.path.abspath(os.path.join(os.path.dirname(os.path.realpath(__file__)), os.pardir)) if __name__ == '__main__': logging.basicConfig(format='%(asctime)s_%(process)d:%(pathname)s:%(lineno)d [%(levelname)s] %(message)s', level=logging.DEBUG) logger = logging.getLogger("EMMLogger") handler = logging.FileHandler('%s/logs/%s.log' % (PATH, os.path.basename(__file__))) handler.setLevel(logging.DEBUG) formatter = logging.Formatter('%(asctime)s_%(process)d:%(pathname)s:%(lineno)d [%(levelname)s] %(message)s') handler.setFormatter(formatter) logger.addHandler(handler) cl = Client() cl.list_servers()
class Cloud(object): def __init__(self): #self.keystoneclient = KeystoneClient() #self.endpoint = self.keystoneclient.get_endpoint(service_type='network', endpoint_type=None) self.novaclient = NovaClient() self.neutronclient = NeutronClient() def get_vm_host_infos(self, vm_id, cloud_info): for hk, hv in cloud_info.items(): for uk in hv['servers']: if uk.get('id') == vm_id: return uk return -1 def read_hypervisor_info(self): host_info = {} hypervisors = self.novaclient.get_hypervisors() for hypervisor in hypervisors: host_info[hypervisor.hypervisor_hostname] = {} # host_info[hypervisor.id]['all'] = hypervisor._info host_info[hypervisor.hypervisor_hostname]['id'] = hypervisor.id host_info[ hypervisor.hypervisor_hostname]['ip'] = hypervisor.host_ip host_info[hypervisor.hypervisor_hostname][ 'instances'] = hypervisor.running_vms host_info[hypervisor. hypervisor_hostname]['cpu_used'] = hypervisor.vcpus_used host_info[ hypervisor.hypervisor_hostname]['cpu_total'] = hypervisor.vcpus host_info[hypervisor.hypervisor_hostname][ 'ram_used'] = hypervisor.memory_mb_used host_info[hypervisor. hypervisor_hostname]['ram_total'] = hypervisor.memory_mb logging.info('Reading info of all hypervisors %s', host_info) return host_info def get_hypervisor_ip(self, hyp_hostname): hypervisors = self.novaclient.get_hypervisors() for hypervisor in hypervisors: if hypervisor.hypervisor_hostname == hyp_hostname: return hypervisor.host_ip def read_server_info(self): server_info = {} servers = self.novaclient.get_servers() for server in servers: server_info[server.id] = {} server_info[server.id] = server._info logging.info('Reading info of all servers %s', server_info) return server_info def get_server_ips(self): servers = self.novaclient.get_servers() ips = {} for server in servers: ips[server.id] = self.get_server_ip(server) logging.info('All server IPs %s', ips) return ips def get_neutron_port(self, ip): port = self.neutronclient.get_ports(ip) logging.info('Getting Neutron port ID %s for IP %s', port, ip) return port """ Format of hypervisor_qos [[[u'uuid', u'1ce97624-e065-4999-a6bc-a980b1fb3b28'], [u'map', [[0, [u'uuid', u'8462e645-6198-4964-984b-004136a5ed72']]]], u'linux-htb'], [[u'uuid', u'52a89cf1-e45a-4c02-b823-769d5be18d13'], [u'map', [[0, [u'uuid', u'69f2dedc-cee9-44d3-80bb-f36f67b5f2ea']]]], u'linux-htb'], [[u'uuid', u'82d072f1-5842-46c5-a879-4f27f265bd79'], [u'map', [[0, [u'uuid', u'c4584bf9-a616-4396-a1ef-b40a18527f61']]]], u'linux-htb'], [[u'uuid', u'cbe550cd-fbdc-4a09-a0e7-525bca5c5313'], [u'map', [[0, [u'uuid', u'75f7025e-a3fd-4220-8a9f-bc24fe0715a5']]]], u'linux-htb'], [[u'uuid', u'f807a246-4064-4577-806e-76dfe623dda0'], [u'map', [[1, [u'uuid', u'ed87baa7-1f20-43fa-96d3-8347592153f5']]]], u'linux-htb'], [[u'uuid', u'9cd461b3-f9b6-49b9-bac4-c71c19d1d2d0'], [u'map', [[1, [u'uuid', u'ecfcb9ce-7f6d-4e29-9361-4faec1ac45bf']]]], u'linux-htb']] Format of queues: [[[u'uuid', u'69f2dedc-cee9-44d3-80bb-f36f67b5f2ea'], [u'set', []], [u'map', []], [u'map', [[u'max-rate', u'50000000'], [u'min-rate', u'10000000']]]], [[u'uuid', u'c4584bf9-a616-4396-a1ef-b40a18527f61'], [u'set', []], [u'map', []], [u'map', [[u'max-rate', u'5000000'], [u'min-rate', u'1000000']]]], [[u'uuid', u'8462e645-6198-4964-984b-004136a5ed72'], [u'set', []], [u'map', []], [u'map', [[u'max-rate', u'10000000000'], [u'min-rate', u'100000000']]]], [[u'uuid', u'75f7025e-a3fd-4220-8a9f-bc24fe0715a5'], [u'set', []], [u'map', []], [u'map', [[u'max-rate', u'10000000000'], [u'min-rate', u'100000000']]]], [[u'uuid', u'ecfcb9ce-7f6d-4e29-9361-4faec1ac45bf'], [u'set', []], [u'map', []], [u'map', [[u'max-rate', u'60000000'], [u'min-rate', u'10000000']]]], [[u'uuid', u'ed87baa7-1f20-43fa-96d3-8347592153f5'], [u'set', []], [u'map', []], [u'map', [[u'max-rate', u'6000000'], [u'min-rate', u'1000000']]]]] """ def get_qos_queue(self, qos_id, queues, hypervisor_qos): qos = {'queues': []} for qoi in hypervisor_qos: match = 0 if type(qoi) == unicode: qos['type'] = qoi else: for li in qoi: if li[1] == qos_id: match = 1 if li[0] == 'uuid': qos['qos_uuid'] = li[1] elif match: match = 0 for item_inner in li: if type(item_inner) == list: for queue in item_inner: queue_info = {} logging.info("queue1 %s", list(enumerate(queue[1]))) for queue_inner in queue[1]: if queue_inner != 'uuid': logging.info( "queue_inner %s", list(enumerate(queue_inner))) #queue_info['qos_uuid'] = queue_inner for qui in queues: if qui[0][0] == 'uuid': if qui[0][ 1] == queue_inner: logging.info( 'Get Queue Rates %s', qui) queue_info = self.get_queue_rates( qui) queue_info[ 'id'] = queue[0] qos['queues'].append( queue_info) logging.info('Getting OVS queue for QoS ID %s: %s', qos_id, qos) return qos @staticmethod def get_server_hypervisor_info(servers, hostname): server_match = [] for server in servers.values(): if server['OS-EXT-SRV-ATTR:hypervisor_hostname'] == hostname: server_match.append(server['id']) logging.info('Getting servers for matching hypervisor %s: %s', hostname, server_match) return server_match """ Get queue rates of output: [u'uuid', u'ed87baa7-1f20-43fa-96d3-8347592153f5'], [u'set', []], [u'map', []], [u'map', [[u'max-rate', u'6000000'], [u'min-rate', u'1000000']]]] """ @staticmethod def get_queue_rates(queue): queue_rates = {} for item in queue: for li in item: if item[0] == 'uuid': queue_rates['queue_uuid'] = item[1] if li == 'map': for item_inner in item: if type(item_inner) == list and len(item_inner) > 0: for rate_inner in item_inner: if rate_inner[0] == 'max-rate': queue_rates['rates'] = {} queue_rates['rates'][ 'max-rate'] = rate_inner[1] if rate_inner[0] == 'min-rate': queue_rates['rates'][ 'min-rate'] = rate_inner[1] logging.info('Queue port rates: %s', queue_rates) return queue_rates @staticmethod def get_server_ip(server): ips = [] if hasattr(server, 'addresses'): for interface in server.addresses.values(): ips.append(interface[0]['addr']) return ips @staticmethod def get_port_id(interfaces, server_port): end = re.search(server_port, interfaces).start() start = end - 75 ovs_port = re.findall("(qvo.*?[^\'])\"", interfaces[start:end]) logging.info('Getting OVS port: %s, for Neutron Port ID: %s', ovs_port, server_port) return ovs_port @staticmethod def get_ovs_port_num(port_desc, ovs_port_id): port_ar = [] end = port_desc.find(ovs_port_id) count = end if end != -1: while port_desc[count] != " ": count -= 1 else: for el in range(count + 1, end - 1): port_ar.append(port_desc[el]) else: return "-1" port_number = ''.join(port_ar) logging.info('Port Number is: %s, for OVS Port ID: %s', port_number, ovs_port_id) return port_number def get_ips(self, server_string): ips = server_string['addresses'] result = [] for addr in ips.values(): for private in addr: if private['OS-EXT-IPS:type'] == "fixed": result.append(private['addr']) return result def get_new_server(self, hypervisor_id, server_name): servers = self.read_server_info() target_server = {} for server in servers.values(): if server[ 'OS-EXT-SRV-ATTR:hypervisor_hostname'] == hypervisor_id and server[ 'name'] == server_name: target_server['id'] = server.get('id') target_server['name'] = server.get('name') target_server['interfaces'] = [] server_ips = self.get_ips(server) for ip in server_ips: interface = {} interface['ip'] = ip neutron_port = self.get_neutron_port(ip) hypervisors = self.read_hypervisor_info() host = Host(hypervisor_id) hypervisor_ip = self.get_hypervisor_ip(hypervisor_id) hypervisor_interfaces = host.list_interfaces_hypervisor( hypervisors) ovs_port_id = self.get_port_id(hypervisor_interfaces, neutron_port)[0] port_desc = host.list_port_desc_hypervisor(hypervisor_ip) interface['ovs_port_number'] = self.get_ovs_port_num( port_desc, ovs_port_id) qos_id = host.get_port_qos(hypervisor_ip, ovs_port_id) queues = host.list_queues_hypervisor(hypervisor_ip) qoss = host.list_qos_hypervisor(hypervisor_ip) interface['qos'] = self.get_qos_queue(qos_id, queues, qoss) target_server['interfaces'].append(interface) return target_server
def __init__(self): #self.keystoneclient = KeystoneClient() #self.endpoint = self.keystoneclient.get_endpoint(service_type='network', endpoint_type=None) self.novaclient = NovaClient() self.neutronclient = NeutronClient()
class Cloud(object): def __init__(self): #self.keystoneclient = KeystoneClient() #self.endpoint = self.keystoneclient.get_endpoint(service_type='network', endpoint_type=None) self.novaclient = NovaClient() self.neutronclient = NeutronClient() def get_vm_host_infos(self, vm_id, cloud_info): for hk, hv in cloud_info.items(): for uk in hv['servers']: if uk.get('id') == vm_id: return uk return -1 def read_hypervisor_info(self): host_info = {} hypervisors = self.novaclient.get_hypervisors() for hypervisor in hypervisors: host_info[hypervisor.hypervisor_hostname] = {} # host_info[hypervisor.id]['all'] = hypervisor._info host_info[hypervisor.hypervisor_hostname]['id'] = hypervisor.id host_info[hypervisor.hypervisor_hostname]['ip'] = hypervisor.host_ip host_info[hypervisor.hypervisor_hostname]['instances'] = hypervisor.running_vms host_info[hypervisor.hypervisor_hostname]['cpu_used'] = hypervisor.vcpus_used host_info[hypervisor.hypervisor_hostname]['cpu_total'] = hypervisor.vcpus host_info[hypervisor.hypervisor_hostname]['ram_used'] = hypervisor.memory_mb_used host_info[hypervisor.hypervisor_hostname]['ram_total'] = hypervisor.memory_mb logging.info('Reading info of all hypervisors %s', host_info) return host_info def get_hypervisor_ip(self, hyp_hostname): hypervisors = self.novaclient.get_hypervisors() for hypervisor in hypervisors: if hypervisor.hypervisor_hostname == hyp_hostname: return hypervisor.host_ip def read_server_info(self): server_info = {} servers = self.novaclient.get_servers() for server in servers: server_info[server.id] = {} server_info[server.id] = server._info logging.info('Reading info of all servers %s', server_info) return server_info def get_server_ips(self): servers = self.novaclient.get_servers() ips = {} for server in servers: ips[server.id] = self.get_server_ip(server) logging.info('All server IPs %s', ips) return ips def get_neutron_port(self, ip): port = self.neutronclient.get_ports(ip) logging.info('Getting Neutron port ID %s for IP %s', port, ip) return port """ Format of hypervisor_qos [[[u'uuid', u'1ce97624-e065-4999-a6bc-a980b1fb3b28'], [u'map', [[0, [u'uuid', u'8462e645-6198-4964-984b-004136a5ed72']]]], u'linux-htb'], [[u'uuid', u'52a89cf1-e45a-4c02-b823-769d5be18d13'], [u'map', [[0, [u'uuid', u'69f2dedc-cee9-44d3-80bb-f36f67b5f2ea']]]], u'linux-htb'], [[u'uuid', u'82d072f1-5842-46c5-a879-4f27f265bd79'], [u'map', [[0, [u'uuid', u'c4584bf9-a616-4396-a1ef-b40a18527f61']]]], u'linux-htb'], [[u'uuid', u'cbe550cd-fbdc-4a09-a0e7-525bca5c5313'], [u'map', [[0, [u'uuid', u'75f7025e-a3fd-4220-8a9f-bc24fe0715a5']]]], u'linux-htb'], [[u'uuid', u'f807a246-4064-4577-806e-76dfe623dda0'], [u'map', [[1, [u'uuid', u'ed87baa7-1f20-43fa-96d3-8347592153f5']]]], u'linux-htb'], [[u'uuid', u'9cd461b3-f9b6-49b9-bac4-c71c19d1d2d0'], [u'map', [[1, [u'uuid', u'ecfcb9ce-7f6d-4e29-9361-4faec1ac45bf']]]], u'linux-htb']] Format of queues: [[[u'uuid', u'69f2dedc-cee9-44d3-80bb-f36f67b5f2ea'], [u'set', []], [u'map', []], [u'map', [[u'max-rate', u'50000000'], [u'min-rate', u'10000000']]]], [[u'uuid', u'c4584bf9-a616-4396-a1ef-b40a18527f61'], [u'set', []], [u'map', []], [u'map', [[u'max-rate', u'5000000'], [u'min-rate', u'1000000']]]], [[u'uuid', u'8462e645-6198-4964-984b-004136a5ed72'], [u'set', []], [u'map', []], [u'map', [[u'max-rate', u'10000000000'], [u'min-rate', u'100000000']]]], [[u'uuid', u'75f7025e-a3fd-4220-8a9f-bc24fe0715a5'], [u'set', []], [u'map', []], [u'map', [[u'max-rate', u'10000000000'], [u'min-rate', u'100000000']]]], [[u'uuid', u'ecfcb9ce-7f6d-4e29-9361-4faec1ac45bf'], [u'set', []], [u'map', []], [u'map', [[u'max-rate', u'60000000'], [u'min-rate', u'10000000']]]], [[u'uuid', u'ed87baa7-1f20-43fa-96d3-8347592153f5'], [u'set', []], [u'map', []], [u'map', [[u'max-rate', u'6000000'], [u'min-rate', u'1000000']]]]] """ def get_qos_queue(self, qos_id, queues, hypervisor_qos): qos = {'queues': []} for qoi in hypervisor_qos: match = 0 if type(qoi) == unicode: qos['type'] = qoi else: for li in qoi: if li[1] == qos_id: match = 1 if li[0] == 'uuid': qos['qos_uuid'] = li[1] elif match: match = 0 for item_inner in li: if type(item_inner) == list: for queue in item_inner: queue_info = {} logging.info("queue1 %s", list(enumerate(queue[1]))) for queue_inner in queue[1]: if queue_inner != 'uuid': logging.info("queue_inner %s", list(enumerate(queue_inner))) #queue_info['qos_uuid'] = queue_inner for qui in queues: if qui[0][0] == 'uuid': if qui[0][1] == queue_inner: logging.info('Get Queue Rates %s', qui) queue_info = self.get_queue_rates(qui) queue_info['id'] = queue[0] qos['queues'].append(queue_info) logging.info('Getting OVS queue for QoS ID %s: %s', qos_id, qos) return qos @staticmethod def get_server_hypervisor_info(servers, hostname): server_match = [] for server in servers.values(): if server['OS-EXT-SRV-ATTR:hypervisor_hostname'] == hostname: server_match.append(server['id']) logging.info('Getting servers for matching hypervisor %s: %s', hostname, server_match) return server_match """ Get queue rates of output: [u'uuid', u'ed87baa7-1f20-43fa-96d3-8347592153f5'], [u'set', []], [u'map', []], [u'map', [[u'max-rate', u'6000000'], [u'min-rate', u'1000000']]]] """ @staticmethod def get_queue_rates(queue): queue_rates = {} for item in queue: for li in item: if item[0] == 'uuid': queue_rates['queue_uuid'] = item[1] if li == 'map': for item_inner in item: if type(item_inner) == list and len(item_inner) > 0: for rate_inner in item_inner: if rate_inner[0] == 'max-rate': queue_rates['rates'] = {} queue_rates['rates']['max-rate'] = rate_inner[1] if rate_inner[0] == 'min-rate': queue_rates['rates']['min-rate'] = rate_inner[1] logging.info('Queue port rates: %s', queue_rates) return queue_rates @staticmethod def get_server_ip(server): ips = [] if hasattr(server, 'addresses'): for interface in server.addresses.values(): ips.append(interface[0]['addr']) return ips @staticmethod def get_port_id(interfaces, server_port): end = re.search(server_port, interfaces).start() start = end - 75 ovs_port = re.findall("(qvo.*?[^\'])\"", interfaces[start:end]) logging.info('Getting OVS port: %s, for Neutron Port ID: %s', ovs_port, server_port) return ovs_port @staticmethod def get_ovs_port_num(port_desc, ovs_port_id): port_ar = [] end = port_desc.find(ovs_port_id) count = end if end != -1: while port_desc[count] != " ": count -= 1 else: for el in range(count+1, end-1): port_ar.append(port_desc[el]) else: return "-1" port_number = ''.join(port_ar) logging.info('Port Number is: %s, for OVS Port ID: %s', port_number, ovs_port_id) return port_number def get_ips(self,server_string): ips = server_string['addresses'] result = [] for addr in ips.values(): for private in addr: if private['OS-EXT-IPS:type'] == "fixed": result.append(private['addr']) return result def get_new_server(self,hypervisor_id,server_name): servers = self.read_server_info() target_server = {} for server in servers.values(): if server['OS-EXT-SRV-ATTR:hypervisor_hostname'] == hypervisor_id and server['name'] == server_name: target_server['id'] = server.get('id') target_server['name'] = server.get('name') target_server['interfaces'] = [] server_ips = self.get_ips(server) for ip in server_ips: interface = {} interface['ip'] = ip neutron_port = self.get_neutron_port(ip) hypervisors = self.read_hypervisor_info() host = Host(hypervisor_id) hypervisor_ip = self.get_hypervisor_ip(hypervisor_id) hypervisor_interfaces = host.list_interfaces_hypervisor(hypervisors) ovs_port_id = self.get_port_id(hypervisor_interfaces,neutron_port)[0] port_desc = host.list_port_desc_hypervisor(hypervisor_ip) interface['ovs_port_number'] = self.get_ovs_port_num(port_desc,ovs_port_id) qos_id = host.get_port_qos(hypervisor_ip,ovs_port_id) queues = host.list_queues_hypervisor(hypervisor_ip) qoss = host.list_qos_hypervisor(hypervisor_ip) interface['qos'] = self.get_qos_queue(qos_id,queues,qoss) target_server['interfaces'].append(interface) return target_server
#!/usr/bin/python from clients.nova import Client __author__ = 'beb' if __name__ == '__main__': client = Client() hypervisors = client.get_hypervisors() for hypervisor in hypervisors: print hypervisor._info servers = client.get_servers() for server in servers: print server._info
def get_quotas(): novaClient = NovaClient(sys_config.props) return novaClient.get_quotas()
def get_flavors(): novaClient = NovaClient(sys_config.props) return novaClient.get_flavors()
def get_images(): novaClient = NovaClient(sys_config.props) return novaClient.get_images()
class Cloud(object): def __init__(self): self.keystoneclient = KeystoneClient() self.endpoint = self.keystoneclient.get_endpoint( service_type='network', endpoint_type=None) self.novaclient = NovaClient() self.neutronclient = NeutronClient(self.endpoint, self.keystoneclient.get_token()) def read_hypervisor_info(self): host_info = {} hypervisors = self.novaclient.get_hypervisors() for hypervisor in hypervisors: host_info[hypervisor.hypervisor_hostname] = {} # host_info[hypervisor.id]['all'] = hypervisor._info host_info[hypervisor.hypervisor_hostname]['id'] = hypervisor.id host_info[ hypervisor.hypervisor_hostname]['ip'] = hypervisor.host_ip host_info[hypervisor.hypervisor_hostname][ 'instances'] = hypervisor.running_vms host_info[hypervisor. hypervisor_hostname]['cpu_used'] = hypervisor.vcpus_used host_info[ hypervisor.hypervisor_hostname]['cpu_total'] = hypervisor.vcpus host_info[hypervisor.hypervisor_hostname][ 'ram_used'] = hypervisor.memory_mb_used host_info[hypervisor. hypervisor_hostname]['ram_total'] = hypervisor.memory_mb logging.info('Reading info of all hypervisors %s', host_info) return host_info def get_hypervisor_ip(self, hyp_hostname): hypervisors = self.novaclient.get_hypervisors() for hypervisor in hypervisors: if hypervisor.hypervisor_hostname == hyp_hostname: return hypervisor.host_ip def read_server_info(self): server_info = {} servers = self.novaclient.get_servers() for server in servers: server_info[server.id] = {} server_info[server.id] = server._info logging.info('Reading info of all servers %s', server_info) return server_info def get_server_ips(self): servers = self.novaclient.get_servers() ips = {} for server in servers: ips[server.id] = self.get_server_ip(server) logging.info('All server IPs %s', ips) return ips def get_neutron_port(self, ip): port = self.neutronclient.get_ports(ip) logging.info('Getting Neutron port ID %s for IP %s', port, ip) return port def get_qos_queue(self, qos_id, queues, hypervisor_qos): qos = {'queues': {}} for qoi in hypervisor_qos: match = 0 if type(qoi) == unicode: qos['type'] = qoi else: for li in qoi: if li[1] == qos_id: match = 1 if li[0] == 'uuid': qos['uuid'] = li[1] elif match: match = 0 for item_inner in li: if type(item_inner ) == list and item_inner[0][0] == 0: qos['queues'][0] = {} for queue_inner in item_inner[0][1]: if queue_inner != 'uuid': qos['queues'][0]['uuid'] = queue_inner for qui in queues: if qui[0][0] == 'uuid': if qui[0][1] == queue_inner: qos['queues'][ 0] = self.get_queue_rates( qui) logging.info('Getting OVS queue for QoS ID %s: %s', qos_id, qos) return qos @staticmethod def get_server_hypervisor_info(servers, hostname): server_match = [] for server in servers.values(): if server['OS-EXT-SRV-ATTR:hypervisor_hostname'] == hostname: server_match.append(server['id']) logging.info('Getting servers for matching hypervisor %s: %s', hostname, server_match) return server_match @staticmethod def get_queue_rates(queue): queue_rates = {} for item in queue: for li in item: if item[0] == 'uuid': queue_rates['uuid'] = item[1] if li == 'map': for item_inner in item: if type(item_inner) == list and len(item_inner) > 0: for rate_inner in item_inner: if rate_inner[0] == 'max-rate': queue_rates['rates'] = {} queue_rates['rates'][ 'max-rate'] = rate_inner[1] if rate_inner[0] == 'min-rate': queue_rates['rates'][ 'min-rate'] = rate_inner[1] logging.info('Queue port rates: %s', queue_rates) return queue_rates @staticmethod def get_server_ip(server): ips = [] if hasattr(server, 'addresses'): for interface in server.addresses.values(): ips.append(interface[0]['addr']) return ips @staticmethod def get_port_id(interfaces, server_port): end = re.search(server_port, interfaces).start() start = end - 75 ovs_port = re.findall("(qvo.*?[^\'])\"", interfaces[start:end]) logging.info('Getting OVS port: %s, for Neutron Port ID: %s', ovs_port, server_port) return ovs_port
class Cloud(object): def __init__(self): self.keystoneclient = KeystoneClient() self.endpoint = self.keystoneclient.get_endpoint(service_type='network', endpoint_type=None) self.novaclient = NovaClient() self.neutronclient = NeutronClient(self.endpoint, self.keystoneclient.get_token()) def read_hypervisor_info(self): host_info = {} hypervisors = self.novaclient.get_hypervisors() for hypervisor in hypervisors: host_info[hypervisor.hypervisor_hostname] = {} # host_info[hypervisor.id]['all'] = hypervisor._info host_info[hypervisor.hypervisor_hostname]['id'] = hypervisor.id host_info[hypervisor.hypervisor_hostname]['ip'] = hypervisor.host_ip host_info[hypervisor.hypervisor_hostname]['instances'] = hypervisor.running_vms host_info[hypervisor.hypervisor_hostname]['cpu_used'] = hypervisor.vcpus_used host_info[hypervisor.hypervisor_hostname]['cpu_total'] = hypervisor.vcpus host_info[hypervisor.hypervisor_hostname]['ram_used'] = hypervisor.memory_mb_used host_info[hypervisor.hypervisor_hostname]['ram_total'] = hypervisor.memory_mb logging.info('Reading info of all hypervisors %s', host_info) return host_info def get_hypervisor_ip(self, hyp_hostname): hypervisors = self.novaclient.get_hypervisors() for hypervisor in hypervisors: if hypervisor.hypervisor_hostname == hyp_hostname: return hypervisor.host_ip def read_server_info(self): server_info = {} servers = self.novaclient.get_servers() for server in servers: server_info[server.id] = {} server_info[server.id] = server._info logging.info('Reading info of all servers %s', server_info) return server_info def get_server_ips(self): servers = self.novaclient.get_servers() ips = {} for server in servers: ips[server.id] = self.get_server_ip(server) logging.info('All server IPs %s', ips) return ips def get_neutron_port(self, ip): port = self.neutronclient.get_ports(ip) logging.info('Getting Neutron port ID %s for IP %s', port, ip) return port def get_qos_queue(self, qos_id, queues, hypervisor_qos): qos = {'queues': {}} for qoi in hypervisor_qos: match = 0 if type(qoi) == unicode: qos['type'] = qoi else: for li in qoi: if li[1] == qos_id: match = 1 if li[0] == 'uuid': qos['uuid'] = li[1] elif match: match = 0 for item_inner in li: if type(item_inner) == list and item_inner[0][0] == 0: qos['queues'][0] = {} for queue_inner in item_inner[0][1]: if queue_inner != 'uuid': qos['queues'][0]['uuid'] = queue_inner for qui in queues: if qui[0][0] == 'uuid': if qui[0][1] == queue_inner: qos['queues'][0] = self.get_queue_rates(qui) logging.info('Getting OVS queue for QoS ID %s: %s', qos_id, qos) return qos @staticmethod def get_server_hypervisor_info(servers, hostname): server_match = [] for server in servers.values(): if server['OS-EXT-SRV-ATTR:hypervisor_hostname'] == hostname: server_match.append(server['id']) logging.info('Getting servers for matching hypervisor %s: %s', hostname, server_match) return server_match @staticmethod def get_queue_rates(queue): queue_rates = {} for item in queue: for li in item: if item[0] == 'uuid': queue_rates['uuid'] = item[1] if li == 'map': for item_inner in item: if type(item_inner) == list and len(item_inner) > 0: for rate_inner in item_inner: if rate_inner[0] == 'max-rate': queue_rates['rates'] = {} queue_rates['rates']['max-rate'] = rate_inner[1] if rate_inner[0] == 'min-rate': queue_rates['rates']['min-rate'] = rate_inner[1] logging.info('Queue port rates: %s', queue_rates) return queue_rates @staticmethod def get_server_ip(server): ips = [] if hasattr(server, 'addresses'): for interface in server.addresses.values(): ips.append(interface[0]['addr']) return ips @staticmethod def get_port_id(interfaces, server_port): end = re.search(server_port, interfaces).start() start = end - 75 ovs_port = re.findall("(qvo.*?[^\'])\"", interfaces[start:end]) logging.info('Getting OVS port: %s, for Neutron Port ID: %s', ovs_port, server_port) return ovs_port
import logging import os from clients.nova import Client __author__ = 'lto' PATH = os.path.abspath( os.path.join(os.path.dirname(os.path.realpath(__file__)), os.pardir)) if __name__ == '__main__': logging.basicConfig( format= '%(asctime)s_%(process)d:%(pathname)s:%(lineno)d [%(levelname)s] %(message)s', level=logging.DEBUG) logger = logging.getLogger("EMMLogger") handler = logging.FileHandler('%s/logs/%s.log' % (PATH, os.path.basename(__file__))) handler.setLevel(logging.DEBUG) formatter = logging.Formatter( '%(asctime)s_%(process)d:%(pathname)s:%(lineno)d [%(levelname)s] %(message)s' ) handler.setFormatter(formatter) logger.addHandler(handler) cl = Client() cl.list_servers()