Python: Statistics on the physical memory consumed by the Apache process

Source: Internet
Author: User


The previous description of how to count the system remaining memory: http://msiyuetian.blog.51cto.com/8637744/1772888

The following is primarily a statistical analysis of the physical memory occupied by the Apache process


We can see which processes and their PID are in Apache using the following command

1) PS aux |grep httpd

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/82/95/wKiom1dbwryCWsD4AABsdPFo8v0358.png "title=" 1.png " alt= "Wkiom1dbwrycwsd4aabsdpfo8v0358.png"/>

2) pidof httpd

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/82/95/wKiom1dbxePQBZiHAAAMuilGXXs292.png "title=" 4.png " alt= "Wkiom1dbxepqbzihaaamuilgxxs292.png"/>


The directories corresponding to these processes are temporarily stored in the/proc/directory under the PID name, while the physical memory information of the recording process is recorded in the status file (Vmrss) in the corresponding PID directory, and the sum of the statistics Vmrss is the physical memory occupied by all Apache processes.

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/82/95/wKiom1dbxCSy8LFUAABAIviJadk120.png "title=" 2.png " alt= "Wkiom1dbxcsy8lfuaabaivijadk120.png"/>


650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/82/93/wKioL1dbxdqBSnXJAABo_IujK8s467.png "title=" 3.png " alt= "Wkiol1dbxdqbsnxjaabo_iujk8s467.png"/>


The code is as follows:

Vim httpd.py

#!/usr/bin/env python


Import OS

From subprocess import Popen, PIPE


Def getpid ():

p = Popen ([' pidof ', ' httpd '], Stdout=pipe, Stderr=pipe)

PIDs = P.stdout.read (). Split ()

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 = Total_mem ('/proc/meminfo ')

Print "Apache memory is:%s KB"% Http_mem

Print "Percent:%.2f"% (http_mem/float (total) *100) + '% '

Code Description :

The Subprocess module can invoke the command pidof httpd get the PID and output the return value


The validation results are as follows:

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/82/95/wKiom1dbzaGQpYIzAAAeAcu3dRs935.png "title=" 5.png " alt= "Wkiom1dbzagqpyizaaaeacu3drs935.png"/>


We can visit Apache several times in the following ways to see the change in memory percentage

Yum Install-y elinks

Elinks-dump http://localhost




This article is from the "M April Days" blog, please be sure to keep this source http://msiyuetian.blog.51cto.com/8637744/1787950

Python: Statistics on the physical memory consumed by the Apache process

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.