Linuxシステム情報を収集&閲覧するsysstatを使ってみる

sysstatを使ってLinuxのシステム情報を収集してみようかと思います。

構成

sysstatをインストールするLinuxの構成です。

bookstore@bookstoreUbuntu:~$ cat /etc/os-release
NAME="Ubuntu"
VERSION="20.04.1 LTS (Focal Fossa)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 20.04.1 LTS"
VERSION_ID="20.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=focal
UBUNTU_CODENAME=focal

インストール

aptで検索してみた結果がこちら。

bookstore@bookstoreUbuntu:~$ sudo apt search sysstat
ソート中... 完了
全文検索... 完了
isag/focal,focal 12.2.0-2 all
  Interactive System Activity Grapher for sysstat

libsysstat-qt5-0/focal 0.4.2-0ubuntu2 amd64
  Qt-based interface to system statistics

libsysstat-qt5-0-dev/focal 0.4.2-0ubuntu2 amd64
  Qt-based interface to system statistics (dev)

pgcluu/focal,focal 3.1-1 all
  PostgreSQL performance monitoring and auditing tool

recap/focal,focal 2.0.2-1 all
  Generates reports of various information about the server

sysstat/focal 12.2.0-2 amd64
  system performance tools for Linux

最後のやつですね。 apt info で見てみたらホームページ等が記載されてます。

bookstore@bookstoreUbuntu:~$ apt info sysstat
Package: sysstat
Version: 12.2.0-2
Priority: optional
Section: admin
Origin: Ubuntu
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Original-Maintainer: Robert Luberda <robert@debian.org>
Bugs: https://bugs.launchpad.net/ubuntu/+filebug
Installed-Size: 1,524 kB
Depends: lsb-base (>= 3.0-6), ucf (>= 2.003), xz-utils, debconf (>= 0.5) | debconf-2.0, libc6 (>= 2.14), libsensors5 (>= 1:3.5.0)
Recommends: cron | cron-daemon
Suggests: isag
Homepage: http://pagesperso-orange.fr/sebastien.godard/
Task: postgresql-server
Download-Size: 453 kB
APT-Sources: http://jp.archive.ubuntu.com/ubuntu focal/main amd64 Packages
Description: system performance tools for Linux
 The sysstat package contains the following system performance tools:
  - sar: collects and reports system activity information;
  - iostat: reports CPU utilization and disk I/O statistics;
  - tapestat: reports statistics for tapes connected to the system;
  - mpstat: reports global and per-processor statistics;
  - pidstat: reports statistics for Linux tasks (processes);
  - sadf: displays data collected by sar in various formats;
  - cifsiostat: reports I/O statistics for CIFS filesystems.
 .
 The statistics reported by sar deal with I/O transfer rates,
 paging activity, process-related activities, interrupts,
 network activity, memory and swap space utilization, CPU
 utilization, kernel activities and TTY statistics, among
 others. Both UP and SMP machines are fully supported.

ソースはGitHubに上がってました。

https://github.com/sysstat/sysstat

とりあえずインストールします。

bookstore@bookstoreUbuntu:~$ sudo apt install sysstat

インストールしたら、cronの設定ファイルが生成されました。

bookstore@bookstoreUbuntu:~$ cat /etc/cron.d/sysstat
# The first element of the path is a directory where the debian-sa1
# script is located
PATH=/usr/lib/sysstat:/usr/sbin:/usr/sbin:/usr/bin:/sbin:/bin

# Activity reports every 10 minutes everyday
5-55/10 * * * * root command -v debian-sa1 > /dev/null && debian-sa1 1 1

# Additional run at 23:59 to rotate the statistics file
59 23 * * * root command -v debian-sa1 > /dev/null && debian-sa1 60 2

初期設定では10分毎に統計情報の収集と、23:59にログファイルのローテーションをしているみたいです。

インストールだけではどうやら有効になっていないようなので下記ファイルを書き換えます。

bookstore@bookstoreUbuntu:~$ sudo vi /etc/default/sysstat

ENABLED を true にします。

