diff --git a/.zuul.yaml b/.zuul.yaml index 5c1ef1c..b3d61c2 100644 --- a/.zuul.yaml +++ b/.zuul.yaml @@ -29,6 +29,11 @@ zuul_copy_output: /etc/prometheus/prometheus.yml: logs /etc/openstack/prometheus.yaml: logs + devstack_local_conf: + post-config: + $AODH_CONF: + DEFAULT: + enable_evaluation_results_metrics: True - project: queue: telemetry diff --git a/aodhclient/shell.py b/aodhclient/shell.py index a38486c..17ef5df 100644 --- a/aodhclient/shell.py +++ b/aodhclient/shell.py @@ -29,6 +29,7 @@ from aodhclient import noauth from aodhclient.v2 import alarm_cli from aodhclient.v2 import alarm_history_cli from aodhclient.v2 import capabilities_cli +from aodhclient.v2 import metrics_cli class AodhCommandManager(commandmanager.CommandManager): @@ -43,6 +44,7 @@ class AodhCommandManager(commandmanager.CommandManager): "alarm-history show": alarm_history_cli.CliAlarmHistoryShow, "alarm-history search": alarm_history_cli.CliAlarmHistorySearch, "capabilities list": capabilities_cli.CliCapabilitiesList, + "alarm metrics": metrics_cli.CliMetrics, } def load_commands(self, namespace): diff --git a/aodhclient/tests/functional/test_alarm.py b/aodhclient/tests/functional/test_alarm.py index 7d48029..d647e6a 100644 --- a/aodhclient/tests/functional/test_alarm.py +++ b/aodhclient/tests/functional/test_alarm.py @@ -28,6 +28,7 @@ class AodhClientTest(base.ClientTestBase): self.aodh("help", params="alarm list") self.aodh("help", params="alarm show") self.aodh("help", params="alarm update") + self.aodh("help", params="alarm metrics") def test_alarm_id_or_name_scenario(self): def _test(name): diff --git a/aodhclient/tests/functional/test_metrics.py b/aodhclient/tests/functional/test_metrics.py new file mode 100644 index 0000000..0c648aa --- /dev/null +++ b/aodhclient/tests/functional/test_metrics.py @@ -0,0 +1,21 @@ +# 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. + +from aodhclient.tests.functional import base + + +class MetricsTest(base.ClientTestBase): + + def test_metrics_scenario(self): + # Test that the metrics command doesn't return errors + params = "metrics" + self.aodh('alarm', params=params) diff --git a/aodhclient/tests/unit/test_metrics.py b/aodhclient/tests/unit/test_metrics.py new file mode 100644 index 0000000..7ffc7d4 --- /dev/null +++ b/aodhclient/tests/unit/test_metrics.py @@ -0,0 +1,65 @@ +# +# 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 testtools +from unittest import mock + +from aodhclient.v2 import metrics_cli + + +class MetricsTest(testtools.TestCase): + def setUp(self): + super(MetricsTest, self).setUp() + self.app = mock.Mock() + self.metrics_mgr_mock = self.app.client_manager.alarming.metrics + self.parser = mock.Mock() + self.metrics = ( + metrics_cli.CliMetrics(self.app, self.parser)) + + def test_metrics(self): + self.metrics_mgr_mock.get.return_value = { + "evaluation_results": [ + { + "alarm_id": "b8e17f58-089a-43fc-a96b-e9bcac4d4b53", + "project_id": "2dd8edd6c8c24f49bf04670534f6b357", + "state_counters": { + "insufficient data": 1646, + "ok": 0, + "alarm": 0 + } + }, + { + "alarm_id": "fa386719-67e3-42ff-aec8-17e547dac77a", + "project_id": "2dd8edd6c8c24f49bf04670534f6b357", + "state_counters": { + "insufficient data": 4, + "ok": 923, + "alarm": 0 + } + }, + { + "alarm_id": "0788bf8b-fc1f-4889-b5fd-5acd4d287bc3", + "project_id": "d45b070bcce04ca99546128a40854e7c", + "state_counters": { + "insufficient data": 1646, + "ok": 0, + "alarm": 0 + } + } + ] + } + ret = self.metrics.take_action([]) + + self.metrics_mgr_mock.get.assert_called_once_with() + self.assertIn('name', ret[0]) + self.assertIn('evaluation_result', ret[1][0]) diff --git a/aodhclient/v2/client.py b/aodhclient/v2/client.py index 69a4457..431dd36 100644 --- a/aodhclient/v2/client.py +++ b/aodhclient/v2/client.py @@ -17,6 +17,7 @@ from aodhclient import client from aodhclient.v2 import alarm from aodhclient.v2 import alarm_history from aodhclient.v2 import capabilities +from aodhclient.v2 import metrics from aodhclient.v2 import quota @@ -35,3 +36,4 @@ class Client(object): self.alarm_history = alarm_history.AlarmHistoryManager(self) self.capabilities = capabilities.CapabilitiesManager(self) self.quota = quota.QuotasManager(self) + self.metrics = metrics.MetricsManager(self) diff --git a/aodhclient/v2/metrics.py b/aodhclient/v2/metrics.py new file mode 100644 index 0000000..bf21889 --- /dev/null +++ b/aodhclient/v2/metrics.py @@ -0,0 +1,26 @@ +# +# 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. + +from aodhclient.v2 import base + + +class MetricsManager(base.Manager): + + url = "v2/metrics" + + def get(self, all_projects=False): + """Get metrics""" + if all_projects: + return self._get(self.url + "?all_projects=true").json() + else: + return self._get(self.url).json() diff --git a/aodhclient/v2/metrics_cli.py b/aodhclient/v2/metrics_cli.py new file mode 100644 index 0000000..8ea835c --- /dev/null +++ b/aodhclient/v2/metrics_cli.py @@ -0,0 +1,45 @@ +# +# 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. + +from cliff import lister + +from aodhclient import utils + +METRIC_COLS = ["name", "labels", "value"] + + +class CliMetrics(lister.Lister): + """Get Metrics""" + + @staticmethod + def metrics2cols(metrics): + cols_data = [] + if "evaluation_results" in metrics: + for alarm in metrics["evaluation_results"]: + for state, count in alarm["state_counters"].items(): + # prometheus style labels + labels = ('alarm_id="{{{}}}", project_id="{{{}}}", ' + 'state="{{{}}}"').format(alarm['alarm_id'], + alarm['project_id'], + state) + cols_data.append(["evaluation_result", labels, count]) + # Extend for other types of metrics here + return METRIC_COLS, cols_data + + def get_parser(self, prog_name): + parser = super(CliMetrics, self).get_parser(prog_name) + return parser + + def take_action(self, parsed_args): + metrics = utils.get_client(self).metrics.get() + return self.metrics2cols(metrics) diff --git a/releasenotes/notes/add-alarm-metrics-command-349e75fbf26171d5.yaml b/releasenotes/notes/add-alarm-metrics-command-349e75fbf26171d5.yaml new file mode 100644 index 0000000..b0625c2 --- /dev/null +++ b/releasenotes/notes/add-alarm-metrics-command-349e75fbf26171d5.yaml @@ -0,0 +1,4 @@ +--- +features: + - | + Added a ``alarm metrics`` command to retrieve alarming metrics from the aodh /v2/metrics API endpoint. diff --git a/setup.cfg b/setup.cfg index 5e62aaf..70fd07b 100644 --- a/setup.cfg +++ b/setup.cfg @@ -48,4 +48,5 @@ openstack.alarming.v2 = alarming capabilities list = aodhclient.v2.capabilities_cli:CliCapabilitiesList alarm quota show = aodhclient.v2.quota_cli:QuotaShow alarm quota set = aodhclient.v2.quota_cli:QuotaSet + alarm metrics = aodhclient.v2.metrics_cli:CliMetrics