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 pids10 11 def parsePidFile(pids):12 http_sum = 013 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_mem20 break21 return http_sum22 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_sum29 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_sum35 print "total memory is %s KB" % total_sum36 print "Percent : %.2f %%" % (http_sum/float(total_sum)*100)