# Default settings for /etc/init.d/sysstat, /etc/cron.d/sysstat
# and /etc/cron.daily/sysstat files
#

# Should sadc collect system activity informations? Valid values
# are "true" and "false". Please do not put other values, they
# will be overwritten by debconf!
ENABLED="true"

sysstatサービスをリスタート。

bookstore@bookstoreUbuntu:~$ sudo systemctl restart sysstat
bookstore@bookstoreUbuntu:~$ systemctl status sysstat
● sysstat.service - Resets System Activity Data Collector
     Loaded: loaded (/lib/systemd/system/sysstat.service; enabled; vendor preset: enabled)
     Active: active (exited) since Tue 2020-09-08 21:53:01 JST; 4s ago
       Docs: man:sa1(8)
             man:sadc(8)
             man:sar(1)
    Process: 18910 ExecStart=/usr/lib/sysstat/debian-sa1 --boot (code=exited, status=0/SUCCESS)
   Main PID: 18910 (code=exited, status=0/SUCCESS)

 9月 08 21:53:01 bookstoreUbuntu systemd[1]: Starting Resets System Activity Data Collector...
 9月 08 21:53:01 bookstoreUbuntu systemd[1]: Finished Resets System Activity Data Collector.

システム情報の閲覧

sysstatサービスが起動するとシステム情報が収集され始めるようです。

sarコマンドでシステム情報を閲覧できるようです。何も指定しない場合はCPUの使用状況が出力されました。

bookstore@bookstoreUbuntu:~$ sar
Linux 5.4.0-45-generic (bookstoreUbuntu)    2020年09月08日   _x86_64_    (8 CPU)

21時53分01秒  LINUX RESTART  (8 CPU)

21時55分01秒     CPU     %user     %nice   %system   %iowait    %steal     %idle
22時05分01秒     all      0.02      0.00      0.08      0.00      0.00     99.89
22時15分01秒     all      0.03      0.00      0.07      0.00      0.00     99.89
Average:        all      0.03      0.00      0.08      0.00      0.00     99.89

インターバルと回数を指定すれば指定された間隔で指定された回数情報を出力します。単位は秒のようです。

bookstore@bookstoreUbuntu:~$ sar 5
Linux 5.4.0-45-generic (bookstoreUbuntu)    2020年09月08日   _x86_64_    (8 CPU)

22時24分04秒     CPU     %user     %nice   %system   %iowait    %steal     %idle
22時24分09秒     all      0.00      0.00      0.10      0.00      0.00     99.90
22時24分14秒     all      0.03      0.00      0.03      0.00      0.00     99.95
22時24分19秒     all      0.00      0.00      0.08      0.00      0.00     99.92
^C

Average:        all      0.01      0.00      0.07      0.00      0.00     99.92

-o オプションを使用すれば結果をファイルに書き込めます。ファイルは -f オプションを使えば読み込んで表示できます。

bookstore@bookstoreUbuntu:~$ sar -o sar20200909 1 5
Linux 5.4.0-47-generic (bookstoreUbuntu)    2020年09月09日   _x86_64_    (8 CPU)

15時51分37秒     CPU     %user     %nice   %system   %iowait    %steal     %idle
15時51分38秒     all      0.00      0.00      0.12      0.00      0.00     99.88
15時51分39秒     all      0.00      0.00      0.12      0.00      0.00     99.88
15時51分40秒     all      0.00      0.12      0.25      0.00      0.00     99.63
15時51分41秒     all      0.12      0.00      0.00      0.00      0.00     99.88
15時51分42秒     all      0.00      0.00      0.00      0.00      0.00    100.00
Average:        all      0.03      0.03      0.10      0.00      0.00     99.85
bookstore@bookstoreUbuntu:~$ ls
sar20200909  ダウンロード  テンプレート  デスクトップ  ドキュメント  ビデオ  ピクチャ  ミュージック  公開
bookstore@bookstoreUbuntu:~$ sar -f sar20200909
Linux 5.4.0-47-generic (bookstoreUbuntu)    2020年09月09日   _x86_64_    (8 CPU)

