useful_scripts/check_openstack.py

77 lines
1.8 KiB
Python
Raw Normal View History

#!/usr/bin/env python
import os
import requests
import yaml
2022-11-03 11:34:34 +00:00
import argparse
from novaclient.client import Client as nova_auth
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']
creds['version'] = '2'
return creds
2022-11-03 11:34:34 +00:00
def get_auth_token(creds):
keystone_client = keystone_auth(**creds)
return keystone_client.auth_token
2022-11-03 11:34:34 +00:00
def get_servers(creds):
nova_client = nova_auth(**creds)
print(nova_client.servers.list())
2022-11-03 11:34:34 +00:00
def get_cores(creds):
url="http://placement.example.com:8778"
2022-11-03 11:34:34 +00:00
token = get_auth_token(creds)
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 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)
get_servers(creds)
get_cores(creds)
if __name__ == '__main__':
2022-11-03 11:34:34 +00:00
main(_parse_args())