Quantcast
Channel: データベース –俺的備忘録 〜なんかいろいろ〜
Viewing all articles
Browse latest Browse all 33

CentOS 7でrsyslogのログをMariaDBに出力させる

$
0
0

個人的には、ログはテキストファイルの方が色々と都合が良い(後からの統計取得だったり検知・加工してエラー確認とかシェル芸でできるので)のだが、諸々の事情(セキュリティ的な色々だったり、コマンド操作があまりできない人でも使える状態にしたいなど)によって、テキストファイルでの出力や保管、閲覧が都合が悪い事がある。で、rsyslogではログをDBに出力可能なので、今回は、OSにはCentOS 7を、出力先のDBにはローカルにインストールしたMariaDBを用いる。

1.MariaDBのインストール

まずはMariaDB自体のインストールから。以下のコマンドでインストールする。

yum install -y mariadb-server
systemctl start mariadb

 

インストール後、以下のコマンドでセキュリティ設定を行う。

mysql_secure_installation

 

2.rsyslogの設定

次にrsyslogの設定を行う。まず、rsyslogでMariaDBとの連携を行うためのプラグインの導入を行う。

yum install -y rsyslog-mysql

 

プラグインの導入後、以下のコマンドを実行してMariaDBにrsyslog用のデータベースやユーザを作成する。

cat /usr/share/doc/rsyslog-*/mysql-createDB.sql | mysql -u root -p
mysql -u root -p -e "GRANT ALL PRIVILEGES ON Syslog.* TO rsyslog@'localhost' identified by 'パスワード';"

MariaDBの設定ファイルを編集し、DBに出力したいログについて、設定を追記・編集する。
以下の例では、ファシリティを制限せずにすべてのログを対象として設定している。ハイライトされている箇所を追記してやれば良い。

●/etc/rsyslog.conf

# rsyslog 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 ####

# The imjournal module bellow is now used as a message source instead of imuxsock.
$ModLoad imuxsock # provides support for local system logging (e.g. via logger command)
$ModLoad imjournal # provides access to the systemd journal
#$ModLoad imklog # reads kernel messages (the same are read from journald)
#$ModLoad immark  # provides --MARK-- message capability
$ModLoad ommysql # 追記

# Provides UDP syslog reception
#$ModLoad imudp
#$UDPServerRun 514

# Provides TCP syslog reception
#$ModLoad imtcp
#$InputTCPServerRun 514


#### GLOBAL DIRECTIVES ####

# Where to place auxiliary files
$WorkDirectory /var/lib/rsyslog

# Use default timestamp format
$ActionFileDefaultTemplate RSYSLOG_TraditionalFileFormat

# File syncing capability is disabled by default. This feature is usually not required,
# not useful and an extreme performance hit
#$ActionFileEnableSync on

