Logging handler to send logs to your OpenSearch cluster with bulk SSL. Forked from https://github.com/logzio/logzio-python-handler
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.

91 lines
2.7 KiB

8 years ago
8 years ago
  1. # noinspection PyUnresolvedReferences
  2. import future
  3. import socket
  4. from http.server import BaseHTTPRequestHandler, HTTPServer
  5. from threading import Thread
  6. from .logsList import logs_list
  7. from .persistentFlags import persistent_flags
  8. class ListenerHandler(BaseHTTPRequestHandler):
  9. def do_POST(self):
  10. try:
  11. content_length = int(self.headers.get("Content-Length"))
  12. all_logs = self.rfile.read(content_length).decode("utf-8").split('\n')
  13. if len(all_logs) == 0:
  14. self._set_response(400, "Bad Request", b"Bad request you got there, pal")
  15. return
  16. for log in all_logs:
  17. if log != "":
  18. if persistent_flags.get_server_error():
  19. self._set_response(500, "Issue!!!!!!!", b"Not good, not good at all.")
  20. return
  21. logs_list.list.append(log)
  22. self._set_response(200, "OK", b"Shabam! got logs.")
  23. return
  24. except IndexError:
  25. self._set_response(400, "Bad Request", b"Bad request you got there, pal")
  26. return
  27. def _set_response(self, http_code, http_description, byte_body):
  28. self.send_response(http_code, http_description)
  29. self.send_header('Content-type', 'text/html')
  30. self.end_headers()
  31. self.wfile.write(byte_body)
  32. class MockLogzioListener:
  33. def __init__(self):
  34. self.port = _find_available_port()
  35. self.host = "localhost"
  36. self.server = HTTPServer((self.host, self.port), ListenerHandler)
  37. self.listening_thread = Thread(target=self._start_listening)
  38. self.listening_thread.daemon = True
  39. self.listening_thread.name = "mock-logzio-listener"
  40. self.listening_thread.start()
  41. self.logs_list = logs_list.list
  42. self.persistent_flags = persistent_flags
  43. def _start_listening(self):
  44. self.server.serve_forever()
  45. def get_port(self):
  46. return self.port
  47. def get_host(self):
  48. return self.host
  49. def find_log(self, search_log):
  50. for current_log in self.logs_list:
  51. if search_log in current_log:
  52. return True
  53. return False
  54. def get_number_of_logs(self):
  55. return len(self.logs_list)
  56. def clear_logs_buffer(self):
  57. self.logs_list[:] = []
  58. def set_server_error(self):
  59. self.persistent_flags.set_server_error()
  60. def clear_server_error(self):
  61. self.persistent_flags.clear_server_error()
  62. def _find_available_port():
  63. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  64. sock.bind(("", 0))
  65. sock.listen(1)
  66. port = sock.getsockname()[1]
  67. sock.close()
  68. return port