LoginSignup
3
4

More than 3 years have passed since last update.

vmstatやらiostatに時刻を付与する

Last updated at Posted at 2018-03-08

サーバの負荷分析にはsarがよく使われる。
sarはCPU、I/O、NW、スワップ状況など色々確認できる便利ツールだが、
最短で1分おきにしかデータがとれない。
秒単位で細かく負荷分析したい場合は、vmstat、iostatがオススメ。

ただし、vmstat、iostatは実行時刻が表示されないので、
そのままだとログとして保存しておいても全く役に立たない。

  • デフォルト
math [root@kouno koizumi_ta]# vmstat 1 10 > vmstat.log
 [root@kouno koizumi_ta]# cat 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 250864 143816  32008 173740    1    1    10    45    7    1  4  2 91  2  0
 0  0 250864 143808  32008 173744    0    0     4     8  323  649  1  1 99  0  0
 0  0 250864 143808  32008 173744    0    0     0     0  283  628  1  1 99  0  0
 0  0 250864 143808  32008 173744    0    0     0     4  302  696  1  1 99  0  0
 0  0 250864 143808  32016 173740    0    0     0    28  307  706  1  0 99  0  0
 0  0 250864 143808  32016 173744    0    0     0     0  269  601  1  1 99  0  0
 0  0 250864 143808  32016 173744    0    0     0     0  261  601  0  1 99  0  0
 0  0 250864 143808  32016 173744    0    0     0     0  285  630  1  1 99  0  0
 0  0 250864 143816  32024 173744    0    0     0   500  388  785  1  1 89 10  0
 0  0 250864 143816  32024 173744    0    0     0     0  254  580  1  1 99  0  0
[root@kouno koizumi_ta]#

リアルタイムで調査する場合はこれでいいけど、後追い調査には全く向かない。

そこで、awk使用して時刻を付与する。

vmstat <実行間隔> <実行回数> |awk '{print strftime("%Y-%m-%d %H:%M:%S"), $0}' > vmstat.log

するとこんな感じに。

  • awkで時刻を付与した場合
[root@kouno koizumi_ta]# vmstat 1 10 |awk '{print strftime("%Y-%m-%d %H:%M:%S"), $0}' > vmstat.log
[root@kouno koizumi_ta]# cat vmstat.log
2014-02-20 20:50:13 procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu-----
2014-02-20 20:50:13  r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
2014-02-20 20:50:13  2  0 253676 144564  33124 174396    1    1    10    45    7    1  4  2 91  2  0
2014-02-20 20:50:14  0  0 253676 144520  33124 174396    0    0     0     0  329  702  1  1 98  0  0
2014-02-20 20:50:15  0  0 253676 144520  33124 174396    0    0     0     0  279  634  1  0 99  0  0
2014-02-20 20:50:16  0  0 253676 144520  33124 174396    0    0     0     4  328  729  1  1 99  0  0
2014-02-20 20:50:17  0  0 253676 144520  33124 174396    0    0     0     0  280  643  1  1 99  0  0
2014-02-20 20:50:18  0  0 253676 144520  33132 174388    0    0     0   464  362  707  1  1 74 25  0
2014-02-20 20:50:19  0  0 253676 144492  33132 174396    0    0     0     0  358  701  1  1 98  0  0
2014-02-20 20:50:20  0  0 253676 144468  33132 174396    0    0     0     0  295  624  1  1 99  0  0
2014-02-20 20:50:21  0  0 253676 144468  33132 174396    0    0     4     8  313  652  1  1 99  0  0
2014-02-20 20:50:22  0  0 253676 144468  33132 174400    0    0     0     0  299  644  1  0 99  0  0
[root@kouno koizumi_ta]# 

これをcronで仕込んでおくと、後追い調査でも負荷状況が詳細に確認できる。

0 0 * * * /usr/bin/vmstat 1 86340 |awk '{print strftime("%Y-%m-%d %H:%M:%S"), $0}' >> vmstat.log_$(date '+%Y%m%d')

こんな感じで。

3
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4