|
2 | 2 |
|
3 | 3 | import collections
|
4 | 4 | import json
|
| 5 | +import time |
| 6 | +import uuid |
5 | 7 |
|
6 | 8 | import openstack
|
| 9 | +import prometheus_client as prom_client |
| 10 | +from prometheus_client import core as prom_core |
7 | 11 |
|
8 | 12 |
|
9 | 13 | def get_capacity_per_flavor(placement_client, flavors):
|
@@ -266,8 +270,46 @@ def print_exporter_data(app):
|
266 | 270 | print_host_free_details(app.compute_client, app.placement_client)
|
267 | 271 |
|
268 | 272 |
|
| 273 | +class OpenStackCapacityCollector(object): |
| 274 | + def __init__(self): |
| 275 | + self.conn = openstack.connect() |
| 276 | + openstack.enable_logging(debug=True) |
| 277 | + print("got openstack connection") |
| 278 | + # for some reason this makes the logging work?! |
| 279 | + self.conn.compute.flavors() |
| 280 | + |
| 281 | + def collect(self): |
| 282 | + start_time = time.perf_counter() |
| 283 | + collect_id = uuid.uuid4().hex |
| 284 | + print(f"Collect started {collect_id}") |
| 285 | + guages = [] |
| 286 | + |
| 287 | + conn = openstack.connect() |
| 288 | + openstack.enable_logging(debug=True) |
| 289 | + try: |
| 290 | + resource_providers = print_host_details(conn.compute, conn.placement) |
| 291 | + print_project_usage(conn.identity, conn.placement, conn.compute) |
| 292 | + print_host_usage(resource_providers, conn.placement) |
| 293 | + except Exception as e: |
| 294 | + print(f"error {e}") |
| 295 | + |
| 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 | + end_time = time.perf_counter() |
| 305 | + duration = end_time - start_time |
| 306 | + print(f"Collect complete {collect_id} it took {duration} seconds") |
| 307 | + return guages |
| 308 | + |
| 309 | + |
269 | 310 | if __name__ == "__main__":
|
270 |
| - conn = openstack.connect() |
271 |
| - resource_providers = print_host_details(conn.compute, conn.placement) |
272 |
| - print_project_usage(conn.identity, conn.placement, conn.compute) |
273 |
| - print_host_usage(resource_providers, conn.placement) |
| 311 | + prom_client.start_http_server(9000) |
| 312 | + prom_core.REGISTRY.register(OpenStackCapacityCollector()) |
| 313 | + # there must be a better way! |
| 314 | + while True: |
| 315 | + time.sleep(5000) |
0 commit comments