2013年11月27日水曜日

iostat の結果を gnuplot でグラフ化してみる

iostat などのステータスをとりあえずグラフ化したくて gnuplot を使ってみる。元データは Linux 上にて iostat -mx 1 で出力した。

[hasegaw@localhost ~]$ iostat -mx
Linux 2.6.32-220.el6.x86_64 (localhost.localdomain)     11/26/2013      _x86_64_        (8 CPU)

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           0.51    0.00    1.23   10.73    0.00   87.52

Device:         rrqm/s   wrqm/s     r/s     w/s    rMB/s    wMB/s avgrq-sz avgqu-sz   await  svctm  %util
sda               1.32     0.49    0.77    0.56     0.01     0.00    29.10     0.03   24.11   5.93   0.79
dm-0              0.00     0.00    1.27    0.71     0.01     0.00    11.29     0.15   73.93   2.43   0.48
dm-1              0.00     0.00    0.01    0.00     0.00     0.00     8.00     0.00    4.67   1.69   0.00
dm-2              0.00     0.00    0.81    0.34     0.01     0.00    13.65     0.01    8.89   2.82   0.32
fioa              0.00     0.00    0.01   36.83     0.00    36.78  2044.82     0.03    6.12   0.78   2.89

[hasegaw@localhost ~]$

さらに iostat -mx 1 とすると毎秒の統計情報になるのでこれをファイルに保存しておく。

$ iostat -mtx 1 | grep fioa | tee iostat.txt


(teeにて標準出力がバッファリングされるのですぐに画面上には表示されない)
これで iostat.txt ができあがった。


一番最初のフィールドにデバイス名がはいっているけど、経過秒数に置き換えてしまおう。 awk にかけてみる。冷静に考えたらコレ .csv って拡張子は付けてみたけどカンマ区切りじゃないので CSV でもなんでもないね。w


$ cat iostat.txt | awk '{ print NR,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14 }' > iostat.csv



gnuplot でこれをプロットするには gnuplot 上で下記を実行する。

set xlabel "time (sec)"
set ylabel "MB/s"
set yrange [0:2000]

# write MB/s with Red
plot "iostat.csv" using 1:7 w l lt 1 lw 1 t 'Write MB/s'

# read MB/s with Blue
replot "iostat.csv" using 1:6 w l lt 3 lw 1 t 'Read MB/s'




なおここで使ったデータは書き込みしかしていないので Read MB/s はグラフの一番下を這っている。

毎回やってるとめんどくさいのでシェルスクリプトにしてみた。 in_file.txt を第一引数として指定すると(CSVじゃないけど) .csv を付けたファイル:p、 .gp を付けたファイルを生成して gnuplot を実行し .png ファイルを生成してくれる。

graph.sh

#!/bin/sh
IN_FILE=$1
[ -e "$IN_FILE" ] || exit
CSV_FILE=$1.csv
GP_FILE=$1.gp
PNG_FILE=$1.png
cat $IN_FILE | grep ^fioa | awk '{ print NR,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14 }' > $CSV_FILE

cat <<___ > $GP_FILE
set xlabel "time (sec)"
set ylabel "MB/s"
set yrange [0:2000]

# write MB/s with Red
plot "${CSV_FILE}" using 1:7 w l lt 1 lw 1 t 'Write MB/s'

# read MB/s with Blue
replot "${CSV_FILE}" using 1:6 w l lt 3 lw 1 t 'Read MB/s'

set terminal png
set output "$PNG_FILE"
replot
___
gnuplot $GP_FILE

これで何度でも様々なインプットファイルに対して同じグラフをお手軽に出力できるようになった。

$ sh -x graph.sh iostat.txt

-rw-r--r--  1 hasegaw  staff  2798 Nov 26 23:09 iostat.txt.csv
-rw-r--r--  1 hasegaw  staff   284 Nov 26 23:09 iostat.txt.gp
-rw-r--r--  1 hasegaw  staff  4041 Nov 26 23:09 iostat.txt.png


もちろん何時間分のデータ食わせてもOKである。 gnuplot が死なない限りだろうけど。