16
21

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 5 years have passed since last update.

Raspberry Piから読み解くログローテションの仕組み

Last updated at Posted at 2018-04-13

はじめに

システムのログはどのようにログローテションされているか、Raspberry Piのrsyslogとcronを題材にして簡単にまとめました。ログはシステムにおいてとても重要です。仕組みを理解することで、プログラム開発にも相乗効果があると思います。

Raspberry Piで動くRaspbianは、Debianをベースとした教育用及び開発用に作られたLinux系OSです。基本的な仕組みはLinuxと変わりません。

OS部分について中身を覗いてみると、Linuxの設計思想を読み取ることができて、案外面白いと思います。

本記事の環境は以下になります。

環境:Raspbian GNU/Linux 9.3 (stretch)

全体的な仕組み

Raspberry Pi(Raspbian)におけるログローテションの仕組みは、cronから呼び出されたrsyslogにより行われています。ryslogはログユーティリティです。ちなみに、rsyslogの"r"はRocketを意味します。ロケットのように早くシステムのログを処理するという意味で、rsyslogのようです。

公式サイトより
RSYSLOG is the rocket-fast system for log processing.

Raspberry Pi(Raspbian)の場合は以下のコマンドで導入されているrsyslogのパッケージ情報を確認できます。

$ dpkg -s rsyslog

また、cronはUNIX系OSにおいて、ジョブとしてコマンドを定時実行させるためにスケジュール管理を行うデーモンプログラムです。

cronについても同じコマンドで導入されているcronのパッケージ情報を確認できます。

$ dpkg -s cron

/etc/crontab

「/etc/crontab」はcronの全体設定ファイルです。中身を見て見ましょう。

$ cat -n /etc/crontab

     1	# /etc/crontab: system-wide crontab
     2	# Unlike any other crontab you don't have to run the `crontab'
     3	# command to install the new version when you edit this file
     4	# and files in /etc/cron.d. These files also have username fields,
     5	# that none of the other crontabs do.
     6	
     7	SHELL=/bin/sh
     8	PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
     9	
    10	# m h dom mon dow user	command
    11	17 *	* * *	root    cd / && run-parts --report /etc/cron.hourly
    12	25 6	* * *	root	test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
    13	47 6	* * 7	root	test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
    14	52 6	1 * *	root	test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
    15	#

/etc/cron.daily/logrotate

Raspberry Pi(Raspbian)のシステムログは、「/var/log/syslog」になります。「/var/log/syslog」を例にログローテションの仕組みを読み解いていきます。「/var/log/syslog」のタイムスタンプを見てみると、06:25に切り替わっているのが確認できます。
スクリーンショット 2018-04-13 20.06.29.png

動きとしては、12行目で指定されたコマンドにより、「/etc/cron.daily/logrotate」が6:25に起動されログローテーションが行われます。

12行目の記述は、毎日06:25にrootにより「test」コマンドが実行されます。-xを付けているので、「/usr/sbin/anacron」が存在し、実行可能であれば真になります。

25 6	* * *	root	test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )

ですが、Raspberry Pi(Raspbian)の場合、**デフォルトでanacronはインストールされていません。よって、testコマンドの結果は偽になり、後ろの"||"**に処理が引き渡されます。そのとき、コマンド1(testコマンド)正常終了しなかったのでコマンド2(cd / && run-parts --report /etc/cron.daily )が実行されます。

コマンド2の「run-parts」コマンドはディレクトリにあるスクリプト・プログラムを実行するので、「/etc/cron.daily」配下にあるスクリプトを実行します。「/etc/cron.daily」配下は以下のようなファイルが存在します。

スクリーンショット 2018-04-13 22.29.45.png

この中にある「logrotate」の中身を見てます。

$ cat /etc/cron.daily/logrotate

# !/bin/sh

test -x /usr/sbin/logrotate || exit 0
/usr/sbin/logrotate /etc/logrotate.conf

