From f5f2f8002dcb17e844f362feb1f231c7d9e7398b Mon Sep 17 00:00:00 2001 From: Garrett Pennington Date: Sat, 15 Dec 2012 22:25:22 -0600 Subject: [PATCH 1/7] added a POST example check --- README.rst | 3 +++ ping/checks.py | 33 ++++++++++++++++++++++++++++++++- ping/views.py | 2 ++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/README.rst b/README.rst index 20f2475..f65a2bb 100644 --- a/README.rst +++ b/README.rst @@ -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 VersionL 0.3.0 +https://github.com/gpennington/django-ping + Installation ------------ diff --git a/ping/checks.py b/ping/checks.py index fd62e95..b004a1c 100644 --- a/ping/checks.py +++ b/ping/checks.py @@ -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: @@ -72,3 +75,31 @@ 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 + + +#Post 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', True + else: + u.delete() + new_user = User.objects.create(username=username) + return 'create_user', True + else: + return 'create_user', False + except: + return 'create_user', False diff --git a/ping/views.py b/ping/views.py index eeab942..0ce897b 100644 --- a/ping/views.py +++ b/ping/views.py @@ -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): """ From e76cf8d1046d102926d58ed6be63a9914a1907c8 Mon Sep 17 00:00:00 2001 From: Garrett Pennington Date: Mon, 17 Dec 2012 15:13:34 -0600 Subject: [PATCH 2/7] added POST example in readme --- README.rst | 17 +++++++++++++++++ ping/checks.py | 19 ------------------- 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/README.rst b/README.rst index f65a2bb..8afdea2 100644 --- a/README.rst +++ b/README.rst @@ -104,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 ~~~~~~~~~~~~~~~~~~~~~~ diff --git a/ping/checks.py b/ping/checks.py index b004a1c..b12c4a0 100644 --- a/ping/checks.py +++ b/ping/checks.py @@ -84,22 +84,3 @@ def check_user_exists(request): return 'user_exists', True except: return 'user_exists', False - - -#Post 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', True - else: - u.delete() - new_user = User.objects.create(username=username) - return 'create_user', True - else: - return 'create_user', False - except: - return 'create_user', False From 84081eea11a0119cc6af6ba296ba7f03c03b1dfc Mon Sep 17 00:00:00 2001 From: Garrett Pennington Date: Mon, 24 Dec 2012 14:48:55 -0600 Subject: [PATCH 3/7] added basic celery check --- ping/checks.py | 21 +++++++++++++++++++++ ping/tasks.py | 5 +++++ 2 files changed, 26 insertions(+) create mode 100644 ping/tasks.py diff --git a/ping/checks.py b/ping/checks.py index b12c4a0..14a84aa 100644 --- a/ping/checks.py +++ b/ping/checks.py @@ -84,3 +84,24 @@ def check_user_exists(request): return 'user_exists', True except: return 'user_exists', False + + +#Celery +def check_celery(request): + from datetime import datetime, timedelta + from time import sleep + from ping.tasks import sample_task + + now = datetime.now() + expires = now + timedelta(seconds=5)#settings + + try: + task = sample_task.apply_async(expires=expires) + while expires > datetime.now(): + if task.ready() and task.result == True: + return 'celery', True + sleep(0.2) + return 'celery', False + except Exception, e: + print e + return 'celery', False diff --git a/ping/tasks.py b/ping/tasks.py new file mode 100644 index 0000000..ac2ce1a --- /dev/null +++ b/ping/tasks.py @@ -0,0 +1,5 @@ +from celery.task import task + +@task() +def sample_task(): + return True \ No newline at end of file From 202dbbbe4aeb72c67513b5bf77c74fd6fa7a8735 Mon Sep 17 00:00:00 2001 From: Garrett Pennington Date: Tue, 25 Dec 2012 16:12:55 -0600 Subject: [PATCH 4/7] docs and default celery timeout --- README.rst | 13 +++++++++++++ ping/checks.py | 2 +- ping/defaults.py | 4 +++- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/README.rst b/README.rst index 8afdea2..09e17bf 100644 --- a/README.rst +++ b/README.rst @@ -136,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 ~~~~~~~~~~~~~~ @@ -145,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? ------------ diff --git a/ping/checks.py b/ping/checks.py index 14a84aa..9f9b156 100644 --- a/ping/checks.py +++ b/ping/checks.py @@ -93,7 +93,7 @@ def check_celery(request): from ping.tasks import sample_task now = datetime.now() - expires = now + timedelta(seconds=5)#settings + expires = now + timedelta(seconds=getattr(settings, 'PING_CELERY_TIMEOUT', PING_CELERY_TIMEOUT)) try: task = sample_task.apply_async(expires=expires) diff --git a/ping/defaults.py b/ping/defaults.py index cb1f551..466c225 100644 --- a/ping/defaults.py +++ b/ping/defaults.py @@ -6,4 +6,6 @@ 'ping.checks.check_database_sites', ) -PING_BASIC_AUTH = False \ No newline at end of file +PING_BASIC_AUTH = False + +PING_CELERY_TIMEOUT = 5 \ No newline at end of file From 6c3b5cbab6704e9d1bbe4e7159301d5b2b5b309f Mon Sep 17 00:00:00 2001 From: Garrett Pennington Date: Tue, 25 Dec 2012 16:42:45 -0600 Subject: [PATCH 5/7] celery check returns time taken to complete --- ping/checks.py | 21 +++++++++++---------- ping/defaults.py | 2 +- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/ping/checks.py b/ping/checks.py index 9f9b156..85e0f55 100644 --- a/ping/checks.py +++ b/ping/checks.py @@ -89,19 +89,20 @@ def check_user_exists(request): #Celery def check_celery(request): from datetime import datetime, timedelta - from time import sleep + from time import sleep, time from ping.tasks import sample_task - now = datetime.now() - expires = now + timedelta(seconds=getattr(settings, 'PING_CELERY_TIMEOUT', PING_CELERY_TIMEOUT)) - + 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: - return 'celery', True - sleep(0.2) - return 'celery', False - except Exception, e: - print e - return 'celery', False + finished = str(time() - now) + return 'celery', { 'success': True, 'time':finished } + sleep(0.25) + return 'celery', { 'success': False } + except Exception: + return 'celery', { 'success': False } diff --git a/ping/defaults.py b/ping/defaults.py index 466c225..9f9a23f 100644 --- a/ping/defaults.py +++ b/ping/defaults.py @@ -8,4 +8,4 @@ PING_BASIC_AUTH = False -PING_CELERY_TIMEOUT = 5 \ No newline at end of file +PING_CELERY_TIMEOUT = 5 \ No newline at end of file From 4eee66db236fb699f4c042fbd96daa0b54333e7d Mon Sep 17 00:00:00 2001 From: Garrett Pennington Date: Tue, 25 Dec 2012 16:48:58 -0600 Subject: [PATCH 6/7] typo --- README.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.rst b/README.rst index 09e17bf..0d4143a 100644 --- a/README.rst +++ b/README.rst @@ -4,7 +4,7 @@ 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 VersionL 0.3.0 +Current Version: 0.3.0 https://github.com/gpennington/django-ping Installation From 1f9e3101378281b29432ab8369c679112deea3ea Mon Sep 17 00:00:00 2001 From: Tom Offermann Date: Sat, 9 Mar 2013 22:42:33 -0800 Subject: [PATCH 7/7] Sort status checks. If a monitoring service is looking for a specific string to be present in the response from /ping to test if the Web site is up and running, then consistent, sorted status check results are necessary to avoid false positives. --- ping/views.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ping/views.py b/ping/views.py index 0ce897b..82cacd4 100644 --- a/ping/views.py +++ b/ping/views.py @@ -21,7 +21,7 @@ def status(request): if request.GET.get('checks') == 'true': response_dict = checks(request) response += "
" - for key, value in response_dict.items(): + for key, value in sorted(response_dict.items()): response += "
%s
" % str(key) response += "
%s
" % str(value) response += "
" @@ -32,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) \ No newline at end of file