# Include all config files in /etc/rsyslog.d/
$IncludeConfig /etc/rsyslog.d/*.conf

# Turn off message reception via local log socket;
# local messages are retrieved through imjournal now.
$OmitLocalLogging on

# File to store the position in the journal
$IMJournalStateFile imjournal.state


#### RULES ####

# Log all kernel messages to the console.
# Logging much else clutters up the screen.
#kern.*                                                 /dev/console

# Log anything (except mail) of level info or higher.
# Don't log private authentication messages!
*.info;mail.none;authpriv.none;cron.none                /var/log/messages

# The authpriv file has restricted access.
authpriv.*                                              /var/log/secure

# Log all the mail messages in one place.
mail.*                                                  -/var/log/maillog


# Log cron stuff
cron.*                                                  /var/log/cron

# Everybody gets emergency messages
*.emerg                                                 :omusrmsg:*

# Save news errors of level crit and higher in a special file.
uucp,news.crit                                          /var/log/spooler

# Save boot messages also to boot.log
local7.*                                                /var/log/boot.log

*.*                                                     :ommysql:localhost,Syslog,rsyslog,パスワード

# ### begin forwarding rule ###
# The statement between the begin ... end define a SINGLE forwarding
# rule. They belong together, do NOT split them. If you create multiple
# forwarding rules, duplicate the whole block!
# Remote Logging (we use TCP for reliable delivery)
#
# An on-disk queue is created for this action. If the remote host is
# down, messages are spooled to disk and sent when it is up again.
#$ActionQueueFileName fwdRule1 # unique name prefix for spool files
#$ActionQueueMaxDiskSpace 1g   # 1gb space limit (use as much as possible)
#$ActionQueueSaveOnShutdown on # save messages to disk on shutdown
#$ActionQueueType LinkedList   # run asynchronously
#$ActionResumeRetryCount -1    # infinite retries if host is down
# remote host is: name/ip:port, e.g. 192.168.0.1:514, port optional
#*.* @@remote-host:514
# ### end of the forwarding rule ###

 

設定ファイル編集後、rsyslogのサービスを再起動する。

systemctl restart rsyslog

 

これで、ログがMariaDBに転送されるようになったはずだ。

 

3.ログの確認

最後に、DBにアクセスしてログの記録状態を確認する。
mysqlコマンドでデータベースにアクセスし、以下のsqlを実行してやる。

select ReceivedAt,
       Facility,
       Priority,
       SysLogTag,
       FromHost,
       Message
from SystemEvents;
[root@BS-PUB-CENT7-01 ~]# mysql -u rsyslog -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 MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 17
Server version: 5.5.52-MariaDB MariaDB Server

Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [Syslog]> select ReceivedAt,
    ->        Facility,
    ->        Priority,
    ->        SysLogTag,
    ->        FromHost,
    ->        Message
    -> from SystemEvents;
+---------------------+----------+----------+-------------------------------------+-----------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| ReceivedAt          | Facility | Priority | SysLogTag                           | FromHost        | Message                                                                                                                                                                                           |
+---------------------+----------+----------+-------------------------------------+-----------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| 2017-03-27 09:20:50 |        5 |        6 | rsyslogd:                           | BS-PUB-CENT7-01 |  [origin software="rsyslogd" swVersion="7.4.7" x-pid="19561" x-info="http://www.rsyslog.com"] start                                                                                               |
| 2017-03-27 09:20:50 |        3 |        4 | systemd:                            | BS-PUB-CENT7-01 | Cannot add dependency job for unit microcode.service, ignoring: Unit is not loaded properly: Invalid argument.                                                                                    |
| 2017-03-27 09:20:50 |        3 |        6 | systemd:                            | BS-PUB-CENT7-01 | Stopping System Logging Service...                                                                                                                                                                |
| 2017-03-27 09:20:50 |        3 |        6 | systemd:                            | BS-PUB-CENT7-01 | Starting System Logging Service...                                                                                                                                                                |
| 2017-03-27 09:20:50 |        3 |        6 | systemd:                            | BS-PUB-CENT7-01 | Started System Logging Service.                                                                                                                                                                   |
| 2017-03-27 09:20:50 |       10 |        5 | polkitd[606]:                       | BS-PUB-CENT7-01 | Unregistered Authentication Agent for unix-process:19554:3646650 (system bus name :1.48, object path /org/freedesktop/PolicyKit1/AuthenticationAgent, locale ja_JP.UTF-8) (disconnected from bus) |
| 2017-03-27 10:01:02 |        3 |        6 | systemd:                            | BS-PUB-CENT7-01 | Started Session 12 of user root.                                                                                                                                                                  |
| 2017-03-27 10:01:02 |        3 |        6 | systemd:                            | BS-PUB-CENT7-01 | Starting Session 12 of user root.                                                                                                                                                                 |
| 2017-03-27 10:01:02 |        9 |        6 | CROND[19582]:                       | BS-PUB-CENT7-01 | (root) CMD (run-parts /etc/cron.hourly)                                                                                                                                                           |
| 2017-03-27 10:01:02 |        9 |        5 | run-parts(/etc/cron.hourly)[19582]: | BS-PUB-CENT7-01 | starting 0anacron                                                                                                                                                                                 |
| 2017-03-27 10:01:02 |        9 |        5 | run-parts(/etc/cron.hourly)[19591]: | BS-PUB-CENT7-01 | finished 0anacron                                                                                                                                                                                 |
| 2017-03-27 11:01:01 |        3 |        6 | systemd:                            | BS-PUB-CENT7-01 | Started Session 13 of user root.                                                                                                                                                                  |
| 2017-03-27 11:01:01 |        3 |        6 | systemd:                            | BS-PUB-CENT7-01 | Starting Session 13 of user root.                                                                                                                                                                 |
| 2017-03-27 11:01:01 |        9 |        6 | CROND[19609]:                       | BS-PUB-CENT7-01 | (root) CMD (run-parts /etc/cron.hourly)                                                                                                                                                           |
| 2017-03-27 11:01:01 |        9 |        5 | run-parts(/etc/cron.hourly)[19609]: | BS-PUB-CENT7-01 | starting 0anacron                                                                                                                                                                                 |
| 2017-03-27 11:01:01 |        9 |        5 | run-parts(/etc/cron.hourly)[19618]: | BS-PUB-CENT7-01 | finished 0anacron                                                                                                                                                                                 |
| 2017-03-27 12:01:01 |        3 |        6 | systemd:                            | BS-PUB-CENT7-01 | Started Session 14 of user root.                                                                                                                                                                  |
| 2017-03-27 12:01:01 |        3 |        6 | systemd:                            | BS-PUB-CENT7-01 | Starting Session 14 of user root.                                                                                                                                                                 |
| 2017-03-27 12:01:01 |        9 |        6 | CROND[19635]:                       | BS-PUB-CENT7-01 | (root) CMD (run-parts /etc/cron.hourly)                                                                                                                                                           |
| 2017-03-27 12:01:01 |        9 |        5 | run-parts(/etc/cron.hourly)[19635]: | BS-PUB-CENT7-01 | starting 0anacron

…

 

これで、rsyslogからのログをMariaDBへ転送できるようになった。
今回の例ではローカルホストに同居させたが、設定してやればリモートのDBにログを転送させることも可能だ。

 

詳解MySQL 5.7 止まらぬ進化に乗り遅れないためのテクニカルガイド (NEXT ONE) 詳解MySQL 5.7 止まらぬ進化に乗り遅れないためのテクニカルガイド (NEXT ONE)

Viewing all articles
Browse latest Browse all 33

Trending Articles