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)
时间: 2024-12-21 04:47:53