hana_shinのLinux技術ブログ

Linuxの技術情報を掲載しています。特にネットワークをメインに掲載していきます。

sadfコマンドの使い方



1 sadfコマンドとは?

sarコマンドで収集した統計情報をCSV,XML等のフォーマットに変換するコマンドです。sarコマンドの使い方は、sarコマンドの使い方 - hana_shinのLinux技術ブログを参照してください。

2 検証環境

CentOS版数は以下のとおりです。

[root@server ~]# cat /etc/redhat-release
CentOS Linux release 7.9.2009 (Core)

カーネル版数は以下のとおりです。

[root@server ~]# uname -r
3.10.0-1160.el7.x86_64

CPUは4つ搭載しています。

[root@server ~]# cat /proc/cpuinfo |grep processor
processor       : 0
processor       : 1
processor       : 2
processor       : 3

3 インストール方法

sysstatパッケージをインストールします。

[root@server ~]# yum -y install sysstat

sadfコマンドの版数を確認します。

[root@server ~]# sadf -V
sysstat バージョン 10.1.5
(C) Sebastien Godard (sysstat <at> orange.fr)

4 オプション一覧

manより抜粋したオプション一覧を以下に示します。

OPTIONS
       -C     Tell sadf to display comments present in file.

       -d     Print  the  contents  of the data file in a format that can easily be ingested by a relational database system. The output consists of
              fields separated by a semicolon. Each record contains the hostname of the host where the file was created, the interval value  (or  -1
              if  not  applicable),  the  timestamp in a form easily acceptable by most databases, and additional semicolon separated data fields as
              specified by sar_options command line options.  Note that timestamp output can be controlled by options -T, -t and -U.

       -e [ hh:mm:ss ]
              Set the ending time of the report, given in local time. The default ending time is 18:00:00. Hours must be given in 24-hour format.

       -H     Display only the header of the report (when applicable). If no format has been specified, then the header of the  data  file  is  dis‐
              played.

       -h     When used in conjunction with option -d, all activities will be displayed horizontally on a single line.

       -j     Print the contents of the data file in JSON (JavaScript Object Notation) format. Timestamps can be controlled by options -T and -t.

       -P { cpu [,...] | ALL }
              Tell  sadf  that  processor dependent statistics are to be reported only for the specified processor or processors. Specifying the ALL
              keyword reports statistics for each individual processor, and globally for all processors. Note that processor 0 is the first  proces‐
              sor.

       -p     Print  the  contents of the data file in a format that can easily be handled by pattern processing commands like awk.  The output con‐
              sists of fields separated by a tab. Each record contains the hostname of the host where the file was created, the interval  value  (or
              -1  if  not  applicable),  the timestamp, the device name (or - if not applicable), the field name and its value.  Note that timestamp
              output can be controlled by options -T, -t and -U.

       -s [ hh:mm:ss ]
              Set the starting time of the data (given in local time), causing the sadf 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.

       -T     Display timestamp in local time instead of UTC (Coordinated Universal Time).

       -t     Display timestamp in the original local time of the data file creator instead of UTC (Coordinated Universal Time).

       -U     Display timestamp (UTC - Coordinated Universal Time) in seconds from the epoch.

       -V     Print version number then exit.

       -x     Print  the  contents of the data file in XML format.  Timestamps can be controlled by options -T and -t.  The corresponding DTD (Docu‐
              ment Type Definition) and XML Schema are included in the sysstat  source  package.  They  are  also  available  at  http://pagesperso-
              orange.fr/sebastien.godard/download.html

5 事前準備

sarコマンドを実行して、テスト用のファイルを作成します。適当なタイミングでCtrl + cを押下してsarコマンドを終了します。

[root@server ~]# sar -o sar.data 2
Linux 3.10.0-1160.el7.x86_64 (server)   2022年04月20日  _x86_64_        (4 CPU)

21時15分26秒     CPU     %user     %nice   %system   %iowait    %steal     %idle
21時15分28秒     all      0.00      0.00      0.13      0.00      0.00     99.87
21時15分30秒     all      0.00      0.00      0.12      0.00      0.00     99.88
-snip-

採取したファイルを確認します。