「logrotate」はコマンドなのでバイナリのプログラムです。「logrotate」に「/etc/logrotate.conf」を渡してログローテションを行ないます。

/etc/logrotate.conf

「/etc/logrotate.conf」はlogrotateの全体ファイルです。個別の設定ファイルが存在する場合は個別の設定ファイルが優先されます。

「/etc/logrotate.conf」の中身を見てます。includeのところで、「/etc/logrotate.d」配下に個別の設定ファイルが存在する場合は個別の設定ファイルを見に行きます。

$ cat /etc/logrotate.conf

# see "man logrotate" for details
# rotate log files weekly
weekly

# keep 4 weeks worth of backlogs
rotate 4

# create new (empty) log files after rotating old ones
create

# uncomment this if you want your log files compressed
# compress

# packages drop log rotation information into this directory
include /etc/logrotate.d

# no packages own wtmp, or btmp -- we'll rotate them here
/var/log/wtmp {
    missingok
    monthly
    create 0664 root utmp
    rotate 1
}

/var/log/btmp {
    missingok
    monthly
    create 0660 root utmp
    rotate 1
}

# system-specific logs may be configured here

「/etc/logrotate.d」の配下には以下のファイルが存在します。

スクリーンショット 2018-04-13 22.32.53.png

/etc/logrotate.d/rsyslog

あとちょっとです。

「/etc/logrotate.d/rsyslog」の中身を見てます。

$ cat /etc/logrotate.d/rsyslog

/var/log/syslog
{
	rotate 7
	daily
	missingok
	notifempty
	delaycompress
	compress
	postrotate
		invoke-rc.d rsyslog rotate > /dev/null
	endscript
}

/var/log/mail.info
/var/log/mail.warn
/var/log/mail.err
/var/log/mail.log
/var/log/daemon.log
/var/log/kern.log
/var/log/auth.log
/var/log/user.log
/var/log/lpr.log
/var/log/cron.log
/var/log/debug
/var/log/messages
{
	rotate 4
	weekly
	missingok
	notifempty
	compress
	delaycompress
	sharedscripts
	postrotate
		invoke-rc.d rsyslog rotate > /dev/null
	endscript
}

設定ファイルの見方としては、ログファイル名{}で括っている設定値が有効になります。この中の設定値をカスタマイズすることで、ログローテションの世代数、日次/週次、圧縮/悲圧縮等を制御できます。

/etc/rsyslog.conf

最後に「/etc/rsyslog.conf」について説明します。
「/etc/rsyslog.conf」はrsyslogの主要設定ファイルです。

「/etc/logrotate.d/rsyslog」の中身を見てます。

$ cat /etc/logrotate.d/rsyslog

〜省略
###############
#### RULES ####
###############

#
# First some standard log files.  Log by facility.
#
auth,authpriv.*			/var/log/auth.log
*.*;auth,authpriv.none		-/var/log/syslog
# cron.*				/var/log/cron.log
daemon.*			-/var/log/daemon.log
kern.*				-/var/log/kern.log
lpr.*				-/var/log/lpr.log
mail.*				-/var/log/mail.log
user.*				-/var/log/user.log

#
# Logging for the mail system.  Split it up so that
# it is easy to write scripts to parse these files.
#
mail.info			-/var/log/mail.info
mail.warn			-/var/log/mail.warn
mail.err			/var/log/mail.err
〜省略
*.emerg				:omusrmsg:*

見方としては左から、「ファシリティ」、「プライオリティ」を基に右側のログファイルが出力先です。

さいごに

Raspberry Pi(Raspbian)はanacronがインストールされていないため、処理が分かりやすいです。しかし、RHEL系の場合はanacronによりcronが呼びだされて処理を行うので、仕組みが少し違います。

anacronがなくてもきちんとrsyslogが動くように設計された、Raspberry Pi(Raspbian)の設計が感慨深いなと思いました。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?