京东-优惠雷达
新人页面
精选商品
首月0月租体验,领12个月京东PLUS
自营热卖

利用python对nmon文件提取相关属性

干净的像风 1年前   阅读数 114 0
  • 一、
# -*- coding: utf-8 -*-
"""
Created on Mon Jan 22 15:11:20 2018

@author: 17100493
"""
import os
import re
fileName = input("输入文件名:\n")
count_two=0.0
CPU_TEST = 0.0
DISK_BUSY = 0.0
NET_IO = 0.0
dict = {}
for dirName in os.listdir(fileName): 
    print(dirName)
    count_two = count_two + 1
    fileText = open(os.path.join(fileName,dirName),'r')
    print(fileText)
    text = fileText.readlines()
    for line in text:
        if 'AAA,command' in line:
            second = float(line.split(' ')[6])
            count = float(line.split(' ')[8])

    #cpu index
    for line in text:
        if 'CPU_ALL,CPU Total' in line:
            cpu_index = re.sub('\n','',line).split(',')
            cpu_index_user = cpu_index.index("User%")
            cpu_index_sys = cpu_index.index("Sys%")

#获取CPU
    CPU_TXT = []
    for line in text:
        if 'CPU_ALL,T' in line:
            CPU_TXT.append(line)
#计算CPU

#将列表的每一项split后 按照idle列 计算CPU
    CPU_VALUE = 0.0
    CPU_AVG_VALUE = 0.0
    
    for CPU in CPU_TXT:
        CPU_VALUE = CPU_VALUE + float(CPU.split(',')[cpu_index_user]) + float(CPU.split(',')[cpu_index_sys])
#平均使用率
    CPU_AVG_VALUE = CPU_VALUE / count
    CPU_TEST = CPU_TEST + CPU_AVG_VALUE
    time = (second * count) / 60
    print("共监控",time, "minutes\n")
    print("CPU的平均使用率为:" ,CPU_AVG_VALUE,'%\n')

#diskbusy index
    for line in text:
        if 'DISKBUSY,Disk' in line:
            diskbusy_index = re.sub('\n','',line).split(',')
            disk_index_dm7= diskbusy_index.index("dm-7")
#获取DISKBUSY
    DISKBUSY_TXT = []
    for line in text:
        if 'DISKBUSY,T' in line:
            DISKBUSY_TXT.append(line)
#计算DISKBUSY
    DISK_VALUE = 0.0
    DISK_AVG_VALUE = 0.0
    for DISKBUSY in DISKBUSY_TXT:
        DISK_VALUE = DISK_VALUE + float(DISKBUSY.split(',')[disk_index_dm7])
    DISK_AVG_VALUE = DISK_VALUE/count
    DISK_BUSY = DISK_BUSY+DISK_AVG_VALUE
    print("DISKBUSY的平均使用率为:" ,DISK_AVG_VALUE,'%\n')

#network io

    for line in text:
        if 'NETPACKET,Network' in line:
            print(line)
            net_index = re.sub('\n','',line).split(',')
    print(net_index)
    net_index_write = net_index.index('eth0-write/s')
    print(net_index_write)
    NETWORKE_TXT = []
    for line in text:
        if 'NET,T' in line:
            NETWORKE_TXT.append(line)
#计算DISKBUSY
    NET_VALUE = 0.0
    NET_AVG_VALUE = 0.0
    MAX_VALUE = 0.0
    MIN_VALUE = 0.0
    SUM_PRODUCT = 0.0
    for NETWORKE in NETWORKE_TXT:
        NET_VALUE_ONE = float(NETWORKE.split(',')[net_index_write])
        SUM_PRODUCT = SUM_PRODUCT + NET_VALUE_ONE * NET_VALUE_ONE
        NET_VALUE = NET_VALUE + NET_VALUE_ONE
        if NET_VALUE_ONE > MAX_VALUE:
            MAX_VALUE = NET_VALUE
    NET_AVG_VALUE = NET_VALUE/count
    NET_IO = NET_IO + NET_AVG_VALUE
    Wavg = SUM_PRODUCT/NET_VALUE-NET_AVG_VALUE
    MAX = MAX_VALUE-Wavg-NET_AVG_VALUE
    print("NETWORK的平均使用率(Avg)为:" ,NET_AVG_VALUE,'%\n',"NETWORK的Wavg为:",Wavg,'%\n',"NETWORK的最大值MAX为:",MAX,'%\n')
    dict[count_two]=str(CPU_TEST)+","+str(DISK_BUSY)+","+str(NET_IO)
    #print(dict,'%\n')
#print text

#print text[3]

#关闭文件
    fileText.close()
print("CPU为:" ,CPU_TEST/count_two,'%\n',"DISK_BUSY为:",DISK_BUSY/count_two,'%\n',"NET_IO的值为:",NET_IO/count_two,'%\n')

二、

# -*- coding: utf-8 -*-
"""
Created on Mon Jan 22 15:11:20 2018

@author: 17100493
"""
import os
import re

