From 04640b951159b8164df079dab3fa6252cc47e69b Mon Sep 17 00:00:00 2001 From: crenduchinta88 <77858438+crenduchinta88@users.noreply.github.com> Date: Tue, 6 Feb 2024 08:51:22 -0500 Subject: [PATCH 1/4] Create cinderservice.py --- .../collectors/cinderservice.py | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 openstack_exporter/collectors/cinderservice.py diff --git a/openstack_exporter/collectors/cinderservice.py b/openstack_exporter/collectors/cinderservice.py new file mode 100644 index 0000000..ac65866 --- /dev/null +++ b/openstack_exporter/collectors/cinderservice.py @@ -0,0 +1,69 @@ +import logging +from prometheus_client.core import GaugeMetricFamily +from keystoneauth1 import session +from keystoneauth1.identity import v3 +from cinderclient import client as cinder +from openstack_exporter import BaseCollector + +LOG = logging.getLogger('openstack_exporter.exporter') + +class CinderServiceCollector(BaseCollector.BaseCollector): + version = "1.0.0" + + def __init__(self, openstack_config, collector_config): + super().__init__(openstack_config, collector_config) + self.cinder_client = self._create_cinder_client() + + def _create_cinder_client(self): + """Create a Cinder client""" + os_auth_url = self.config['auth_url'] + os_username = self.config['username'] + os_password = self.config['password'] + os_project_name = self.config['project_name'] + os_project_domain_name = self.config['project_domain_name'] + os_user_domain_name = self.config['user_domain_name'] + + auth = v3.Password(auth_url=os_auth_url, + username=os_username, + password=os_password, + project_name=os_project_name, + project_domain_name=os_project_domain_name, + user_domain_name=os_user_domain_name) + + sess = session.Session(auth=auth) + return cinder.Client('3.64', session=sess, region_name=self.region) # Adjust API version when required + + def describe(self): + yield GaugeMetricFamily('cinder_service_status', + 'Current status of Cinder services (enabled/disabled)', + labels=['service', 'host', 'zone']) + yield GaugeMetricFamily('cinder_service_state', + 'Current state of Cinder services (up/down)', + labels=['service', 'host', 'zone']) + + def collect(self): + LOG.info("Collecting Cinder service info.") + try: + services = self.cinder_client.services.list() + except Exception as e: + LOG.error(f"Error while collecting Cinder service metrics: {e}") + return + + g_status = GaugeMetricFamily('cinder_service_status', + 'Current status of Cinder services (enabled/disabled)', + labels=['service', 'host', 'zone']) + g_state = GaugeMetricFamily('cinder_service_state', + 'Current state of Cinder services (up/down)', + labels=['service', 'host', 'zone']) + + for service in services: + LOG.debug(f"Service: {service.binary}, Host: {service.host}, " + f"Zone: {service.zone}, Status: {service.status}, State: {service.state}") + + status_value = 1 if service.status == 'enabled' else 0 + state_value = 1 if service.state == 'up' else 0 + g_status.add_metric([service.binary, service.host, service.zone], status_value) + g_state.add_metric([service.binary, service.host, service.zone], state_value) + + yield g_status + yield g_state From 42448ba526e0143b43f5922e6d3a85c75f71c9b5 Mon Sep 17 00:00:00 2001 From: crenduchinta88 <77858438+crenduchinta88@users.noreply.github.com> Date: Tue, 6 Feb 2024 08:55:52 -0500 Subject: [PATCH 2/4] Update cinderservice.py --- openstack_exporter/collectors/cinderservice.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/openstack_exporter/collectors/cinderservice.py b/openstack_exporter/collectors/cinderservice.py index ac65866..945541d 100644 --- a/openstack_exporter/collectors/cinderservice.py +++ b/openstack_exporter/collectors/cinderservice.py @@ -1,3 +1,17 @@ +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + import logging from prometheus_client.core import GaugeMetricFamily from keystoneauth1 import session From 6b1535b96f4876962bd4f5bd1e0fed6a034a169d Mon Sep 17 00:00:00 2001 From: crenduchinta88 <77858438+crenduchinta88@users.noreply.github.com> Date: Tue, 6 Feb 2024 08:57:08 -0500 Subject: [PATCH 3/4] Update cinderservice.py --- openstack_exporter/collectors/cinderservice.py | 1 - 1 file changed, 1 deletion(-) diff --git a/openstack_exporter/collectors/cinderservice.py b/openstack_exporter/collectors/cinderservice.py index 945541d..b23a169 100644 --- a/openstack_exporter/collectors/cinderservice.py +++ b/openstack_exporter/collectors/cinderservice.py @@ -22,7 +22,6 @@ LOG = logging.getLogger('openstack_exporter.exporter') class CinderServiceCollector(BaseCollector.BaseCollector): - version = "1.0.0" def __init__(self, openstack_config, collector_config): super().__init__(openstack_config, collector_config) From 6f439f10fe87b7f2f47d88a0f24fe017eb233f25 Mon Sep 17 00:00:00 2001 From: crenduchinta88 <77858438+crenduchinta88@users.noreply.github.com> Date: Wed, 7 Feb 2024 13:42:53 -0500 Subject: [PATCH 4/4] Update cinderservice.py --- .../collectors/cinderservice.py | 51 ++++++++++++++----- 1 file changed, 37 insertions(+), 14 deletions(-) diff --git a/openstack_exporter/collectors/cinderservice.py b/openstack_exporter/collectors/cinderservice.py index b23a169..da3a439 100644 --- a/openstack_exporter/collectors/cinderservice.py +++ b/openstack_exporter/collectors/cinderservice.py @@ -22,13 +22,14 @@ LOG = logging.getLogger('openstack_exporter.exporter') class CinderServiceCollector(BaseCollector.BaseCollector): + version = "1.0.0" def __init__(self, openstack_config, collector_config): super().__init__(openstack_config, collector_config) self.cinder_client = self._create_cinder_client() def _create_cinder_client(self): - """Create a Cinder client""" + """Create a Cinder client.""" os_auth_url = self.config['auth_url'] os_username = self.config['username'] os_password = self.config['password'] @@ -44,16 +45,35 @@ def _create_cinder_client(self): user_domain_name=os_user_domain_name) sess = session.Session(auth=auth) - return cinder.Client('3.64', session=sess, region_name=self.region) # Adjust API version when required + return cinder.Client('3.64', session=sess, region_name=self.region) + + def _create_cinder_client(self): + """Create a Cinder client.""" + os_auth_url = self.config['auth_url'] + os_username = self.config['username'] + os_password = self.config['password'] + os_project_name = self.config['project_name'] + os_project_domain_name = self.config['project_domain_name'] + os_user_domain_name = self.config['user_domain_name'] + + auth = v3.Password(auth_url=os_auth_url, + username=os_username, + password=os_password, + project_name=os_project_name, + project_domain_name=os_project_domain_name, + user_domain_name=os_user_domain_name) + + sess = session.Session(auth=auth) + return cinder.Client('3.64', session=sess, region_name=self.region) def describe(self): yield GaugeMetricFamily('cinder_service_status', 'Current status of Cinder services (enabled/disabled)', - labels=['service', 'host', 'zone']) + labels=['service', 'host', 'zone', 'disabled_reason']) yield GaugeMetricFamily('cinder_service_state', 'Current state of Cinder services (up/down)', - labels=['service', 'host', 'zone']) - + labels=['service', 'host', 'zone', 'disabled_reason']) + def collect(self): LOG.info("Collecting Cinder service info.") try: @@ -61,22 +81,25 @@ def collect(self): except Exception as e: LOG.error(f"Error while collecting Cinder service metrics: {e}") return - + g_status = GaugeMetricFamily('cinder_service_status', 'Current status of Cinder services (enabled/disabled)', - labels=['service', 'host', 'zone']) + labels=['service', 'host', 'zone', 'disabled_reason']) g_state = GaugeMetricFamily('cinder_service_state', 'Current state of Cinder services (up/down)', - labels=['service', 'host', 'zone']) - + labels=['service', 'host', 'zone', 'disabled_reason']) + for service in services: LOG.debug(f"Service: {service.binary}, Host: {service.host}, " - f"Zone: {service.zone}, Status: {service.status}, State: {service.state}") - + f"Zone: {service.zone}, Status: {service.status}, State: {service.state}, " + f"Disabled Reason: {service.disabled_reason or 'N/A'}") + status_value = 1 if service.status == 'enabled' else 0 state_value = 1 if service.state == 'up' else 0 - g_status.add_metric([service.binary, service.host, service.zone], status_value) - g_state.add_metric([service.binary, service.host, service.zone], state_value) - + disabled_reason = service.disabled_reason if service.disabled_reason else "N/A" + + g_status.add_metric([service.binary, service.host, service.zone, disabled_reason], status_value) + g_state.add_metric([service.binary, service.host, service.zone, disabled_reason], state_value) + yield g_status yield g_state