LoginSignup
11
16

More than 3 years have passed since last update.

ログの基本  〜syslogとWebサーバのログ〜

Last updated at Posted at 2019-11-04

概要

ログとは何か、システムでのログの収集と管理の方法やその設定について、代表的なsyslogについてまとめました。またWebサーバーの主なものの一つであるApacheでのログの取り扱いについての基本をまとめました。

この記事でのサンプルの実行環境

OS Amazon Linux2

ログの基本

ログとは

プログラムが実行された時に、あらかじめ設定された出力先へプログラムの情報を出力したもの。

syslogはログの基本

Unix系OSではログといえばsyslogと言われています。
Mail(SMTP:Simple Mail Transfer Protocol)サーバの代表的なアプリケーションであるsendmailのログアプリケーションとして開発されたのが始まりで、これが便利とわかってFTPなど他のサーバでも使われるようになって広がりました。

syslogの基本機能① ログの書き込み

1つ目の機能がログの書き込みです。

スクリーンショット 2019-11-04 0.58.31.png

例えば上図のように、MailサーバーやFTPサーバーからのログ情報をsyslogがどのサーバからのログか確認し、各サーバ用のログファイルへ書き込みます

syslogの基本機能② ログの収集

二つ目の機能はログの収集機能管理機能で、複数のサーバのログ情報を一台のログ専用サーバーで集中的に収集し管理する機能です。

スクリーンショット 2019-11-04 1.19.44.png

例えば上図のように、MailサーバとFTPサーバのそれぞれのログ情報をsyslogの二つ目の機能でログサーバへ転送し、その後ログサーバ内のsyslogでそのログ情報をサーバー毎に判別して各サーバ用のログファイルに出力します(1つ目の機能)

現在のsyslog

現在はsyslogの改定版アプリケーションとして二つのアプリケーションがあります

1. syslog-ng

syslogプロトコルのサポートに加え以下のような機能が実装されています

  • ログの分類機能
  • TCPによるログの送受信(ログ情報紛失の回避)
  • SSL/TLSを使用したセキュアログ
  • データベースへのログ出力

2. rsyslog

rsyslog: rocket-fast system for logの略。syslogプロトコルのサポートはもちろん、以下の機能が実装されています

  • ログの分類機能
  • TCPによるログの送受信
  • SSL/TLSを使用したセキュアログ
  • データベースへのログ出力

CentOS6やDebianでrsyslogが採用されています

syslogへコマンドを使って出力してみる

psコマンドでsyslogのプロセスを確認

ps ax | grep syslog
  602 pts/0    S+     0:00 grep --color=auto syslog
 3199 ?        Ssl    0:00 /usr/sbin/rsyslogd -n

loggerコマンドにより、syslogにログ情報を渡します。
-pオプションでそのログのファシリティとプライオリティを指定します。

logger -p authpriv.info secure-log

tailコマンドでログを確認

sudo tail -f /var/log/secure

Nov  4 08:01:41 ip-172-31-44-123 ec2-user: secure-log
Nov  4 08:01:44 ip-172-31-44-123 ec2-user: secure-log
Nov  4 08:01:50 ip-172-31-44-123 ec2-user: secure-log

syslogの設定については /etc/rsyslog.confファイルで確認できます

syslogのfacilityやpriolityなどに関して、syslogはプロトコルとしてRFCに定義されています。詳細はこちらを参考にしてください。
https://tools.ietf.org/html/rfc5424

Webサーバのログを確認(Apache)

httpd(Apache)のインストールと起動設定

sudo yum install httpd
sudo systemctl start httpd

httpdの起動を確認

systemctl status httpd.service

実行結果でターミナルに Active: active(running)と表示されれば起動しています

Apacheの設定ファイルを確認

Apacheの基本設定は /etc/httpd/conf/httpd.confで行います。
このうちログに関する設定は447行目付近から行なっています。

#
# ErrorLog: The location of the error log file.
# If you do not specify an ErrorLog directive within a <VirtualHost>
# container, error messages relating to that virtual host will be
# logged here.  If you *do* define an error logfile for a <VirtualHost>
# container, that host's errors will be logged there and not here.
#
ErrorLog "logs/error_log"

#
# LogLevel: Control the number of messages logged to the error_log.
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
#
LogLevel warn

<IfModule log_config_module>
    #
    # The following directives define some format nicknames for use with
    # a CustomLog directive (see below).
    #
    LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
    LogFormat "%h %l %u %t \"%r\" %>s %b" common

    <IfModule logio_module>
      # You need to enable mod_logio.c to use %I and %O
      LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
    </IfModule>

    #
    # The location and format of the access logfile (Common Logfile Format).
    # If you do not define any access logfiles within a <VirtualHost>
    # container, they will be logged here.  Contrariwise, if you *do*
    # define per-<VirtualHost> access logfiles, transactions will be
    # logged therein and *not* in this file.
    #
    #CustomLog "logs/access_log" common

    #
    # If you prefer a logfile with access, agent, and referer information
    # (Combined Logfile Format) you can use the following directive.
    #
    CustomLog "logs/access_log" combined

