From baacfe43f0f0e2b0ddadebf4f297cdc3f110b957 Mon Sep 17 00:00:00 2001 From: Ricky Moorhouse Date: Mon, 13 Mar 2023 09:23:28 +0000 Subject: [PATCH] fix: Make timeouts for DataPower customisable in config (#61) * fix: Make timeouts for DataPower customisable in config Also move to use ObjectInstanceCounts to improve performance doc: include timeout in example Signed-off-by: Ricky Moorhouse --- README.md | 1 + datapower_net.py | 41 ++++++++++++++++++++--------------------- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index ebae96b..5b07ab5 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,7 @@ logging: nets: datapower: enabled: true + timeout: 5 username: trawler-monitor namespace: apic-gateway product: diff --git a/datapower_net.py b/datapower_net.py index b648a1c..a582a05 100644 --- a/datapower_net.py +++ b/datapower_net.py @@ -18,6 +18,7 @@ class DataPowerNet(): password = None use_kubeconfig = False items = {} + timeout = 1 api_tests = None def __init__(self, config, trawler): @@ -28,9 +29,10 @@ def __init__(self, config, trawler): self.namespace = config.get('namespace', None) # Datapower username to use for REST calls self.username = config.get('username', 'admin') + self.timeout = config.get('timeout', 1) self.secret = config.get('secret', 'gateway-admin-secret') api_test_config = config.get('api_tests', None) - if api_test_config and api_test_config['enabled'] == True: + if api_test_config and api_test_config['enabled'] is True: self.api_tests = api_test_config['apis'] # Load password from secret `datapower_password` try: @@ -119,16 +121,18 @@ class DataPower(): port = 5554 apiPort = 9443 trawler = None + timeout = 1 api_tests = None labels = {} - def __init__(self, ip, port, name, namespace, username, password, trawler, api_tests=None): + def __init__(self, ip, port, name, namespace, username, password, trawler, api_tests=None, timeout=1): self.ip = ip self.port = port self.name = name self.namespace = namespace self.username = username self.password = password + self.timeout = timeout self.get_info() self.trawler = trawler self.api_tests = api_tests @@ -147,7 +151,7 @@ def get_info(self): state = requests.get(url, auth=(self.username, self.password), verify=False, - timeout=1 + timeout=self.timeout ) logger.trace(state) if state.status_code == 200: @@ -175,7 +179,7 @@ def are_statistics_enabled(self): state = requests.get(url, auth=(self.username, self.password), verify=False, - timeout=1 + timeout=self.timeout ) logger.trace(state.text) if state.status_code == 200: @@ -223,7 +227,7 @@ def fetch_data(self, provider, label, suffix=''): provider) status = requests.get(url, auth=(self.username, self.password), - verify=False, timeout=1).json() + verify=False, timeout=self.timeout).json() logger.debug(status) data = status.get(provider, {}) labels = self.labels @@ -252,30 +256,24 @@ def fetch_data(self, provider, label, suffix=''): except requests.exceptions.RequestException as e: logger.info("{}: {} (Check rest-mgmt is enabled and you have network connectivity)".format(provider, e.strerror)) -# https://127.0.0.1:5554/mgmt/status/apiconnect/ObjectStatus +# https://127.0.0.1:5554/mgmt/status/apiconnect/ObjectInstanceCounts def object_counts(self): """ Count objects within datapower domain """ - logger.info("Processing status provider ObjectStatus") + logger.info("Processing status provider ObjectInstanceCounts") try: - url = "https://{}:{}/mgmt/status/{}/ObjectStatus".format( + url = "https://{}:{}/mgmt/status/{}/ObjectInstanceCounts".format( self.ip, self.port, self.domain) status = requests.get(url, - auth=(self.username, self.password), - verify=False, timeout=1).json() + auth=(self.username, self.password), + verify=False, + timeout=self.timeout).json() logger.debug(status) - data = status.get('ObjectStatus', []) - counts = {} + data = status.get('ObjectInstanceCounts', []) for item in data: - if item['Class'] in counts: - counts[item['Class']] += 1 - else: - counts[item['Class']] = 1 - for item_class in counts: - self.trawler.set_gauge('datapower', "{}_total".format(item_class), counts[item_class], pod_name=self.name, labels=self.labels) + self.trawler.set_gauge('datapower', "{}_total".format(item['Class']), item['Count'], pod_name=self.name, labels=self.labels) - logger.debug(counts) except requests.exceptions.RequestException as e: logger.info("Failed to get object count: {} (Check rest-mgmt is enabled and you have network connectivity)".format(e.strerror)) @@ -297,7 +295,7 @@ def fetch_document_cache_summary(self, suffix=''): provider) status = requests.get(url, auth=(self.username, self.password), - verify=False, timeout=1).json() + verify=False, timeout=self.timeout).json() logger.debug(status) data = status.get(provider, {}) if type(data) is not list: @@ -340,7 +338,7 @@ def gateway_peering_status(self): self.domain) status = requests.get(url, auth=(self.username, self.password), - verify=False, timeout=1).json() + verify=False, timeout=self.timeout).json() logger.debug(status) for entry in status["GatewayPeeringStatus"]: @@ -401,6 +399,7 @@ def invoke_api(self, api): 0, pod_name=self.name, labels={**status_labels, **self.labels}) + if __name__ == "__main__": net = DataPowerNet() net.find()