您好,登錄后才能下訂單哦!
統計apache進程占用的物理內存
[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%
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。