import multiprocessing
import os
import sys
project_root = os.path.dirname(os.path.abspath(file))
os.environ[‘PYTHONPATH’] = f"{project_root}:{os.environ.get(‘PYTHONPATH’, ‘’)}"
from lib import expand, cal_metrics
from datetime import datetime
import os, shutil, json, csv, psutil, sys
from multiprocessing import Process
M = 1024*1024
def addtimeline(weightmethod, weight_input):
current_datetime = datetime.now()
timestamp = current_datetime.strftime(“%Y%m%d%H%M%S%f”)
new_folder_path = os.path.join('result/history/', timestamp) os.makedirs(new_folder_path) os.makedirs(new_folder_path+'/single_res') os.makedirs(new_folder_path+'/inputs') # 要复制的文件列表 file_list = ['graph_stat.json', 'weight.json', 'path_metrics.csv', 'rank.json'] for file_name in file_list: source_file = os.path.join('result/', file_name) shutil.copy2(source_file, new_folder_path+'/single_res') shutil.copytree(os.path.join('result/', 'tmp'), os.path.join(new_folder_path, 'tmp')) file_list = ['input.json', 'MainInput.json', weightmethod+'_input/'+weight_input] for file_name in file_list: source_file = os.path.join('input/', file_name) shutil.copy2(source_file, new_folder_path+'/inputs') return current_datetime
def save(addnum=None, linknum=None, multi=None):
with open(‘input/MainInput.json’, ‘r’) as f:
input = json.loads(f.read())
f.close()
weightmethod = input[‘weight’]; rankmethod = input[‘rank’]
weight_input = input[‘weight_input’]
jounal1_path = ‘result/stat_all.csv’; jounal2_path = ‘result/perform_log.csv’
# 添加时间戳并备份 timestamp = addtimeline(weightmethod, weight_input) # stat_all.csv jounal1 = [] with open("result/graph_stat.json", 'r') as f: stat = json.loads(f.read()) f.close() hostsnum=stat["hostsnum"]; edgesnum=stat["edgesnum"]; pathsnum=stat["pathsnum"] with open("result/tmp/time.log", 'r') as f: log = f.read() f.close() ls1 = log.split() # print(ls1) err = []; ls = []; j=0; flag=0 for i in range(len(ls1)): if flag==1: flag-=1 continue try: ls.append(float(ls1[i])) j+=1 except: if j+1 == i or j+2 == i: ls.append('None') err.append(ls1[i]) if ls1[i] == 'signal' or ls1[i] == 'code': err.append(ls1[i+1]) flag += 1 exception = ' '.join(err) print(ls) with open('result/tmp/exception.txt', 'w') as f: f.write(exception) expandtime=ls[0]; expandmem=ls[1] weighttime=ls[2]; weightmem=ls[3] metricstime=ls[4]; metricsmem=ls[5] ranktime=ls[6]; rankmem=ls[7] jounal1.append([timestamp, weightmethod, rankmethod, hostsnum, edgesnum, pathsnum, expandtime, expandmem, weighttime, weightmem, metricstime, metricsmem, ranktime, rankmem, exception]) with open(jounal1_path, 'a', newline='') as f: writer = csv.writer(f) writer.writerow(jounal1[0]) # perform_log.csv jounal2 = [] if addnum is not None and linknum is None and multi is None: expansion = addnum jounal2.append([timestamp, weightmethod, rankmethod, expansion]) else: jounal2.append([timestamp, weightmethod, rankmethod, addnum, linknum, multi]) with open(jounal2_path, 'a', newline='') as f: writer = csv.writer(f) writer.writerow(jounal2[0])
def save_risk(addnum=None, linknum=None, multi=None):
with open(‘input/MainInput.json’, ‘r’) as f:
input = json.loads(f.read())
f.close()
weightmethod = input[‘weight’]; rankmethod = input[‘rank’]
weight_input = input[‘weight_input’]
method = input[“rrisk_cal”][“method”]
if method==‘s’:
riskmethod=‘static’
elif method==‘d’:
riskmethod=‘dynamic’
jounal1_path = ‘result/stat_all.csv’; jounal2_path = ‘result/perform_log.csv’
# 添加时间戳并备份 timestamp = addtimeline(weightmethod, weight_input) # stat_all.csv jounal1 = [] with open("result/graph_stat.json", 'r') as f: stat = json.loads(f.read()) f.close() hostsnum=stat["hostsnum"]; edgesnum=stat["edgesnum"]; pathsnum=stat["pathsnum"] with open("result/tmp/time.log", 'r') as f: log = f.read() f.close() ls1 = log.split() err = []; ls = []; j=0; flag=0 for i in range(len(ls1)): if flag==1: flag-=1 continue try: ls.append(float(ls1[i])) j+=1 except: if j+1 == i or j+2 == i: ls.append('None') err.append(ls1[i]) if ls1[i] == 'signal' or ls1[i] == 'code': err.append(ls1[i+1]) flag += 1 with open("result/tmp/risk_time.log", 'r') as f: log = f.read() f.close() ls2 = log.split() err = []; ls_risk = []; j=0; flag=0 for i in range(len(ls2)): if flag==1: flag-=1 continue try: ls_risk.append(float(ls2[i])) j+=1 except: if j+1 == i or j+2 == i: ls_risk.append('None') err.append(ls2[i]) if ls2[i] == 'signal' or ls2[i] == 'code': err.append(ls1[i+1]) flag += 1 for lr in ls_risk: ls.append(lr) # print(lr) # print(ls) exception = ' '.join(err) with open('result/tmp/exception.txt', 'w') as f: f.write(exception) expandtime=ls[0]; expandmem=ls[1] weighttime=ls[2]; weightmem=ls[3] metricstime=ls[4]; metricsmem=ls[5] ranktime=ls[6]; rankmem=ls[7] risktime=ls[8]; riskmem=ls[9] jounal1.append([timestamp, weightmethod, rankmethod,riskmethod, hostsnum, edgesnum, pathsnum, expandtime, expandmem, weighttime, weightmem, metricstime, metricsmem, ranktime, rankmem,risktime,riskmem, exception]) with open(jounal1_path, 'a', newline='') as f: writer = csv.writer(f) writer.writerow(jounal1[0]) # perform_log.csv jounal2 = [] if addnum is not None and linknum is None and multi is None: expansion = addnum jounal2.append([timestamp, weightmethod, rankmethod,riskmethod, expansion]) else: jounal2.append([timestamp, weightmethod, rankmethod,riskmethod, addnum, linknum, multi]) with open(jounal2_path, 'a', newline='') as f: writer = csv.writer(f) writer.writerow(jounal2[0])
def simple_ranking(addnum=None, linknum=None, multi=None):
with open(‘input/MainInput.json’, ‘r’) as f:
input = json.loads(f.read())
f.close()
weightmethod = input[‘weight’]; rankmethod = input[‘rank’]
jounal1_path = ‘result/stat_all.csv’; jounal2_path = ‘result/perform_log.csv’
cmethod=input[‘rw_ws’]
if cmethod==1:
os.system(‘python3 ./lib/cal_rw_ws.py’)
else:
while True:
if addnum is not None and linknum is None and multi is None:
expansion = addnum
#print(‘expanding with expansion…\n’)
p = os.system(‘/usr/bin/time -f"%e %M" -o ./result/tmp/time.log python3 ./lib/expand.py {} {} {} {}’.format(expansion, ‘input/input.json’, ‘result/tmp/topo’, ‘result/graph_stat’))
#print(p)
if p!=0:
break
else:
#print(‘expanding with addnum, linknum, multi…\n’)
p = os.system(‘/usr/bin/time -f"%e %M" -o ./result/tmp/time.log python3 ./lib/expand.py {} {} {} {} {} {}’.format(addnum, linknum, multi, ‘input/input.json’, ‘result/tmp/topo’, ‘result/graph_stat’))
#print(p)
if p!=0:
break
#print(‘begin caculating weights…\n’)
weight_input = input[‘weight_input’]
p = os.system(‘/usr/bin/time -f"%e %M" -a -o ./result/tmp/time.log python3 ./lib/Weight/{}/{}.py {} {}’.format(weightmethod , weightmethod , ‘input/’+weightmethod+‘_input/’+weight_input, ‘result/weight.json’))
if p!=0:
break
#print('begin caculating metrics...\n') p = os.system('/usr/bin/time -f"%e %M" -a -o ./result/tmp/time.log python3 ./lib/cal_metrics.py {} {}'.format('result/tmp/topo.json', 'result/path_metrics.csv')) if p!=0: break #print('begin caculating ranks...\n') os.system('/usr/bin/time -f"%e %M" -a -o ./result/tmp/time.log python3 ./lib/Rank/{}/{}.py {} {} {} {} {}'.format(rankmethod , rankmethod , 'result/tmp/topo.json', 'result/weight.json', 'result/tmp/data.csv', 'result/rank.json', 'result/path_metrics.csv')) break
def main(addnum=None, linknum=None, multi=None,method=None):
# print(addnum,linknum,multi,method)
with open(‘input/MainInput.json’, ‘r’) as f:
input = json.loads(f.read())
f.close()
weightmethod = input[‘weight’]; rankmethod = input[‘rank’]
jounal1_path = ‘result/stat_all.csv’; jounal2_path = ‘result/perform_log.csv’
while True:
if addnum is not None and linknum is None and multi is None:
expansion = addnum
# print(“#########”,flush=True)
#print(‘expanding with expansion…\n’)
p = os.system(‘/usr/bin/time -f"%e %M" -o ./result/tmp/time.log python3 ./lib/expand.py {} {} {} {}’.format(expansion, ‘input/input.json’, ‘result/tmp/topo’, ‘result/graph_stat’))
#print(p)
if p!=0:
break
else:
# print(“#########”,flush=True)
#print(‘expanding with addnum, linknum, multi…\n’)
p = os.system(‘/usr/bin/time -f"%e %M" -o ./result/tmp/time.log python3 ./lib/expand.py {} {} {} {} {} {}’.format(addnum, linknum, multi, ‘input/input.json’, ‘result/tmp/topo’, ‘result/graph_stat’))
#print(p)
if p!=0:
break
#print('begin caculating weights...\n') weight_input = input['weight_input'] p = os.system('/usr/bin/time -f"%e %M" -a -o ./result/tmp/time.log python3 ./lib/Weight/{}/{}.py {} {}'.format(weightmethod , weightmethod , 'input/'+weightmethod+'_input/'+weight_input, 'result/weight.json')) if p!=0: break #print('begin caculating metrics...\n') p = os.system('/usr/bin/time -f"%e %M" -a -o ./result/tmp/time.log python3 ./lib/cal_metrics.py {} {}'.format('result/tmp/topo.json', 'result/path_metrics.csv')) if p!=0: break #print('begin caculating ranks...\n') os.system('/usr/bin/time -f"%e %M" -a -o ./result/tmp/time.log python3 ./lib/Rank/{}/{}.py {} {} {} {} {}'.format(rankmethod , rankmethod , 'result/tmp/topo.json', 'result/weight.json', 'result/tmp/data.csv', 'result/rank.json', 'result/path_metrics.csv')) if p!=0: break if method=='s': os.system('/usr/bin/time -f"%e %M" -o ./result/tmp/risk_time.log python3 ./lib/RRisk/static.py {} {} --likehood --impact '.format('result/tmp/topo.json', 'result/static_risk.json')) elif method=='d': os.system('/usr/bin/time -f"%e %M" -o ./result/tmp/risk_time.log python3 ./lib/RRisk/dynamic.py {} {} {}'.format('result/tmp/topo.json', 'result/dynamic_risk.json','"result/tmp/ranking.json"')) break
if name == ‘main’:
argnum=len(sys.argv)
if argnum2:
expansion = int(sys.argv[1])
simple_ranking(expansion)
save(expansion)
elif argnum3:
# print(“#########”,flush=True)
expansion = int(sys.argv[1])
method=sys.argv[2]
main(expansion,method=method)
save_risk(expansion)
elif argnum==4:
addnum = int(sys.argv[1]); linknum = int(sys.argv[2]); multi = int(sys.argv[3])
simple_ranking(addnum, linknum, multi)
save(addnum, linknum, multi)
else:
addnum = int(sys.argv[1]); linknum = int(sys.argv[2]); multi = int(sys.argv[3])
method = sys.argv[4]
main(addnum, linknum, multi,method)
save_risk(addnum, linknum, multi)
sys.exit()解析每个函数的功能以及调用,还有整体代码的功能
最新发布