2022-11-03 11:08:06 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
import os
|
|
|
|
import requests
|
2022-11-03 11:17:18 +00:00
|
|
|
import yaml
|
2022-11-03 11:34:34 +00:00
|
|
|
import argparse
|
2022-11-03 11:08:06 +00:00
|
|
|
|
|
|
|
from keystoneclient.v3.client import Client as keystone_auth
|
|
|
|
|
2022-11-03 11:34:34 +00:00
|
|
|
def get_credentials(site_id):
|
|
|
|
home = os.environ['HOME']
|
|
|
|
with open('{}/clouds.yaml'.format(home),'r') as stream:
|
|
|
|
creds = yaml.safe_load(stream)['clouds'][site_id]['auth']
|
2022-11-03 11:08:06 +00:00
|
|
|
|
2022-11-03 11:17:18 +00:00
|
|
|
creds['version'] = '2'
|
|
|
|
return creds
|
2022-11-03 11:08:06 +00:00
|
|
|
|
2022-11-03 11:50:08 +00:00
|
|
|
def get_keystoneclient(creds):
|
|
|
|
return keystone_auth(**creds)
|
|
|
|
|
2022-11-03 11:34:34 +00:00
|
|
|
def get_auth_token(creds):
|
2022-11-03 16:59:58 +00:00
|
|
|
keystone_client = get_keystoneclient(creds)
|
2022-11-03 11:08:06 +00:00
|
|
|
|
|
|
|
return keystone_client.auth_token
|
|
|
|
|
2022-11-03 16:59:58 +00:00
|
|
|
def get_servers(keystone,token):
|
2022-11-03 11:08:06 +00:00
|
|
|
|
2022-11-03 16:59:58 +00:00
|
|
|
url = keystone.service_catalog.get_endpoints(service_name="nova",endpoint_type="public")['compute'][0]['url']
|
2022-11-03 11:08:06 +00:00
|
|
|
|
2022-11-03 16:59:58 +00:00
|
|
|
headers = {}
|
|
|
|
headers['content-type'] = 'application/json'
|
|
|
|
headers['x-auth-token'] = token
|
|
|
|
|
|
|
|
request = "/servers"
|
|
|
|
|
|
|
|
r = requests.get("{}/{}".format(url,request),headers=headers)
|
|
|
|
|
|
|
|
servers = r.json()['servers']
|
|
|
|
|
|
|
|
for server in servers:
|
|
|
|
print("id: {}".format(server['id']))
|
|
|
|
|
|
|
|
|
|
|
|
def get_cores(keystone,token):
|
2022-11-03 11:08:06 +00:00
|
|
|
|
2022-11-03 11:50:08 +00:00
|
|
|
url = keystone.service_catalog.get_endpoints(service_name="placement",endpoint_type="public")['placement'][0]['url']
|
2022-11-03 11:08:06 +00:00
|
|
|
|
|
|
|
headers = {}
|
|
|
|
headers['content-type'] = 'application/json'
|
|
|
|
headers['x-auth-token'] = token
|
|
|
|
|
|
|
|
request = "/resource_providers"
|
|
|
|
|
|
|
|
r = requests.get("{}/{}".format(url,request),headers=headers)
|
|
|
|
|
|
|
|
for res in r.json()['resource_providers']:
|
|
|
|
uuid = res['uuid']
|
|
|
|
hostname = res['name']
|
|
|
|
|
|
|
|
request = "/resource_providers/{}/inventories".format(uuid)
|
|
|
|
|
|
|
|
r = requests.get("{}/{}".format(url,request),headers=headers)
|
|
|
|
|
|
|
|
inventory = r.json()['inventories']
|
|
|
|
|
|
|
|
cores=0
|
|
|
|
|
|
|
|
if 'VCPU' in inventory:
|
|
|
|
cores+=inventory['VCPU']['total']
|
|
|
|
if 'PCPU' in inventory:
|
|
|
|
cores+=inventory['PCPU']['total']
|
|
|
|
|
|
|
|
print("{}: {}".format(hostname,cores))
|
|
|
|
|
2022-11-03 16:59:58 +00:00
|
|
|
def get_networks(keystone,token):
|
2022-11-03 16:22:59 +00:00
|
|
|
|
|
|
|
url = keystone.service_catalog.get_endpoints(service_name="neutron",endpoint_type="public")['network'][0]['url']
|
|
|
|
|
|
|
|
headers = {}
|
|
|
|
headers['content-type'] = 'application/json'
|
|
|
|
headers['x-auth-token'] = token
|
|
|
|
|
|
|
|
data = {}
|
|
|
|
|
|
|
|
request = "networks"
|
|
|
|
|
|
|
|
r = requests.get("{}/v2.0/{}".format(url,request),headers=headers)
|
|
|
|
|
|
|
|
networks = r.json()['networks']
|
|
|
|
|
|
|
|
for network in networks:
|
2022-11-03 16:59:58 +00:00
|
|
|
print("id: {}".format(network['id']))
|
2022-11-03 16:22:59 +00:00
|
|
|
|
2022-11-03 11:34:34 +00:00
|
|
|
def _parse_args():
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument('-s', '--site', metavar="<site-id>",
|
|
|
|
help="Site to run against", dest="site_id",
|
|
|
|
required=True)
|
|
|
|
return parser.parse_args()
|
|
|
|
|
|
|
|
def main(args):
|
|
|
|
creds = get_credentials(args.site_id)
|
|
|
|
|
2022-11-03 16:59:58 +00:00
|
|
|
keystone = get_keystoneclient(creds)
|
|
|
|
token = keystone.auth_token
|
|
|
|
|
|
|
|
get_servers(keystone,token)
|
|
|
|
get_cores(keystone,token)
|
|
|
|
get_networks(keystone,token)
|
2022-11-03 11:08:06 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2022-11-03 11:34:34 +00:00
|
|
|
main(_parse_args())
|