15時51分37秒     CPU     %user     %nice   %system   %iowait    %steal     %idle
15時51分38秒     all      0.00      0.00      0.12      0.00      0.00     99.88
15時51分39秒     all      0.00      0.00      0.12      0.00      0.00     99.88
15時51分40秒     all      0.00      0.12      0.25      0.00      0.00     99.63
15時51分41秒     all      0.12      0.00      0.00      0.00      0.00     99.88
15時51分42秒     all      0.00      0.00      0.00      0.00      0.00    100.00
Average:        all      0.03      0.03      0.10      0.00      0.00     99.85

ファイルはバイナリ形式で保存されるのでsarコマンドからのみ閲覧できるみたいですね。

bookstore@bookstoreUbuntu:~$ file /var/log/sysstat/sa09
/var/log/sysstat/sa09: data

-o にてファイル名を指定しない場合にはデフォルトのファイルに書き込まれるようです。デフォルトのファイルは /var/log/sysstat ディレクトリ内に一日づつファイルが分かれて保存されています。

bookstore@bookstoreUbuntu:~$ ls /var/log/sysstat
sa08  sa09

これら日付別のファイルはcronにより勝手にシステム情報が収集されて増えていくようですね。ただ、saDD の形式なので一ヶ月のサイクルでファイルが上書きされていくようです。

ちなみに -D オプションを同時に使用すれば YYYYMMDD 形式でファイルを保存できます。こうするとファイルのローテーションでシステム情報を永続的に保持できますね。

bookstore@bookstoreUbuntu:~$ sudo sar -o -D 1 3
Linux 5.4.0-47-generic (bookstoreUbuntu)    2020年09月09日   _x86_64_    (8 CPU)

16時01分38秒     CPU     %user     %nice   %system   %iowait    %steal     %idle
16時01分39秒     all      0.00      0.00      0.00      0.00      0.00    100.00
16時01分40秒     all      0.12      0.00      0.00      0.12      0.00     99.75
16時01分41秒     all      0.00      0.00      0.00      0.00      0.00    100.00
Average:        all      0.04      0.00      0.00      0.04      0.00     99.92
bookstore@bookstoreUbuntu:~$ ls /var/log/sysstat
sa08  sa09  sa20200909

CPU以外のシステム情報

オプションを指定すればCPU以外の情報を閲覧できます。

例えば -r オプションでメモリに関する情報を閲覧できます。

bookstore@bookstoreUbuntu:~$ sar -r
Linux 5.4.0-47-generic (bookstoreUbuntu)    2020年09月09日   _x86_64_    (8 CPU)

08時06分30秒  LINUX RESTART  (8 CPU)

08時15分01秒 kbmemfree   kbavail kbmemused  %memused kbbuffers  kbcached  kbcommit   %commit  kbactive   kbinact   kbdirty
08時25分01秒  14940260  15373916    464340      2.85    134356    488360   2690344      8.17    609316    358200         0
08時35分01秒  14933872  15373436    463892      2.85    134468    493944   2709056      8.22    613796    359616         0
08時45分01秒  14629268  15402540    431872      2.65    137404    816900   2289792      6.95    873836    396212        24
08時55分01秒  14587532  15363848    460500      2.83    137552    818896   2686372      8.16    902168    396476         8
09時05分01秒  14586692  15363180    460840      2.83    137616    818908   2686372      8.16    902432    396464        40
Average:     14735525  15375384    456289      2.80    136279    687402   2612387      7.93    780310    381394        14

-b オプションではI/Oに関する情報を閲覧できます。

bookstore@bookstoreUbuntu:~$ sar -b
Linux 5.4.0-47-generic (bookstoreUbuntu)    2020年09月09日   _x86_64_    (8 CPU)

08時06分30秒  LINUX RESTART  (8 CPU)

