diff --git a/check_openstack.py b/check_openstack.py old mode 100644 new mode 100755 index 5b8b511..85e5968 --- a/check_openstack.py +++ b/check_openstack.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 import os import requests @@ -7,6 +7,10 @@ import argparse from keystoneclient.v3.client import Client as keystone_auth +headers = {} +headers['content-type'] = 'application/json' + + def get_credentials(site_id): home = os.environ['HOME'] with open('{}/clouds.yaml'.format(home),'r') as stream: @@ -15,81 +19,66 @@ def get_credentials(site_id): creds['version'] = '2' return creds + def get_keystoneclient(creds): return keystone_auth(**creds) -def get_auth_token(creds): - keystone_client = get_keystoneclient(creds) - - return keystone_client.auth_token -def get_servers(keystone,token): +def get_servers(keystone): url = keystone.service_catalog.get_endpoints(service_name="nova",endpoint_type="public")['compute'][0]['url'] - headers = {} - headers['content-type'] = 'application/json' - headers['x-auth-token'] = token - - request = "/servers" + request = "servers" r = requests.get("{}/{}".format(url,request),headers=headers) - servers = r.json()['servers'] + servers = r.json()[request] for server in servers: - print("id: {}".format(server['id'])) + print("server_id: {}".format(server['id'])) -def get_cores(keystone,token): +def get_cores(keystone): url = keystone.service_catalog.get_endpoints(service_name="placement",endpoint_type="public")['placement'][0]['url'] - headers = {} - headers['content-type'] = 'application/json' - headers['x-auth-token'] = token + psc_request = "resource_providers" - request = "/resource_providers" + r = requests.get("{}/{}".format(url,psc_request),headers=headers) - r = requests.get("{}/{}".format(url,request),headers=headers) + if psc_request in r.json(): + for res in r.json()[psc_request]: + uuid = res['uuid'] + hostname = res['name'] - for res in r.json()['resource_providers']: - uuid = res['uuid'] - hostname = res['name'] - - request = "/resource_providers/{}/inventories".format(uuid) + request = "/{}/{}/inventories".format(psc_request,uuid) - r = requests.get("{}/{}".format(url,request),headers=headers) + r = requests.get("{}/{}".format(url,request),headers=headers) - inventory = r.json()['inventories'] + inventory = r.json()['inventories'] - cores=0 + cores=0 + if 'VCPU' in inventory: + cores+=inventory['VCPU']['total'] + if 'PCPU' in inventory: + cores+=inventory['PCPU']['total'] - if 'VCPU' in inventory: - cores+=inventory['VCPU']['total'] - if 'PCPU' in inventory: - cores+=inventory['PCPU']['total'] + print("{}: {}".format(hostname,cores)) - print("{}: {}".format(hostname,cores)) -def get_networks(keystone,token): +def get_networks(keystone): 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'] + networks = r.json()[request] for network in networks: - print("id: {}".format(network['id'])) + print("network_id: {}".format(network['id'])) + def _parse_args(): parser = argparse.ArgumentParser() @@ -98,15 +87,19 @@ def _parse_args(): required=True) return parser.parse_args() + def main(args): creds = get_credentials(args.site_id) keystone = get_keystoneclient(creds) token = keystone.auth_token - get_servers(keystone,token) - get_cores(keystone,token) - get_networks(keystone,token) + headers['x-auth-token'] = token + + get_servers(keystone) + get_cores(keystone) + get_networks(keystone) + if __name__ == '__main__': - main(_parse_args()) + main(_parse_args()) \ No newline at end of file