From af0d2be35c8a8ec97c33648fe73a01d716776ef2 Mon Sep 17 00:00:00 2001 From: Arif Ali Date: Thu, 3 Nov 2022 11:08:06 +0000 Subject: [PATCH] Add check_openstack.py A sample python script to use REST API and novaclient python module --- check_openstack.py | 71 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 check_openstack.py diff --git a/check_openstack.py b/check_openstack.py new file mode 100644 index 0000000..41286fe --- /dev/null +++ b/check_openstack.py @@ -0,0 +1,71 @@ +#!/usr/bin/env python + +import os +import requests + +from novaclient.client import Client as nova_auth +from keystoneclient.v3.client import Client as keystone_auth + +def get_credentials(): + cred = {} + cred['version'] = '2' + cred['username'] = "admin" + cred['password'] = "openstack" + cred['auth_url'] = "http://10.0.1.216:5000/v3" + cred['project_name'] = "admin" + cred['user_domain_name'] = "admin_domain" + cred['project_domain_name'] = "admin_domain" + + return cred + +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()