Skip to content
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
10 changes: 5 additions & 5 deletions README.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Django Ping
===========

Django Ping is utility that provides a lightweight endpoint for availability and uptime monitoring services. It
Django Ping is utility that provides a lightweight endpoint for availability and uptime monitoring services. It
also provides hooks for testing stack components and reporting them via JSON.

Current Version: 0.3.0
Expand All @@ -25,12 +25,12 @@ Hitting the endpoint returns a simple status 200 response.
You can customize the message by adding to your Django settings::

PING_DEFAULT_RESPONSE = "All systems go!"
PING_DEFAULT_MIMETYPE = 'text/html'
PING_DEFAULT_CONTENT_TYPE = 'text/html'

Hitting the url::

/ping

displays::

All systems go!
Expand All @@ -47,7 +47,7 @@ a series of status checks and reports the results.
For example::

/ping?checks=true

displays::

Your site is up!
Expand All @@ -74,7 +74,7 @@ Specifying a ``fmt`` parameter to ``json`` returns more detailed and serialized
For example::

/ping?fmt=json

displays::

{
Expand Down
27 changes: 16 additions & 11 deletions ping/checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@
from django.utils.importlib import import_module
from django.core.exceptions import ImproperlyConfigured

from ping.defaults import *
from django.contrib.auth import get_user_model

from ping.defaults import PING_DEFAULT_CHECKS, PING_CELERY_TIMEOUT


AUTH_USER_MODEL = getattr(settings, "AUTH_USER_MODEL", "auth.User")


def checks(request):
"""
Expand All @@ -11,7 +17,7 @@ def checks(request):
for that check.
"""
response_dict = {}

#Taken straight from Django
#If there is a better way, I don't know it
for path in getattr(settings, 'PING_CHECKS', PING_DEFAULT_CHECKS):
Expand All @@ -25,15 +31,15 @@ def checks(request):
func = getattr(mod, attr)
except AttributeError:
raise ImproperlyConfigured('Module "%s" does not define a "%s" callable' % (module, attr))

key, value = func(request)
response_dict[key] = value

return response_dict

#DEFAULT SYSTEM CHECKS

#Database
#Database
def check_database_sessions(request):
from django.contrib.sessions.models import Session
try:
Expand All @@ -55,15 +61,15 @@ def check_database_sites(request):
CACHE_KEY = 'django-ping-test'
CACHE_VALUE = 'abc123'

def check_cache_set(request):
def check_cache_set(request):
from django.core.cache import cache
try:
cache.set(CACHE_KEY, CACHE_VALUE, 30)
return 'cache_set', True
except:
return 'cache_set', False

def check_cache_get(request):
def check_cache_get(request):
from django.core.cache import cache
try:
data = cache.get(CACHE_KEY)
Expand All @@ -75,12 +81,11 @@ def check_cache_get(request):
return 'cache_get', False


#User
def check_user_exists(request):
from django.contrib.auth.models import User
# User
def check_user_exists(request):
try:
username = request.GET.get('username')
u = User.objects.get(username=username)
get_user_model().objects.get(username=username)
return 'user_exists', True
except:
return 'user_exists', False
Expand All @@ -95,7 +100,7 @@ def check_celery(request):
now = time()
datetimenow = datetime.now()
expires = datetimenow + timedelta(seconds=getattr(settings, 'PING_CELERY_TIMEOUT', PING_CELERY_TIMEOUT))

try:
task = sample_task.apply_async(expires=expires)
while expires > datetime.now():
Expand Down
4 changes: 2 additions & 2 deletions ping/defaults.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
PING_DEFAULT_RESPONSE = "Your site is up!"
PING_DEFAULT_MIMETYPE = 'text/html'
PING_DEFAULT_CONTENT_TYPE = 'text/html'

PING_DEFAULT_CHECKS = (
'ping.checks.check_database_sessions',
Expand All @@ -8,4 +8,4 @@

PING_BASIC_AUTH = False

PING_CELERY_TIMEOUT = 5
PING_CELERY_TIMEOUT = 5
2 changes: 1 addition & 1 deletion ping/urls.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from django.conf.urls.defaults import patterns, include, url
from django.conf.urls import patterns, include, url
from ping.views import status

urlpatterns = patterns('',
Expand Down
15 changes: 8 additions & 7 deletions ping/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json

from django.http import HttpResponse
from django.conf import settings
from django.utils import simplejson
from django.contrib.auth.decorators import login_required
from django.views.decorators.csrf import csrf_exempt

Expand All @@ -14,10 +15,10 @@ def status(request):
"""
Returns a simple HttpResponse
"""

response = "<h1>%s</h1>" % getattr(settings, 'PING_DEFAULT_RESPONSE', PING_DEFAULT_RESPONSE)
mimetype = getattr(settings, 'PING_DEFAULT_MIMETYPE', PING_DEFAULT_MIMETYPE)
content_type = getattr(settings, 'PING_DEFAULT_CONTENT_TYPE', PING_DEFAULT_CONTENT_TYPE)

if request.GET.get('checks') == 'true':
response_dict = checks(request)
response += "<dl>"
Expand All @@ -28,11 +29,11 @@ def status(request):

if request.GET.get('fmt') == 'json':
try:
response = simplejson.dumps(response_dict)
response = json.dumps(response_dict)
except UnboundLocalError:
response_dict = checks(request)
response = simplejson.dumps(response_dict)
response = simplejson.dumps(response_dict, sort_keys=True)
mimetype = 'application/json'
content_type = 'application/json'

return HttpResponse(response, mimetype=mimetype, status=200)
return HttpResponse(response, content_type=content_type, status=200)