• python编程实例-统计apache进程占用的物理内存


     1 #!/usr/bin/env python
     2 
     3 import os
     4 from subprocess import PIPE,Popen
     5 
     6 def getPids():
     7         p = Popen(['pidof','httpd'],stdout=PIPE,stderr=PIPE)
     8         pids = p.stdout.read().split()
     9         return pids
    10 
    11 def parsePidFile(pids):
    12         http_sum = 0
    13         for i in pids:
    14                 fn = os.path.join('/proc/',i,'status')
    15                 with open(fn) as fd:
    16                         for line in fd:
    17                                 if line.startswith('VmRSS'):
    18                                         http_mem = int(line.split()[1])
    19                                         http_sum += http_mem
    20                                         break
    21         return http_sum
    22 
    23 def total_mem(f):
    24         with open(f) as fd:
    25                 for line in fd:
    26                         if line.startswith('MemTotal'):
    27                                 total_sum = int(line.split()[1])
    28                                 return total_sum
    29 
    30 if __name__ == '__main__':
    31         pids = getPids()
    32         http_sum = parsePidFile(pids)
    33         total_sum = total_mem('/proc/meminfo')
    34         print "Apache memory is %s KB" % http_sum
    35         print "total memory is %s KB" % total_sum
    36         print "Percent : %.2f %%" % (http_sum/float(total_sum)*100)
  • 相关阅读:
    数据表管理admin
    HDU 5057
    HDU 5056
    HDU 6035(树形dp)
    CodeForces 586D
    Codeforces 940D
    CodeForces 820C
    TOJ4114(活用树状数组)
    2017CCPC中南地区赛 H题(最长路)
    CodeForces 544C (Writing Code)(dp,完全背包)
  • 原文地址:https://www.cnblogs.com/Nyan-Workflow-FC/p/5680442.html
Copyright © 2020-2023  润新知