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

python计算apache总内存

时间:2016-11-26 14:40:18      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:tde   meminfo   process   get   name   apache   pre   bre   out   

#!/usr/bin/env python
import os
from subprocess import Popen, PIPE
def getPid():
    p=Popen([‘pidof‘,‘httpd‘],stdout=PIPE,stderr=PIPE)
    pids = p.stdout.read().split()
    return pids

def paresPidFile(pids):
     sum =0
     for i in pids:
         fn = os.path.join(‘/proc/‘,i,‘status‘)
         with open(fn) as fd:
            for line in fd:
                 if line.startswith(‘VmRSS‘):
                     http_mem =int(line.split()[1])
                     sum += http_mem
                     break
     return sum
def total_mem(f):
    with open(f) as fd:
        for line in fd:
             if line.startswith(‘MemTotal‘):
                    total_mem =int(line.split()[1])
                    return total_mem
if __name__ ==‘__main__‘:
    pids = getPid()
    http_mem = paresPidFile(pids)
    total = total_mem(‘/proc/meminfo‘)
    print"Apache memory is: %s KB"% http_mem
    print "Percent: %.2f%%"%(http_mem/float(total)*100)

  

python计算apache总内存

标签:tde   meminfo   process   get   name   apache   pre   bre   out   

原文地址:http://www.cnblogs.com/itfenqing/p/6104008.html

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