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.

105 lines
2.9 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. from datetime import timedelta as td
  2. import json
  3. from django.db.models import F
  4. from django.http import HttpResponse, HttpResponseBadRequest, JsonResponse
  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 import schemas
  9. from hc.api.decorators import check_api_key, uuid_or_400, validate_json
  10. from hc.api.models import Check, Ping
  11. @csrf_exempt
  12. @uuid_or_400
  13. @never_cache
  14. def ping(request, code):
  15. try:
  16. check = Check.objects.get(code=code)
  17. except Check.DoesNotExist:
  18. return HttpResponseBadRequest()
  19. check.n_pings = F("n_pings") + 1
  20. check.last_ping = timezone.now()
  21. if check.status == "new":
  22. check.status = "up"
  23. check.save()
  24. check.refresh_from_db()
  25. ping = Ping(owner=check)
  26. headers = request.META
  27. ping.n = check.n_pings
  28. remote_addr = headers.get("HTTP_X_FORWARDED_FOR", headers["REMOTE_ADDR"])
  29. ping.remote_addr = remote_addr.split(",")[0]
  30. ping.scheme = headers.get("HTTP_X_FORWARDED_PROTO", "http")
  31. ping.method = headers["REQUEST_METHOD"]
  32. # If User-Agent is longer than 200 characters, truncate it:
  33. ping.ua = headers.get("HTTP_USER_AGENT", "")[:200]
  34. ping.save()
  35. response = HttpResponse("OK")
  36. response["Access-Control-Allow-Origin"] = "*"
  37. return response
  38. @csrf_exempt
  39. def handle_email(request):
  40. if request.method != "POST":
  41. return HttpResponseBadRequest()
  42. events = json.loads(request.POST["mandrill_events"])
  43. for event in events:
  44. for recipient_address, recipient_name in event["msg"]["to"]:
  45. code, domain = recipient_address.split("@")
  46. try:
  47. check = Check.objects.get(code=code)
  48. except ValueError:
  49. continue
  50. except Check.DoesNotExist:
  51. continue
  52. check.n_pings = F("n_pings") + 1
  53. check.last_ping = timezone.now()
  54. if check.status == "new":
  55. check.status = "up"
  56. check.save()
  57. ping = Ping(owner=check)
  58. ping.scheme = "email"
  59. ping.save()
  60. response = HttpResponse("OK")
  61. return response
  62. @csrf_exempt
  63. @check_api_key
  64. @validate_json(schemas.check)
  65. def create_check(request):
  66. if request.method != "POST":
  67. return HttpResponse(status=405)
  68. check = Check(user=request.user)
  69. check.name = str(request.json.get("name", ""))
  70. check.tags = str(request.json.get("tags", ""))
  71. if "timeout" in request.json:
  72. check.timeout = td(seconds=request.json["timeout"])
  73. if "grace" in request.json:
  74. check.grace = td(seconds=request.json["grace"])
  75. check.save()
  76. # This needs to be done after saving the check, because of
  77. # the M2M relation between checks and channels:
  78. if request.json.get("channels") == "*":
  79. check.assign_all_channels()
  80. response = {
  81. "ping_url": check.url()
  82. }
  83. return JsonResponse(response, status=201)