[root@server ~]# ls -l sar.data
-rw-r--r--. 1 root root 1553200  4月 20 21:16 sar.data

6 時刻の表示方法

6.1 時刻をUTCで表示する方法(オプションなし)

[root@server ~]# sadf sar.data
server  2       2022-04-19 12:39:36 UTC all     %user   0.00
server  2       2022-04-19 12:39:36 UTC all     %nice   0.00
server  2       2022-04-19 12:39:36 UTC all     %system 0.13
-snip-

6.2 時刻をローカル時刻で表示する方法(-T)

[root@server ~]# sadf -T sar.data
server  2       2022-04-19 21:39:36     all     %user   0.00
server  2       2022-04-19 21:39:36     all     %nice   0.00
server  2       2022-04-19 21:39:36     all     %system 0.13
-snip-

7 時刻で絞り込む方法(-s,-e)

ローカル時刻の21:39:36から21:39:38までの情報を出力してみます。-sで開始時刻、-eで終了時刻を指定します。

[root@server ~]# sadf -T -s 21:39:00 -e 21:39:38 sar.data
server  2       2022-04-19 21:39:36     all     %user   0.00
server  2       2022-04-19 21:39:36     all     %nice   0.00
server  2       2022-04-19 21:39:36     all     %system 0.13
server  2       2022-04-19 21:39:36     all     %iowait 0.00
server  2       2022-04-19 21:39:36     all     %steal  0.00
server  2       2022-04-19 21:39:36     all     %idle   99.87
server  2       2022-04-19 21:39:38     all     %user   0.00
server  2       2022-04-19 21:39:38     all     %nice   0.00
server  2       2022-04-19 21:39:38     all     %system 0.12
server  2       2022-04-19 21:39:38     all     %iowait 0.00
server  2       2022-04-19 21:39:38     all     %steal  0.00
server  2       2022-04-19 21:39:38     all     %idle   99.88
[root@server ~]#

8 CPUの統計情報を表示する方法

8.1 全CPUの統計情報を表示する方法(-P ALL)

[root@server ~]# sadf -P ALL sar.data
server	2	2022-04-19 12:39:36 UTC	all	%user	0.00
server	2	2022-04-19 12:39:36 UTC	all	%nice	0.00
server	2	2022-04-19 12:39:36 UTC	all	%system	0.13
-snip-

8.2 特定CPUの統計情報を表示する方法(-P 0,1..)

CPU0の統計情報を表示してみます。

[root@server ~]# sadf -P 0 sar.data
server  2       2022-04-19 12:39:36 UTC cpu0    %user   0.00
server  2       2022-04-19 12:39:36 UTC cpu0    %nice   0.00
server  2       2022-04-19 12:39:36 UTC cpu0    %system 0.00
-snip-

次に、CPU1,CPU3の統計情報を表示してみます。

[root@server ~]# sadf -P 1,3 sar.data
server  2       2022-04-19 12:39:36 UTC cpu1    %user   0.00
server  2       2022-04-19 12:39:36 UTC cpu1    %nice   0.00
server  2       2022-04-19 12:39:36 UTC cpu1    %system 0.50
server  2       2022-04-19 12:39:36 UTC cpu1    %iowait 0.00
server  2       2022-04-19 12:39:36 UTC cpu1    %steal  0.00
server  2       2022-04-19 12:39:36 UTC cpu1    %idle   99.50
server  2       2022-04-19 12:39:36 UTC cpu3    %user   0.00
server  2       2022-04-19 12:39:36 UTC cpu3    %nice   0.00
server  2       2022-04-19 12:39:36 UTC cpu3    %system 0.00
server  2       2022-04-19 12:39:36 UTC cpu3    %iowait 0.00
server  2       2022-04-19 12:39:36 UTC cpu3    %steal  0.00
server  2       2022-04-19 12:39:36 UTC cpu3    %idle   100.00
-snip-

9 出力ファイルのフォーマットを指定する方法

9.1 JSONフォーマットで出力する方法(-j)

