python 统计nmon 监控结果 简易版

当前版本为简易版,仅能够统计在监控时设定的时间。

如 nmon -fs 1 -c 10

即统计10秒钟内,系统cpu和内存的平均使用率,后续将进一步完善。

#获取文件名
import string

fileName = raw_input("输入文件名:\n")

fileText = open(fileName,'r')

text = fileText.readlines()

##获取监控频率 和 次数
second = 0.0
count = 0.0

#监控时要严格按照该格式 nmon -fs second -c count

for line in text:
    if 'AAA,command' in line:
        second = string.atof(line.split(' ')[2])
        count = string.atof(line.split(' ')[4])
        
#处理text
        
#获取CPU
CPU_TXT = []

for line in text:
    if 'CPU_ALL,T' in line:
        CPU_TXT.append(line)
#计算CPU

#将列表的每一项split后 按照idle列 计算CPU
CPUVALUE=''
CPU_VALUE = 0.0
CPU_AVG_VALUE = 0.0

for CPU in CPU_TXT:
    CPU_VALUE = CPU_VALUE + 100.0 - string.atof(CPU.split(',')[5])
#平均使用率
CPU_AVG_VALUE = CPU_VALUE/count
time = second * count /60
print "共监控",time, "minutes\n"
print "CPU的平均使用率为:" ,CPU_AVG_VALUE,'%\n'
    
#获取内存
MEM_TXT = []

for line in text:
    if 'MEMUSE,T' in line:
        MEM_TXT.append(line)
#计算内存

#将列表的每一项split后 ,根据第三项Memory Use sop21 来统计MEMUSE
MEMVALUE=''
MEM_VALUE = 0.0
MEM_AVG_VALUE = 0.0

for MEM in MEM_TXT:
    MEM_VALUE = MEM_VALUE + string.atof(MEM.split(',')[2])



    
#平均使用率
    
MEM_AVG_VALUE = MEM_VALUE/count

print "内存的平均使用率为:",MEM_AVG_VALUE,'%'
#print text

#print text[3]

#关闭文件
fileText.close()
原文地址:https://www.cnblogs.com/katero/p/2918755.html