zoukankan      html  css  js  c++  java
  • 使用 python 获取 httpd 程序所占用物理内存

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    #!/usr/bin/env python
    #encoding: utf-8
    '''
    思路: /proc/xx_pid/status 文件中的关键字段 VmRSS 来获取某个进程占用的物理内存
    步骤: 获取 httpd 进程ID列表 --> 通过每个进程id来获取该进程占用物理内存
    '''
     
    from subprocess import Popen, PIPE
    import os,sys
     
    # 通过程序名称获取 pid 列表
    def getProgPids(prog):
        = Popen(['pidof', prog], stdout=PIPE, stderr=PIPE)
        pids = p.stdout.read().split()
        return pids
     
    # 通过具体的进程 id 来获取该进程占用的物理内存
    def getMemByPid(pid):
        fn = os.path.join('/proc', pid, 'status')
        with open(fn) as fd:
            for line in fd:
                if line.startswith('VmRSS'):
                    mem = int(line.split()[1])
                    break
        return mem
     
    # 获取 httpd 服务所有进程占用的物理内存
    def getHttpdMem():
        httpd_mem_sum = 0
        pids = getProgPids('httpd')
        for pid in pids:
            httpd_mem_sum += getMemByPid(pid)
     
        return httpd_mem_sum
     
     
    # 获取系统总的物理内存
    def getOsTotalMemory():
         
        with open('/proc/meminfo') as fd:
            for line in fd:
                if line.startswith('MemTotal'):
                    total_mem = int(line.split()[1])
                    break
        return total_mem
     
     
    if __name__ == '__main__':
        http_mem  =  getHttpdMem()
        total_mem =  getOsTotalMemory()
     
        scale = http_mem / float(total_mem) * 100
        print 'Httpd: %d KB' % http_mem
        print 'Percent: %.2f%%' % scale
  • 相关阅读:
    各種語系的unicode對應以及local編碼方式
    测试邮箱采集
    devexpress控件例子
    2015结束,2016开始
    MYSQL 导出表结构
    mysql 远程访问
    jQuery的选择器中的通配符[id^='code']
    jquery判断checkbox是否选中及改变checkbox状态
    MYSQL 删除重复记录,只保留最大ID
    java.lang.IllegalArgumentException: Comparison method violates its general contract!
  • 原文地址:https://www.cnblogs.com/chenliyang/p/6634777.html
Copyright © 2011-2022 走看看