From d303deb214aae2b09156f0f4a817c3f89f576efc Mon Sep 17 00:00:00 2001 From: Chris Stein Date: Tue, 16 Jun 2020 11:56:56 -0500 Subject: [PATCH 1/2] Submit harbor's read-only status as gauge metric --- harbor/datadog_checks/harbor/api.py | 4 ++++ harbor/datadog_checks/harbor/harbor.py | 6 ++++++ harbor/tests/common.py | 3 +++ harbor/tests/test_unit.py | 10 ++++++++++ 4 files changed, 23 insertions(+) diff --git a/harbor/datadog_checks/harbor/api.py b/harbor/datadog_checks/harbor/api.py index 0ebbae83a53d4..4cda660eb53ce 100644 --- a/harbor/datadog_checks/harbor/api.py +++ b/harbor/datadog_checks/harbor/api.py @@ -61,6 +61,10 @@ def _fetch_and_set_harbor_version(self): self.harbor_version = [int(s) for s in version_str] self.with_chartrepo = systeminfo.get('with_chartmuseum', False) + def read_only_status(self): + systeminfo = self._make_get_request(SYSTEM_INFO_URL) + return systeminfo.get('read_only', None) + def _make_paginated_get_request(self, url): http_params = {'page_size': 100} resp = self.http.get(self._resolve_url(url), params=http_params) diff --git a/harbor/datadog_checks/harbor/harbor.py b/harbor/datadog_checks/harbor/harbor.py index a1c6edb1c1a87..bfa47f8483ea5 100644 --- a/harbor/datadog_checks/harbor/harbor.py +++ b/harbor/datadog_checks/harbor/harbor.py @@ -97,6 +97,11 @@ def _submit_disk_metrics(self, api, base_tags): self.gauge('harbor.disk.free', disk_free, tags=base_tags) self.gauge('harbor.disk.total', disk_total, tags=base_tags) + def _submit_read_only_status(self, api, base_tags): + read_only_status = api.read_only_status() + if read_only_status is not None: + self.gauge('harbor.registry.read_only', int(read_only_status), tags=base_tags) + def check(self, instance): harbor_url = instance["url"] tags = instance.get("tags", []) @@ -106,6 +111,7 @@ def check(self, instance): self._check_registries_health(api, tags) self._submit_project_metrics(api, tags) self._submit_disk_metrics(api, tags) + self._submit_read_only_status(api, tags) except Exception: self.log.exception("An error occured when collecting Harbor metrics") self.service_check(CAN_CONNECT, AgentCheck.CRITICAL) diff --git a/harbor/tests/common.py b/harbor/tests/common.py index 07a96c8e0e110..a7991c8eb2712 100644 --- a/harbor/tests/common.py +++ b/harbor/tests/common.py @@ -17,6 +17,7 @@ ('harbor.projects.count', False), ('harbor.disk.free', True), ('harbor.disk.total', True), + ('harbor.registry.read_only', False), ] HERE = os.path.dirname(os.path.abspath(__file__)) @@ -95,5 +96,7 @@ VOLUME_INFO_FIXTURE = {"storage": {"total": 1e6, "free": 5e5}} SYSTEM_INFO_FIXTURE = {"harbor_version": "v{}-25bb24ca".format(os.environ['HARBOR_VERSION'])} +if HARBOR_VERSION >= VERSION_1_5: + SYSTEM_INFO_FIXTURE['read_only'] = False if HARBOR_VERSION >= VERSION_1_6: SYSTEM_INFO_FIXTURE['with_chartmuseum'] = True diff --git a/harbor/tests/test_unit.py b/harbor/tests/test_unit.py index b6d98bdd73057..a918e0ddf5108 100644 --- a/harbor/tests/test_unit.py +++ b/harbor/tests/test_unit.py @@ -52,6 +52,16 @@ def test_submit_disk_metrics(aggregator, harbor_check, harbor_api): aggregator.assert_metric('harbor.disk.total', 1e6, tags=tags) +@pytest.mark.usefixture("patch_requests") +def test_submit_read_only_status(aggregator, harbor_check, harbor_api): + if harbor_api.harbor_version >= VERSION_1_5: + tags = ['tag1:val1', 'tag2'] + harbor_check._submit_read_only_status(harbor_api, tags) + aggregator.assert_metric('harbor.registry.read_only', False, tags=tags) + else: + None + + def test_api__make_get_request(harbor_api): harbor_api.http = MagicMock() harbor_api.http.get = MagicMock(return_value=MockResponse({"json": True}, 200)) From 79431f36172a4fdf655ea6c3cbfcc82c04b562c6 Mon Sep 17 00:00:00 2001 From: Chris Stein Date: Mon, 22 Jun 2020 09:09:33 -0500 Subject: [PATCH 2/2] update metadata.csv and test_unit.py --- harbor/metadata.csv | 1 + harbor/tests/common.py | 6 ++++-- harbor/tests/test_unit.py | 12 +++++------- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/harbor/metadata.csv b/harbor/metadata.csv index 5950ba5b9a308..409552367145e 100644 --- a/harbor/metadata.csv +++ b/harbor/metadata.csv @@ -2,3 +2,4 @@ metric_name,metric_type,interval,unit_name,per_unit_name,description,orientation harbor.projects.count,gauge,,,,The total number of projects.,0,harbor,nb projects harbor.disk.free,gauge,,byte,,The amount of storage space that is free.,0,harbor,disk free harbor.disk.total,gauge,,byte,,The total amount of storage space.,0,harbor,disk total +harbor.registry.read_only,gauge,,,,The 'read_only' status of a registry.,0,harbor,registry read_only diff --git a/harbor/tests/common.py b/harbor/tests/common.py index a7991c8eb2712..a3096fb1b15b8 100644 --- a/harbor/tests/common.py +++ b/harbor/tests/common.py @@ -12,16 +12,18 @@ HARBOR_COMPONENTS = ['chartmuseum', 'registry', 'redis', 'jobservice', 'registryctl', 'portal', 'core', 'database'] +HARBOR_VERSION = [int(i) for i in os.environ['HARBOR_VERSION'].split('.')] + HARBOR_METRICS = [ # Metric_name, requires admin privileges ('harbor.projects.count', False), ('harbor.disk.free', True), ('harbor.disk.total', True), - ('harbor.registry.read_only', False), ] +if HARBOR_VERSION >= VERSION_1_5: + HARBOR_METRICS.append(('harbor.registry.read_only', False)) HERE = os.path.dirname(os.path.abspath(__file__)) -HARBOR_VERSION = [int(i) for i in os.environ['HARBOR_VERSION'].split('.')] URL = 'http://{}'.format(get_docker_hostname()) INSTANCE = {'url': URL, 'username': 'NotAnAdmin', 'password': 'Str0ngPassw0rd'} ADMIN_INSTANCE = {'url': URL, 'username': 'admin', 'password': 'Harbor12345'} diff --git a/harbor/tests/test_unit.py b/harbor/tests/test_unit.py index a918e0ddf5108..871ddbbd05fad 100644 --- a/harbor/tests/test_unit.py +++ b/harbor/tests/test_unit.py @@ -7,7 +7,7 @@ from datadog_checks.base import AgentCheck -from .common import HARBOR_COMPONENTS, VERSION_1_5, VERSION_1_6, VERSION_1_8 +from .common import HARBOR_COMPONENTS, HARBOR_VERSION, VERSION_1_5, VERSION_1_6, VERSION_1_8 from .conftest import MockResponse @@ -53,13 +53,11 @@ def test_submit_disk_metrics(aggregator, harbor_check, harbor_api): @pytest.mark.usefixture("patch_requests") +@pytest.mark.skipif(HARBOR_VERSION < VERSION_1_5, reason="The registry.read_only metric is submitted for Harbor 1.5+") def test_submit_read_only_status(aggregator, harbor_check, harbor_api): - if harbor_api.harbor_version >= VERSION_1_5: - tags = ['tag1:val1', 'tag2'] - harbor_check._submit_read_only_status(harbor_api, tags) - aggregator.assert_metric('harbor.registry.read_only', False, tags=tags) - else: - None + tags = ['tag1:val1', 'tag2'] + harbor_check._submit_read_only_status(harbor_api, tags) + aggregator.assert_metric('harbor.registry.read_only', False, tags=tags) def test_api__make_get_request(harbor_api):