Skip to content
This repository has been archived by the owner on Feb 17, 2020. It is now read-only.

new plugin added for checking 3rd party services url #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ Add the ``health_check`` applications to your ``INSTALLED_APPS``:
'health_check.contrib.s3boto_storage', # requires boto and S3BotoStorage backend
'health_check.contrib.rabbitmq', # requires RabbitMQ broker
'health_check.contrib.redis', # required Redis broker
'health_check.contrib.third_party_services' # to check whether third party urls are working or not. Add URLs in list `HEALTHCHECK_THIRD_PARTY_URLS`. It checks whether each URL provided responds with 2xx response or not.
'health_check.contrib.vault' # to check whether vault is running or not. Add `HEALTHCHECK_VAULT_URL` in settings.
]

(Optional) If using the ``psutil`` app, you can configure disk and memory
Expand Down
1 change: 1 addition & 0 deletions health_check/contrib/third_party_services/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
default_app_config = 'health_check.contrib.third_party_services.apps.HealthCheckConfig'
13 changes: 13 additions & 0 deletions health_check/contrib/third_party_services/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

from django.apps import AppConfig

from health_check.plugins import plugin_dir


class HealthCheckConfig(AppConfig):
name = 'health_check.contrib.third_party_services'

def ready(self):
from .backends import ThirdPartyServicesHealthCheck

plugin_dir.register(ThirdPartyServicesHealthCheck)
43 changes: 43 additions & 0 deletions health_check/contrib/third_party_services/backends.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

import requests
from threading import Thread

from django.conf import settings

from health_check.backends import BaseHealthCheckBackend
from health_check.exceptions import HealthCheckException


class ThirdPartyServicesHealthCheck(BaseHealthCheckBackend):

def fetch_url(self, url, results, i):
timeout = getattr(settings, 'HTTP_REQUEST_TIMEOUT', 5)
try:
response = requests.get(url, timeout=timeout)
if response.ok:
msg = None
else:
msg = '%s -- is not OK. Returned %s' % (url,
response.status_code)
except Exception as e:
msg = '%s -- %s' % (url, str(e.__class__.__name__))

results[i] = msg

def check_status(self):
urls = getattr(settings, 'HEALTHCHECK_THIRD_PARTY_URLS', [])

total_urls = len(urls)
threads = [None] * total_urls
results = [None] * total_urls

for i, url in enumerate(urls):
threads[i] = Thread(target=self.fetch_url, args=(url, results, i))
threads[i].start()

for i in range(total_urls):
threads[i].join()

results = [result for result in results if result]
if results:
self.add_error(HealthCheckException(results))
1 change: 1 addition & 0 deletions health_check/contrib/vault/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
default_app_config = 'health_check.contrib.vault.apps.HealthCheckConfig'
13 changes: 13 additions & 0 deletions health_check/contrib/vault/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

from django.apps import AppConfig

from health_check.plugins import plugin_dir


class HealthCheckConfig(AppConfig):
name = 'health_check.contrib.vault'

def ready(self):
from .backends import VaultHealthCheck

plugin_dir.register(VaultHealthCheck)
31 changes: 31 additions & 0 deletions health_check/contrib/vault/backends.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

import requests

from django.conf import settings

from health_check.backends import BaseHealthCheckBackend
from health_check.exceptions import HealthCheckException


class VaultHealthCheck(BaseHealthCheckBackend):

def check_status(self):
url = getattr(settings, 'HEALTHCHECK_VAULT_URL', None)
timeout = getattr(settings, 'HTTP_REQUEST_TIMEOUT', 5)

if not url:
return

error = None
try:
response = requests.get(url, timeout=timeout)
if not response.ok:
error = 'Vault is not OK. Returned %s' % (response.status_code)
else:
if response.get('sealed'):
error = 'Vault is sealed'
except Exception as e:
error = 'Vault Exception -- %s' % (str(e.__class__.__name__))

if error:
self.add_error(HealthCheckException(error))