码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2016-07-18 13:23:05      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:

 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)

 

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

标签:

原文地址:http://www.cnblogs.com/Nyan-Workflow-FC/p/5680442.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!