Skip to main content

Run checks on services like databases, queue servers, celery processes, etc.

Project description

version ci coverage health license

This project checks for various conditions and provides reports when anomalous behavior is detected.

The following health checks are bundled with this project:

  • cache

  • database

  • storage

  • disk and memory utilization (via psutil)

  • AWS S3 storage

  • Celery task queue

  • RabbitMQ

Writing your own custom health checks is also very quick and easy.

We also like contributions, so don’t be afraid to make a pull request.

Use Cases

The primary intended use case is to monitor conditions via HTTP(S), with responses available in HTML and JSON formats. When you get back a response that includes one or more problems, you can then decide the appropriate course of action, which could include generating notifications and/or automating the replacement of a failing node with a new one. If you are monitoring health in a high-availability environment with a load balancer that returns responses from multiple nodes, please note that certain checks (e.g., disk and memory usage) will return responses specific to the node selected by the load balancer.

Supported Versions

We officially only support the latest version of Python as well as the latest version of Django and the latest Django LTS version.

Installation

First install the django-health-check package:

pip install django-health-check

Add the health checker to a URL you want to use:

urlpatterns = [
    # ...
    url(r'^ht/', include('health_check.urls')),
]

Add the health_check applications to your INSTALLED_APPS:

INSTALLED_APPS = [
    # ...
    'health_check',                             # required
    'health_check.db',                          # stock Django health checkers
    'health_check.cache',
    'health_check.storage',
    'health_check.contrib.celery',              # requires celery
    'health_check.contrib.psutil',              # disk and memory utilization; requires psutil
    'health_check.contrib.s3boto_storage',      # requires boto and S3BotoStorage backend
    'health_check.contrib.rabbitmq',            # requires RabbitMQ broker
    'health_check.contrib.redis',               # required Redis broker
]

(Optional) If using the psutil app, you can configure disk and memory threshold settings; otherwise below defaults are assumed. If you want to disable one of these checks, set its value to None.

HEALTH_CHECK = {
    'DISK_USAGE_MAX': 90,  # percent
    'MEMORY_MIN': 100,    # in MB
}

If using the DB check, run migrations:

django-admin migrate

To use the RabbitMQ healthcheck, please make sure that there is a variable named BROKER_URL on django.conf.settings with the required format to connect to your rabbit server. For example:

BROKER_URL = amqp://myuser:mypassword@localhost:5672/myvhost

To use the Redis healthcheck, please make sure that there is a variable named REDIS_URL on django.conf.settings with the required format to connect to your redis server. For example:

REDIS_URL = redis://localhost:6370

Setting up monitoring

You can use tools like Pingdom or other uptime robots to monitor service status. The /ht/ endpoint will respond a HTTP 200 if all checks passed and a HTTP 500 if any of the tests failed.

$ curl -v -X GET -H http://www.example.com/ht/

