如何测量每小时总磁盘 I/O

mar*_*lar 5 linux io disk

我有一个云服务器,我们还需要为磁盘 IO 使用付费。以下是统计数据的示例:

04/Sep/2013 07:24:19
04/Sep/2013 08:24:19
0,5 GB / 1 vCPU (0,08 Kr. per hour): Charge for 44.7578125GB disk I/O
Run Code Online (Sandbox Code Playgroud)

因此,一小时内,我们需要为大约 45 GB 的磁盘 I/O 付费。

对我来说,这听起来流量很大,我想自己做一些监控来检查。我知道诸如此类的工具dstatsysstat但我没有找到任何显示一小时(或其他时间范围)总计的示例。大多数示例都是对结果求平均值,例如以下命令:

dstat -tdD total 60
Run Code Online (Sandbox Code Playgroud)

此处,它显示了 60 秒的磁盘 I/O 测量,但它是平均值。因此,如果我复制一个大文件,复制时我会看到数字增加,但一旦完成,数字就会再次减少。换句话说,我最终并没有得到那段时间的真实总数。

如何记录给定时间范围内的磁盘 I/O 总量?

slm*_*slm 5

您可以使用该工具iostat收集磁盘利用率信息。它需要几个参数,包括开关-d

   -d     Display the device utilization report.
Run Code Online (Sandbox Code Playgroud)

它还需要一个以秒为单位的参数,即它应该重新运行的频率间隔。该值3600是一小时中的秒数。

例子

$ iostat -d 3600
Linux 2.6.35.14-106.fc14.x86_64 (grinchy)   09/04/2013  _x86_64_    (4 CPU)

Device:            tps   Blk_read/s   Blk_wrtn/s   Blk_read   Blk_wrtn
sda              20.53        71.86      1259.61   20308334  356000380
dm-0              4.92        39.02        28.81   11027610    8143376
dm-1              0.54         0.93         3.38     261472     954912
dm-2            156.65        31.87      1227.42    9006394  346902056
Run Code Online (Sandbox Code Playgroud)

该命令的输出可以重定向到一个文件:

$ iostat -d 3600 >> iostat_hrly.log
Run Code Online (Sandbox Code Playgroud)

单位的含义

如果您查阅手册页,iostat它对单位有很好的描述。

摘抄

   Blk_read/s
         Indicate the amount of data read from the device expressed in a 
         number of blocks per second. Blocks are equivalent to sectors with
         kernels 2.4 and later and  therefore have a size of 512 bytes. With
         older kernels, a block is of indeterminate size.

  Blk_wrtn/s
         Indicate the amount of data written to the device expressed in a 
         number of blocks per second.

  Blk_read
         The total number of blocks read.

  Blk_wrtn
         The total number of blocks written.
Run Code Online (Sandbox Code Playgroud)

因此,一个块为 512 字节,因此设备的 Blk_read/s(以 MB 为单位)为sda71.86 * 512 字节 = 36.79232 千字节/秒。

还有其他开关可以自动更改输出中的单位。

iostat手册页摘录

-h     Make the NFS report displayed by option -n easier to read by a human.

-k     Display statistics in kilobytes per second instead of blocks per 
       second.  Data displayed are valid only with kernels 2.4 and later.

-m     Display statistics in megabytes per second instead of blocks or 
       kilobytes per second.  Data displayed are valid only with kernels 
       2.4 and later.
Run Code Online (Sandbox Code Playgroud)

示例(以 KB/秒为单位)

因此,这可能更有用,以 KB/s 为单位显示吞吐量:

$ iostat -dk 3600
Linux 2.6.35.14-106.fc14.x86_64 (grinchy)   09/05/2013  _x86_64_    (4 CPU)

Device:            tps    kB_read/s    kB_wrtn/s    kB_read    kB_wrtn
sda              20.85        47.25       663.81   15475096  217427086
dm-0              5.01        20.00        14.43    6549301    4725068
dm-1              0.54         0.58         1.60     189064     524872
dm-2            165.30        26.65       647.78    8730281  212177124
Run Code Online (Sandbox Code Playgroud)