一碍侦、top獲取到的數(shù)據(jù)
保存在session.log中傻咖,主要字段如下
...
[root@*** ~]$ top
top - 10:16:21 up 1 day, 16:56, 1 user, load average: 0.32, 0.09, 0.07
...
KiB Mem : 1014904 total, 69072 free, 525736 used, 420096 buff/cache
...
top - 10:16:24 up 1 day, 16:56, 1 user, load average: 0.30, 0.09, 0.07
...
KiB Mem : 1014904 total, 70480 free, 524256 used, 420168 buff/cache
...
二罢维、py程序
處理后保存在serverMem.csv中
數(shù)據(jù)行數(shù)大概有36100行旷余,while循環(huán)暫時這樣寫奕扣,后續(xù)優(yōu)化
#!/usr/bin/env python
import re
srcfile = "session.log"
descfile = "serverMem.csv"
def GetLine():
srcfd = open(srcfile, mode='r')
descfd = open(descfile, mode='w+')
i = 0
while (i < 36100):
line = srcfd.readline()
searchTime = re.search( r'(.*)top - (.*?) .*', line, re.M|re.I)
searchMem = re.search(r'(.*)KiB Mem :(.*?) .*', line, re.M|re.I)
if searchTime:
time = searchTime.group(2)
descfd.writelines(time + ',')
elif searchMem:
pattern = re.compile(r'\d+')
result1 = pattern.findall(line)
descfd.writelines(result1[0]+','+result1[1]+','+result1[2]+','+result1[3])
descfd.writelines("\n")
i += 1
descfd.close()
srcfd.close()
GetLine()
對正則表達(dá)式不熟薪鹦,只能這樣了,有誰比較熟悉的可以給出指導(dǎo)意見
三惯豆、輸出結(jié)果
serverMem.csv部分結(jié)果如下
10:16:21,1014904,69072,525736,420096
10:16:24,1014904,70480,524256,420168
10:16:27,1014904,71476,523140,420288
10:16:30,1014904,66964,527408,420532
10:16:33,1014904,68308,526000,420596
10:16:36,1014904,66532,527832,420540
10:16:39,1014904,67636,526704,420564
...
需要加個頭池磁,代碼不過關(guān),寫文件頭沒有成功楷兽,后續(xù)優(yōu)化
server Mem (KiB): ,,,,
time, total, free, used, buff/cache
10:16:21,1014904,69072,525736,420096
10:16:24,1014904,70480,524256,420168
10:16:27,1014904,71476,523140,420288
10:16:30,1014904,66964,527408,420532
10:16:33,1014904,68308,526000,420596
10:16:36,1014904,66532,527832,420540
10:16:39,1014904,67636,526704,420564
...