def main(): if '--calc' in sys.argv: if len(sys.argv) != 4: print "[*]Useage: python main.py group_id clusterFileName" exit() else: cross_cluster(sys.argv[1], sys.argv[2]) exit() if '--status' in sys.argv: init_options() show_database_status() exit() if len(sys.argv) < 3 or '-h' in sys.argv: helpmsg() cpanel.group = sys.argv[1] init_options() read_packets(cpanel.group) split_flow() split_cflow() scale_cflow() if '--save' in sys.argv: save_calc_results() if '--debug' in sys.argv: test() if '--outfile' in sys.argv: output_path = './output/results.txt' save(output_path)
def execution_path(df_train_log, df_test_log): # 提取normal日志键执行流 train_log_key_sequence_str = " ".join([str(EventId) for EventId in df_train_log["EventId"]]) # 将日志流通过滑动窗口分为4个日志为一个日志序列 X_train, Y_train = standard_log_key(train_log_key_sequence_str) # 对日志序列进行lstm训练和测试 lg = log_key_model() execution_path_model_filepath = "tmpdata/ExecutePathModel/model.pkl" if os.path.isfile(execution_path_model_filepath): model = load(execution_path_model_filepath) else: model = lg.train(X_train, Y_train) save(execution_path_model_filepath, model) # 提取test日志键执行流 test_log_key_sequence_str = " ".join([str(EventId) for EventId in df_test_log["EventId"]]) # 将日志流通过滑动窗口分为4个日志为一个日志序列 X_test, Y_test = standard_log_key(test_log_key_sequence_str) anomaly_sequence = lg.predcit(model, X_test, Y_test) print('the length of anomaly_sequence {} is {}'.format(anomaly_sequence, len(anomaly_sequence))) # anomaly_sequence为异常的序列 anormal_lineid_list = [i+3 for i in anomaly_sequence] df_anormal = df_test_log.loc[df_test_log["LineId"].isin(anormal_lineid_list)] df_anormal.to_csv("tmpdata/execute_path_anormal.csv", index=False)
def login(): print("登录") if user_info['name']: print('您已经登录') return count = 0 user_dic = common.reads() while True: name = input('请输入名字:').strip() if name == 'q': break password = input('请输入密码:').strip() if name in user_dic: if password == user_dic[name] and user_dic['locked'] == False: user_info['name'] = name print('登录成功!') user_logger.info('%s 登录成功' % name) else: count += 1 print('输入错误或者已被锁定!') if count == 3: user_dic['locked'] = True common.save(user_dic) print('尝试过多,锁定') user_logger.info('%s 已被锁定' % name) break
def register(): print('注册') if user_info['name']: print('您已经登陆') return user_dic = common.reads() while True: name = input('请输入名字:').strip() if name == 'q': break if name in user_dic: print('该用户名已存在!') break password = input('请输入密码').strip() conf_password = input('请确认密码').strip() if password == conf_password: account = input('请输入余额').strip() user_dic = { 'name': name, 'password': password, 'account': account, 'locked': False, 'shoppingcart': {} } common.save(user_dic) user_logger.info('%s 注册成功' % name) else: print('两次密码不一致')
def model_generate(key_para_dict): # 对每个日志键进行训练,模型放在model_dict中,eventID为键,model为值 model_dict = {} # 依次加载每个日志键的日志参数组成的矩阵,依次检测 for eventID, params in key_para_dict.items(): model_file = f"tmpdata/ParamModel/{eventID}.pkl" if os.path.exists(model_file): model_dict[eventID] = load(model_file) continue # 如果这个日志键对应的日志条目少于8个,则跳过对该日志键的模型生成 if len(params) <= 8: continue else: # 设置滑动窗口大小为3 n_steps = 3 X, Y = training_data_generate(params, n_steps) model = LSTM_model(X, Y) model_dict[eventID] = model save(model_file, model) # yhat = model.predict(test_x) # print("the predicted y shapeis:", yhat.shape) # (4, 2) # print("the test y shape is:", test_y.shape) # (4, 2) # # 测量实际值和预测值的均方误差 # mses = mean_squared_error_modified(test_y, yhat) # print(f"mses: {mses}") # sys.exit() return model_dict
def spell_log(df_log, df_type="train"): spell_result_path = "tmpdata/SpellResult/spell.pkl" if os.path.isfile(spell_result_path): slm = load(spell_result_path) # 加载保存好的结果 else: # 首先需要训练一遍,找出所有的日志健,保存在spell_result_path中 # 要选取可以涵盖所有日志类型的数据用来训练 slm = spell.lcsmap('[\\s]+') for i in range(len(df_log)): log_message = df_log["Content"][i] # print(log_message) sub = log_message.strip('\n') slm.insert(sub) # 将spell的训练结果保存在这里 save(spell_result_path, slm) # 对每条日志进行训练一遍,然后保存在spell_result.txt中 templates = [0] * df_log.shape[0] ids = [0] * df_log.shape[0] ParameterList = [0] * df_log.shape[0] time_interval = [0] * df_log.shape[0] for i in range(len(df_log)): log_message = df_log["Content"][i].strip() obj = slm.insert(log_message) # seq = re.split('[\\s]+', log_message) # ParameterList[i] = obj.param(seq) # 取出log中的参数 # if param != []: # param = reduce(operator.add, param) # 多维数组变一维数组 obj_json = obj.tojson(log_message) templates[i] = obj_json["lcsseq"] # 获取该日志条目的日志键 ids[i] = obj_json["lcsseq_id"] # 获取日志键id 也就是事件编号 ParameterList[i] = obj_json["param"] # 取出log中的参数 # 生成两个日志时间差,加入param参数中 # print(df_log.shape) # print(len(df_log)) for id in range(len(df_log)): if id == 0: time_interval[id] = "0" else: time_last = df_log["Time"][id - 1] time_now = df_log["Time"][id] elapsed = time_elapsed(time_last, time_now) time_interval[id] = elapsed ParameterList[id].append(time_interval[id]) # 将结果保存在df_log里面 df_log['EventId'] = ids # 事件向量 df_log['EventTemplate'] = templates # 日志模板 日志键 df_log["ParameterList"] = ParameterList df_log.to_csv(f"tmpdata/struct/{df_type}_structured.csv", index=False) return df_log
def shopping(): print('购物') prod_list = [['coffee', 10], ['chicken', 20], ['iphone', 8000], ['macPro', 15000], ['car', 100000]] shoppingcart = {} cost = 0 user_dic = common.reads() account = user_dic['account'] while True: for i, prod in enumerate(prod_list): print('%s : %s ' % (i, prod)) choice = input('请输入要购买的商品:').strip() if choice.isdigit(): choice = int(choice) if choice >= len(prod_list): continue prod_name = prod_list[choice][0] prod_price = prod_list[choice][1] if account >= prod_price: if prod_name in shoppingcart: shoppingcart[prod_name]['count'] += 1 else: shoppingcart[prod_name] = {'price': prod_price, 'count': 1} account -= prod_price cost += prod_price else: print('余额不足') elif choice == 'q': if cost == 0: break print(shoppingcart) buy = input('确认购买吗?(y/n)').strip() if buy == 'y': user_dic['account'] = account, user_dic['shoppingcart'] = shoppingcart common.save(user_dic) else: print('您没买东西') break else: print('输入非法')