查看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: -3-8 # Author:simonzhang # web:# Email: simon-zzm@ ### END INIT INFOimport osimport reimport sysimport timefrom string import stripsys_proc_path = '/proc/'re_find_process_number = '^\d+$'#Get IO information from /proc/$pid/iodef collect_info():_tmp = {}re_find_process_dir = pile(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}/statprocess_name = open("%s%s/stat" % (sys_proc_path, i), "rb").read().split(" ")[1]# get io information from /proc/${pid}/iorw_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 _tmpdef main(_sleep_time, _list_num):_sort_read_dict = {}_sort_write_dict = {}#Get system io informationprocess_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 = 0try: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# sortsort_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 resultprint "pidprocessread(bytes) pidprocesswrite(btyes)"for _num in range(_list_num):read_pid = sort_read_dict[_num][0]write_pid = sort_write_dict[_num][0]res = "%s" % read_pidres += " " * (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_pidres += " " * (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 resprint "\n" * 1if __name__ == '__main__':try:_sleep_time = sys.argv[1]except:_sleep_time = 3try:_num = sys.argv[2]except:_num = 3try:loop = sys.argv[3]except:loop = 1for i in range(int(loop)):main(int(_sleep_time), int(_num))
参考资料
/question/1882904486137130588.html