对数据库、队列服务器、芹菜进程等服务运行检查。
django-health-check的Python项目详细描述
此项目检查各种情况,并在异常情况下提供报告 检测到行为。
以下健康检查与此项目捆绑在一起:
- 缓存
- 数据库
- 存储
- 磁盘和内存利用率(通过psutil)
- AWS S3存储
- 芹菜任务队列
- 拉比特mq
编写您自己的自定义健康检查也非常快速和容易。
我们也喜欢捐款,所以不要害怕提出要求。
用例
主要的预期用例是通过http(s)监视条件,其中 响应以HTML和JSON格式提供。当你得到回复时 包括一个或多个问题,然后您可以决定 操作,包括生成通知和/或自动化 用新节点替换故障节点。如果你正在监测 具有负载平衡器的高可用性环境,它返回来自 多个节点,请注意某些检查(例如磁盘和内存使用情况) 将返回特定于负载平衡器所选节点的响应。
支持的版本
我们正式只支持最新版本的python以及 最新版本的django和最新的django lts版本。
注意
支持Python2的最新版本是2.4.0
安装
首先安装django-health-check包:
pip install django-health-check
将健康检查程序添加到您要使用的URL:
urlpatterns=[# ...url(r'^ht/',include('health_check.urls')),]
将health_check应用程序添加到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]
(可选)如果使用psutil应用程序,则可以配置磁盘和内存 阈值设置;否则假定低于默认值。如果你想禁用 其中一个检查,将其值设置为None。
HEALTH_CHECK={'DISK_USAGE_MAX':90,# percent'MEMORY_MIN':100,# in MB}
如果使用数据库检查,请运行迁移:
django-admin migrate
要使用rabbitmq healthcheck,请确保有一个名为^{tt7}的变量$ 在django.conf.settings上使用连接到Rabbit服务器所需的格式。例如:
BROKER_URL = amqp://myuser:mypassword@localhost:5672/myvhost
设置监视
您可以使用诸如Pingdom或其他正常运行时间机器人之类的工具来监视服务状态。 如果所有检查都通过,/ht/端点将响应http 200 如果有任何测试失败,则返回http 500。
$ 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>
获取机器可读的json报告
如果您想要机器可读的状态报告,可以请求/ht/ 将Accepthttp头设置为^{tt11}的终结点$ 或者将format=json作为查询参数传递。
后端将返回一个json响应:
$ 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/?json=1 > 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" }
编写自定义健康检查
写健康检查既快捷又简单:
fromhealth_check.backendsimportBaseHealthCheckBackendclassMyHealthCheckBackend(BaseHealthCheckBackend):#: The status endpoints will respond with a 200 status code#: even if the check errors.critical_service=Falsedefcheck_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.
编写自定义检查器后,请在应用程序配置中注册它:
fromdjango.appsimportAppConfigfromhealth_check.pluginsimportplugin_dirclassMyAppConfig(AppConfig):name='my_app'defready(self):from.backendsimportMyHealthCheckBackendplugin_dir.register(MyHealthCheckBackend)
确保将检查程序写入的应用程序已在INSTALLED_APPS中注册。
自定义输出
通过从^{tt15}中的MainView继承,可以自定义HTML或JSON呈现$ 以及自定义template_name、get、render_to_response和render_to_response_json属性:
# 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'),]
其他资源
- django-watchman是一个以稍微不同的方式执行某些相同操作的包。
- 有关使用aws弹性负载平衡器配置django和运行状况检查的信息,请参见weblog。