LoginSignup
28
16

More than 1 year has passed since last update.

CPU使用率を取得する方法(Linux、vmstatコマンド)

Last updated at Posted at 2021-09-22

LinuxのvmstatコマンドでCPU使用率を取得する方法を紹介します。

環境

  • OS:CentOS Linux release 8.1.1911
実行結果
[root@centos8 ~]# cat /etc/redhat-release
CentOS Linux release 8.1.1911 (Core)
[root@centos8 ~]#

1. vmstatコマンド実行

vmstatコマンドを実行すると以下のように結果が表示されます。

実行結果
[root@centos8 ~]# vmstat
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 2  0      0 481508   2216 688484    0    0   361    65  229  402  2  2 93  4  0
[root@centos8 ~]#

出力結果の意味は以下となります。

項目1 項目2 値の意味
procs r ランタイム待ちのプロセス数
b 割り込み不可能なスリープ状態にあるプロセス数
memory swpd 使用中の仮想メモリの量(KB)
free 空きメモリの量(KB)
buff バッファとして使用しているメモリの量(KB)
cache キャッシュに使用しているメモリの量(KB)
swap si ディスクからスワップインしているメモリの量(KB/s)
so ディスクにスワップアウトしているメモリの量(KB/s)
io bi ブロックデバイスから受け取ったブロック数(blocks/s)
bo ブロックデバイスに送られたブロック数(blocks/s)
system in クロック割り込みも含む、1秒あたりの割り込み回数
cs 1秒あたりのコンテキストスイッチの回数
cpu us カーネルコード以外の実行に使用した時間(ユーザー時間、nice 時間を含む)(%)
sy カーネルコードの実行に使用した時間(システム時間)(%)
id アイドル時間(%)
wa IO 待ち時間(%)
st 仮想マシンから盗まれた時間(%)

2. CPU使用率

vmstatコマンドの結果のcpuの項目(us、sy、id、wa、st)を足すと100になります。

従って、CPU使用率は

CPU使用率(%)=100 - idの値

となります。

3. vmstatコマンドの実行結果をファイルに出力

1秒間隔でvmstatコマンドの実行結果を表示させる場合、以下のコマンドを実行します。

vmstat 1

実行結果
[root@centos8 ~]# 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
 2  0      0 479484   2216 689364    0    0    83    16  181  349  1  1 98  1  0
 0  0      0 479424   2216 689364    0    0     0     0  165  338  0  0 100  0  0
 0  0      0 479424   2216 689364    0    0     0     0  163  335  0  1 99  0  0
 0  0      0 479424   2216 689364    0    0     0     0  162  333  0  1 99  0  0
 0  0      0 479424   2216 689364    0    0     0     0  183  339  1  1 98  0  0
 0  0      0 479424   2216 689364    0    0     0     0  158  333  0  0 100  0  0
 0  0      0 479424   2216 689364    0    0     0     0  174  342  0  1 99  0  0
 0  0      0 479424   2216 689364    0    0     0     0  156  320  0  2 98  0  0
 0  0      0 479424   2216 689364    0    0     0     0  174  341  0  0 100  0  0

vmstatコマンドの実行結果に「時刻」を付けて表示させる場合、以下のコマンドを実行します。

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

実行結果
[root@centos8 ~]# vmstat 1 | awk '{ print strftime("%Y/%m/%d %H:%M:%S"), $0 }'
2021/09/23 02:01:32 procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
2021/09/23 02:01:32  r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
2021/09/23 02:01:32  3  0      0 478964   2216 689424    0    0    67    13  177  346  1  1 98  1  0
2021/09/23 02:01:33  0  0      0 478844   2216 689424    0    0     0     0  187  372  0  1 99  0  0
2021/09/23 02:01:34  0  0      0 478844   2216 689424    0    0     0     0  191  370  0  0 100  0  0
2021/09/23 02:01:35  0  0      0 478844   2216 689424    0    0     0     0  196  364  1  1 98  0  0
2021/09/23 02:01:36  1  0      0 478844   2216 689424    0    0     0     0  179  369  0  1 99  0  0
2021/09/23 02:01:37  0  0      0 478844   2216 689424    0    0     0     0  185  373  0  3 97  0  0
2021/09/23 02:01:38  0  0      0 478844   2216 689424    0    0     0     0  187  372  0  0 100  0  0
2021/09/23 02:01:39  0  0      0 478844   2216 689424    0    0     0     0  187  370  0  1 99  0  0
2021/09/23 02:01:40  0  0      0 478844   2216 689424    0    0     0     0  187  375  0  1 99  0  0
2021/09/23 02:01:41  0  0      0 478844   2216 689424    0    0     0    17  196  377  0  1 99  0  0