エラーログの設定

ディレクティブ:ErrorLogを使います。

ErrorLog "logs/error_log"

実際のログファイルのパスはServerRootの設定+ここの設定パスになります。
つまり /etc/httpd/logs/error_logとなります。
また、関連したディレクトティブにLogLevelがあります。

LogLevel warn

ここで、エラーログを出力するログレベルを指定します。この場合の設定だとログレベルが warn以上だとエラーログへ出力されるようになっています。

アクセスログの設定

ディレクトティブ:CustomLogを使用します。

CustomLog "logs/access_log" combined

実際のログファイルのパスは先ほどのErrorLogと同様にServerRootの設定+ここの設定パスになります。つまり、/etc/httpd/logs/access_logになります。
フォーマットは combinedを使用します。

LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\"" combined

参考:https://httpd.apache.org/docs/2.4/ja/logs.html

Webサイトへアクセスしてログを出力

スクリーンショット 2019-11-04 15.54.33.png
サーバーのIPアドレスまたはドメイン名をブラウザの検索バーに入力してアクセスする上のようなApacheのTest Pageを表示できます。

tailコマンドを使ってログを確認

スクリーンショット 2019-11-04 16.00.02.png

エラーログを確認

スクリーンショット 2019-11-04 16.02.29.png

上のように、Cannot serve directory /var/www/html/: No matching DirectoryIndex (index.html) found, and server-generated directory index forbidden by Options directiveとあります。
DirectoryIndex()にマッチするものがなく、Options directiveによって/var/www/html/の提供ができませというようなことがわかります。

エラーを修正

まずデフォルトサイトの設定(/etc/httpd/conf.d/welcome.conf)を確認します。

# This configuration file enables the default "Welcome" page if there
# is no default index page present for the root URL.  To disable the
# Welcome page, comment out all the lines below.
#
# NOTE: if this file is removed, it will be restored on upgrades.
#
<LocationMatch "^/+$">
    Options -Indexes
    ErrorDocument 403 /.noindex.html
</LocationMatch>

Optionsディレクトティブで -Indexesと指定されています。この設定はURLにファイル名が省略されてアクセスされた場合に「もし表示するものがなかったとしてもディレクトリ一覧を表示しない」という意味です。エラーログのNo matching DirectoryIndex (index.html) found, and server-generated directory index forbidden by Options directiveという内容と一致します。

さらに、上のErrorDocumentディレクトティブで「403が発生したら /.noindex.htmlを出力する」と設定されています。このnoindex.htmlが先ほどのTest Pageになります。

IPアドレスだけでアクセスした時、Apacheではあらかじめ設定された省略時のデフォルトファイルを探します。そのデフォルトファイルはDirectoryIndexで設定されます。上のデフォルトサイトの設定にはそのDirectoryIndexディレクティブの設定がなかったので基本設定ファイル(/etc/httpd/conf/httpd.conf)の設定を引き継ぎます。

$ cat /etc/httpd/conf/httpd.conf | grep DirectoryIndex
# DirectoryIndex: sets the file that Apache will serve if a directory
    DirectoryIndex index.html

上の結果のように、Apacheではファイル名を指定せずにアクセスした場合は index.htmlを探しにいくように設定されています。
どこのディレクトリのindex.htmlをみにいくのかというと、このケースのサーバーIPを指定した場合はデフォルトサイトのドキュメントルートディレクトリはどこかというのと同義で、上のデフォルトサイトの設定にはなかったのでこれも基本設定ファイルの設定内容を引き継ぐことになります。

$ cat /etc/httpd/conf/httpd.conf | grep DocumentRoot
# DocumentRoot: The directory out of which you will serve your
DocumentRoot "/var/www/html"
    # access content that does not live under the DocumentRoot.

上の結果よりDocumentRootディレクティブには/var/www/htmlと指定されているのでこれがドキュメントルートディレクトリということになります。

まとめると

/var/www/htmlに表示すべきファイル(index.html)が存在しなかったのでディレクトリ一覧を出力しようとしたが禁止されていたので
エラー403を出力した。403の飛び先が /.noindex.htmlと指定されていてそのファイルが返された

上の結果を受けて /var/www/html/配下にindex.htmlファイルを作成することでエラーを解決してみます。

echo 'Hello Apache!!!' > /var/www/html/index.html

再度ブラウザでアクセス

スクリーンショット 2019-11-04 16.38.38.png

Hello Apache!!!の文字を確認できました。
アクセスログも確認すると
スクリーンショット 2019-11-04 16.39.58.png

ステータスコード200が返ってくるようになりました。
エラーログについて、

スクリーンショット 2019-11-04 16.41.18.png

アクセスログと同じ時刻にエラーが表示されてないことを確認できました。

まとめ

代表的なログ管理用のアプリケーションであるsyslogと、代表的なWebサーバの一つであるApacheでのログの扱いについてみていきました。
システムに障害が起きた際に原因を追求できるようにしたり分析できるように今回の基礎的な知識を応用していきたいと思います。

参考

インフラエンジニア教本 Volume2
Apache ログファイル
rfc5424

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