-
Notifications
You must be signed in to change notification settings - Fork 0
/
capacity.py
142 lines (131 loc) · 5.04 KB
/
capacity.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/python
import os
from novaclient.client import Client
# Print member variable of one class
def getAllAttrs(obj):
strAttrs = ''
for o in dir(obj):
strAttrs =strAttrs + o + ' := ' + str(getattr(obj,o)) + '\n'
return strAttrs;
def get_nova_credentials_v2():
d = {}
d['version'] = '2'
d['username'] = os.environ['OS_USERNAME']
d['api_key'] = os.environ['OS_PASSWORD']
d['auth_url'] = os.environ['OS_AUTH_URL']
d['project_id'] = os.environ['OS_TENANT_NAME']
d['cacert']=os.environ['OS_CACERT']
return d
def servers_usage(hypervisors):
servers = nova_client.servers.list()
print "% 30s% 10s % 10s% 10s% 30s" % ('Name', 'CPU', 'RAM', 'Disk', 'hypervisor_hostname')
total_vcpu = 0
total_ram = 0
total_disk = 0
for s in servers:
fid = s.flavor['id']
flavor = nova_client.flavors.get(fid)
total_vcpu += flavor.vcpus
total_ram += flavor.ram
total_disk += flavor.disk
host = getattr(s, "OS-EXT-SRV-ATTR:hypervisor_hostname")
if host:
s.flavor_vcpus = flavor.vcpus
if host not in hypervisors:
hypervisors[host] = {'servers': [s]}
else:
hypervisors[host]['servers'].append(s)
print "% 30s% 10d % 10d% 10d% 30s" % (s.name, flavor.vcpus, flavor.ram, flavor.disk, host)
else:
print "% 30s% 10d % 10d% 10d" % (s.name, flavor.vcpus, flavor.ram, flavor.disk)
print "% 30s% 10d % 10d% 10d" % ('Total', total_vcpu, total_ram, total_disk)
return
def short_name(host):
nx = host.split('.')[0].split('-')
return nx[1] + "-" + nx[2]
def cpu_layout(hypervisors):
hosts = hypervisors.keys()
hosts.sort(compare_name)
line = '%-30s' % 'CPU Layout:'
for host in hosts:
line += "% 10s" % short_name(host)
print line
for host, values in hypervisors.items():
stat_hv_t = stat_hv_u = stat_own_u = 0
if 'hv' in values:
stat_hv_t = values['hv'].vcpus
stat_hv_u = values['hv'].vcpus_used
if 'servers' in values:
idx = hosts.index(host)
for s in values['servers']:
print '% 30s' % s.name + ' ' * (10 * idx) + '% 10s' % s.flavor_vcpus
stat_own_u += s.flavor_vcpus
hypervisors[host]['stat'] = (stat_hv_t, stat_hv_u, stat_own_u)
print '-' * (30 + 10 * len(hosts))
line1 = '% 30s' % 'Total:'
line2 = '% 30s' % 'Idle:'
for host in hosts:
(stat_hv_t, stat_hv_u, stat_own_u) = hypervisors[host]['stat']
if stat_hv_u == stat_own_u:
stat_str = "%d/%d" % (stat_hv_u, stat_hv_t)
else:
stat_str = "%d(%d)/%d" % (stat_hv_u, stat_own_u, stat_hv_t)
line1 += "% 10s" % stat_str
line2 += "% 10d" % (stat_hv_t - stat_hv_u)
print line1
print line2
def compare_hv(x, y):
return compare_name(x.hypervisor_hostname, y.hypervisor_hostname)
def compare_name(x, y):
nx = x.split('.')[0].split('-')
ny = y.split('.')[0].split('-')
nx_shelf = int(nx[1])
nx_slot = int(nx[2])
ny_shelf = int(ny[1])
ny_slot = int(ny[2])
if nx_shelf == ny_shelf:
if nx_slot > ny_slot:
return 1
elif nx_slot == ny_slot:
return 0
else:
return -1
elif nx_shelf > ny_shelf:
return 1
else:
return -1
def hypervisors_usage(hypervisors):
hvs = nova_client.hypervisors.list(detailed=True)
hvs.sort(compare_hv)
_vcpu_t = _vcpu_u = _vcpu_l = 0
_mem_t = _mem_u = _mem_l = 0
_disk_t = _disk_u = _disk_l = 0
print "% 25s% 10s% 10s% 10s% 10s% 10s% 10s% 10s% 10s% 10s" % ('Name', 'CPU_T', 'CPU_U', '*CPU_L', 'MEM_T', 'MEM_U', '*MEM_L', 'Disk_T', 'Disk_U', '*Disk_L')
for hv in hvs:
if hv.hypervisor_hostname not in hypervisors:
hypervisors[hv.hypervisor_hostname] = {'hv': hv}
else:
hypervisors[hv.hypervisor_hostname]['hv'] = hv
_vcpu_t += hv.vcpus
_vcpu_u += hv.vcpus_used
_vcpu_l += (hv.vcpus-hv.vcpus_used)
_mem_t += hv.memory_mb
_mem_u += hv.memory_mb_used
_mem_l += (hv.memory_mb-hv.memory_mb_used)
_disk_t += hv.local_gb
_disk_u += hv.local_gb_used
_disk_l += (hv.local_gb-hv.local_gb_used)
print "% 25s% 10d% 10d% 10d% 10d% 10d% 10d% 10d% 10d% 10d" % (
hv.hypervisor_hostname, hv.vcpus, hv.vcpus_used, hv.vcpus-hv.vcpus_used,
hv.memory_mb, hv.memory_mb_used, hv.memory_mb-hv.memory_mb_used,
hv.local_gb, hv.local_gb_used, hv.local_gb-hv.local_gb_used)
print "% 25s% 10d% 10d% 10d% 10d% 10d% 10d% 10d% 10d% 10d" % (
'Total', _vcpu_t, _vcpu_u, _vcpu_l, _mem_t, _mem_u, _mem_l, _disk_t, _disk_u, _disk_l)
hypervisors = {}
credentials = get_nova_credentials_v2()
nova_client = Client(**credentials)
servers_usage(hypervisors)
print "\n"
hypervisors_usage(hypervisors)
print "\n"
cpu_layout(hypervisors)