fileName = input("输入文件名:\n")
count_two = 0.0
CPU_TEST = 0.0
DISK_BUSY = 0.0
NET_IO = 0.0
for dirName in os.listdir(fileName):
    print(dirName)
    count_two = count_two + 1
    fileText = open(os.path.join(fileName, dirName), 'r',encoding='UTF-8')
    print(fileText)
    text = fileText.readlines()
    for line in text:
        if 'AAA,command' in line:
            second = float(line.split(' ')[6])
            count = float(line.split(' ')[8])

    # cpu index
    for line in text:
        if 'CPU_ALL,CPU Total' in line:
            cpu_index = re.sub('\n', '', line).split(',')
            cpu_index_user = cpu_index.index("User%")
            cpu_index_sys = cpu_index.index("Sys%")

    # 获取CPU
    CPU_TXT = []
    for line in text:
        if 'CPU_ALL,T' in line:
            CPU_TXT.append(line)
    # 计算CPU

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

    for CPU in CPU_TXT:
        CPU_VALUE = CPU_VALUE + float(CPU.split(',')[cpu_index_user]) + float(CPU.split(',')[cpu_index_sys])
    # 平均使用率
    CPU_AVG_VALUE = CPU_VALUE / count
    CPU_TEST = CPU_TEST + CPU_AVG_VALUE
    time = (second * count) / 60
    # diskread index
    for line in text:
        if 'DISKREAD,Disk' in line:
            diskread_index = re.sub('\n', '', line).split(',')
            diskread = diskread_index[2:]
    # 获取DISKREAD
    DISKREAD_TXT = []
    for line in text:
        if 'DISKREAD,T' in line:
            DISKREAD_TXT.append(re.sub('\n', '', line).split(',')[2:])
    # 计算DISKREAD
    DISK_VALUE = 0.0
    DISK_AVG_VALUE = []
    DISKREAD_TEST = []
    MAXDISKREAD = 0.0
    for i in range(len(diskread)):
        context=[]
        for j in range(len(DISKREAD_TXT)):
            context.append(float(DISKREAD_TXT[j][i]))
        DISK_AVG_VALUE.append(sum(context)/count)
        MAXDISKREAD=max(DISK_AVG_VALUE)
        # DISK_BUSY = DISK_BUSY + DISK_AVG_VALUE[i]

    for line in text:
        if 'DISKWRITE,Disk' in line:
            diskwrite_index = re.sub('\n', '', line).split(',')
            diskwrite = diskwrite_index[2:]
    DISKWRITE_TXT = []
    for line in text:
        if 'DISKWRITE,T' in line:
            DISKWRITE_TXT.append(re.sub('\n', '', line).split(',')[2:])
    DISK_VALUE = 0.0
    DISK_AVG_VALUE = []
    DISKWIRTE_TEST = []
    MAXDISKWRITE = 0.0
    for i in range(len(diskwrite)):
        context=[]
        for j in range(len(DISKWRITE_TXT)):
            context.append(float(DISKWRITE_TXT[j][i]))
        DISK_AVG_VALUE.append(sum(context)/count)
        # DISK_BUSY = DISK_BUSY + DISK_AVG_VALUE[i]
        MAXDISKWRITE= max(DISK_AVG_VALUE)

#MEM
    for line in text:
        if 'MEM,T1440' in line:
           men_index = re.sub('\n', '', line).split(',')
           memtotal = men_index[2]
           memfree = men_index[6]
    # network io

    for line in text:
        if 'NET,Network' in line:
            net_index = re.sub('\n', '', line).split(',')
            bond0read = net_index[3]
            bond0write = net_index[4]
    NETWORKE_TXT = []
    for line in text:
        if 'NET,T' in line:
            NETWORKE_TXT.append(line.split(','))
    # 计算DISKBUSY
    NET_VALUEONE = 0.0
    NET_VALUETWO = 0.0
    NET_AVG_VALUEREAD = 0.0
    NET_AVG_VALUEWRITE = 0.0
    SUM_PRODUCT = 0.0
    for NETWORKE in NETWORKE_TXT:
        NET_VALUEONE = NET_VALUEONE+float(NETWORKE[3])
        NET_VALUETWO = NET_VALUETWO+float(NETWORKE[4])

    NET_AVG_VALUEREAD =  NET_VALUEONE / count
    NET_AVG_VALUEWRITE = NET_VALUETWO / count


    # 关闭文件
    fileText.close()
    print("----------------------------------------------------------------------------------------------------")
    print("CPU为:", CPU_AVG_VALUE, '%\n', "DISKREAD为:",MAXDISKREAD, '%\n', "DISKKWRITE为:",MAXDISKWRITE, '%\n',"MEM为:",memtotal,memfree,'%\n', "NET_IO的值为:",
      NET_AVG_VALUEREAD,NET_AVG_VALUEWRITE ,'%\n')



注意:本文归作者所有,未经作者允许,不得转载

全部评论: 0

    我有话说: