|
| 1 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 2 | +# not use this file except in compliance with the License. You may obtain |
| 3 | +# a copy of the License at |
| 4 | +# |
| 5 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +# |
| 7 | +# Unless required by applicable law or agreed to in writing, software |
| 8 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 9 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 10 | +# License for the specific language governing permissions and limitations |
| 11 | +# under the License. |
| 12 | +import openstack |
| 13 | +from prometheus_client import core as prom_core |
| 14 | + |
| 15 | +RETRY_COUNT = 3 |
| 16 | + |
| 17 | + |
| 18 | +class OpenStackAPITestCollector(object): |
| 19 | + def __init__(self): |
| 20 | + openstack.connect() |
| 21 | + print("got openstack connection\n") |
| 22 | + |
| 23 | + def _check_compute(self, conn): |
| 24 | + for i in range(RETRY_COUNT): |
| 25 | + flavors = list(conn.compute.flavors()) |
| 26 | + flavor_count = prom_core.GaugeMetricFamily( |
| 27 | + "openstack_flavor_count", |
| 28 | + "The number of flavors in the OpenStack deployment.", |
| 29 | + labels=["project_id"], |
| 30 | + ) |
| 31 | + flavor_count.add_metric([conn.current_project_id], len(flavors)) |
| 32 | + |
| 33 | + for i in range(RETRY_COUNT): |
| 34 | + servers = list(conn.compute.servers()) |
| 35 | + server_count = prom_core.GaugeMetricFamily( |
| 36 | + "openstack_server_count", |
| 37 | + "The number of servers in the OpenStack deployment.", |
| 38 | + labels=["project_id"], |
| 39 | + ) |
| 40 | + server_count.add_metric([conn.current_project_id], len(servers)) |
| 41 | + return [flavor_count, server_count] |
| 42 | + |
| 43 | + def _check_images(self, conn): |
| 44 | + for i in range(RETRY_COUNT): |
| 45 | + images = list(conn.image.images()) |
| 46 | + image_count = prom_core.GaugeMetricFamily( |
| 47 | + "openstack_image_count", |
| 48 | + "The number of images in the OpenStack deployment.", |
| 49 | + labels=["project_id"], |
| 50 | + ) |
| 51 | + image_count.add_metric([conn.current_project_id], len(images)) |
| 52 | + return [image_count] |
| 53 | + |
| 54 | + def _check_volumes(self, conn): |
| 55 | + for i in range(RETRY_COUNT): |
| 56 | + volumes = list(conn.block_storage.volumes()) |
| 57 | + volume_count = prom_core.GaugeMetricFamily( |
| 58 | + "openstack_volume_count", |
| 59 | + "The number of volumes in the OpenStack deployment.", |
| 60 | + labels=["project_id"], |
| 61 | + ) |
| 62 | + volume_count.add_metric([conn.current_project_id], len(volumes)) |
| 63 | + return [volume_count] |
| 64 | + |
| 65 | + def _check_ironic(self, conn): |
| 66 | + for i in range(RETRY_COUNT): |
| 67 | + nodes = list(conn.bare_metal.nodes()) |
| 68 | + ironic_node_count = prom_core.GaugeMetricFamily( |
| 69 | + "openstack_ironic_node_count", |
| 70 | + "The number of ironic nodes in the OpenStack deployment.", |
| 71 | + labels=["project_id"], |
| 72 | + ) |
| 73 | + ironic_node_count.add_metric([conn.current_project_id], len(nodes)) |
| 74 | + return [ironic_node_count] |
| 75 | + |
| 76 | + def _check_identity(self, conn): |
| 77 | + user = conn.current_user_id |
| 78 | + for i in range(RETRY_COUNT): |
| 79 | + projects = list(conn.identity.user_projects(user)) |
| 80 | + project_count = prom_core.GaugeMetricFamily( |
| 81 | + "openstack_project_count", |
| 82 | + "The number of projects in the OpenStack deployment.", |
| 83 | + labels=["project_id"], |
| 84 | + ) |
| 85 | + project_count.add_metric([conn.current_project_id], len(projects)) |
| 86 | + return [project_count] |
| 87 | + |
| 88 | + def _check_network(self, conn): |
| 89 | + for i in range(RETRY_COUNT): |
| 90 | + networks = list(conn.network.networks()) |
| 91 | + network_count = prom_core.GaugeMetricFamily( |
| 92 | + "openstack_network_count", |
| 93 | + "The number of networks in the OpenStack deployment.", |
| 94 | + labels=["project_id"], |
| 95 | + ) |
| 96 | + network_count.add_metric([], len(networks)) |
| 97 | + |
| 98 | + for i in range(RETRY_COUNT): |
| 99 | + ports = list(conn.network.ports()) |
| 100 | + port_count = prom_core.GaugeMetricFamily( |
| 101 | + "openstack_port_count", |
| 102 | + "The number of ports in the OpenStack deployment.", |
| 103 | + labels=["project_id"], |
| 104 | + ) |
| 105 | + port_count.add_metric([conn.current_project_id], len(ports)) |
| 106 | + return [network_count, port_count] |
| 107 | + |
| 108 | + def _check_load_balancer(self, conn): |
| 109 | + for i in range(RETRY_COUNT): |
| 110 | + lbs = list(conn.load_balancer.load_balancers()) |
| 111 | + lb_count = prom_core.GaugeMetricFamily( |
| 112 | + "openstack_load_balancer_count", |
| 113 | + "The number of load balancers in the OpenStack deployment.", |
| 114 | + labels=["project_id"], |
| 115 | + ) |
| 116 | + lb_count.add_metric([conn.current_project_id], len(lbs)) |
| 117 | + return [lb_count] |
| 118 | + |
| 119 | + def collect(self): |
| 120 | + openstack.enable_logging(debug=False) |
| 121 | + conn = openstack.connect() |
| 122 | + print("collecting api test metrics\n") |
| 123 | + guages = [] |
| 124 | + |
| 125 | + guages += self._check_compute(conn) |
| 126 | + guages += self._check_images(conn) |
| 127 | + guages += self._check_volumes(conn) |
| 128 | + guages += self._check_ironic(conn) |
| 129 | + guages += self._check_identity(conn) |
| 130 | + guages += self._check_network(conn) |
| 131 | + guages += self._check_load_balancer(conn) |
| 132 | + |
| 133 | + print("finished collecting metrics\n") |
| 134 | + return guages |
0 commit comments