Package: django-health-checkers
This project checks for various conditions and provides reports when anomalous
behavior is detected.The following health checks are bundled with this project:cachedatabasestoragedisk and memory utilization (viapsutil)AWS S3 storageCelery task queueWriting 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 CasesThe 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 VersionsWe officially only support the latest version of Python as well as the
latest version of Django and the latest Django LTS version.NoteThe latest version to support Python 2 is 2.4.0InstallationFirst install thedjango-health-checkpackage:pip install django-health-checkAdd the health checker to a URL you want to use:urlpatterns=[# ...url(r'^ht/',include('health_check.urls')),]Add thehealth_checkapplications to yourINSTALLED_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](Optional) If using thepsutilapp, 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 toNone.HEALTH_CHECK={'DISK_USAGE_MAX':90,# percent'MEMORY_MIN'=100,# in MB}If using the DB check, run migrations:django-admin migrateSetting up monitoringYou can use tools likePingdomor 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 reportsIf you want machine readable status reports you can request the/ht/endpoint with theAcceptHTTP header set toapplication/json.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"
}Writing a custom health checkWriting a health check is quick and easy:fromhealth_check.backendsimportBaseHealthCheckBackendclassMyHealthCheckBackend(BaseHealthCheckBackend):defcheck_status(self):# The test code goes here.# You can use `self.add_error` or# raise a `HealthCheckException`,# similar to Django's form validation.passdefidentifier(self):returnself.__class__.__name__# Display name on the endpoint.After writing a custom checker, register it in your app configuration:fromdjango.appsimportAppConfigfromhealth_check.pluginsimportplugin_dirclassMyAppConfig(AppConfig):name='my_app'defready(self):from.backendsimportMyHealthCheckBackendplugin_dir.register(MyHealthCheckBackend)Make sure the application you write the checker into is registered in yourINSTALLED_APPS.Customizing outputYou can customize HTML or JSON rendering by inheriting fromMainViewinhealth_check.viewsand customizing thetemplate_name,get,render_to_responseandrender_to_response_jsonproperties:# views.pyfromhealth_check.viewsimportMainViewclassHealthCheckCustomView(MainView):template_name='myapp/health_check_dashboard.html'# customize the used templatesdefget(self,request,*args,**kwargs):plugins=[]# ...if'application/json'inrequest.META.get('HTTP_ACCEPT',''):returnself.render_to_response_json(plugins,status)returnself.render_to_response(plugins,status)defrender_to_response(self,plugins,status):# customize HTML outputreturnHttpResponse('COOL'ifstatus==200else'SWEATY',status=status)defrender_to_response_json(self,plugins,status):# customize JSON outputreturnJsonResponse({str(p.identifier()):'COOL'ifstatus==200else'SWEATY'forpinplugins}status=status)# urls.pyimportviewsurlpatterns=[# ...url(r'^ht/$',views.HealthCheckCustomView.as_view(),name='health_check_custom'),]Other resourcesdjango-watchmanis a package that does some of the same things in a slightly different way.See thisweblogabout configuring Django and health checking with AWS Elastic Load Balancer.