iostat命令详解
iostat
(I/O Statistics)是Linux系统中用于监控磁盘I/O活动和CPU使用情况的重要工具,属于sysstat软件包,是系统性能监控的核心工具之一。
安装和基本信息
# 在CentOS/RHEL系统安装
yum install sysstat
# 在Ubuntu/Debian系统安装
apt install sysstat
# 检查版本
iostat -V
基本语法
iostat [选项] [间隔时间] [次数]
主要选项
基本选项
-c
- 只显示CPU使用情况-d
- 只显示磁盘使用情况-k
- 以KB为单位显示-m
- 以MB为单位显示-h
- 人类可读格式显示
扩展选项
-x
- 显示扩展统计信息-t
- 显示时间戳-N
- 显示磁盘阵列(LVM)信息-n
- 显示NFS使用情况-p [设备]
- 显示块设备和分区的情况-j ID
- 显示持久设备名称
输出控制
-g
- 以组形式显示-z
- 忽略不活跃的设备-y
- 跳过第一次报告(显示自启动以来的统计)
基本使用示例
CPU和磁盘概览
# 显示CPU和磁盘使用情况
iostat
# 每2秒更新一次,共5次
iostat 2 5
# 只显示CPU信息
iostat -c
# 只显示磁盘信息
iostat -d
# 以人类可读格式显示
iostat -h
扩展磁盘信息
# 显示扩展磁盘统计信息(最常用)
iostat -x
# 显示扩展信息,每3秒更新
iostat -x 3
# 显示扩展信息,以MB为单位
iostat -x -m
# 显示时间戳
iostat -x -t
输出字段详解
CPU报告字段
$ iostat -c
avg-cpu: %user %nice %system %iowait %steal %idle
2.50 0.00 1.25 0.75 0.00 95.50
%user
- 用户态CPU使用率%nice
- nice值为负的进程CPU使用率%system
- 系统态CPU使用率%iowait
- CPU等待I/O操作的时间百分比%steal
- 虚拟机被偷取的CPU时间%idle
- CPU空闲时间百分比
基本磁盘报告字段
$ iostat -d
Device tps kB_read/s kB_wrtn/s kB_read kB_wrtn
sda 15.25 125.50 89.25 1255000 892500
Device
- 设备名称tps
- 每秒传输次数(IOPS)kB_read/s
- 每秒读取的KB数kB_wrtn/s
- 每秒写入的KB数kB_read
- 读取的总KB数kB_wrtn
- 写入的总KB数
扩展磁盘报告字段
$ iostat -x
Device r/s w/s rkB/s wkB/s rrqm/s wrqm/s %rrqm %wrqm r_await w_await aqu-sz rareq-sz wareq-sz svctm %util
sda 5.25 3.75 125.5 89.2 0.25 1.50 4.55 28.57 2.50 4.25 0.05 23.90 23.79 1.85 1.75
I/O操作统计
r/s
- 每秒读请求数w/s
- 每秒写请求数rkB/s
- 每秒读取KB数wkB/s
- 每秒写入KB数
请求合并统计
rrqm/s
- 每秒合并的读请求数wrqm/s
- 每秒合并的写请求数%rrqm
- 读请求合并百分比%wrqm
- 写请求合并百分比
延迟和队列统计
r_await
- 读操作平均等待时间(ms)w_await
- 写操作平均等待时间(ms)aqu-sz
- 平均队列长度rareq-sz
- 平均读请求大小(KB)wareq-sz
- 平均写请求大小(KB)
性能指标
svctm
- 平均服务时间(ms)%util
- 设备利用率百分比
实际应用场景
系统性能监控
# 实时监控系统I/O性能
iostat -x -t 1
# 监控特定设备
iostat -x -d sda 2
# 监控所有分区
iostat -x -p sda
# 以MB为单位监控
iostat -x -m 3 10
I/O瓶颈分析
#!/bin/bash
# io_analysis.sh - I/O性能分析脚本
echo "I/O性能分析报告"
echo "================"
# 显示当前I/O状态
iostat -x 1 3 | awk '
BEGIN {
print "设备性能分析:"
print "设备名\t利用率\t平均等待\tIOPS\t状态"
print "--------------------------------------------"
}
/^[a-z]/ {
device = $1
util = $NF
await = $(NF-4)
iops = $4 + $5
status = "正常"
if (util > 80) status = "高负载"
if (await > 20) status = "延迟高"
if (util > 90) status = "严重拥塞"
printf "%s\t%.1f%%\t\t%.2fms\t\t%.1f\t%s\n", device, util, await, iops, status
}'
echo -e "\n性能建议:"
iostat -x 1 1 | awk '
/^[a-z]/ {
if ($NF > 80) {
print "- 设备 " $1 " 利用率过高(" $NF "%),考虑负载均衡"
}
if ($(NF-4) > 20) {
print "- 设备 " $1 " 响应时间过长(" $(NF-4) "ms),检查磁盘健康"
}
}'
磁盘性能基准测试
#!/bin/bash
# disk_benchmark.sh - 磁盘性能基准
DEVICE=${1:-"sda"}
DURATION=60
echo "开始磁盘性能基准测试: $DEVICE"
echo "测试时长: ${DURATION}秒"
echo "================================="
# 记录测试开始前的状态
iostat -x $DEVICE 1 1 > /tmp/iostat_before.txt
echo "执行I/O压力测试..."
# 这里可以运行fio或dd等I/O测试工具
# fio --name=test --rw=randrw --bs=4k --numjobs=4 --size=1G --runtime=60
# 监控测试过程
iostat -x $DEVICE 1 $DURATION | awk '
BEGIN {
max_util = 0
max_iops = 0
max_await = 0
count = 0
}
/^'$DEVICE'/ {
count++
util = $NF
iops = $4 + $5
await = $(NF-4)
if (util > max_util) max_util = util
if (iops > max_iops) max_iops = iops
if (await > max_await) max_await = await
total_util += util
total_iops += iops
total_await += await
}
END {
if (count > 0) {
print "\n测试结果汇总:"
print "=============="
printf "平均利用率: %.1f%%\n", total_util/count
printf "平均IOPS: %.1f\n", total_iops/count
printf "平均响应时间: %.2fms\n", total_await/count
printf "峰值利用率: %.1f%%\n", max_util
printf "峰值IOPS: %.1f\n", max_iops
printf "最大响应时间: %.2fms\n", max_await
}
}'
存储设备健康监控
#!/bin/bash
# storage_health_monitor.sh
LOG_FILE="/var/log/iostat_monitor.log"
THRESHOLD_UTIL=85
THRESHOLD_AWAIT=30
monitor_storage() {
local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
iostat -x 1 1 | while read line; do
if [[ $line =~ ^[a-z] ]]; then
read -r device tps rd_sec_s wr_sec_s avgrq_sz avgqu_sz await svctm util <<< "$line"
# 检查设备利用率
if (( $(echo "$util > $THRESHOLD_UTIL" | bc -l) )); then
echo "[$timestamp] 警告: 设备 $device 利用率过高 (${util}%)" | tee -a $LOG_FILE
fi
# 检查响应时间
if (( $(echo "$await > $THRESHOLD_AWAIT" | bc -l) )); then
echo "[$timestamp] 警告: 设备 $device 响应时间过长 (${await}ms)" | tee -a $LOG_FILE
fi
# 记录性能数据
echo "[$timestamp] $device: 利用率=${util}%, 响应时间=${await}ms, IOPS=${tps}" >> $LOG_FILE
fi
done
}
# 持续监控
while true; do
monitor_storage
sleep 60
done
高级监控技巧
特定设备监控
# 监控特定磁盘和分区
iostat -x -p sda
# 监控RAID设备
iostat -x -N
# 监控NFS
iostat -n
# 监控特定分区的详细信息
iostat -x -p sda1 2 10
性能趋势分析
#!/bin/bash
# io_trend_analysis.sh
DEVICE="sda"
SAMPLES=100
INTERVAL=5
echo "I/O性能趋势分析: $DEVICE"
echo "采样间隔: ${INTERVAL}秒, 样本数: $SAMPLES"
iostat -x $DEVICE $INTERVAL $SAMPLES | awk '
BEGIN {
count = 0
sum_util = 0
sum_await = 0
sum_iops = 0
print "时间\t\t利用率\t响应时间\tIOPS\t\t趋势"
print "--------------------------------------------------------"
}
/^'$DEVICE'/ {
count++
util = $NF
await = $(NF-4)
iops = $4 + $5
sum_util += util
sum_await += await
sum_iops += iops
# 计算趋势
if (count > 1) {
util_trend = (util > prev_util) ? "↑" : (util < prev_util) ? "↓" : "→"
await_trend = (await > prev_await) ? "↑" : (await < prev_await) ? "↓" : "→"
} else {
util_trend = await_trend = "-"
}
printf "%s\t%.1f%% %s\t%.2fms %s\t%.1f\n",
strftime("%H:%M:%S"), util, util_trend, await, await_trend, iops
prev_util = util
prev_await = await
# 每10个样本显示平均值
if (count % 10 == 0) {
printf "-- 最近10个样本平均: 利用率=%.1f%%, 响应时间=%.2fms, IOPS=%.1f --\n",
sum_util/10, sum_await/10, sum_iops/10
sum_util = sum_await = sum_iops = 0
}
}
'
多设备对比监控
#!/bin/bash
# multi_device_compare.sh
echo "多设备I/O性能对比"
echo "=================="
iostat -x 1 1 | awk '
BEGIN {
printf "%-10s %8s %10s %10s %8s\n", "设备", "利用率", "IOPS", "响应时间", "状态"
print "---------------------------------------------------"
}
/^[a-z]/ {
device = $1
util = $NF
iops = $4 + $5
await = $(NF-4)
# 性能评级
if (util > 90) status = "拥塞"
else if (util > 70) status = "繁忙"
else if (util > 30) status = "活跃"
else status = "空闲"
printf "%-10s %7.1f%% %9.1f %9.2fms %8s\n", device, util, iops, await, status
}
END {
print "---------------------------------------------------"
print "性能等级: 空闲(<30%) < 活跃(30-70%) < 繁忙(70-90%) < 拥塞(>90%)"
}'
性能调优指导
基于iostat的调优建议
# 分析并给出调优建议
analyze_io_performance() {
iostat -x 1 3 | awk '
/^[a-z]/ {
device = $1
util = $NF
await = $(NF-4)
iops = $4 + $5
rrqm = $6
wrqm = $7
print "设备: " device
print "利用率: " util "%"
print "IOPS: " iops
print "响应时间: " await "ms"
# 调优建议
if (util > 85) {
print "建议: 利用率过高,考虑以下优化:"
print " - 增加更多磁盘做RAID"
print " - 使用SSD替换HDD"
print " - 优化应用I/O模式"
}
if (await > 20) {
print "建议: 响应时间过长,可能原因:"
print " - 磁盘老化或故障"
print " - 磁盘碎片严重"
print " - I/O队列过长"
}
if (rrqm + wrqm < 5) {
print "建议: I/O合并率低,考虑:"
print " - 调整应用I/O大小"
print " - 优化文件系统配置"
}
print "---"
}'
}
注意事项和最佳实践
- 第一次输出: 第一次输出显示系统启动以来的累计统计,建议跳过
- 监控频率: 生产环境建议1-5秒间隔,避免过于频繁影响性能
- 关键指标: 重点关注%util、await、IOPS这三个指标
- 告警阈值:
- %util > 80% 需要关注
- await > 20ms 可能有问题
- %util > 90% 表示设备饱和
相关命令
iotop
- 实时I/O监控dstat
- 系统资源统计vmstat
- 虚拟内存统计sar
- 系统活动报告lsblk
- 列出块设备
iostat
是Linux系统I/O性能监控的核心工具,掌握其用法对于系统性能优化和故障排查至关重要。