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

栏目: Python · 发布时间: 8年前

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

实现代码一:

此方法适用于对进行名统一的进行进行统计,如httpd,ngins或php-fpm等

#!/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()[1])
               memsum += pidmem

print("%d %s" %(memsum,"KB"))

实现代码二:

此方法适用于某个用户进行使用内存统计,最后的结果是,一个用户所使用的所有内存信息,脚本使用方法,在执行脚本的时候在脚本后面加上要统计的用户名

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

"""此脚本需要系统安装smem软件,安装方法:yum -y install smem"""

from subprocess import Popen, PIPE
import sys

allmeminfo = Popen(["smem", "-u"], stdout=PIPE)
allmeminfo = allmeminfo.stdout.read().split("\n")

for i in allmeminfo:
    if i.startswith(sys.argv[1]):
        meminfo = i.split()
        print("Process Name: "+meminfo[0])
        print("Total Process: "+meminfo[1])
        print("Physics Memroy Use: "+meminfo[5]+" KB")

执行结果如下:

使用 <a href='https://www.codercto.com/topics/20097.html'>python</a> 实现统计Nginx进程所占用的物理内存


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Beginning iPhone and iPad Web Apps

Beginning iPhone and iPad Web Apps

Chris Apers、Daniel Paterson / Apress / 2010-12-15 / USD 39.99

It seems that everyone and her sister has developed an iPhone App—everyone except you, the hard-working web professional. And now with the introduction of the iPad, you may even feel farther behind. B......一起来看看 《Beginning iPhone and iPad Web Apps》 这本书的介绍吧!

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换