icinga2-checks/checker/http.py

31 lines
1011 B
Python
Raw Normal View History

2023-06-15 11:00:41 -06:00
import sys
from time import sleep
import requests
from . import nagios
from .print import print_icinga2_check_status
def get_with_retry(url, retries=3, delay=1, **kwargs):
"""
Wrapper function for requests.get() with a retry mechanism.
:param url: URL to send the GET request
:param retries: Number of retries in case of HTTP failures (default: 3)
:param delay: Time delay between retries in seconds (default: 1)
:param kwargs: Additional keyword arguments for requests.get()
:return: Response object
"""
for i in range(retries):
try:
response = requests.get(url, **kwargs)
response.raise_for_status()
return response
except requests.exceptions.RequestException as e:
if i == retries - 1:
# raise e
print_icinga2_check_status(f'HTTP request failed after {i} retries: {url}\n{e}', nagios.STATE_UNKNOWN)
sys.exit(nagios.STATE_UNKNOWN)
sleep(delay)