You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
2.7 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. import json
  2. from django.contrib.humanize.templatetags.humanize import naturaltime
  3. from django.db.models import F
  4. from django.http import HttpResponse, HttpResponseBadRequest
  5. from django.utils import timezone
  6. from django.views.decorators.cache import never_cache
  7. from django.views.decorators.csrf import csrf_exempt
  8. from hc.api.decorators import uuid_or_400
  9. from hc.api.models import Check, Ping
  10. @csrf_exempt
  11. @uuid_or_400
  12. @never_cache
  13. def ping(request, code):
  14. try:
  15. check = Check.objects.get(code=code)
  16. except Check.DoesNotExist:
  17. return HttpResponseBadRequest()
  18. check.n_pings = F("n_pings") + 1
  19. check.last_ping = timezone.now()
  20. if check.status == "new":
  21. check.status = "up"
  22. check.save()
  23. check.refresh_from_db()
  24. ping = Ping(owner=check)
  25. headers = request.META
  26. ping.n = check.n_pings
  27. remote_addr = headers.get("HTTP_X_FORWARDED_FOR", headers["REMOTE_ADDR"])
  28. ping.remote_addr = remote_addr.split(",")[0]
  29. ping.scheme = headers.get("HTTP_X_FORWARDED_PROTO", "http")
  30. ping.method = headers["REQUEST_METHOD"]
  31. # If User-Agent is longer than 200 characters, truncate it:
  32. ping.ua = headers.get("HTTP_USER_AGENT", "")[:200]
  33. ping.save()
  34. response = HttpResponse("OK")
  35. response["Access-Control-Allow-Origin"] = "*"
  36. return response
  37. @csrf_exempt
  38. def handle_email(request):
  39. if request.method != "POST":
  40. return HttpResponseBadRequest()
  41. events = json.loads(request.POST["mandrill_events"])
  42. for event in events:
  43. for recipient_address, recipient_name in event["msg"]["to"]:
  44. code, domain = recipient_address.split("@")
  45. try:
  46. check = Check.objects.get(code=code)
  47. except ValueError:
  48. continue
  49. except Check.DoesNotExist:
  50. continue
  51. check.n_pings = F("n_pings") + 1
  52. check.last_ping = timezone.now()
  53. if check.status == "new":
  54. check.status = "up"
  55. check.save()
  56. ping = Ping(owner=check)
  57. ping.scheme = "email"
  58. ping.save()
  59. response = HttpResponse("OK")
  60. return response
  61. @uuid_or_400
  62. def status(request, code):
  63. response = {
  64. "last_ping": None,
  65. "last_ping_human": None,
  66. "secs_to_alert": None
  67. }
  68. check = Check.objects.get(code=code)
  69. if check.last_ping and check.alert_after:
  70. response["last_ping"] = check.last_ping.isoformat()
  71. response["last_ping_human"] = naturaltime(check.last_ping)
  72. duration = check.alert_after - timezone.now()
  73. response["secs_to_alert"] = int(duration.total_seconds())
  74. return HttpResponse(json.dumps(response),
  75. content_type="application/javascript")