一键读取topmen
2023-02-19 本文已影响0人
西厌西厌
import base64
import os
import tarfile
import gzip
import zipfile
import re
# 对gz类型进行解压
def gzip_file(file):
c_path = file+"\\logcat_log.tar.gz"
try:
g = gzip.GzipFile(mode='rb',fileobj=open(c_path,'rb'))
open(c_path.replace('.gz',''),'wb').write(g.read())
except Exception as e:
print(e)
else:
untar(c_path.replace('.gz',''),file+'./',file)
#print('文件解压成功!')
def untar(fname,dirs,origin):
print("fname="+fname)
try:
t = tarfile.open(fname)
t.extractall(path=dirs)
except Exception as e:
print(e)
else:
#print('解压tar成功')
zip_allfile(origin+"\\log")
def zip_allfile(dirs):
for f in os.listdir(dirs):
if '.zip' in f:
zip_file = zipfile.ZipFile(dirs+"\\"+f)
zip_file.extract(zip_file.namelist()[0],dirs)
index = 0
for f in os.listdir(dirs):
if 'topmen' in f:
l_file = dirs+"\\"+f
with open(l_file, "r", encoding="utf-8" ,errors="ignore") as L:
for line in L:
if '%CPU' in line:
index = 1
elif index >= 1:
index += 1
if index == 5:
index = 0
s_line = re.sub(' +',' ',line)
s_line = s_line.split(' ')
data = [0,0,0,0,0]
if len(s_line) >= 12:
data[0] = s_line[2]
data[1] = s_line[6]
data[2] = s_line[7]
data[3] = s_line[9]
data[4] = l_file
if '.' in data[3]:
v = data[3][0:data[3].index('.')]
if int(v) > 40:
print(data)
else:
if int(v) > 40:
print(data)
# print(data)
if __name__ == "__main__":
#modify_name()
path = 'D:\\log分析\\0111-1\\hx11_dhulog_20230111165755\\log'
zip_allfile(path)