11#!/usr/bin/env python3 
2+ # Licensed under the Apache License, Version 2.0 (the "License"); you may 
3+ # not use this file except in compliance with the License. You may obtain 
4+ # a copy of the License at 
5+ # 
6+ #      http://www.apache.org/licenses/LICENSE-2.0 
7+ # 
8+ # Unless required by applicable law or agreed to in writing, software 
9+ # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 
10+ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 
11+ # License for the specific language governing permissions and limitations 
12+ # under the License. 
213
3- import  os 
414import  collections 
5- import  json 
15+ import  os 
616import  time 
717import  uuid 
818
@@ -174,12 +184,12 @@ def get_host_details(compute_client, placement_client):
174184        counts  =  capacity_per_flavor .get (flavor .name , {}).values ()
175185        total  =  0  if  not  counts  else  sum (counts )
176186        free_by_flavor_total .add_metric ([flavor .name , str (flavor .is_public )], total )
177-         # print(f'openstack_free_capacity_by_flavor{{flavor="{flavor_name}"}} {total}') 
178187
179188    # capacity per host 
180189    free_by_flavor_hypervisor  =  prom_core .GaugeMetricFamily (
181190        "openstack_free_capacity_hypervisor_by_flavor" ,
182-         "Free capacity for each hypervisor if you fill remaining space full of each flavor" ,
191+         "Free capacity for each hypervisor if you fill " 
192+         "remaining space full of each flavor" ,
183193        labels = ["hypervisor" , "flavor_name" , "az_aggregate" , "project_aggregate" ],
184194    )
185195    resource_providers , project_to_aggregate  =  get_resource_provider_info (
@@ -203,7 +213,8 @@ def get_host_details(compute_client, placement_client):
203213            )
204214            free_space_found  =  True 
205215        if  not  free_space_found :
206-             # TODO(johngarbutt) allocation candidates only returns some not all candidates! 
216+             # TODO(johngarbutt) allocation candidates only returns some, 
217+             # not all candidates! 
207218            print (f"# WARNING - no free spaces found for { hostname }  )
208219
209220    project_filter_aggregates  =  prom_core .GaugeMetricFamily (
@@ -214,9 +225,6 @@ def get_host_details(compute_client, placement_client):
214225    for  project , names  in  project_to_aggregate .items ():
215226        for  name  in  names :
216227            project_filter_aggregates .add_metric ([project , name ], 1 )
217-             # print( 
218-             #    f'openstack_project_filter_aggregate{{project_id="{project}",aggregate="{name}"}} 1' 
219-             # ) 
220228    return  resource_providers , [
221229        free_by_flavor_total ,
222230        free_by_flavor_hypervisor ,
@@ -312,10 +320,6 @@ def get_host_usage(resource_providers, placement_client):
312320    return  [usage_guage , capacity_guage ]
313321
314322
315- def  print_exporter_data (app ):
316-     print_host_free_details (app .compute_client , app .placement_client )
317- 
318- 
319323class  OpenStackCapacityCollector (object ):
320324    def  __init__ (self ):
321325        self .conn  =  openstack .connect ()
@@ -346,25 +350,28 @@ def collect(self):
346350            host_time  =  time .perf_counter ()
347351            host_duration  =  host_time  -  start_time 
348352            print (
349-                 f"1 of 3: host flavor capacity complete for { collect_id } { host_duration }  
353+                 "1 of 3: host flavor capacity complete " 
354+                 f"for { collect_id } { host_duration }  
350355            )
351356
352357            if  not  skip_project_usage :
353358                guages  +=  get_project_usage (conn .identity , conn .placement , conn .compute )
354359                project_time  =  time .perf_counter ()
355360                project_duration  =  project_time  -  host_time 
356361                print (
357-                     f"2 of 3: project usage complete for { collect_id } { project_duration }  
362+                     "2 of 3: project usage complete " 
363+                     f"for { collect_id } { project_duration }  
358364                )
359365            else :
360366                print ("2 of 3: skipping project usage" )
361367
362-             if  not  skip_project_usage :
368+             if  not  skip_host_usage :
363369                guages  +=  get_host_usage (resource_providers , conn .placement )
364370                host_usage_time  =  time .perf_counter ()
365371                host_usage_duration  =  host_usage_time  -  project_time 
366372                print (
367-                     f"3 of 3: host usage complete for { collect_id } { host_usage_duration }  
373+                     "3 of 3: host usage complete for " 
374+                     f"{ collect_id } { host_usage_duration }  
368375                )
369376            else :
370377                print ("3 of 3: skipping host usage" )
@@ -391,4 +398,4 @@ def main():
391398
392399
393400if  __name__  ==  "__main__" :
394-     main ()
401+     main ()
0 commit comments