Beispiel #1
0
    def run(self):
        """
        Start monitoring the mobile network. This is usually the entrance of monitoring and analysis.

        This function does NOT return or raise any exception.
        """

        # Stop running loggers
        # self._stop_collection()

        self.broadcast_info('STARTED', {})

        generate_diag_cfg = True
        if not self._type_names:
            raise RuntimeError(
                "Log type not specified. Please specify the log types with enable_log()."
            )
            # if os.path.exists(os.path.join(self.DIAG_CFG_DIR, "Diag.cfg")):
            #     generate_diag_cfg = False
            #     # print "AndroidDevDiagMonitor: existing Diag.cfg file will be used."
            # else:
            #     raise RuntimeError("Log type not specified. Please call enable_log() first.")

        try:
            if generate_diag_cfg:
                if not os.path.exists(self.DIAG_CFG_DIR):
                    os.makedirs(self.DIAG_CFG_DIR)

                # fd = open(os.path.join(self.DIAG_CFG_DIR, "Diag.cfg"), "w+b")
                # Overwrite Diag.cfg, not append it
                fd = open(os.path.join(self.DIAG_CFG_DIR, "Diag.cfg"), "wb")
                dm_collector_c.generate_diag_cfg(fd, self._type_names)
                fd.close()

            self._mkfifo(self._fifo_path)

            # Launch diag_revealer, and protection daemon
            self._start_diag_revealer()
            self.diag_revealer_daemon = threading.Thread(
                target=self._protect_diag_revealer)
            self.diag_revealer_daemon.start()

            # fifo = os.open(self._fifo_path, os.O_RDONLY | os.O_NONBLOCK)
            # Blocking mode: save CPU
            fifo = os.open(self._fifo_path, os.O_RDONLY)

            # Read log packets from diag_revealer
            chproc = ChronicleProcessor()
            while True:
                try:
                    # self.log_info("Before os.read(fifo, self.BLOCK_SIZE)")
                    s = os.read(fifo, self.BLOCK_SIZE)
                    # self.log_info("After os.read(fifo, self.BLOCK_SIZE)")
                except OSError as err:
                    if err.errno == errno.EAGAIN or err.errno == errno.EWOULDBLOCK:
                        # self.log_info("err.errno="+str(err.errno))
                        s = None
                    else:
                        raise err  # something else has happened -- better reraise

                while s:  # preprocess metadata
                    # self.log_info("Before chproc.process: %s" % s)
                    ret_msg_type, ret_ts, ret_payload, ret_filename, remain = chproc.process(
                        s)
                    # self.log_info("After chproc.process(s)")
                    if ret_msg_type == ChronicleProcessor.TYPE_LOG:
                        if ret_ts:
                            self._last_diag_revealer_ts = ret_ts
                        if ret_payload:
                            dm_collector_c.feed_binary(ret_payload)
                    elif ret_msg_type == ChronicleProcessor.TYPE_START_LOG_FILE:
                        if ret_filename:
                            pass
                            # print "Start of %s" % ret_filename
                    elif ret_msg_type == ChronicleProcessor.TYPE_END_LOG_FILE:
                        if ret_filename:
                            # res_dict = {'filename':ret_filename}
                            # msg = ("new_diag_log",res_dict,"dict")
                            msg = ('filename', ret_filename, "")
                            # print "End of %s" % ret_filename
                            event = Event(timeit.default_timer(),
                                          "new_diag_log", DMLogPacket([msg]))
                            # ret_filename)
                            self.send(event)
                    elif ret_msg_type is not None:
                        raise RuntimeError("Unknown ret msg type: %s" %
                                           str(ret_msg_type))
                    s = remain

                result = dm_collector_c.receive_log_packet(
                    self._skip_decoding,
                    True,  # include_timestamp
                )
                if result:  # result = (decoded, posix_timestamp)
                    try:
                        packet = DMLogPacket(result[0])
                        d = packet.decode()
                        # print d["type_id"], d["timestamp"], result[1]
                        # xml = packet.decode_xml()
                        # print xml
                        # print ""
                        # Send event to analyzers
                        event = Event(result[1], d["type_id"], packet)
                        self.send(event)
                    except FormatError as e:
                        # skip this packet
                        print "FormatError: ", e

        except (KeyboardInterrupt, RuntimeError) as e:
            os.close(fifo)
            # proc.terminate()
            self._stop_collection()
            packet = DMLogPacket([])
            event = Event(timeit.default_timer(), "sys_shutdown", packet)
            self.send(event)
            import traceback
            sys.exit(str(traceback.format_exc()))
            # sys.exit(e)
        except Exception as e:
            os.close(fifo)
            # proc.terminate()
            self._stop_collection()
            packet = DMLogPacket([])
            event = Event(timeit.default_timer(), "sys_shutdown", packet)
            self.send(event)
            import traceback
            sys.exit(str(traceback.format_exc()))