ファイル(ここでは「vmstat.log」)に出力する場合、以下のコマンドを実行します。
パイプをリフレッシュするためにsystem(":")を使用します。

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

実行結果
[root@centos8 ~]# vmstat 1 | awk '{ print strftime("%Y/%m/%d %H:%M:%S"), $0 } { system(":") }' > vmstat.log
vmstat.log
[root@centos8 ~]# cat vmstat.log
2021/09/23 02:06:53 procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
2021/09/23 02:06:53  r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
2021/09/23 02:06:53  2  0      0 478796   2216 689456    0    0    65    12  177  346  1  1 98  1  0
2021/09/23 02:06:54  1  0      0 478436   2216 689456    0    0     0     0  197  355  1  2 97  0  0
2021/09/23 02:06:55  0  0      0 478436   2216 689456    0    0     0     0  164  329  0  1 99  0  0
2021/09/23 02:06:56  1  0      0 478436   2216 689456    0    0     0     4  182  346  1  1 98  0  0
2021/09/23 02:06:57  0  0      0 478436   2216 689456    0    0     0     0  165  338  0  1 99  0  0
2021/09/23 02:06:58  0  0      0 478436   2216 689456    0    0     0     0  169  327  0  2 98  0  0
[root@centos8 ~]#

長時間、実行結果を取得する場合、バックグラウンドで動かして、ログアウトしても終了しないようにしたいこともあります。
その場合、nohupを使用して以下のコマンドを実行します。

nohup bash -c "vmstat 1 | awk '{print strftime(\"%y/%m/%d %H:%M:%S\"), \$0} { system(\":\") }'" > vmstat.log 2>&1 &

[root@centos8 ~]# nohup bash -c "vmstat 1 | awk '{print strftime(\"%y/%m/%d %H:%M:%S\"), \$0} { system(\":\") }'" > vmstat.log 2>&1 &
[1] 3872
[root@centos8 ~]#

出力ファイル名に時刻を持たせたい場合、以下のコマンドを実行します。

nohup bash -c "vmstat 1 | awk '{print strftime(\"%y/%m/%d %H:%M:%S\"), \$0} { system(\":\") }'" > vmstat_$(date "+%Y%m%d_%H%M%S").log 2>&1 &

[root@centos8 log]# nohup bash -c "vmstat 1 | awk '{print strftime(\"%y/%m/%d %H:%M:%S\"), \$0} { system(\":\") }'" > vmstat_$(date "+%Y%m%d_%H%M%S").log 2>&1 &
[1] 3931
[root@centos8 log]# ls
vmstat_20210923_021542.log
[root@centos8 log]# cat vmstat_20210923_021542.log
nohup: 入力を無視します
21/09/23 02:15:42 procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
21/09/23 02:15:42  r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
21/09/23 02:15:42  2  0      0 478404   2216 689688    0    0    61    12  177  346  1  1 98  1  0
21/09/23 02:15:43  0  0      0 477684   2216 689692    0    0     0     0  221  381  1  1 98  0  0
21/09/23 02:15:44  1  0      0 477684   2216 689692    0    0     0     0  208  402  1  2 97  0  0
21/09/23 02:15:45  0  0      0 477684   2216 689692    0    0     0     0  189  370  0  1 99  0  0
21/09/23 02:15:46  0  0      0 477684   2216 689692    0    0     0     0  199  381  0  1 99  0  0
21/09/23 02:15:47  0  0      0 477684   2216 689692    0    0     0     5  208  395  0  1 99  0  0
21/09/23 02:15:48  1  0      0 477684   2216 689692    0    0     0     0  214  401  1  3 96  0  0
21/09/23 02:15:49  0  0      0 477684   2216 689692    0    0     0     0  219  390  1  1 98  0  0
21/09/23 02:15:50  0  0      0 477684   2216 689692    0    0     0     0  225  417  0  2 98  0  0
21/09/23 02:15:51  0  0      0 477684   2216 689692    0    0     0     0  210  390  1  1 98  0  0
[root@centos8 log]#

vmstatの結果を「時刻」を付けてファイルに出力させると、Excelでグラフを作ったりできて便利です。

参考

【 vmstat 】コマンド――仮想メモリやディスクI/Oの統計情報を表示する:Linux基本コマンドTips(126) - @IT

vmstatコマンドについて調べてみた


以上

28
16
1

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
28
16