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:08:06 +00:00
|
|
|
|
|
|
|
from novaclient.client import Client as nova_auth
|
|
|
|
from keystoneclient.v3.client import Client as keystone_auth
|
|
|
|
|
|
|
|
def get_credentials():
|
2022-11-03 11:17:18 +00:00
|
|
|
with open('/home/arif/clouds.yaml','r') as stream:
|
|
|
|
creds = yaml.safe_load(stream)['clouds']['arif-home']['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
|
|
|
|
|
|
|
def get_auth_token():
|
|
|
|
creds = get_credentials()
|
|
|
|
keystone_client = keystone_auth(**creds)
|
|
|
|
|
|
|
|
return keystone_client.auth_token
|
|
|
|
|
|
|
|
def get_servers():
|
|
|
|
creds = get_credentials()
|
|
|
|
nova_client = nova_auth(**creds)
|
|
|
|
|
|
|
|
print(nova_client.servers.list())
|
|
|
|
|
|
|
|
def get_cores():
|
|
|
|
url="http://placement.example.com:8778"
|
|
|
|
|
|
|
|
token = get_auth_token()
|
|
|
|
|
|
|
|
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))
|
|
|
|
|
|
|
|
def main():
|
|
|
|
get_servers()
|
|
|
|
get_cores()
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|