6
6
import openstack
7
7
8
8
9
- def get_capacity_per_flavor (conn , flavors ):
9
+ def get_capacity_per_flavor (placement_client , flavors ):
10
10
capacity_per_flavor = {}
11
11
12
12
for flavor in flavors :
13
13
resources , traits = get_placement_request (flavor )
14
- max_per_host = get_max_per_host (conn , resources , traits )
14
+ max_per_host = get_max_per_host (placement_client , resources , traits )
15
15
capacity_per_flavor [flavor .name ] = max_per_host
16
16
17
17
return capacity_per_flavor
@@ -27,20 +27,22 @@ def get_placement_request(flavor):
27
27
if "resources:" == key [:10 ]:
28
28
count = int (value )
29
29
resources [key [10 :]] = count
30
+ if "hw:cpu_policy" == key and value == "dedicated" :
31
+ resources ["PCPU" ] = flavor .vcpus
30
32
if "PCPU" not in resources .keys () and "VCPU" not in resources .keys ():
31
33
resources ["VCPU" ] = flavor .vcpus
32
34
return resources , required_traits
33
35
34
36
35
- def get_max_per_host (conn , resources , required_traits ):
37
+ def get_max_per_host (placement_client , resources , required_traits ):
36
38
resource_str = "," .join (
37
39
[key + ":" + str (value ) for key , value in resources .items () if value ]
38
40
)
39
41
required_str = "," .join (required_traits )
40
42
# TODO(johngarbut): remove disabled!
41
43
forbidden_str = "COMPUTE_STATUS_DISABLED"
42
44
43
- response = conn . placement .get (
45
+ response = placement_client .get (
44
46
"/allocation_candidates" ,
45
47
params = {"resources" : resource_str , "required" : required_str },
46
48
headers = {"OpenStack-API-Version" : "placement 1.29" },
@@ -63,11 +65,9 @@ def get_max_per_host(conn, resources, required_traits):
63
65
return count_per_rp
64
66
65
67
66
- def main ():
67
- conn = openstack .connect ()
68
-
69
- flavors = list (conn .compute .flavors ())
70
- capacity_per_flavor = get_capacity_per_flavor (conn , flavors )
68
+ def print_exporter_data (app ):
69
+ flavors = list (app .compute_client .flavors ())
70
+ capacity_per_flavor = get_capacity_per_flavor (app .placement_client , flavors )
71
71
72
72
# total capacity per flavor
73
73
flavor_names = sorted ([f .name for f in flavors ])
@@ -77,17 +77,16 @@ def main():
77
77
print (f'openstack_total_capacity_per_flavor{{flavor="{ flavor_name } "}} { total } ' )
78
78
79
79
# capacity per host
80
- raw_rps = list (conn . placement .resource_providers ())
80
+ raw_rps = list (app . placement_client .resource_providers ())
81
81
resource_providers = {rp .name : rp .id for rp in raw_rps }
82
82
hostnames = sorted (resource_providers .keys ())
83
83
for hostname in hostnames :
84
84
rp_id = resource_providers [hostname ]
85
85
for flavor_name in flavor_names :
86
86
all_counts = capacity_per_flavor .get (flavor_name , {})
87
87
our_count = all_counts .get (rp_id , 0 )
88
+ if our_count == 0 :
89
+ continue
88
90
print (
89
91
f'openstack_capacity_by_hostname{{hypervisor="{ hostname } ",flavor="{ flavor_name } "}} { our_count } '
90
92
)
91
-
92
-
93
- main ()
0 commit comments