Beispiel #2
0
    def run(self):
        """
        Start monitoring the mobile network. This is usually the entrance of monitoring and analysis.
        """

        # fd = open('./Diag.cfg','wb')
        # dm_collector_c.generate_diag_cfg(fd, self._type_names)
        # fd.close()

        try:

            self.broadcast_info('STARTED', {})

            log_list = []
            if os.path.isfile(self._input_path):
                log_list = [self._input_path]
            elif os.path.isdir(self._input_path):
                for file in os.listdir(self._input_path):
                    if file.endswith(".mi2log") or file.endswith(".qmdl"):
                        # log_list.append(self._input_path+"/"+file)
                        log_list.append(os.path.join(self._input_path, file))
            else:
                return

            log_list.sort(
            )  # Hidden assumption: logs follow the diag_log_TIMSTAMP_XXX format

            for file in log_list:
                self.log_info("Loading " + file)
                self._input_file = open(file, "rb")
                dm_collector_c.reset()
                while True:
                    s = self._input_file.read(64)
                    if not s:  # EOF encountered
                        break

                    dm_collector_c.feed_binary(s)
                    # decoded = dm_collector_c.receive_log_packet()
                    decoded = dm_collector_c.receive_log_packet(
                        self._skip_decoding,
                        True,  # include_timestamp
                    )
                    if decoded:
                        try:
                            # packet = DMLogPacket(decoded)
                            packet = DMLogPacket(decoded[0])
                            # print "DMLogPacket decoded[0]:",str(decoded[0])
                            d = packet.decode()
                            # print d["type_id"], d["timestamp"]
                            # xml = packet.decode_xml()
                            # print xml
                            # print ""
                            # Send event to analyzers

                            if d["type_id"] in self._type_names:
                                event = Event(timeit.default_timer(),
                                              d["type_id"], packet)
                                self.send(event)

                        except FormatError as e:
                            # skip this packet
                            print "FormatError: ", e

                self._input_file.close()

        except Exception as e:
            import traceback
            sys.exit(str(traceback.format_exc()))
    def run(self):
        """
        Start monitoring the mobile network. This is usually the entrance of monitoring and analysis.
        """

        # fd = open('./Diag.cfg','wb')
        # dm_collector_c.generate_diag_cfg(fd, self._type_names)
        # fd.close()

        try:

            self.broadcast_info('STARTED', {})
            self.log_info('STARTED: ' + str(time.time()))
            log_list = []
            if os.path.isfile(self._input_path):
                log_list = [self._input_path]
            elif os.path.isdir(self._input_path):
                for file in os.listdir(self._input_path):
                    if file.endswith(".mi2log") or file.endswith(".qmdl"):
                        # log_list.append(self._input_path+"/"+file)
                        log_list.append(os.path.join(self._input_path, file))
            else:
                return

            log_list.sort(
            )  # Hidden assumption: logs follow the diag_log_TIMSTAMP_XXX format

            decoding_inter = 0
            sending_inter = 0
            for file in log_list:
                self.log_info("Loading " + file)
                self.log_info('Loading: ' + str(time.time()))
                self._input_file = open(file, "rb")
                dm_collector_c.reset()
                while True:
                    s = self._input_file.read(64)
                    if not s:  # EOF encountered
                        break

                    dm_collector_c.feed_binary(s)
                    decoded = dm_collector_c.receive_log_packet(
                        self._skip_decoding,
                        True,  # include_timestamp
                    )
                    if decoded:
                        try:
                            before_decode_time = time.time()
                            # self.log_info('Before decoding: ' + str(time.time()))
                            packet = DMLogPacket(decoded[0])
                            d = packet.decode()
                            after_decode_time = time.time()
                            decoding_inter += after_decode_time - before_decode_time

                            if d["type_id"] in self._type_names:
                                event = Event(timeit.default_timer(),
                                              d["type_id"], packet)
                                self.send(event)
                            after_sending_time = time.time()
                            sending_inter += after_sending_time - after_decode_time
                            # pself.log_info('After sending event: ' + str(time.time()))

                        except FormatError as e:
                            # skip this packet
                            print "FormatError: ", e
                self.log_info('Decoding_inter: ' + str(decoding_inter))
                self.log_info('sending_inter: ' + str(sending_inter))
                self._input_file.close()

        except Exception as e:
            import traceback
            sys.exit(str(traceback.format_exc()))
            # sys.exit(e)
        event = Event(timeit.default_timer(), 'Monitor.STOP', None)
        self.send(event)
        self.log_info("Offline replay is completed.")