08時15分01秒       tps      rtps      wtps      dtps   bread/s   bwrtn/s   bdscd/s
08時25分01秒      0.22      0.00      0.22      0.00      0.01      3.05      0.00
08時35分01秒      0.32      0.10      0.22      0.00     18.03      3.15      0.00
08時45分01秒     13.63      8.60      5.04      0.00    878.59   1048.86      0.00
08時55分01秒      0.67      0.10      0.56      0.00      6.23      8.31      0.00
09時05分01秒      0.12      0.00      0.12      0.00      0.00      1.80      0.00
Average:         2.99      1.76      1.23      0.00    180.57    213.03      0.00

-n オプションではネットワークに関する情報を閲覧できるのですが、TCPの情報を見ようとしたら見れませんでした。どうやら情報を収集していないので設定が必要な模様。

bookstore@bookstoreUbuntu:/etc/sysstat$ sar -n TCP
Requested activities not available in file /var/log/sysstat/sa09

sarのmanページを確認したらバックグラウンドでシステム情報を収集するsadcにてSNMP情報を収集してないとみれないぞ、と書いてありました。sysstatのmanページを見たら /etc/sysstat/sysstat/ に設定ファイルがあるようなので確認します。

# sysstat configuration file. See sysstat(5) manual page.

# How long to keep log files (in days).
# Used by sa2(8) script
# If value is greater than 28, then use sadc's option -D to prevent older
# data files from being overwritten. See sadc(8) and sysstat(5) manual pages.
HISTORY=7

# Compress (using xz, gzip or bzip2) sa and sar files older than (in days):
COMPRESSAFTER=10

# Parameters for the system activity data collector (see sadc(8) manual page)
# which are used for the generation of log files.
# By default contains the `-S DISK' option responsible for generating disk
# statisitcs. Use `-S XALL' to collect all available statistics.
SADC_OPTIONS="-S DISK"

# Directory where sa and sar files are saved. The directory must exist.
SA_DIR=/var/log/sysstat

# Compression program to use.
ZIP="xz"

# By default sa2 script generates yesterday's summary, since the cron job
# usually runs right after midnight. If you want sa2 to generate the summary
# of the same day (for example when cron job runs at 23:53) set this variable.
#YESTERDAY=no

# By default sa2 script generates reports files (the so called sarDD files).
# Set this variable to false to disable reports generation.
#REPORTS=false

# The sa1 and sa2 scripts generate system activity data and report files in
# the /var/log/sysstat directory. By default the files are created with umask 0022
# and are therefore readable for all users. Change this variable to restrict
# the permissions on the files (e.g. use 0027 to adhere to more strict
# security standards).
UMASK=0022

SMDC_OPTIONSにSNMPを追加しましょう。

SADC_OPTIONS="-S DISK,SNMP"

ファイルを上書きし、sysstatサービスを再起動。

bookstore@bookstoreUbuntu:/etc/sysstat$ systemctl restart sysstat

しかし、もう一度試してみましたが見れませんでした。

bookstore@bookstoreUbuntu:/etc/sysstat$ sar -n TCP
Requested activities not available in file /var/log/sysstat/sa09

sadcのmanページをよく見てみると、オプションを変更したとしても既存の出力ファイルが有る場合にはそちらの設定が優先されるとのこと。というわけで /var/log/sysstat/ のファイルを全部削除し、sysstatサービスがシステム情報を書き出すのを待ってみたらうまく見れました。

bookstore@bookstoreUbuntu:/etc/sysstat$ sar -n TCP
Linux 5.4.0-47-generic (bookstoreUbuntu)    2020年09月09日   _x86_64_    (8 CPU)

18時45分01秒  active/s passive/s    iseg/s    oseg/s
18時55分01秒      0.00      0.00      0.02      0.02
19時05分01秒      0.00      0.00      0.02      0.02
19時15分01秒      0.00      0.00      0.08      0.06
19時25分01秒      0.00      0.00      0.02      0.02
19時35分01秒      0.00      0.00      0.02      0.02
19時45分01秒      0.00      0.00      0.02      0.02
19時55分01秒      0.00      0.00      0.02      0.02
20時05分01秒      0.00      0.00      0.02      0.02
Average:         0.00      0.00      0.03      0.02

他にもいろいろな機能があるので後でまとめる機会があれば書き足そうと思います。