美文网首页
通过py处理linux中top命令获取到的mem数据

通过py处理linux中top命令获取到的mem数据

作者: 幕枫楚雨 | 来源:发表于2020-09-10 17:46 被阅读0次

    一、top获取到的数据

    保存在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中
    数据行数大概有36100行,while循环暂时这样写,后续优化

    #!/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()
    
    
    

    对正则表达式不熟,只能这样了,有谁比较熟悉的可以给出指导意见

    三、输出结果

    serverMem.csv部分结果如下

    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
    ...
    

    需要加个头,代码不过关,写文件头没有成功,后续优化

    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
    ...
    

    相关文章

      网友评论

          本文标题:通过py处理linux中top命令获取到的mem数据

          本文链接:https://www.haomeiwen.com/subject/fvipektx.html