脳汁portal

アメリカ在住(だった)新米エンジニアがその日学んだIT知識を書き綴るブログ

vmstatに時間の情報をつけて、標準出力とログの両方に出力する方法

コマンド

主にリアルタイム監視などの際に

  • vmstatを1秒毎にコンソールに表示
  • かつ後で確認するために日時情報つけてログにも保存しておきたい

ときのコマンドです。

vmstat 1 | gawk '{ print strftime("%Y/%m/%d %H:%M:%S"), $0 } { fflush() }' | tee vmstat.log

説明

通常(オプション無し)

# vmstat
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 0  0      0 436512  50408  55528    0    0   514     9   53  114  1  1 94  4  0

リアルタイムで秒数指定して監視する方法

# vmstat ${秒数}

# vmstat 1
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 0  0      0 436512  50408  55528    0    0   408     7   44   93  1  0 96  3  0
 0  0      0 436504  50408  55528    0    0     0     0   13   14  0  0 100  0  0
 0  0      0 436504  50408  55528    0    0     0     0   17   22  0  0 100  0  0
・
・
・

日時情報の追加

vmstat | gawk '{ print strftime("%Y/%m/%d %H:%M:%S"), $0 } { fflush() }'

標準出力とログの両方に出力する方法

# vmstat | tee vmstat.log
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 0  0      0 436256  50408  55532    0    0   266     5   35   69  0  0 97  2  0

# ls -l
total 8
-rw-r--r-- 1 root root 1127 Aug  3 09:48 vmstat.log

全部組み合わせると一番上のコマンドになります。