今回は、CentOS 7で採用されている新しいログ管理の仕組み「journald」を取り上げます。またjournaldとrsyslogとの連携やシステム全体に渡って様々なログを収集するsosreportについても簡単に触れます。
CentOS 7におけるログ機構「Systemd Journal」を使いこなす
CentOS 7は、ログの管理を行う新しい仕組みが導入されています。従来のCentOS 6系では、長年親しまれてきたsyslogがベースのログ管理手法が採用されていましたが、CentOS 7からは、ログに関するより細かい指定や操作を行うことができるようになっています。CentOS 7でのログ管理は、systemdが担当しています。サービス名は、「systemd-journald.service」です。一般的には、「journald」と呼ばれています。CentOS 6までのsyslogではsyslogデーモンを稼働させていましたが、CentOS 7からは、もはやsyslogdデーモンの起動は不要です。その代わり、ログの収集の仕組みであるjournaldのサービスを起動する必要があります。journaldが起動しているかどうかは、systemctlコマンドで確認できます。
01 | # systemctl status systemd-journald |
02 | systemd-journald.service - Journal Service |
03 | Loaded: loaded (/usr/lib/systemd/system/systemd-journald.service; static) |
04 | Active: active (running) since 月 2014-10-20 03:04:05 JST; 10h ago |
05 | Docs: man:systemd-journald.service(8) |
07 | Main PID: 482 (systemd-journal) |
08 | Status: "Processing requests..." |
09 | CGroup: /system.slice/systemd-journald.service |
10 | mq482 /usr/lib/systemd/systemd-journald |
12 | 10月 20 03:04:06 localhost.localdomain systemd-journal[482]: Runtime journal is using 6..... |
13 | 10月 20 03:04:06 localhost.localdomain systemd-journal[482]: Runtime journal is using 6..... |
14 | 10月 20 03:04:06 localhost.localdomain systemd-journal[482]: Journal started |
15 | 10月 20 03:04:17 localhost.localdomain systemd-journal[482]: Runtime journal is using 6..... |
16 | Hint: Some lines were ellipsized, use -l to show in full. |
journaldが収集したログを適宜整形、加工できるのが、journalctlコマンドになります。journalctlコマンドは、ログの出力に関する様々なオプションを備えています。以下では、CentOS 7の管理者が、知っておくべきjournalctlコマンドを使ったログ管理の基礎をご紹介します。
ブートログを出力する
サーバーに搭載されている各種ハードウェアの認識状態等を確認するために、CentOS 7のブート時のログを見たい場合があります。直前のブートのログを確認するには、journalctlコマンドに-bオプションを付与します。
日時でフィルタリングする
以下は、2014年9月5日午前1時23分45秒から2014年9月7日午前4時56分0秒までのログを出力する例です。
1 | # journalctl --since="2014-09-05 01:23:45" --until="2014-09-07 04:56:00" |
2 | -- Logs begin at Sat 2014-09-06 00:03:27 JST, end at Sat 2014-09-06 10:46:25 JST. -- |
3 | Sep 06 00:03:27 c70n2530.jpn.linux.hp.com systemd-journal[95]: Runtime journal is using 8.0M (max 93.3M, leaving 140.0M of free 925.3M, current limit 93.3M). |
4 | Sep 06 00:03:27 c70n2530.jpn.linux.hp.com systemd-journal[95]: Runtime journal is using 8.0M (max 93.3M, leaving 140.0M of free 925.3M, current limit 93.3M). |
5 | Sep 06 00:03:27 c70n2530.jpn.linux.hp.com kernel: Initializing cgroup subsys cpuset |
デフォルトでは、自動的にlessページャが起動し、ログをスクロールさせて閲覧することができます。lessページャの自動起動が不要の場合は、--no-pagerオプションを付与します。特定のサービスに限ってログを出力させたい場合は、そのサービスのユニット名を指定します。
1 | # journalctl --since="2014-09-05 01:23:45" --until="2014-09-07 04:56:00" -u sshd.service |
2 | -- Logs begin at Sat 2014-09-06 00:03:27 JST, end at Sat 2014-09-06 10:46:25 JST. -- |
3 | Sep 06 00:03:58 c70n2530.jpn.linux.hp.com systemd[1]: Starting OpenSSH server daemon... |
4 | Sep 06 00:03:58 c70n2530.jpn.linux.hp.com systemd[1]: Started OpenSSH server daemon. |
--sinceオプションに「today」を指定することで、今日ログされたものから現在までのものを抽出して出力することも可能です。
1 | # journalctl --since=today |
2 | -- Logs begin at Sat 2014-09-06 00:03:27 JST, end at Sat 2014-09-06 11:03:55 JST. -- |
3 | Sep 06 00:03:27 c70n2530.jpn.linux.hp.com systemd-journal[95]: Runtime journal is using 8.0M (max 93.3M, leaving 140.0M of free 925.3M, current limit 93.3M). |
4 | Sep 06 00:03:27 c70n2530.jpn.linux.hp.com systemd-journal[95]: Runtime journal is using 8.0M (max 93.3M, leaving 140.0M of free 925.3M, current limit 93.3M). |
他にも、--sinceオプションに「yesterday」や「tomorrow」を指定することも可能です。
過去のログだけでなく、現在のシステムが出力しているログをリアルタイムで閲覧したい場合があります。従来の「tail -f /var/log/messages」のように、tailコマンドを駆使していたようなログの閲覧方法を実現するには、-fオプションを付与して実行します。
2 | -- Logs begin at Sat 2014-09-06 00:03:27 JST. -- |
3 | Sep 06 10:30:01 c70n2530.jpn.linux.hp.com CROND[10619]: (root) CMD (/usr/lib64/sa/sa1 1 1) |
4 | Sep 06 10:40:01 c70n2530.jpn.linux.hp.com systemd[1]: Starting Session 76 of user root. |
5 | Sep 06 10:40:01 c70n2530.jpn.linux.hp.com systemd[1]: Started Session 76 of user root. |
従来のsyslogでは、ログのプライオリティ管理ができました。journalctlでも同様にプライオリティによる出力のフィルタリングを行うことができます。特定のプライオリティのログのみを出力させる場合は、-pオプションにプライオリティを付与してjournalctlコマンドを実行します。
warningプライオリティの場合
2 | -- Logs begin at Sat 2014-09-06 00:03:27 JST, end at Sat 2014-09-06 10:46:25 JST. -- |
3 | Sep 06 00:03:27 c70n2530.jpn.linux.hp.com kernel: crashkernel=auto resulted in zero bytes of reserved memory. |
4 | Sep 06 00:03:27 c70n2530.jpn.linux.hp.com kernel: ACPI: RSDP 00000000000f68c0 00024 (v02 HPQOEM) |
errプライオリティの場合
2 | -- Logs begin at Sat 2014-09-06 00:03:27 JST, end at Sat 2014-09-06 11:01:01 JST. -- |
3 | Sep 06 00:03:27 c70n2530.jpn.linux.hp.com kernel: tpm_tis 00:03: A TPM error (7) occurred attempting to read a pcr value |
4 | Sep 06 00:03:40 c70n2530.jpn.linux.hp.com systemd-udevd[423]: error opening ATTR{/sys/devices/pci0000:00/0000:00:1a.1/usb4/4-1/4-1:1.0/power/control} for writing: No such file or directory |
上記の-pオプションには、プライオリティの値で指定することも可能です。プライオリティの値とログレベルの対応関係を以下に示します。
1 | # journalctl -p 0 ← emerg |
2 | # journalctl -p 1 ← alert |
3 | # journalctl -p 2 ← crit |
5 | # journalctl -p 4 ← warning |
6 | # journalctl -p 5 ← notice |
7 | # journalctl -p 6 ← debug |
カーネルログのみを表示させることも可能です。従来のsyslog管理のdmesgに相当します。
02 | -- Logs begin at 日 2014-08-31 01:02:16 JST, end at 土 2014-09-13 10:10:01 JST. -- |
05 | 9月 07 01:30:16 centos70n01.jpn.linux.hp.com kernel: NX (Execute Disable) protection: active |
06 | 9月 07 01:30:16 centos70n01.jpn.linux.hp.com kernel: SMBIOS 2.4 present. |
07 | 9月 07 01:30:16 centos70n01.jpn.linux.hp.com kernel: DMI: Red Hat KVM, BIOS 0.5.1 01/01/2007 |
08 | 9月 07 01:30:16 centos70n01.jpn.linux.hp.com kernel: Hypervisor detected: KVM |
09 | 9月 07 01:30:16 centos70n01.jpn.linux.hp.com kernel: e820: update [mem 0x00000000-0x00000fff] usable ==> reserved |
10 | 9月 07 01:30:16 centos70n01.jpn.linux.hp.com kernel: e820: remove [mem 0x000a0000-0x000fffff] usable |
11 | 9月 07 01:30:16 centos70n01.jpn.linux.hp.com kernel: No AGP bridge found |
特定のプロセスIDのみに関するログを出力したい場合は、「_PID=プロセス番号」を付与して実行します。
2 | -- Logs begin at Sat 2014-09-06 00:03:27 JST, end at Sat 2014-09-06 10:50:01 JST. -- |
3 | Sep 06 10:46:25 c70n2530.jpn.linux.hp.com sshd[10765]: Accepted password for root from 172.16.27.10 port 57150 ssh2 |
4 | Sep 06 10:46:25 c70n2530.jpn.linux.hp.com sshd[10765]: pam_unix(sshd:session): session opened for user root by (uid=0) |
サービスを提供する実行ファイルのパスを指定することも可能です。以下では、コマンドの実行をスケジューリングするサービス「crond」の実体である/usr/sbin/crondに関連するログのみを出力する例です。
1 | # journalctl /usr/sbin/crond |
2 | -- Logs begin at Sat 2014-09-06 00:03:27 JST, end at Sat 2014-09-06 11:01:01 JST. -- |
3 | Sep 06 00:03:51 c70n2530.jpn.linux.hp.com crond[585]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 92% if used.) |
4 | Sep 06 00:03:53 c70n2530.jpn.linux.hp.com crond[585]: (CRON) INFO (running with inotify support) |
5 | Sep 06 00:20:01 c70n2530.jpn.linux.hp.com CROND[2473]: (root) CMD (/usr/lib64/sa/sa1 1 1) |
6 | Sep 06 01:01:01 c70n2530.jpn.linux.hp.com CROND[2972]: (root) CMD (run-parts /etc/cron.hourly) |