#!/usr/bin/env python
#coding:utf-8
import os
from subprocess import Popen,PIPE
def gitPid(): #拿到java系统进程值
p = Popen([‘pidof‘,‘java‘],stdout=PIPE,stderr=PIPE)
pids = p.stdout.read().split()
return pids
def parsePidFile(pids): #访问VmRSS物理内存值并作累计
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(self):
with open(self) as fd:
for line in fd:
if line.startswith(‘MemTotal‘):
total_mem = int(line.split()[1])
return total_mem
if __name__ == ‘__main__‘:
pids = gitPid()
http_mem = parsePidFile(pids)
total = total_mem(‘/proc/meminfo‘)
print "nginx使用内存:%s KB" % http_mem
print "占总共内存的使用量: %.2f%% " % (http_mem/float(total)*100)