• Linux查看CPU、内存、IO占用高的进程


    查看CPU占用高的top15进程

    ps -eo pid,user,%cpu,%mem,argu --cols 150 | sort -nrk 3 | head -n 15

    查看内存占用高的top15进程

    ps -eo pid,user,%cpu,%mem,argu --cols 150 | sort -nrk 4 | head -n 15

    查看IO占用高的top15进程

    ./ind_high_io_process.py 3 4 5。其中3表示间隔3秒获取一次数据。4表示获取读、写IO最高的前4个IO进程。5表示程序运行5次。

    #!/bin/python 
    #-*- coding:utf-8 -*- 
    # Filename:    ind_high_io_process
    # Revision:    1.0 
    # Date:        2013-3-8 
    # Author:      simonzhang 
    # web:         www.simonzhang.net 
    # Email:       simon-zzm@163.com 
    ### END INIT INFO
    import os
    import re
    import sys
    import time
    from string import strip
      
    
    sys_proc_path = '/proc/'
    re_find_process_number = '^d+$'
      
    #Get IO information from /proc/$pid/io
    
    def collect_info():
        _tmp = {}
        re_find_process_dir = re.compile(re_find_process_number)
        for i in os.listdir(sys_proc_path):
            if re_find_process_dir.search(i):
                #get process name from /proc/${pid}/stat
                process_name = open("%s%s/stat" % (sys_proc_path, i), "rb").read().split(" ")[1]
                # get io information from /proc/${pid}/io
                rw_io = open("%s%s/io" % (sys_proc_path, i), "rb").readlines()
                for _info in rw_io:
                    cut_info = strip(_info).split(':')
                    if strip(cut_info[0]) == "read_bytes":
                        read_io = int(strip(cut_info[1]))
                    if strip(cut_info[0]) == "write_bytes":
                        write_io = int(strip(cut_info[1]))
                _tmp[i] = {"name":process_name, "read_bytes":read_io, "write_bytes":write_io}
        return _tmp
      
      
    def main(_sleep_time, _list_num):
        _sort_read_dict = {}
        _sort_write_dict = {}
        #Get system io information
        process_info_list_frist = collect_info()
        time.sleep(_sleep_time)
        process_info_list_second = collect_info()
     
        for loop in process_info_list_second.keys():
            second_read_v = process_info_list_second[loop]["read_bytes"]
            second_write_v = process_info_list_second[loop]["write_bytes"]
            try:
                frist_read_v = process_info_list_frist[loop]["read_bytes"]
            except:
                frist_read_v = 0
            try:
                frist_write_v = process_info_list_frist[loop]["write_bytes"]
            except:
                frist_write_v = 0
            # compute
            _sort_read_dict[loop] = second_read_v - frist_read_v
            _sort_write_dict[loop] = second_write_v - frist_write_v
        # sort
        sort_read_dict = sorted(_sort_read_dict.items(),key=lambda _sort_read_dict:_sort_read_dict[1],reverse=True)
        sort_write_dict = sorted(_sort_write_dict.items(),key=lambda _sort_write_dict:_sort_write_dict[1],reverse=True)
        # print result
        print "pid     process     read(bytes) pid     process     write(btyes)"
        for _num in range(_list_num):
            read_pid = sort_read_dict[_num][0]
            write_pid = sort_write_dict[_num][0]
            res = "%s" % read_pid
            res += " " * (8 - len(read_pid)) + process_info_list_second[read_pid]["name"]
            res += " " * (12 - len(process_info_list_second[read_pid]["name"])) + "%s" % sort_read_dict[_num][1]
            res += " " * (12 - len("%s" % sort_read_dict[_num][1])) + write_pid
            res += " " * (8 - len(write_pid)) + process_info_list_second[write_pid]["name"]
            res += " " * (12 - len("%s" % process_info_list_second[write_pid]["name"])) + "%s" % sort_write_dict[_num][1]
            print res
        print "
    " * 1
      
      
    if __name__ == '__main__':
        try:
            _sleep_time = sys.argv[1]
        except:
            _sleep_time = 3
        try:
            _num = sys.argv[2]
        except:
            _num = 3
        try:
            loop = sys.argv[3]
        except:
            loop = 1
        for i in range(int(loop)):
            main(int(_sleep_time), int(_num))

    参考资料

    https://zhidao.baidu.com/question/1882904486137130588.html

  • 相关阅读:
    SQL Server 存储过程中处理多个查询条件的几种常见写法分析,我们该用那种写法
    转:SqlServer2012自增列值突然增大1000的原因及解决方法
    sql server 自增列,值突然增大1000的情况
    C# 复制数组容易踩到的坑--引用类型与值类型
    sql中的表值函数与标量值函数区别与用法
    Swift4.0复习循环
    获取视频第一帧图片
    UITableView实现行纵向颜色渐变
    iOS点击按钮第二次不能旋转View
    iOS扩大按钮的点击范围
  • 原文地址:https://www.cnblogs.com/linyfeng/p/6746115.html
Copyright © 2020-2023  润新知