lx_io_list
Linux 磁盘 I/O 统计 美化列表
一键脚本
bash <(curl -sL gitee.com/meimolihan/cmdbox/raw/master/sh/lx_io_list.sh)
bash <(curl -sL ../sh/lx_io_list.sh)
效果预览
补充说明
该脚本用于美化显示 Linux 磁盘 I/O 统计信息,基于 iostat 或 /proc/diskstats 命令实现,适合快速查看磁盘读写性能和 I/O 负载情况。
功能特点
- 彩色输出:设备名称(青色)、读写数据(蓝色/绿色)使用不同颜色区分
- 完整信息:显示设备名、每秒读请求、每秒写请求、读吞吐量、写吞吐量、I/O 等待时间
- 表格格式化:自动使用
column命令对齐输出 - 双数据源:优先使用
iostat,不可用时自动回退到/proc/diskstats
输出说明
脚本输出包含以下字段:
| 字段 | 说明 |
|---|---|
| 设备 | 磁盘设备名称 |
| 读/s | 每秒读请求次数 |
| 写/s | 每秒写请求次数 |
| 读kB/s | 每秒读取数据量(KB) |
| 写kB/s | 每秒写入数据量(KB) |
| 等待 | 平均每次 I/O 等待时间(毫秒) |
| 使用率 | 磁盘 I/O 使用率百分比 |
注意事项
- 需要系统已安装
iostat或可读取/proc/diskstats - 需要
column命令(通常系统已自带) iostat属于 sysstat 包,可能需要额外安装- 回退模式使用
/proc/diskstats数据,字段含义略有差异 - 高 I/O 等待时间(>100ms)会显示红色警告
脚本源码
#!/bin/bash
set -uo pipefail
list_color_init() {
export gl_hui=$'\033[38;5;59m'
export gl_hong=$'\033[38;5;9m'
export gl_lv=$'\033[38;5;10m'
export gl_huang=$'\033[38;5;11m'
export gl_lan=$'\033[38;5;32m'
export gl_bai=$'\033[38;5;15m'
export gl_zi=$'\033[38;5;13m'
export gl_bufan=$'\033[38;5;14m'
export reset=$'\033[0m'
}
list_color_init
break_end() {
echo -e "${gl_lv}操作完成${gl_bai}"
echo -e "${gl_bai}按任意键继续 ${gl_hong}.${gl_huang}.${gl_lv}.${gl_bai}\c"
read -r -n 1 -s -r -p ""
echo ""
clear
}
column_if_available() {
if command -v column &> /dev/null; then
column -t -s $'\t'
else
cat
fi
}
list_beautify_linux_io() {
{
printf "%s%s\t%s\t%s\t%s\t%s\t%s\t%s%s\n" "$gl_hui" "设备" "读/s" "写/s" "读kB/s" "写kB/s" "等待" "使用率" "$reset"
printf "%s%s\t%s\t%s\t%s\t%s\t%s\t%s%s\n" "$gl_hui" "--------" "--------" "--------" "--------" "--------" "--------" "--------" "$reset"
if command -v iostat &> /dev/null; then
data=$(iostat -x 2>/dev/null | awk 'NR>2 && /^[a-z]/ {print $1, $4, $5, $6, $7, $10, $12}' 2>/dev/null)
else
data=$(cat /proc/diskstats 2>/dev/null | awk '$3 ~ /^[a-z]+$/ && $3 !~ /loop|ram/ {
device=$3; rs=$4; ws=$8;
rkb=$6*512/1024; wkb=$10*512/1024;
await_t=($7+$11)/($4+$8+0.01);
printf "%s %.0f %.0f %.1f %.1f %.1f 0.0\n", device, rs, ws, rkb, wkb, await_t
}')
fi
if [ -z "$data" ]; then
printf "%s%s\t%s\t%s\t%s\t%s\t%s\t%s%s\n" "$gl_huang" "(无数据)" "(无数据)" "(无数据)" "(无数据)" "(无数据)" "(无数据)" "(无数据)" "$reset"
else
echo "$data" | awk -v dev_color="$gl_lan" \
-v rs_color="$gl_lv" \
-v ws_color="$gl_bufan" \
-v rkb_color="$gl_lv" \
-v wkb_color="$gl_bufan" \
-v await_color="$gl_huang" \
-v util_color="$gl_huang" \
-v gl_hong_io="$gl_hong" \
-v reset="$reset" '
BEGIN {
FS="[[:space:]]+";
OFS="\t"
}
{
device = $1
rs = $2
ws = $3
rkb = $4
wkb = $5
await = $6
util = $7
await_color_actual = await_color
if (await + 0 > 100) {
await_color_actual = gl_hong_io
} else if (await + 0 > 50) {
await_color_actual = "\033[38;5;11m"
}
util_color_actual = util_color
if (util + 0 > 80) {
util_color_actual = gl_hong_io
} else if (util + 0 > 50) {
util_color_actual = "\033[38;5;11m"
}
print dev_color device reset,
rs_color rs reset,
ws_color ws reset,
rkb_color rkb reset,
wkb_color wkb reset,
await_color_actual await reset,
util_color_actual util "%" reset
}'
fi
} | column_if_available
}
list_beautify_all() {
clear
echo -e "${gl_zi}>>> Linux磁盘I/O列表${gl_bai}"
echo -e "${gl_bufan}————————————————————————————————————————————————${gl_bai}"
list_beautify_linux_io
echo -e "${gl_bufan}————————————————————————————————————————————————${gl_bai}"
break_end
}
list_beautify_all
创建本地脚本
new_script="new_test.sh"
cat > "$new_script" << 'EOF'
#!/bin/bash
# 粘贴脚本源码
EOF
# 保留本地脚本,去掉 rm -f "$new_script"
chmod +x "$new_script" && ./"$new_script" && rm -f "$new_script"
相关命令
- lx_block_list
- lx_cert_list
- lx_cron_list
- lx_disk_list
- lx_env_list
- lx_firewall_list
- lx_fstab_list
- lx_gpu_list
- lx_hardware_list
- lx_inode_list
- lx_io_list 👈 当前所在位置
- lx_ip_list
- lx_journal_log_list
- lx_load_list
- lx_locale_list
- lx_login_log_list
- lx_ls_list
- lx_lscpu_list
- lx_lspci_list
- lx_lvm_list
- lx_modules_list
- lx_mounts_list
- lx_nic_list
- lx_package_list
- lx_port_list
- lx_process_list
- lx_raid_list
- lx_services_list
- lx_sshd_conf_list
- lx_swap_list
- lx_systemd_list
- lx_tcp_list
- lx_timer_list
- lx_timezone_list
- lx_uptime_list
- lx_usb_list
- lx_user_list
- lx_zombie_list