iostat : 报告CPU统计信息以及设备和分区的输入/输出统计信息
1.基本参数
-p : 指定设备名称,例如/dev/nvme0n1...
-t : 时间戳
-m : 以megabytes per second显示信息
-o JSON : 以JSON格式显示统计信息
2.设备io信息
获取设备nvme0n1的io信息并输出到iostat1.log文件中
iostat -p /dev/nvme0n1 -tm 1 -o JSON >> iostat1.log
展示部分内容
{"sysstat": {
"hosts": [
{
"nodename": "localhost.localdomain",
"sysname": "Linux",
"release": "5.14.0-404.el9.aarch64",
"machine": "aarch64",
"number-of-cpus": 64,
"date": "2024年01月23日",
"statistics": [
{
"timestamp": "2024年01月23日 19时02分16秒",
"avg-cpu": {"user": 1.16, "nice": 0.00, "system": 1.51, "iowait": 0.10, "steal": 0.00, "idle": 97.23},
"disk": [
{"disk_device": "nvme1n1", "tps": 59766.08, "MB_read/s": 207.10, "MB_wrtn/s": 419.83, "MB_dscd/s": 0.00, "MB_read": 216248253, "MB_wrtn": 438369438, "MB_dscd": 0}
]
},{
"timestamp": "2024年01月23日 19时40分21秒",
"avg-cpu": {"user": 2.86, "nice": 0.00, "system": 2.65, "iowait": 0.00, "steal": 0.00, "idle": 94.49},
"disk": [
{"disk_device": "nvme1n1", "tps": 291424.00, "MB_read/s": 0.00, "MB_wrtn/s": 1138.38, "MB_dscd/s": 0.00, "MB_read": 0, "MB_wrtn": 1138, "MB_dscd": 0}
]
}
]
}
]
}}
3.io信息处理
3.1.Shell方式
jq是Linux用来处理JSON格式数据的工具
#黄色标记可以更换成"MB_read/s"、"tps"#
num=$(cat iostat1.log |jq '.sysstat.hosts[0].statistics|length')
for (( i=0 ; i<$num ; i++))
do
time=$(cat iostat1.log |jq '.sysstat.hosts[0].statistics['$i'].timestamp')
spd=$(cat iostat1.log |jq '.sysstat.hosts[0].statistics['$i'].disk[0]."MB_wrtn/s"')
echo $time,$spd >> iostat_result.log
done
3.2.python方式
导入ijson模块处理数据,减少内存的占用
3.2.1.安装ijson模块
pip install ijson
3.2.2.逐行的方式读取io信息
import ijson
def AppendFile(text,filename):
with open(filename,'a') as f:
f.write(text)
f.write("\n")
def getdata():
with open('iostat1.log', 'r', encoding='utf-8') as file:
parser = ijson.parse(file)
for prefix, event, value in parser:
if prefix.endswith(".timestamp") and event == "string":
time = value
if prefix.endswith(".MB_wrtn/s") and event == "number":
spd_w = value
AppendFile(f'{time}:{spd_w}','iostat-result.log')
getdata()