1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Linux sarコマンドでシステムの統計情報を取得する

Last updated at Posted at 2021-10-18

Linux sarコマンド を試してみる でコマンドの動作を確認してみたので今回はシステムの統計情報を取得してみます。

1. 環境

さくらのクラウド(IaaS) 2CPU 2GB

$ cat /etc/redhat-release
CentOS Stream release 8

2. 情報のありか

前回設定したときからの統計情報が /var/log/sa に格納されています。
saxx
sarxx
xxは日付です。

$ cd /var/log/sa
$ ls
sa07  sa09  sa11  sa13  sa15  sa17  sa19   sar09  sar11  sar13  sar15  sar17
sa08  sa10  sa12  sa14  sa16  sa18  sar08  sar10  sar12  sar14  sar16  sar18

デフォルトの設定では saxx は23:50で終了し、sarxx は翌日00:07に作成されています。

$ ls -l
合計 5568
-rw-r--r-- 1 root root   3804 10月  7 23:50 sa07
-rw-r--r-- 1 root root 150084 10月  8 23:50 sa08
-rw-r--r-- 1 root root 199736 10月  9 23:50 sa09
-rw-r--r-- 1 root root 199736 10月 10 23:50 sa10
-rw-r--r-- 1 root root 199736 10月 11 23:50 sa11
-rw-r--r-- 1 root root 199736 10月 12 23:50 sa12
-rw-r--r-- 1 root root 199736 10月 13 23:50 sa13
-rw-r--r-- 1 root root 199736 10月 14 23:50 sa14
-rw-r--r-- 1 root root 199736 10月 15 23:50 sa15
-rw-r--r-- 1 root root 199736 10月 16 23:50 sa16
-rw-r--r-- 1 root root 199736 10月 17 23:50 sa17
-rw-r--r-- 1 root root 199736 10月 18 23:50 sa18
-rw-r--r-- 1 root root  52076 10月 19 06:00 sa19
-rw-r--r-- 1 root root 238570 10月  9 00:07 sar08
-rw-r--r-- 1 root root 317710 10月 10 00:07 sar09
-rw-r--r-- 1 root root 317710 10月 11 00:07 sar10
-rw-r--r-- 1 root root 317710 10月 12 00:07 sar11
-rw-r--r-- 1 root root 317710 10月 13 00:07 sar12
-rw-r--r-- 1 root root 317710 10月 14 00:07 sar13
-rw-r--r-- 1 root root 317710 10月 15 00:07 sar14
-rw-r--r-- 1 root root 317710 10月 16 00:07 sar15
-rw-r--r-- 1 root root 317710 10月 17 00:07 sar16
-rw-r--r-- 1 root root 317710 10月 18 00:07 sar17
-rw-r--r-- 1 root root 317710 10月 19 00:07 sar18

3.sarコマンドのオプションを使って取得する

man によると
-f オプションでファイルを設定
-s オプションで開始時刻を設定
-e オプションで終了時刻を設定
となっています。

$ man sar
SAR(1)                                     Linux User's Manual                                     SAR(1)

NAME
       sar - Collect, report, or save system activity information.

SYNOPSIS
       sar  [ -A ] [ -B ] [ -b ] [ -C ] [ -D ] [ -d ] [ -F [ MOUNT ] ] [ -H ] [ -h ] [ -p ] [ -q ] [ -r [
       ALL ] ] [ -S ] [ -t ] [ -u [ ALL ] ] [ -V ] [ -v ] [ -W ] [ -w ] [ -y ]  [  -z  ]  [  --help  ]  [
       --human  ]  [  --sadc  ]  [ -I { int_list | SUM | ALL } ] [ -P { cpu_list | ALL } ] [ -m { keyword
       [,...] | ALL } ] [ -n { keyword [,...] | ALL } ] [ -j { ID | LABEL | PATH | UUID | ... } ] [ -f  [
       filename ] | -o [ filename ] | -[0-9]+ ] [ -i interval ] [ -s [ hh:mm[:ss] ] ] [ -e [ hh:mm[:ss] ]
       ] [ interval [ count ] ]

DESCRIPTION
(中略)
       -e [ hh:mm[:ss] ]
              Set the ending time of the report. The default ending time is 18:00:00. Hours must be given
              in 24-hour format.  This option can be used when data are read from or written  to  a  file
              (options -f or -o).
(中略)
       -f [ filename ]
              Extract  records  from filename (created by the -o filename flag). The default value of the
              filename parameter is the current standard system activity daily data file.  If filename is
              a  directory instead of a plain file then it is considered as the directory where the stan‐
              dard system activity daily data files are located. The -f option is  exclusive  of  the  -o
              option.
(中略)
       -s [ hh:mm[:ss] ]
              Set  the  starting time of the data, causing the sar command to extract records time-tagged
              at, or following, the time specified. The default starting time is 08:00:00.  Hours must be
              given  in  24-hour  format.  This  option  can  be used only when data are read from a file
              (option -f).
(後略)

では試しに 18日の14時から15時までの情報を取得してみます。

$ sar -f /var/log/sa/sa18 -s 14:00:00 -e 15:00:00
Linux 4.18.0-257.el8.x86_64 (localhost.localdomain)     2021年10月18日  _x86_64_        (2 CPU)

14時00分03秒     CPU     %user     %nice   %system   %iowait    %steal     %idle
14時10分03秒     all      0.07      0.00      0.15      0.01      0.20     99.57
14時20分03秒     all      0.07      0.17      0.19      0.02      0.15     99.40
14時30分03秒     all      0.06      0.00      0.14      0.01      0.15     99.64
14時40分03秒     all      0.07      0.00      0.16      0.01      0.18     99.56
14時50分03秒     all      0.07      0.00      0.15      0.01      0.15     99.62
平均値:      all      0.07      0.03      0.16      0.01      0.17     99.56

4.当日の情報ならば grep と awk でも

当日の情報ならばsarコマンドのオプションを思い出せなくてもざっくりとは grep と awk でも取得することができます。
午前4時台の情報を取得したい場合は

$ sar | grep 04時
04時00分03秒     all      0.07      0.00      0.15      0.01      0.07     99.72
04時10分03秒     all      0.07      0.00      0.15      0.01      0.09     99.69
04時20分03秒     all      0.07      0.00      0.15      0.01      0.06     99.71
04時30分03秒     all      0.08      0.04      0.17      0.01      0.07     99.64
04時40分03秒     all      0.08      0.00      0.15      0.01      0.09     99.68
04時50分03秒     all      0.07      0.00      0.15      0.01      0.06     99.72

午前4時20分台~40分台の情報を取得したい場合は

$ sar | grep 04時[2-4]
04時20分03秒     all      0.07      0.00      0.15      0.01      0.06     99.71
04時30分03秒     all      0.08      0.04      0.17      0.01      0.07     99.64
04時40分03秒     all      0.08      0.00      0.15      0.01      0.09     99.6

午前4時台のrunq-sz情報を取得したい場合は

$ sar -q | grep 04時 | awk '{print $1, $2}'
04時00分03秒 1
04時10分03秒 1
04時20分03秒 0
04時30分03秒 2
04時40分03秒 1
04時50分03秒 1
1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?