013_针对单个pid的cpu/内存/io的资源占用统计

#!/usr/bin/env python

import sys
import os
import subprocess
from decimal import Decimal
from decimal import getcontext

def cpu_proc(pid):
    '''
    Reference:https://stackoverflow.com/questions/16726779/how-do-i-get-the-total-cpu-usage-of-an-application-from-proc-pid-stat
    :param pid:
    :return:
    '''
    try:
        with open(os.path.join('/proc/', pid, 'stat'), 'r') as pidfile:
            proctimes = pidfile.readline()
            # get utime from /proc/<pid>/stat, 14 item
            utime = proctimes.split(' ')[13]
            # get stime from proc/<pid>/stat, 15 item
            stime = proctimes.split(' ')[14]
            cutime = proctimes.split(' ')[15]
            cstime = proctimes.split(' ')[16]
            total_time =int(utime) + int(stime) + int(cutime) + int(cstime)
            uptime = cput()
            starttime = proctimes.split(' ')[21]
            Hertz = subprocess.Popen("getconf CLK_TCK", shell=True,stdout=subprocess.PIPE).communicate()[0].strip('
')
            seconds =Decimal(str(uptime)) + Decimal(str(starttime)) +Decimal(str(Hertz))
            getcontext().prec = 4
            cpu_usag = Decimal('100') * (( Decimal(str(total_time)) / Decimal(str(Hertz)) )/ Decimal(str(seconds)))
            return cpu_usag
    except IOError as e:
        print('ERROR: %s' % e)
        sys.exit(2)

def cpu_top(pid):
    try:
        proc = subprocess.Popen("top -p %s -b -n 1 | grep -w ezk-agent | awk '{print $9}'" % pid, shell=True, stdout=subprocess.PIPE)
        cpu_percentage = proc.communicate()
        return cpu_percentage[0].rstrip('
')
    except KeyboardInterrupt:
        sys.exit(0)

def cput():
    try:
        with open('/proc/uptime', 'r') as procfile:
            cputimes = procfile.readline()
            return(float(cputimes.split(' ')[0]))
    except IOError as e:
        print('ERROR: %s' % e)
        sys.exit(3)

def mem_usage_calc(pid):
    (memkb, err)= subprocess.Popen("pmap -x  "+pid+"|grep -i total|awk '{print $3}'", shell=True,
                                  stdout=subprocess.PIPE).communicate()
    return  memkb.strip('
')

def fd_usage_calc(pid):
    (fd_lsof_num, err) = subprocess.Popen("lsof -n|grep " + pid + "| wc -l", shell=True,
                                  stdout=subprocess.PIPE).communicate()
    pro_command="ls /proc/"+pid+"/fd|wc -l"
    (fd_proc_num, err) = subprocess.Popen(pro_command, shell=True,
                                  stdout=subprocess.PIPE).communicate()
    return fd_lsof_num.strip('
'),fd_proc_num.strip('
')

def main(pid):
    print("pid:{}".format(pid))
    print("*"*14 + "CPU" + "*"*14)
    print "proc_get_value:{}".format(cpu_proc(pid))
    print "top_get_value:{}".format(cpu_top(pid))

    print("*"*14 + "MEM" + "*"*14)
    print "mem_get_value:{}kB".format(mem_usage_calc(pid))

    print("*"*10 + "File_Handler" + "*"*10)
    print "File_Handler_lsof_get_value:{}".format(fd_usage_calc(pid)[0])
    print "File_Handler_fd_get_value:{}".format(fd_usage_calc(pid)[1])

if __name__ == '__main__':
    if len(sys.argv) == 2:
        pid = sys.argv[1]
    else:
        try:
            (out, err) = subprocess.Popen("ps -ef|grep ezk-agent/ezk-agent|grep -v grep|awk '{print $2}'", shell=True,
                                          stdout=subprocess.PIPE).communicate()
            pid = out.strip('
')
        except:
            print('No PID specified. Usage: %s <PID>' % os.path.basename(__file__))
            sys.exit(1)
    main(pid)
原文地址:https://www.cnblogs.com/arun-python/p/9936454.html