@@ -140,18 +140,29 @@ def get_resource_provider_info(compute_client, placement_client):
140
140
return resource_providers , project_to_aggregate
141
141
142
142
143
- def print_host_details (compute_client , placement_client ):
143
+ def get_host_details (compute_client , placement_client ):
144
144
flavors = list (compute_client .flavors ())
145
145
capacity_per_flavor = get_capacity_per_flavor (placement_client , flavors )
146
146
147
147
# total capacity per flavor
148
+ free_by_flavor_total = prom_core .GaugeMetricFamily (
149
+ "openstack_free_capacity_by_flavor_total" ,
150
+ "Free capacity if you fill the cloud full of each flavor" ,
151
+ labels = ["flavor_name" ],
152
+ )
148
153
flavor_names = sorted ([f .name for f in flavors ])
149
154
for flavor_name in flavor_names :
150
155
counts = capacity_per_flavor .get (flavor_name , {}).values ()
151
156
total = 0 if not counts else sum (counts )
152
- print (f'openstack_free_capacity_by_flavor{{flavor="{ flavor_name } "}} { total } ' )
157
+ free_by_flavor_total .add_metric ([flavor_name ], total )
158
+ # print(f'openstack_free_capacity_by_flavor{{flavor="{flavor_name}"}} {total}')
153
159
154
160
# capacity per host
161
+ free_by_flavor_hypervisor = prom_core .GaugeMetricFamily (
162
+ "openstack_free_capacity_hypervisor_by_flavor" ,
163
+ "Free capacity if you fill the cloud full of each flavor" ,
164
+ labels = ["hypervisor" , "flavor_name" , "az_aggregate" , "project_aggregate" ],
165
+ )
155
166
resource_providers , project_to_aggregate = get_resource_provider_info (
156
167
compute_client , placement_client
157
168
)
@@ -165,27 +176,32 @@ def print_host_details(compute_client, placement_client):
165
176
our_count = all_counts .get (rp_id , 0 )
166
177
if our_count == 0 :
167
178
continue
168
- host_str = f'hypervisor="{ hostname } "'
169
- az = rp .get ("az" )
170
- if az :
171
- host_str += f',az="{ az } "'
172
- project_filter = rp .get ("project_filter" )
173
- if project_filter :
174
- host_str += f',project_filter="{ project_filter } "'
175
- print (
176
- f'openstack_free_capacity_by_hypervisor{{{ host_str } ,flavor="{ flavor_name } "}} { our_count } '
179
+ az = rp .get ("az" , "" )
180
+ project_filter = rp .get ("project_filter" , "" )
181
+ free_by_flavor_hypervisor .add_metric (
182
+ [hostname , flavor_name , az , project_filter ], our_count
177
183
)
178
184
free_space_found = True
179
185
if not free_space_found :
180
186
# TODO(johngarbutt) allocation candidates only returns some not all candidates!
181
187
print (f"# WARNING - no free spaces found for { hostname } " )
182
188
189
+ project_filter_aggregates = prom_core .GaugeMetricFamily (
190
+ "openstack_project_filter_aggregate" ,
191
+ "Free capacity if you fill the cloud full of each flavor" ,
192
+ labels = ["project_id" , "aggregate" ],
193
+ )
183
194
for project , names in project_to_aggregate .items ():
184
195
for name in names :
185
- print (
186
- f'openstack_project_filter_aggregate{{project_id="{ project } ",aggregate="{ name } "}} 1'
187
- )
188
- return resource_providers
196
+ project_filter_aggregates .add_metric ([project , name ], 1 )
197
+ # print(
198
+ # f'openstack_project_filter_aggregate{{project_id="{project}",aggregate="{name}"}} 1'
199
+ # )
200
+ return resource_providers , [
201
+ free_by_flavor_total ,
202
+ free_by_flavor_hypervisor ,
203
+ project_filter_aggregates ,
204
+ ]
189
205
190
206
191
207
def print_project_usage (indentity_client , placement_client , compute_client ):
@@ -287,20 +303,16 @@ def collect(self):
287
303
conn = openstack .connect ()
288
304
openstack .enable_logging (debug = True )
289
305
try :
290
- resource_providers = print_host_details (conn .compute , conn .placement )
306
+ resource_providers , host_guages = get_host_details (
307
+ conn .compute , conn .placement
308
+ )
309
+ guages += host_guages
310
+
291
311
print_project_usage (conn .identity , conn .placement , conn .compute )
292
312
print_host_usage (resource_providers , conn .placement )
293
313
except Exception as e :
294
314
print (f"error { e } " )
295
315
296
- gauge = prom_core .GaugeMetricFamily (
297
- "random_number" ,
298
- "A random number generator, I have no better idea" ,
299
- labels = ["randomNum" ],
300
- )
301
- gauge .add_metric (["mine" ], 42 )
302
- guages .append (gauge )
303
-
304
316
end_time = time .perf_counter ()
305
317
duration = end_time - start_time
306
318
print (f"Collect complete { collect_id } it took { duration } seconds" )
0 commit comments