Create: 2014/07/10
LastUpdate: 2014/07/11
CentOS6 には、rsyslog がデフォルトでインストールされています。
この rsyslog は、デフォルトの設定では、ログをテキストファイル(例:/var/log/messages)に保存しますが、モジュールを追加して機能拡張することでデータベースにログを保存できるようになります。
ここでは、rsyslog のログを MySQL に保存する手順を紹介します。
使用したサーバの環境は以下のとおりです。
- OS: CentOS6.5 (64bit)
- LANG: ja_JP.UTF-8
- ファイヤーウォール: 無効
- SELinux: 無効
- パッケージ: 最小構成
- IPアドレス: 192.168.1.70/24
- ホスト名: centos65
1.パッケージの追加インストール
MySQL と rsyslog 機能拡張用のパッケージをインストールします。
# yum install mysql mysql-server rsyslog-mysqlMySQL は起動しておきます。
# service mysqld start
2.MySQLの準備
MySQLの文字セットをutf-8 に設定します。/etc/my.cnf を以下のようにします。
[mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock user=mysql # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 # server charset character-set-server = utf8 skip-character-set-client-handshake [mysqld_safe] log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid [client] default-character-set = utf8設定変更後は、MySQL を再起動します。
# service mysqld restartrsyslog のログを格納するデータベースを作成します。rsyslog-mysql をインストールすると SQL スクリプトが配置されるのでそれを利用します。
以下のようにSQLスクリプト実行すると、データベース(Syslog)が作成されます。
# mysql < /usr/share/doc/rsyslog-mysql-5.8.10/createDB.sql上記で作成したデータベース(Syslog)にアクセスするためのユーザ(syslog)を作成します。
以下の例ではパスワードを "p@ssw0rd" にしています。
# mysql Syslog Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 390 Server version: 5.1.73 Source distribution Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | Syslog | | mysql | | test | +--------------------+ 5 rows in set (0.00 sec) mysql> grant all privileges on Syslog.* to syslog@localhost identified by 'p@ssw0rd'; Query OK, 0 rows affected (0.07 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) mysql> exit Bye
3.rsyslog の設定変更
rsyslogd が MySQLにログを出力するように設定します。
/etc/rsyslog.conf を以下のように修正します。
青字はコメントを削除して有効にした行です。赤字は追加した行です。
# rsyslog v5 configuration file # For more information see /usr/share/doc/rsyslog-*/rsyslog_conf.html # If you experience problems, see http://www.rsyslog.com/doc/troubleshoot.html #### MODULES #### $ModLoad imuxsock # provides support for local system logging (e.g. via logger command) $ModLoad imklog # provides kernel logging support (previously done by rklogd) #$ModLoad immark # provides --MARK-- message capability # Provides UDP syslog reception $ModLoad imudp $UDPServerRun 514 # Provides TCP syslog reception $ModLoad imtcp $InputTCPServerRun 514 # rsyslog-mysql $ModLoad ommysql *.* :ommysql:127.0.0.1,Syslog,syslog,p@ssw0rd $AllowedSender UDP, 127.0.0.1, 192.168.1.0/24 $AllowedSender TCP, 127.0.0.1, 192.168.1.0/24 #### GLOBAL DIRECTIVES ####ommysql に設定した値の意味は以下のとおり。rsyslog のログを出力するデータベースの情報です。
- 127.0.0.1 ・・・ DBサーバ
- Syslog ・・・ データベース名
- syslog ・・・ DBユーザ
- p@ssw0rd ・・・ DBユーザのパスワード
4.動作確認
rsyslogd を再起動すると、MySQLにログが出力されます。
# service rsyslogd restartちゃんとログが格納されているか、データベースを参照してみます。
# mysql -u syslog -p Syslog Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 399 Server version: 5.1.73 Source distribution Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> select ID, ReceivedAt, Facility, Priority, FromHost, Message from SystemEvents Limit 10; +----+---------------------+----------+----------+----------+-----------------------------------------------------------------------------------------------------+ | ID | ReceivedAt | Facility | Priority | FromHost | Message | +----+---------------------+----------+----------+----------+-----------------------------------------------------------------------------------------------------+ | 1 | 2014-07-10 08:43:09 | 0 | 6 | centos65 | imklog 5.8.10, log source = /proc/kmsg started. | | 2 | 2014-07-10 08:43:09 | 5 | 6 | centos65 | [origin software="rsyslogd" swVersion="5.8.10" x-pid="1540" x-info="http://www.rsyslog.com"] start | | 3 | 2014-07-10 08:47:43 | 1 | 6 | centos65 | Installed: apr-1.3.9-5.el6_2.x86_64 | | 4 | 2014-07-10 08:47:44 | 1 | 6 | centos65 | Installed: apr-util-1.3.9-3.el6_0.1.x86_64 | | 5 | 2014-07-10 08:47:44 | 1 | 6 | centos65 | Installed: apr-util-ldap-1.3.9-3.el6_0.1.x86_64 | | 6 | 2014-07-10 08:47:45 | 1 | 6 | centos65 | Installed: httpd-tools-2.2.15-30.el6.centos.x86_64 | | 7 | 2014-07-10 08:47:45 | 1 | 6 | centos65 | Installed: mailcap-2.1.31-2.el6.noarch | | 8 | 2014-07-10 08:47:45 | 10 | 6 | centos65 | group added to /etc/group: name=apache, GID=48 | | 9 | 2014-07-10 08:47:45 | 10 | 6 | centos65 | group added to /etc/gshadow: name=apache | | 10 | 2014-07-10 08:47:45 | 10 | 6 | centos65 | new group: name=apache, GID=48 | +----+---------------------+----------+----------+----------+-----------------------------------------------------------------------------------------------------+ 10 rows in set (0.00 sec) mysql> exit Bye上記のようにログが参照できればOKです。
一応、DBのステータスを表示して、文字セットが utf8 であることを確認しておきます。
# mysql -u syslog -p Syslog Enter password: Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 403 Server version: 5.1.73 Source distribution Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> status -------------- mysql Ver 14.14 Distrib 5.1.73, for redhat-linux-gnu (x86_64) using readline 5.1 Connection id: 403 Current database: Syslog Current user: syslog@localhost SSL: Not in use Current pager: stdout Using outfile: '' Using delimiter: ; Server version: 5.1.73 Source distribution Protocol version: 10 Connection: Localhost via UNIX socket Server characterset: utf8 Db characterset: utf8 Client characterset: utf8 Conn. characterset: utf8 UNIX socket: /var/lib/mysql/mysql.sock Uptime: 10 hours 46 min 58 sec Threads: 3 Questions: 19073 Slow queries: 0 Opens: 39 Flush tables: 1 Open tables: 24 Queries per second avg: 0.491 -------------- mysql> Bye