简单计算磁盘平均IO

前端之家收集整理的这篇文章主要介绍了简单计算磁盘平均IO前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

如果有多个磁盘, 计算的时候就需要计算平均值,显然直接利用linux的iostat的命令不能完成这个需求,下面是个简单的计算磁盘相关平均指标的脚本

#! /bin/bash 
#iostat -x 1 2 > 1.txt

#sed -i '1,22'd 1.txt


# $1 is file name

file_name=$1

grep  -E "(sd)[b-m]" $file_name | awk '{print $4}' | awk 'BEGIN {sum=0;c=0;}{sum+=$1;c++;} END {print "r/s: \n" c,sum,sum/c}'

grep  -E "(sd)[b-m]" $file_name | awk '{print $5}' | awk 'BEGIN {sum=0;c=0;}{sum+=$1;c++;} END {print "w/s: \n" c,sum/c}'

grep  -E "(sd)[b-m]" $file_name | awk '{print $4,$5}' | awk 'BEGIN {sum=0;c=0;}{sum+=$1+$2;c++;} END {print "rw/s: \n" c,sum/c}'

grep  -E "(sd)[b-m]" $file_name | awk '{print $6}' | awk 'BEGIN {sum=0;c=0;}{sum+=$1;c++;} END {print "rMB/s: \n" c,sum/c,sum/1024/1024*8}'

grep  -E "(sd)[b-m]" $file_name | awk '{print $7}' | awk 'BEGIN {sum=0;c=0;}{sum+=$1;c++;} END {print "wMB/s: \n" c,sum/1024/1024*8}'

grep  -E "(sd)[b-m]" $file_name | awk '{print $6,$7}' | awk 'BEGIN {sum=0;c=0;}{sum+=$1+$2;c++;} END {print "rwMB/s: \n" c,sum/1024/1024*8}'

grep  -E "(sd)[b-m]" $file_name | awk '{print $4,$5,$13}' | awk 'BEGIN {rwsum=0;c=0;svctm=0}{rwsum+=$1+$2;svctm+=$3;c++;} END {print "%util: \n" c,rwsum*(svctm/10)/c/c}'

执行 iostat -x 1,截取第二次的磁盘IO相关值,放在aa文件中, 比如

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
          22.48    0.00   41.12   36.03    0.00    0.38

Device:         rrqm/s   wrqm/s     r/s     w/s    rkB/s    wkB/s avgrq-sz avgqu-sz   await r_await w_await  svctm  %util
sda               0.00     5.60   50.47   77.13   202.13  6131.73    99.28     4.36   34.18    1.17   55.77   0.36   4.64
sdm               0.00     0.00   51.80   38.53 19755.47 19729.07   874.20     2.08   23.06   12.90   36.72   9.74  87.95
sdj               0.00     0.00   60.87   45.07 21913.07 23074.13   849.35     2.25   21.26   16.18   28.13   9.37  99.31
sdi            1845.93     0.00   94.07   45.33 23076.80 23210.67   664.10     5.00   35.89   30.75   46.56   7.16  99.84
sdd               0.00     0.00   59.87   43.13 22012.00 22084.27   856.24     2.20   21.47   16.49   28.37   9.62  99.09
sdh               0.00     0.00   58.60   43.33 21451.73 22186.67   856.21     2.22   21.89   16.56   29.11   9.64  98.24
sdg               0.00     0.00   63.93   32.87 24493.87 16827.73   853.75     1.82   18.69   13.66   28.47   9.59  92.85
sde               0.00     0.00   57.20   44.80 21725.87 22937.60   875.75     2.27   22.24   16.81   29.18   9.60  97.92
sdb              91.80     0.00   65.60   35.73 24418.13 18295.47   843.03     2.15   21.21   16.13   30.54   9.82  99.52
sdl               0.00     0.00   58.13   38.87 20926.93 19899.73   841.79     2.03   20.98   16.17   28.17   9.89  95.95
sdf               0.00     0.00   56.67   41.80 21868.27 21401.60   878.87     2.05   20.82   16.38   26.85   9.78  96.29
sdk               0.00     0.00   57.53   46.60 20996.53 23859.20   861.51     2.35   22.60   17.13   29.34   9.54  99.36
sdc               0.00     0.00   59.40   44.20 21436.27 22630.40   850.71     2.32   22.29   16.61   29.93   9.60  99.47

直接执行脚本 sh stat_iostat.sh aa

r/s: 
12 743.67 61.9725
w/s: 
12 500.26 41.6883
rw/s: 
12 1243.93 103.661
rMB/s: 
12 264075 22006.2 2.01473
wMB/s: 
12 256137 21344.7 1.95417
rwMB/s: 
12 520211 43351 3.9689
%util: 
12 97.916

上面数据解读: 1. 磁盘的读写直接转化成流量就是 3.96G 2. 磁盘的平均%util达到 97%

猜你在找的Bash相关文章