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
33 changes: 33 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ Django Ping
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
https://github.com/gpennington/django-ping

Installation
------------

Expand Down Expand Up @@ -101,6 +104,23 @@ Then, add that to the ``PING_CHECKS`` tuple to display::
'foo', true
}

You can send ``POST`` requests too. As an example::

def check_create_user(request):
from django.contrib.auth.models import User
try:
if request.method == 'POST':
username = request.GET.get('username')
u, created = User.objects.get_or_create(username=username)
if created:
return 'create_user', "User: %s has been created" % u.username
else:
return 'create_user', "User: %s already exists" % u.username
else:
return 'create_user', "User cannot be created with GET"
except:
return 'create_user', "User not created"


Included Status Checks
~~~~~~~~~~~~~~~~~~~~~~
Expand All @@ -116,6 +136,8 @@ live.

**check_cache_get** - Attempts to retrieve a cached value using the current cache backend defined.

**check_celery** - Adds a task to the queue and checks for celery to complete it.


Authentication
~~~~~~~~~~~~~~
Expand All @@ -125,6 +147,17 @@ set ``PING_BASIC_AUTH`` to ``True`` in your Django settings.

Provide in the request the username/password of a valid User.

Complete Settings List
~~~~~~~~~~~~~~~~~~~~~~~~~

Check ``ping.defaults`` for default values.

PING_RESPONSE = "Some string"
PING_MIMETYPE = 'text/html' or valid content type
PING_DEFAULT_CHECKS = tuple of paths to check methods
PING_BASIC_AUTH = Boolean (default is False)
PING_CELERY_TIMEOUT = In seconds as integers (5 is default)


What's Next?
------------
Expand Down
36 changes: 35 additions & 1 deletion ping/checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ def checks(request):
response_dict[key] = value

return response_dict


#DEFAULT SYSTEM CHECKS

#Database
def check_database_sessions(request):
from django.contrib.sessions.models import Session
try:
Expand Down Expand Up @@ -72,3 +75,34 @@ def check_cache_get(request):
return 'cache_get', False


#User
def check_user_exists(request):
from django.contrib.auth.models import User
try:
username = request.GET.get('username')
u = User.objects.get(username=username)
return 'user_exists', True
except:
return 'user_exists', False


#Celery
def check_celery(request):
from datetime import datetime, timedelta
from time import sleep, time
from ping.tasks import sample_task

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():
if task.ready() and task.result == True:
finished = str(time() - now)
return 'celery', { 'success': True, 'time':finished }
sleep(0.25)
return 'celery', { 'success': False }
except Exception:
return 'celery', { 'success': False }
4 changes: 3 additions & 1 deletion ping/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@
'ping.checks.check_database_sites',
)

PING_BASIC_AUTH = False
PING_BASIC_AUTH = False

PING_CELERY_TIMEOUT = 5
5 changes: 5 additions & 0 deletions ping/tasks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from celery.task import task

@task()
def sample_task():
return True
6 changes: 4 additions & 2 deletions ping/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
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

from ping.defaults import *
from ping.checks import checks
from ping.decorators import http_basic_auth

@csrf_exempt
@http_basic_auth
def status(request):
"""
Expand All @@ -19,7 +21,7 @@ def status(request):
if request.GET.get('checks') == 'true':
response_dict = checks(request)
response += "<dl>"
for key, value in response_dict.items():
for key, value in sorted(response_dict.items()):
response += "<dt>%s</dt>" % str(key)
response += "<dd>%s</dd>" % str(value)
response += "</dl>"
Expand All @@ -30,7 +32,7 @@ def status(request):
except UnboundLocalError:
response_dict = checks(request)
response = simplejson.dumps(response_dict)
response = simplejson.dumps(response_dict)
response = simplejson.dumps(response_dict, sort_keys=True)
mimetype = 'application/json'

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