> GET /ht/ HTTP/1.1
> Host: www.example.com
> Accept: */*
>
< HTTP/1.1 200 OK
< Content-Type: text/html; charset=utf-8

<!-- This is an excerpt -->
<div class="container">
    <h1>System status</h1>
    <table>
        <tr>
            <td class="status_1"></td>
            <td>CacheBackend</td>
            <td>working</td>
        </tr>
        <tr>
            <td class="status_1"></td>
            <td>DatabaseBackend</td>
            <td>working</td>
        </tr>
        <tr>
            <td class="status_1"></td>
            <td>S3BotoStorageHealthCheck</td>
            <td>working</td>
        </tr>
    </table>
</div>

Getting machine readable JSON reports

If you want machine readable status reports you can request the /ht/ endpoint with the Accept HTTP header set to application/json or pass format=json as a query parameter.

The backend will return a JSON response:

$ curl -v -X GET -H "Accept: application/json" http://www.example.com/ht/

> GET /ht/ HTTP/1.1
> Host: www.example.com
> Accept: application/json
>
< HTTP/1.1 200 OK
< Content-Type: application/json

{
    "CacheBackend": "working",
    "DatabaseBackend": "working",
    "S3BotoStorageHealthCheck": "working"
}

$ curl -v -X GET http://www.example.com/ht/?format=json

> GET /ht/?format=json HTTP/1.1
> Host: www.example.com
>
< HTTP/1.1 200 OK
< Content-Type: application/json

{
    "CacheBackend": "working",
    "DatabaseBackend": "working",
    "S3BotoStorageHealthCheck": "working"
}

Writing a custom health check

Writing a health check is quick and easy:

from health_check.backends import BaseHealthCheckBackend

class MyHealthCheckBackend(BaseHealthCheckBackend):
    #: The status endpoints will respond with a 200 status code
    #: even if the check errors.
    critical_service = False

    def check_status(self):
        # The test code goes here.
        # You can use `self.add_error` or
        # raise a `HealthCheckException`,
        # similar to Django's form validation.
        pass

    def identifier(self):
        return self.__class__.__name__  # Display name on the endpoint.

After writing a custom checker, register it in your app configuration:

from django.apps import AppConfig

from health_check.plugins import plugin_dir

class MyAppConfig(AppConfig):
    name = 'my_app'

    def ready(self):
        from .backends import MyHealthCheckBackend
        plugin_dir.register(MyHealthCheckBackend)

Make sure the application you write the checker into is registered in your INSTALLED_APPS.

Customizing output

You can customize HTML or JSON rendering by inheriting from MainView in health_check.views and customizing the template_name, get, render_to_response and render_to_response_json properties:

# views.py
from health_check.views import MainView

class HealthCheckCustomView(MainView):
    template_name = 'myapp/health_check_dashboard.html'  # customize the used templates

    def get(self, request, *args, **kwargs):
        plugins = []
        # ...
        if 'application/json' in request.META.get('HTTP_ACCEPT', ''):
            return self.render_to_response_json(plugins, status)
        return self.render_to_response(plugins, status)

    def render_to_response(self, plugins, status):       # customize HTML output
        return HttpResponse('COOL' if status == 200 else 'SWEATY', status=status)

    def render_to_response_json(self, plugins, status):  # customize JSON output
        return JsonResponse(
            {str(p.identifier()): 'COOL' if status == 200 else 'SWEATY' for p in plugins}
            status=status
        )

# urls.py
import views

urlpatterns = [
    # ...
    url(r'^ht/$', views.HealthCheckCustomView.as_view(), name='health_check_custom'),
]

Other resources

  • django-watchman is a package that does some of the same things in a slightly different way.

  • See this weblog about configuring Django and health checking with AWS Elastic Load Balancer.

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-health-check-3.11.1.tar.gz (25.3 kB view details)

Uploaded Source

Built Distribution

django_health_check-3.11.1-py2.py3-none-any.whl (23.7 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file django-health-check-3.11.1.tar.gz.

File metadata

  • Download URL: django-health-check-3.11.1.tar.gz
  • Upload date:
  • Size: 25.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.5.1 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.6.7

File hashes

Hashes for django-health-check-3.11.1.tar.gz
Algorithm Hash digest
SHA256 c9bcc1b3ef77db3a9ba2d33585916707963cbb958220689f77741235f5fa553a
MD5 e02ddaf790a752628a193d474f8d7512
BLAKE2b-256 c93ad0c968c7b20cdb71c8c5b44ada9c419e1008812327d9fe1bc2893b3c8da2

See more details on using hashes here.

File details

Details for the file django_health_check-3.11.1-py2.py3-none-any.whl.

File metadata

  • Download URL: django_health_check-3.11.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 23.7 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.5.1 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.6.7

File hashes

Hashes for django_health_check-3.11.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 06efac9dcd30eb83c3d0184b834d7d86467bdb710da361f20c2c28853d7e6a5e
MD5 81d618736c6e0795b25fa4e00d52c796
BLAKE2b-256 3d2d144cc71b1d963eb58be2e295c2a05e9fe661dd0198fb5a68c7e4fa94742f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page