Skip to content

Commit f97eb87

Browse files
committed
Add more host guages
1 parent 17819a8 commit f97eb87

File tree

1 file changed

+36
-24
lines changed

1 file changed

+36
-24
lines changed

os_capacity/prometheus.py

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -140,18 +140,29 @@ def get_resource_provider_info(compute_client, placement_client):
140140
return resource_providers, project_to_aggregate
141141

142142

143-
def print_host_details(compute_client, placement_client):
143+
def get_host_details(compute_client, placement_client):
144144
flavors = list(compute_client.flavors())
145145
capacity_per_flavor = get_capacity_per_flavor(placement_client, flavors)
146146

147147
# 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+
)
148153
flavor_names = sorted([f.name for f in flavors])
149154
for flavor_name in flavor_names:
150155
counts = capacity_per_flavor.get(flavor_name, {}).values()
151156
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}')
153159

154160
# 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+
)
155166
resource_providers, project_to_aggregate = get_resource_provider_info(
156167
compute_client, placement_client
157168
)
@@ -165,27 +176,32 @@ def print_host_details(compute_client, placement_client):
165176
our_count = all_counts.get(rp_id, 0)
166177
if our_count == 0:
167178
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
177183
)
178184
free_space_found = True
179185
if not free_space_found:
180186
# TODO(johngarbutt) allocation candidates only returns some not all candidates!
181187
print(f"# WARNING - no free spaces found for {hostname}")
182188

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+
)
183194
for project, names in project_to_aggregate.items():
184195
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+
]
189205

190206

191207
def print_project_usage(indentity_client, placement_client, compute_client):
@@ -287,20 +303,16 @@ def collect(self):
287303
conn = openstack.connect()
288304
openstack.enable_logging(debug=True)
289305
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+
291311
print_project_usage(conn.identity, conn.placement, conn.compute)
292312
print_host_usage(resource_providers, conn.placement)
293313
except Exception as e:
294314
print(f"error {e}")
295315

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-
304316
end_time = time.perf_counter()
305317
duration = end_time - start_time
306318
print(f"Collect complete {collect_id} it took {duration} seconds")

0 commit comments

Comments
 (0)