useful_scripts/check_openstack.py

159 lines
3.7 KiB
Python
Raw Permalink Normal View History

2022-11-03 22:06:19 +00:00
#!/usr/bin/env python3
import os
import requests
import yaml
2022-11-03 11:34:34 +00:00
import argparse
from keystoneclient.v3.client import Client as keystone_auth
2022-11-03 22:06:19 +00:00
headers = {}
headers['content-type'] = 'application/json'
endpoint_type = "public"
2022-11-03 22:06:19 +00:00
2022-11-03 11:34:34 +00:00
def get_credentials(site_id):
home = os.environ['HOME']
2022-11-03 22:50:11 +00:00
with open('{}/clouds.yaml'.format(home), 'r') as stream:
clouds = yaml.safe_load(stream)['clouds']
creds = clouds[site_id]['auth']
creds['version'] = clouds[site_id]['identity_api_version']
return creds
2022-11-03 22:06:19 +00:00
def get_keystoneclient(creds):
return keystone_auth(**creds)
2022-11-03 22:06:19 +00:00
def get_servers(keystone):
2022-11-03 22:50:11 +00:00
url = keystone.service_catalog.get_endpoints(
service_name="nova",
endpoint_type=endpoint_type,
2022-11-03 22:50:11 +00:00
)['compute'][0]['url']
2022-11-03 22:06:19 +00:00
request = "servers"
2022-11-03 22:50:11 +00:00
r = requests.get("{}/{}".format(url, request), headers=headers)
2022-11-03 22:06:19 +00:00
servers = r.json()[request]
for server in servers:
2022-11-03 22:06:19 +00:00
print("server_id: {}".format(server['id']))
2022-11-04 14:54:26 +00:00
def get_projects(keystone):
url = keystone.service_catalog.get_endpoints(
service_name="keystone",
endpoint_type=endpoint_type,
)['identity'][0]['url']
request = "projects"
r = requests.get("{}/{}".format(url, request), headers=headers)
projects = r.json()[request]
for project in projects:
print("project_id: {}".format(project['id']))
def get_hosts(keystone):
url = keystone.service_catalog.get_endpoints(
service_name="nova",
endpoint_type=endpoint_type,
)['compute'][0]['url']
request = "os-services"
r = requests.get("{}/{}".format(url, request), headers=headers)
services = r.json()['services']
for service in services:
if service['zone'] == 'nova':
print("host: {}, updated_at: {}".format(
service['host'],
service['updated_at'],
))
2022-11-03 22:06:19 +00:00
def get_cores(keystone):
2022-11-03 22:50:11 +00:00
url = keystone.service_catalog.get_endpoints(
service_name="placement",
endpoint_type=endpoint_type,
2022-11-03 22:50:11 +00:00
)['placement'][0]['url']
2022-11-03 22:06:19 +00:00
psc_request = "resource_providers"
2022-11-03 22:50:11 +00:00
r = requests.get("{}/{}".format(url, psc_request), headers=headers)
2022-11-03 22:06:19 +00:00
if psc_request in r.json():
for res in r.json()[psc_request]:
uuid = res['uuid']
hostname = res['name']
2022-11-03 22:50:11 +00:00
request = "/{}/{}/inventories".format(psc_request, uuid)
r = requests.get("{}/{}".format(url, request), headers=headers)
2022-11-03 22:06:19 +00:00
inventory = r.json()['inventories']
2022-11-03 22:50:11 +00:00
cores = 0
2022-11-03 22:06:19 +00:00
if 'VCPU' in inventory:
2022-11-03 22:50:11 +00:00
cores += inventory['VCPU']['total']
2022-11-03 22:06:19 +00:00
if 'PCPU' in inventory:
2022-11-03 22:50:11 +00:00
cores += inventory['PCPU']['total']
2022-11-03 22:50:11 +00:00
print("{}: {}".format(hostname, cores))
2022-11-03 22:06:19 +00:00
def get_networks(keystone):
2022-11-03 16:22:59 +00:00
2022-11-03 22:50:11 +00:00
url = keystone.service_catalog.get_endpoints(
service_name="neutron",
endpoint_type=endpoint_type,
2022-11-03 22:50:11 +00:00
)['network'][0]['url']
2022-11-03 16:22:59 +00:00
request = "networks"
2022-11-03 22:50:11 +00:00
r = requests.get("{}/v2.0/{}".format(url, request), headers=headers)
2022-11-03 16:22:59 +00:00
2022-11-03 22:06:19 +00:00
networks = r.json()[request]
2022-11-03 16:22:59 +00:00
for network in networks:
2022-11-03 22:06:19 +00:00
print("network_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()
2022-11-03 22:06:19 +00:00
2022-11-03 11:34:34 +00:00
def main(args):
creds = get_credentials(args.site_id)
keystone = get_keystoneclient(creds)
token = keystone.auth_token
2022-11-03 22:06:19 +00:00
headers['x-auth-token'] = token
get_servers(keystone)
get_cores(keystone)
get_networks(keystone)
2022-11-04 14:54:26 +00:00
get_projects(keystone)
get_hosts(keystone)
2022-11-03 22:06:19 +00:00
if __name__ == '__main__':
2022-11-03 22:50:11 +00:00
main(_parse_args())