1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
| import math import re import sys
""" 设置读取的文本路径 1.cpuUsagePath 2.memUsagePath 3.memRatePath 4.netInputPath 5.netOutputPath 6.blockInputPath 7.blockOutputPath """
PATH = sys.argv[1] server = sys.argv[2]
cpuUsagePath = PATH+'CPU_Usage.log' memUsagePath = PATH+'MEM_Usage.log' memRatePath = PATH+'MEM_Rate.log' netInputPath = PATH+'NET_input.log' netOutputPath = PATH+'NET_output.log' blockInputPath = PATH+'Block_input.log' blockOutputPath = PATH+'Block_output.log' arrayPath = [cpuUsagePath, memUsagePath, memRatePath, netInputPath, netOutputPath, blockInputPath, blockOutputPath]
def write_calc(): txtPath = PATH + 'calc.txt' with open(txtPath,'a+',encoding='utf-8') as f: f.write(server+'\n') f.write("======================================================\n") f.write("%s\t%s\t%s\t%s\t%s\t%s\t%s\n" %("type","Max","Min","Avg","PCT90","PCT95","PCT99"))
""" 1.calc 函数接收两个参数,一个是shell过滤后的值的单位,一个是shell过滤后的数组 2.calc 仅接受排序后的数组 """ def calc(unit,array,server,listName): percent1 = 99 percent2 = 95 percent3 = 90 a1 = percent1/100 a2 = percent2/100 a3 = percent3/100
num_count = len(array)
maxValue = array[-1] mixValue = array[0] averageValue = sum(array)/len(array)
if math.ceil(num_count*a1) == num_count: percent_99 = array[num_count-1] else: percent_99 = array[math.ceil(num_count*a1)] if math.ceil(num_count*a2) == num_count: percent_95 = array[num_count-1] else: percent_95 = array[math.ceil(num_count*a2)] if math.ceil(num_count*a3) == num_count: percent_90 = array[num_count-1] else: percent_90 = array[math.ceil(num_count*a3)]
txtPath = PATH + 'calc.txt' with open(txtPath,'a+',encoding='utf-8') as f: f.write("%s%s%s%s\t %.2f %.2f %.2f %.2f %.2f %.2f\n" %(listName,"(",unit,")",maxValue,mixValue,averageValue,percent_90,percent_95,percent_99))
""" 用于分离出unit
用于读取数据,并创建列表,将其送入calc计算最大、最小值 1.循环列表 2.打印要计算的列表的名称,如:CPU_Usage、MEM_Usage 3.读取该路径文件 4.按照read_file()分离出来的unit,添加数组元素 5.送入calc()计算 """ def read_file(): for i in arrayPath: with open(i,'r+',encoding='utf-8') as f: firstLine = f.readline() pattern_value = re.findall(r'\D+',firstLine) unit = pattern_value[-1] unit = unit.split()[0]
listName = i.split('/')[-1] listName = listName.split('.')[0] array = [] with open(i,'r+',encoding='utf-8') as f: for line in f.readlines(): lineValue = line.split(unit)[0] try: array.append(float(lineValue)) except ValueError: print("加入列表进行计算的值格式有误!") array.sort() calc(unit,array,server,listName) if __name__ == '__main__': write_calc() read_file()
|