永不落伍 发表于 2018-8-3 11:14:19

使用python实现统计Nginx进程所占用的物理内存

#!/usr/bin/python  
#coding:utf8
  

  
from subprocess import Popen, PIPE
  
import os
  

  
#如果需要对httpd进行统计可以把nginx改为httpd,其它服务统计同理,但有部分无法实现,如oracle
  
nginxpid = Popen(["pidof", "nginx"], stdout=PIPE)
  
nginxpid = nginxpid.stdout.read().split()
  

  
memsum = 0
  
for i in nginxpid:
  
    pidfile = os.path.join("/proc/", str(i), "status")
  
    with open(pidfile) as f:
  
      for mem in f:
  
            if mem.startswith("VmRSS"):
  
               pidmem = int(mem.split())
  
               memsum += pidmem
  

  
print("%d %s" %(memsum,"KB"))
页: [1]
查看完整版本: 使用python实现统计Nginx进程所占用的物理内存