Skip to content

Instantly share code, notes, and snippets.

@xei
Created June 25, 2023 10:40
Show Gist options
  • Save xei/f5205145b2f95e8ef708c0618e4eff94 to your computer and use it in GitHub Desktop.
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.
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