Пример #1
0
    def push_log_line(self, log_line):
        """
        log_line
            one log entry
        """
        self._sequence += 1

        header = {"hostname"    : _hostname,
                  "uuid"        : self._uuid.hex,
                  "sequence"    : self._sequence,
                  "pid"         : os.getpid(),
                  "timestamp"   : time.time(),
                  "log_path"    : self._log_path}
        if self._nodename is not None:
            header["nodename"] = self._nodename

        header_json = json.dumps(header)
        compressed_header = zlib.compress(header_json.encode("utf-8"))

        compressed_record = zlib.compress(log_line.encode("utf-8"))

        for push_socket in self._push_sockets:
            try:
                push_socket.send(compressed_header, zmq.SNDMORE)
                push_socket.send(compressed_record)
            except zmq.ZMQError:
                instance = sys.exc_info()[1]
                # allow interrupted system call at shutdown
                if not is_interrupted_system_call(instance):
                    raise
Пример #2
0
def main():
    """
    main entry point
    """
    args = _parse_commandline()

    _initialize_logging(args.verbose)
    _log.debug("program starts")

    if is_ipc_protocol(args.push_socket_address):
        prepare_ipc_path(args.push_socket_address)

    context = zmq.Context()

    push_socket = context.socket(zmq.PUSH)
    _log.info("binding push socket to {0}".format(args.push_socket_address))
    push_socket.bind(args.push_socket_address)

    halt_event = set_signal_handler()
    line_count = 0
    _log.debug("opening {0}".format(args.source_path))
    with open(args.source_path, "r") as input_file:
        for line in input_file.readlines():
            halt_event.wait(args.interval)
            if halt_event.is_set():
                _log.warn("breaking read loop: halt_event is set")
                break
            line_count += 1
            try:
                push_socket.send(str(line_count).encode("utf-8"), zmq.SNDMORE)
                push_socket.send(line[:-1].encode("utf-8"))
            except zmq.ZMQError:
                instance = sys.exc_info()[1]
                if is_interrupted_system_call(instance) and halt_event.is_set():
                    pass
                else:
                    raise

    _log.debug("shutting down: published {0} lines".format(line_count))
    push_socket.close()
    context.term()
    return 0