def check_cmdlines(): try: opts, args = getopt.getopt(sys.argv[1:], "ht:p", ["type=", "path=", "mode="]) except getopt.GetoptError: print "gen_nelog.py --type <netype> --path <target path> --mode <start mode>" sys.exit() netype = tgt_path = start_mode = "" for opt, value in opts: if opt == "-h": print "gen_nelog.py -t <netype> -p <target path>" sys.exit() elif opt in ["-t", "--type"]: netype = value elif opt in ["-p", "--path"]: tgt_path = value elif opt in ["-m", "--mode"]: start_mode = value if netype != "HSS" and netype != "IMS" and netype != "LBS" and netype != "OCGAS" and netype != "EPG": print "only HSS,IMS and LBS are supported" sys.exit() if not os.path.exists(tgt_path): msg = tgt_path + " not existed, and trying to create one" writeLog(msg) os.makedirs(tgt_path) return netype, tgt_path, start_mode
def copy_file(src_file, tgt_file): if os.path.exists(src_file): msg = "Copy file from '" + src_file + "' to '" + tgt_file + "'." print msg writeLog(msg) shutil.copyfile(src_file, tgt_file) else: msg = src_file + " not existed." print msg writeLog(msg)
def change_logtime_hss(srcfile, tgtfile, newtime, netype): f_s = open(srcfile, "rb") # for iwu newlines = "" newtime = newtime + timedelta(minutes=-5) for line in f_s.readlines(): if line: time1 = newtime.strftime("%Y-%m-%d") time2 = newtime.strftime("%H:%M:%S.") + newtime.strftime("%f")[:3] arrInfo = line.strip().split("\t") arrInfo[1] = time1 + " " + time2 new_line = "\t".join(arrInfo) newlines = newlines + new_line + "\n" newtime = newtime + pydate.timedelta(milliseconds=30) newlines.rstrip("\n") f_s.close() if newlines: f_t = open(tgtfile, "wb") print "Start to generate file " + tgtfile writeLog("Start to generate file " + tgtfile) f_t.writelines(newlines) f_t.close()
def write_nelog(netype, tgt_path, start_mode): if start_mode == "m": root_dir = run_dir + os.sep + "conf" + os.sep + "spm-templates" + os.sep + "NELog" else: root_dir = run_dir + os.sep + "conf" + os.sep + "templates" + os.sep + "NELog" while True: nowtime = pydate.datetime.now() if nowtime.minute % 5 == 0: if netype == "HSS": writeLog("start to write neLog for " + netype) src_path = root_dir + os.sep + "HSS" if not os.path.exists(src_path): msg = src_path + " not existed. pls create one" print msg writeLog(msg) sys.exit() gen_nelog_hss(src_path, tgt_path, nowtime, netype) elif netype == "IMS": src_path = root_dir + os.sep + "IMS" if not os.path.exists(src_path): msg = src_path + " not existed. pls create one" print msg writeLog(msg) sys.exit() gen_nelog_ims(src_path, tgt_path, nowtime, netype) elif netype == "LBS": src_path = root_dir + os.sep + "LBS" if not os.path.exists(src_path): msg = src_path + " not existed.pls create one" print msg writeLog(msg) sys.exit() if start_mode == "m": gen_nelog_lbs_spm(src_path, tgt_path, nowtime) else: gen_nelog_lbs(src_path, tgt_path, nowtime, netype) elif netype == "OCGAS": src_path = root_dir + os.sep + "OCGAS" if not os.path.exists(src_path): msg = src_path + " not existed.pls create one" print msg writeLog(msg) sys.exit() if start_mode == "m": gen_nelog_ocgas_spm(src_path, tgt_path, nowtime) else: gen_nelog_ocgas(src_path, tgt_path, nowtime, netype) elif netype == "EPG": src_path = root_dir + os.sep + "EPG" if not os.path.exists(src_path): msg = src_path + " not existed.pls create one" print msg writeLog(msg) sys.exit() gen_nelog_epg(src_path, tgt_path) sleep(60) else: sleep(10)
def check_clean(nowtime, src_path): if nowtime.strftime("%H:%M") == "00:00": f = open(src_path, "w") f.truncate() f.close() writeLog(src_path + "truncated successfully")
def append_new_log(src_path, newlines): f = open(src_path, "a") f.write(newlines) f.close() writeLog("Append log to " + src_path + " successfully")
msg = src_path + " not existed.pls create one" print msg writeLog(msg) sys.exit() gen_nelog_epg(src_path, tgt_path) sleep(60) else: sleep(10) if __name__ == "__main__": try: startLog("gen_nelog.txt") netype, tgt_path, start_mode = check_cmdlines() t_cleanthread = deletePM.RmPmEveryHour(tgt_path, "rmnelog.txt") t_cleanthread.start() write_nelog(netype, tgt_path, start_mode) except KeyboardInterrupt: if t_cleanthread is not None: msg = "will kill the clean file thread...." writeLog(msg) t_cleanthread.stop() msg = "user try to stop the tool manually, I am going to exit now..." writeLog(msg) stopLog() t_cleanthread.stop() stopLog() os.kill(os.getpid(), 9)