Created
June 25, 2023 10:40
-
-
Save xei/f5205145b2f95e8ef708c0618e4eff94 to your computer and use it in GitHub Desktop.
A simple Python script to check if all the services are healthy and notify the issues.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
skype_notifier_url = 'https://ml.hosseinkhani.me/skype-notifier/send-msg/chat/incident-group' | |
api_endpoints = { | |
'Service 1': 'http://ml.hosseinkhani.me/service1/healthz', | |
'Service 2': 'http://ml.hosseinkhani.me/service2/healthz', | |
} | |
failed_requests = [] | |
for name, endpoint in api_endpoints.items(): | |
try: | |
response = requests.get(endpoint) | |
if response.status_code == 200: | |
# Health check passed | |
print(f'Health check passed for {name}: {response.json()}') | |
else: | |
# Health check failed, add to the failed_requests list | |
failed_requests.append({ | |
'service': name, | |
'error': response.text | |
}) | |
except requests.exceptions.RequestException as e: | |
# An exception occurred during the request, add to the failed_requests list | |
failed_requests.append({ | |
'service': name, | |
'error': str(e) | |
}) | |
if len(failed_requests) != 0: | |
# Send the failed requests to Skype Notifier service | |
msg = "ALERT!!!\n\n" + "\n".join( | |
[f"Service: {failed['service']}\nError: {failed['error']}" for failed in failed_requests] | |
) | |
payload = {'msg': msg} | |
try: | |
response = requests.post(skype_notifier_url, json=payload) | |
if response.status_code == 200: | |
print('Log successfully sent to the Skype Notifier service.') | |
else: | |
print(f'Failed to send log to the Skype Notifier service. Status code: {response.status_code}') | |
except requests.exceptions.RequestException as e: | |
print(f'Error occurred while sending log to the Skype Notifier service: {str(e)}') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment