Adding db health checks.

This commit is contained in:
Samuel Clay 2015-08-05 19:33:19 -07:00
parent e82b1974a1
commit 9e9318fe6e
3 changed files with 65 additions and 1 deletions

View file

@ -1,9 +1,12 @@
import os
import yaml
import redis
from django.conf import settings
from django.http import HttpResponse
from django.shortcuts import render_to_response
from django.template import RequestContext
from apps.rss_feeds.models import Feed, MStory
from apps.search.models import SearchFeed
from utils import log as logging
def about(request):
@ -65,3 +68,35 @@ def ios_ipa(request):
def haproxy_check(request):
return HttpResponse("OK")
def postgres_check(request):
feed = Feed.objects.latest('pk').pk
if feed:
return HttpResponse(unicode(feed))
assert False, "Cannot read from postgres database"
def mongo_check(request):
stories = MStory.objects.count()
if stories:
return HttpResponse(unicode(stories))
assert False, "Cannot read from mongo database"
def elasticsearch_check(request):
client = SearchFeed.ES()
if client.indices.exists_index(SearchFeed.index_name()):
return HttpResponse(SearchFeed.index_name())
assert False, "Cannot read from elasticsearch database"
def redis_check(request):
pool = request.GET['pool']
if pool == 'main':
r = redis.Redis(connection_pool=settings.REDIS_POOL)
elif pool == 'story':
r = redis.Redis(connection_pool=settings.REDIS_STORY_HASH_POOL)
elif pool == 'sessions':
r = redis.Redis(connection_pool=settings.REDIS_SESSION_POOL)
key = r.randomkey()
if key:
return HttpResponse(unicode(key))
assert False, "Cannot read from redis-%s database" % pool

View file

@ -92,7 +92,32 @@ backend maintenance
http-check expect status 404
http-check send-state
server nginxdebug 127.0.0.1:81 check inter 2000ms
backend postgres
option httpchk GET /_dbcheck/postgres
timeout 5s
server postgres-db01 127.0.0.1:8888 check inter 2000ms
backend mongo
option httpchk GET /_dbcheck/mongo
timeout 5s
server mongo-db22 127.0.0.1:8888 check inter 2000ms
backend redis
option httpchk GET /_dbcheck/redis?pool=main
timeout 5s
server redis-db40 127.0.0.1:8888 check inter 2000ms
backend redis_story
option httpchk GET /_dbcheck/redis?pool=story
timeout 5s
server redis-db41 127.0.0.1:8888 check inter 2000ms
backend redis_sessions
option httpchk GET /_dbcheck/redis?pool=sessions
timeout 5s
server redis-db42 127.0.0.1:8888 check inter 2000ms
backend elasticsearch
option httpchk GET /_dbcheck/elasticsearch
timeout 5s
server elasticsearch-db10 127.0.0.1:8888 check inter 2000ms
frontend stats
bind :1936 ssl crt newsblur.pem
default_backend stats

View file

@ -37,6 +37,10 @@ urlpatterns = patterns('',
(r'^push/', include('apps.push.urls')),
(r'^categories/', include('apps.categories.urls')),
(r'^_haproxychk', static_views.haproxy_check),
(r'^_dbcheck/postgres', static_views.postgres_check),
(r'^_dbcheck/mongo', static_views.mongo_check),
(r'^_dbcheck/redis', static_views.redis_check),
(r'^_dbcheck/elasticsearch', static_views.elasticsearch_check),
url(r'^admin/', include(admin.site.urls)),
url(r'^about/?', static_views.about, name='about'),
url(r'^faq/?', static_views.faq, name='faq'),