def upload_log(filename): succeed = False form = MultiPartForm() form.add_field('file[]', filename) form.add_file('file', filename) request = urllib2.Request( 'http://metro.cs.ucla.edu/mobile_insight/upload_file.php') request.add_header("Connection", "Keep-Alive") request.add_header("ENCTYPE", "multipart/form-data") request.add_header('Content-Type', form.get_content_type()) body = str(form) request.add_data(body) try: response = urllib2.urlopen(request, timeout=3).read() if response.startswith("TW9iaWxlSW5zaWdodA==FILE_SUCC") \ or response.startswith("TW9iaWxlSW5zaWdodA==FILE_EXST"): succeed = True except urllib2.URLError as e: pass except socket.timeout as e: pass if succeed is True: try: file_base_name = os.path.basename(filename) uploaded_file = os.path.join( util.get_mobileinsight_log_uploaded_path(), file_base_name) # TODO: print to screen # print "debug 58, file uploaded has been renamed to %s" % uploaded_file # shutil.copyfile(filename, uploaded_file) util.run_shell_cmd("cp %s %s" % (filename, uploaded_file)) os.remove(filename) finally: util.detach_thread()
def _logger_filter(self, msg): """ Callback to process new generated logs. :param msg: the message from trace collector. :type msg: Event """ # when a new log comes, save it to external storage and upload if msg.type_id.find("new_diag_log") != -1: self.__log_timestamp = datetime.datetime.now().strftime( '%Y%m%d_%H%M%S') self.__orig_file = msg.data.decode().get("filename") # FIXME (Zengwen): the change access command is a walkaround # solution util.run_shell_cmd("chmod 777 %s" % self.__orig_file) self._save_log() self.__is_wifi_enabled = util.get_wifi_status() if self.__is_use_wifi is True and self.__is_wifi_enabled is True and self.__upload_log_flag is True: try: for f in os.listdir(self.__log_dir): if f.endswith(".mi2log"): orphan_file = os.path.join(self.__log_dir, f) t = threading.Thread(target=upload_log, args=(orphan_file, )) t.start() except Exception as e: pass else: # use cellular data to upload. Skip for now. pass if self.__is_dec_log is True: if self.__dec_log_type == "LTE Control Plane": if (msg.type_id.startswith("LTE_RRC") or msg.type_id.startswith("LTE_NAS")): self._decode_msg(msg) elif self.__dec_log_type == "LTE Control/Data Plane": if (msg.type_id.startswith("LTE") and not msg.type_id.startswith("LTE_PHY")): self._decode_msg(msg) elif self.__dec_log_type == "LTE Control/Data/PHY": if (msg.type_id.startswith("LTE")): self._decode_msg(msg) elif self.__dec_log_type == "LTE/3G Control Plane": if ("RRC" in msg.type_id or "NAS" in msg.type_id): self._decode_msg(msg) elif self.__dec_log_type == "All": if (msg.type_id.startswith("LTE") or msg.type_id.startswith("WCDMA") or msg.type_id.startswith("UMTS")): self._decode_msg(msg) else: pass else: pass
def _check_orphan_log(self): ''' Check if there is any orphan log left in cache folder ''' dated_files = [] mi2log_folder = os.path.join(util.get_cache_dir(), "mi2log") orphan_filename = "" for subdir, dirs, files in os.walk(mi2log_folder): for f in files: fn = os.path.join(subdir, f) dated_files.append((os.path.getmtime(fn), fn)) dated_files.sort() dated_files.reverse() for dated_file in dated_files: self.__orig_file = dated_file[1] # print "self.__orig_file = %s" % str(self.__orig_file) # print "self.__orig_file modified time = %s" % str(time.strftime('%Y%m%d_%H%M%S', time.localtime(os.path.getmtime(self.__orig_file)))) util.run_shell_cmd("chmod 777 %s" % self.__orig_file) orphan_filename = self._save_log() self.log_info("Found undersized orphan log, file saved to %s" % orphan_filename)
def _logger_filter(self, msg): """ Callback to process new generated logs. :param msg: the message from trace collector. :type msg: Event """ if msg.type_id == "LTE_NAS_EMM_OTA_Outgoing_Packet": log_item = msg.data.decode() if msgs[0] in log_item['Msg'].lower(): # self.log_warning("Service request") if self.trigger(msgs[0]): self.round += 1 self.start_ts = log_item['timestamp'] self.log_warning("Now state == %s" % self.state) self.record['round'] = self.sr_round self.record['msg'].append( "[%s; %s]" % (log_item['timestamp'], self.state)) if msg.type_id == "LTE_RRC_OTA_Packet": log_item = msg.data.decode() for i in range(1, 8): if msgs[i] in log_item['Msg'].lower(): if self.trigger(msgs[i]): # convert the rrc message timestamp to epoch epoch = time.mktime( time.strptime( log_item['timestamp'].strftime( "%Y-%m-%d %H:%M:%S"), "%Y-%m-%d %H:%M:%S")) # ts.strftime('%Y%m%d_%H%M%S') # time.strptime("2008-09-17 14:04:00", "%Y-%m-%d %H:%M:%S") # self.log_warning("%s" % epoch) # epoch = (datetime.datetime.utcfromtimestamp(ts) - datetime.datetime(1970,1,1)).total_seconds() self.log_warning( "[epoch = %s][log_utc = %s] Incoming RRC msg[%d] = %s" % (epoch, str(log_item['timestamp']), i, msgs[i])) # self.log_warning("Now state == %s" % self.state) # self.record['round'] = self.sr_round self.record['msg'].append( "[%s; %s]" % (log_item['timestamp'], self.state)) if msgs[i] == msgs[-1]: new_ts = (log_item['timestamp'] - self.start_ts).microseconds / 1000 self.log_warning( "Total SR time for round %d = %s ms" % (self.sr_round, new_ts)) self.record['msg'].append( "[%s; %s; %s]" % (epoch, log_item['timestamp'], self.state)) self.record['total'] = "%d" % new_ts try: with open(self.__sr_log_path, 'a') as f: f.writelines(str(self.record)) f.writelines('\n') except BaseException: pass self.sr_record.append(self.record) # self.log_warning("%s" % (self.record)) # self.log_warning("%s" % (self.sr_record)) self.record = {'round': "", 'msg': [], 'total': ""} # when a new log comes, save it to external storage and upload if msg.type_id.find("new_diag_log") != -1: self.__log_timestamp = datetime.now().strftime('%Y%m%d_%H%M%S') self.__orig_file = msg.data.decode().get("filename") # FIXME (Zengwen): the change access command is a walkaround # solution util.run_shell_cmd("chmod 644 %s" % self.__orig_file) self._save_log() self.__is_wifi_enabled = util.get_wifi_status() if self.__is_use_wifi is True and self.__is_wifi_enabled is True: try: for f in os.listdir(self.__log_dir): if f.endswith(".mi2log"): orphan_file = os.path.join(self.__log_dir, f) t = threading.Thread(target=upload_log, args=(orphan_file, )) t.start() except Exception as e: pass else: # use cellular data to upload. Skip for now. pass