如何檢查Linux 00下進程的IO活動嘿,Linux
妳好,很高興回答妳的問題。服務器cpu利用率不高,負載比較高,檢查IO。硬盤IO可以通過命令vmstat或iostat(或者通過用yum安裝dstat)獲得,網絡IO可以通過iftop命令獲得。但是不知道哪個進程在硬盤上使用了很高的IO,所以通過搜索找不到相關命令,只好寫腳本進行統計處理。這個腳本在CentOS6下(內核2.6以上)通過了python2.6測試。直接運行腳本,默認采集3秒數據,顯示讀寫最高的前三個進程。如果使用參數,可以使用命令“python hip . py 453”。第壹個數字是讀取和寫入數據收集之間的秒數,第二個數字是讀取和寫入最多的N個進程的數量,第三個數字是腳本運行的次數。因為參數部分相對簡單,所以必須編寫所有三個參數。。#!/bin/python #-*-coding:utf-8-*-# filename:ind _ high _ io _ process # revision:1.0 # date:2013-3-8 #作者:Simon Zhang # web:# # # endinitinfoimportosimportreimportsysimporttimefromstringimportstrip####sys_proc_path='/proc/'re_find_process_number='^\ d+$ ' # # #通過/proc/$ PID/io # # # def collect _ info():_ tmp = { } re _ find _ process _ dir = re . pile(re _ find _ process _ number)for inos . listdir獲取讀寫信息閱讀()。split(" "[1]# Read io information rw _ io = open(" % s % s/io " %(sys _ proc _ path,I)," rb ")。readlines()for _ info inrw _ 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 _ tmpdefmain(_ sleep _ time,_)睡眠(_ sleep _ time)。process _ info _ list _ second = collect _ info()# Group read data and Write data,Write in two dictionary for looping 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 = process _ info _ list _ frist[loop][" write _ bytes "]Except:frist _ write _ v = 0 #計算數據字段中第二次獲得的數據的差值_ sort _ read _ dict[= second _ read _ read = second _ write _ v-frist _ write # 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 "pidprocessread(字節) 如有疑問,請點擊提問。希望我的回答對妳有幫助。望采納!~O(∩_∩)O~