[root@server ~]# sadf -T -j sar.data
{"sysstat": {
        "sysdata-version": 2.16,
        "hosts": [
                {
                        "nodename": "server",
                        "sysname": "Linux",
                        "release": "3.10.0-1160.el7.x86_64",
                        "machine": "x86_64",
                        "number-of-cpus": 4,
                        "file-date": "2022-04-19",
                        "statistics": [
                                {
                                        "timestamp": {"date": "2022-04-19", "time": "21:39:36", "utc": 0, "interval": 2},
                                        "cpu-load": [
                                                {"cpu": "all", "user": 0.00, "nice": 0.00, "system": 0.13, "iowait": 0.00, "steal": 0.00, "idle": 99.87}
                                        ]
                                },
-snip-

9.2 XMLフォーマットで出力する方法(-x)

[root@server ~]# sadf -T -x sar.data
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sysstat PUBLIC "DTD v2.16 sysstat //EN"
"http://pagesperso-orange.fr/sebastien.godard/sysstat-2.16.dtd">
<sysstat>
        <sysdata-version>2.16</sysdata-version>
        <host nodename="server">
                <sysname>Linux</sysname>
                <release>3.10.0-1160.el7.x86_64</release>
                <machine>x86_64</machine>
                <number-of-cpus>4</number-of-cpus>
                <file-date>2022-04-19</file-date>
                <statistics>
                        <timestamp date="2022-04-19" time="21:39:36" utc="0" interval="2">
                                <cpu-load>
                                        <cpu number="all" user="0.00" nice="0.00" system="0.13" iowait="0.00" steal="0.00" idle="99.87"/>
                                </cpu-load>
                        </timestamp>
-snip-

9.3 csv形式(セミコロンで区切る)で出力する方法(-d)

まず全CPUの統計情報を表示してみます。

[root@server ~]# sadf -T -d sar.data
# hostname;interval;timestamp;CPU;%user;%nice;%system;%iowait;%steal;%idle
server;2;2022-04-19 21:39:36;-1;0.00;0.00;0.13;0.00;0.00;99.87
server;2;2022-04-19 21:39:38;-1;0.00;0.00;0.12;0.00;0.00;99.88
server;2;2022-04-19 21:39:40;-1;0.12;0.00;0.12;0.00;0.00;99.75
-snip-

次に、CPU3の統計情報を表示してみます。

[root@server ~]# sadf -T -P 3 -d sar.data
# hostname;interval;timestamp;CPU;%user;%nice;%system;%iowait;%steal;%idle
server;2;2022-04-19 21:39:36;3;0.00;0.00;0.00;0.00;0.00;100.00
server;2;2022-04-19 21:39:38;3;0.00;0.00;0.00;0.00;0.00;100.00
server;2;2022-04-19 21:39:40;3;0.00;0.00;0.00;0.00;0.00;100.00
-snip-

10 sarコマンドのオプションを使う方法(-- sarのオプション)

sarコマンドのオプションを使うには、--に続けて、sarコマンドのオプションを指定します。このオプションを使うことで、CPU以外の統計情報を表示することができるようになります。

10.1 メモリ使用量の統計情報を表示する方法(-- -r)

[root@server ~]# sadf -T -- -r sar.data
server  2       2022-04-19 21:39:36     -       kbmemfree       3168184
server  2       2022-04-19 21:39:36     -       kbmemused       693108
server  2       2022-04-19 21:39:36     -       %memused        17.95
-snip-

10.2 メモリページの統計情報を表示する方法(-- -R)

[root@server ~]# sadf -T -- -R sar.data
server  2       2022-04-19 21:39:36     -       frmpg/s -34.00
server  2       2022-04-19 21:39:36     -       bufpg/s 0.00
server  2       2022-04-19 21:39:36     -       campg/s 3.00
-snip-

10.3 ブロックデバイスの統計情報を表示する方法(-- -d)

[root@server ~]# sadf -T -- -d sar.data
server	2	2022-04-19 21:39:36	dev8-0	tps	0.00
server	2	2022-04-19 21:39:36	dev8-0	rd_sec/s	0.00
server	2	2022-04-19 21:39:36	dev8-0	wr_sec/s	0.00
server	2	2022-04-19 21:39:36	dev8-0	avgrq-sz	0.00
server	2	2022-04-19 21:39:36	dev8-0	avgqu-sz	0.00
-snip-

10.4 パケット送受信の統計情報を表示する方法(-- -n DEV)

[root@server ~]# sadf -T -- -n DEV sar.data
server	2	2022-04-19 21:39:36	lo	rxpck/s	0.00
server	2	2022-04-19 21:39:36	lo	txpck/s	0.00
server	2	2022-04-19 21:39:36	lo	rxkB/s	0.00
-snip-

10.5 TCPの統計情報を表示する方法(-- -n TCP)

[root@server ~]# sadf -T -- -n TCP sar.data
server	2	2022-04-19 21:39:36	-	active/s	0.00
server	2	2022-04-19 21:39:36	-	passive/s	0.00
server	2	2022-04-19 21:39:36	-	iseg/s	0.00
-snip-

10.6 IPの統計情報を表示する方法(-- -n IP)

[root@server ~]# sadf -T -- -n IP sar.data
server	2	2022-04-19 21:39:36	-	irec/s	0.00
server	2	2022-04-19 21:39:36	-	fwddgm/s	0.00
server	2	2022-04-19 21:39:36	-	idel/s	0.00
-snip-

11 統計情報を視覚化する方法

sadfコマンドの-gオプションを使うと、sarコマンドで採取した統計情報をSVG(Scalable Vector Graphics)形式に変換することができます。SVG形式に変換することで統計情報を視覚化することができます。なお、CentOS7.9のsadfコマンドには-gオプションがないため、CentOS8.3のsadfコマンドを使ってみました。

[root@server ~]# sadf -V
sysstat バージョン 11.7.3
(C) Sebastien Godard (sysstat <at> orange.fr)

11.1 事前準備

以下の条件でstressコマンドを実行します。
・CPU0:ユーザモードのCPU使用率(%user)が100%になるような負荷を与える
・CPU1:メモリの獲得、解放を繰り返す負荷を与える

sarコマンドを実行して、統計情報を採取します。

[root@server ~]# sar -o sar.data 1

stressコマンドを実行します。なお、tasksetコマンドの使い方は、tasksetコマンドの使い方 - hana_shinのLinux技術ブログを参照してください。

[root@server ~]# taskset -c 0 stress -c 1 -q &
[1] 37519

プロセスの状態を確認します。CPU0のCPU使用率が101%であることがわかります。

[root@server ~]# ps -C stress -o comm,pid,ppid,psr,%cpu,args
COMMAND             PID    PPID PSR %CPU COMMAND
stress            37519    2079   0  0.0 stress -c 1 -q
stress            37520   37519   0  101 stress -c 1 -q

stressコマンドを実行します。

[root@server ~]# taskset -c 1 stress -m 1 -q &
[2] 37522

プロセスの状態を確認します。CPU1でメモリの獲得、解放の負荷を与えるプロセスが実行されていることがわかります。

[root@server ~]# ps -C stress -o comm,pid,ppid,psr,%cpu,args
COMMAND             PID    PPID PSR %CPU COMMAND
stress            37519    2079   0  0.0 stress -c 1 -q
stress            37520   37519   0 97.2 stress -c 1 -q
stress            37522    2079   1  0.0 stress -m 1 -q
stress            37523   37522   1  100 stress -m 1 -q

適当な時間が経過したら、stressコマンドを終了します。

[root@server ~]# pkill stress
[1]-  Terminated              taskset -c 0 stress -c 1 -q
[2]+  Terminated              taskset -c 1 stress -m 1 -q

11.2 実行結果の確認

Ctrl+Cを押下してsarコマンドを終了します。そして、採取した統計情報をSVG形式に変換します。そして、SVG形式に変換したファイルをWebブラウザにドロップします。

[root@server ~]# sadf -g -- -P ALL sar.data > cpu.svg

CPU0のユーザモードでのCPU使用率(%user)が100%程度になっていることがわかります。そして、搭載CPUが4個のため、システム全体のCPU使用率は25%程度であることがわかります。

次にCPU1の状態を確認します。CPU1ではメモリの獲得、解放を繰り返す負荷を与えているので、ユーザモードのCPU使用率(%user)、カーネルモードのCPU使用率(%system)が増加していることがわかります。

Z 参考情報

私が業務や記事執筆で参考にした書籍を以下のページに記載します。
Linux技術のスキルアップをしよう! - hana_shinのLinux技術ブログ