扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
统计apache进程占用的物理内存
创新互联建站科技有限公司专业互联网基础服务商,为您提供成都移动云计算中心,高防服务器,成都IDC机房托管,成都主机托管等互联网服务。[root@133 ~]# ps aux | grep httpd root 13698 0.0 0.0 185628 4356 ? Ss 17:12 0:00 /usr/sbin/httpd apache 13705 0.3 0.1 293504 27340 ? S 17:12 0:00 /usr/sbin/httpd apache 13706 0.3 0.1 293504 27324 ? S 17:12 0:00 /usr/sbin/httpd apache 13707 0.3 0.1 293504 27324 ? S 17:12 0:00 /usr/sbin/httpd apache 13708 0.3 0.1 293504 27324 ? S 17:12 0:00 /usr/sbin/httpd apache 13709 0.3 0.1 293504 27324 ? S 17:12 0:00 /usr/sbin/httpd apache 13710 0.3 0.1 293504 27328 ? S 17:12 0:00 /usr/sbin/httpd apache 13711 0.3 0.1 293504 27324 ? S 17:12 0:00 /usr/sbin/httpd apache 13712 0.3 0.1 293504 27324 ? S 17:12 0:00 /usr/sbin/httpd root 13831 0.0 0.0 103320 908 pts/6 S+ 17:15 0:00 grep httpd #使用pidof查看所有httpd的进程号 [root@133 ~]# pidof httpd 13712 13711 13710 13709 13708 13707 13706 13705 13698 [root@133 ~]# cd /proc/13698 #apache的主进称,其它都是子进称 [root@133 13698]# cat status Name: httpd State: S (sleeping) Tgid: 13698 Pid: 13698 PPid: 1 TracerPid: 0 Uid: 0 0 0 0 Gid: 0 0 0 0 Utrace: 0 FDSize: 64 Groups: 0 VmPeak: 185628 kB VmSize: 185628 kB VmLck: 0 kB VmHWM: 4356 kB VmRSS: 4356 kB #这是该进称占用的物理内存 VmData: 1812 kB VmStk: 88 kB VmExe: 336 kB VmLib: 9964 kB VmPTE: 376 kB VmSwap: 0 kB
那么将所有httpd的进程所占用的物理内存之和统计出来,即可统计出apache服务占用的所有内存
In [15]: from subprocess import Popen,PIPE In [16]: p=Popen(['pidof','httpd'],stdout=PIPE,stderr=PIPE) In [22]: p.stdout.read().split() Out[22]: ['13712', '13711', '13710', '13709', '13708', '13707', '13706', '13705', '13698']
测试
[root@133 systeminformation]# vim 10_httpd.py #!/usr/bin/env python import os import subprocess from subprocess import Popen,PIPE def getPid(): p = Popen(['pidof', 'httpd'], stdout=PIPE,stderr=PIPE) pids = p.stdout.read().split() print pids #打印httpd进称号 return pids def parsePidFile(pids): sum = 0 for i in pids: fn = os.path.join('/proc/',i,'status') print fn #打印文件路径 with open(fn) as fd: for line in fd: if line.startswith('VmRSS'): http_mem = int(line.split()[1]) print http_mem #打印每个进称占用的内存,单位k sum += http_mem #计算httpd占用的总内存 break return sum if __name__ == '__main__': pids = getPid() print parsePidFile(pids) [root@133 systeminformation]# python 10_httpd.py ['18547', '18546', '18545', '18544', '18543', '18542', '18541', '18540', '18533'] /proc/18547/status 41404 /proc/18546/status 41364 /proc/18545/status 58100 /proc/18544/status 41388 /proc/18543/status 41372 /proc/18542/status 41376 /proc/18541/status 58100 /proc/18540/status 41416 /proc/18533/status 4348 368868 #httpd占用的总内存,单位kb
计算所有的httpd进称占用的物理内存,(VmRSS)的大小,占所有物理内存的比例。
/proc/meminfo
[root@133 systeminformation]# vim /proc/meminfo MemTotal: 20455792 kB [root@133 systeminformation]# vim 10_httpd.py #!/usr/bin/env python import os import subprocess from subprocess import Popen,PIPE def getPid(): p = Popen(['pidof', 'httpd'], stdout=PIPE,stderr=PIPE) pids = p.stdout.read().split() return pids def parsePidFile(pids): 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(f): with open(f) as fd: for line in fd: if line.startswith('MemTotal'): total_mem = int(line.split()[1]) return total_mem if __name__ == '__main__': pids = getPid() http_mem = parsePidFile(pids) total_mem = total_mem('/proc/meminfo') print "Apache memory is: %s KB" % http_mem print "Total memory is: %s KB" % total_mem print "Percent: %.2f%%" % (http_mem/float(total_mem)*100) [root@133 systeminformation]# python 10_httpd.py Apache memory is: 435760 KB Total memory is: 20455792 KB Percent: 2.13%
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流