Skip to content

Commit

Permalink
fix: Make timeouts for DataPower customisable in config (#61)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
rickymoorhouse authored Mar 13, 2023
1 parent 28bf86d commit baacfe4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ logging:
nets:
datapower:
enabled: true
timeout: 5
username: trawler-monitor
namespace: apic-gateway
product:
Expand Down
41 changes: 20 additions & 21 deletions datapower_net.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class DataPowerNet():
password = None
use_kubeconfig = False
items = {}
timeout = 1
api_tests = None

def __init__(self, config, trawler):
Expand All @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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))

Expand All @@ -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:
Expand Down Expand Up @@ -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"]:
Expand Down Expand Up @@ -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()

0 comments on commit baacfe4

Please sign in to comment.