2013年3月17日日曜日

rsyslog + MySQL

2014/11/07追記: ”データベースの作成の箇所”に赤字でコメントを追加


シスログをMySQLに書き出す方法です。

今回の環境はCentOS6.4+rsyslogのVer7.2.6になり、こちらの記事のようにリポジトリを用意してある前提で話を進めます。


rsyslog-mysqlパッケージの導入

リポジトリに用意されているパッケージをインストールします。

[root@cent6 ~]# yum install rsyslog-mysql


データベースの作成

インストールしたrsyslog-mysqlパッケージに、シスログをMySQLに書き出す為に必要なデータベースとテーブルを作成するSQL(青字の部分)が含まれているので、今回はそれをそのまま利用します。
(2014/11/07追記:)
・SystemEventsテーブルのIDはint型で定義されていますが、受け取るログが多いと直ぐにあふれます。最初からbigint型にしておいたほうが良いと思います。
・デフォルトのMyISAMで運用していたら、比較的簡単に壊れました。最初からInnoDBにしておいたほうが無難です。
[root@cent6 ~]# rpm -ql rsyslog-mysql
/lib64/rsyslog/ommysql.so
/usr/share/doc/rsyslog-mysql-7.2.6
/usr/share/doc/rsyslog-mysql-7.2.6/createDB.sql
[root@cent6 ~]#

以下のように実行(インポート)します。
※この作業で、「Syslog」データベース、その中に2つのテーブルが作成されます


[root@cent6 ~]# mysql -u root < /usr/share/doc/rsyslog-mysql-7.2.6/createDB.sql
[root@cent6 ~]# mysql
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| Syslog             |
| mysql              |
+--------------------+
3 rows in set (0.00 sec)
mysql> use Syslog;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+------------------------+
| Tables_in_Syslog       |
+------------------------+
| SystemEvents           |
| SystemEventsProperties |
+------------------------+
2 rows in set (0.00 sec)
mysql>




ログ出力用ユーザの作成

rsyslogからMySQLへアクセスする為のユーザを以下のように作成します。
※ここでは、ユーザ名をrsyslog、パスワードをpasswordとして作成しています

mysql> grant all privileges on Syslog.* to rsyslog@localhost identified by 'password';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql>select host,user,password from mysql.user where user='rsyslog';
+-----------+---------+-------------------------------------------+
| host      | user    | password                                  |
+-----------+---------+-------------------------------------------+
| localhost | rsyslog | *2470C0C06DEE42FD1618BB99005ADCA2EC9D1E19 |
+-----------+---------+-------------------------------------------+
1 row in set (0.00 sec)
mysql>



rsyslogの設定

/etc/rsyslog.confにMySQLにシスログを出力する為の設定(以下の2行)を追記します。
※下記では、全てのログをMySQLに出力するよう設定しています
※MySQLに出力する為の書式は、:ommysql: / 接続ホスト / データベース名 / ユーザ名 / パスワード となります

module(load="ommysql")
*.*     :ommysql:localhost,Syslog,rsyslog,password

設定を追加したら、rsyslogを再起動します。



動作確認

rsyslogの再起動が完了すると、MySQLにログが格納されるようになります。
[root@cent6 ~]# mysql Syslog
mysql> show tables;
+------------------------+
| Tables_in_Syslog       |
+------------------------+
| SystemEvents           |
| SystemEventsProperties |
+------------------------+
2 rows in set (0.00 sec)
mysql> select * from SystemEvents \G
*************************** 1. row ***************************
                ID: 1
        CustomerID: NULL
        ReceivedAt: 2013-03-17 23:34:43
DeviceReportedTime: 2013-03-17 23:34:43
          Facility: 5
          Priority: 6
          FromHost: cent6
           Message:  [origin software="rsyslogd" swVersion="7.2.6" x-pid="18401" x-info="http://www.rsyslog.com"] start
        NTSeverity: NULL
        Importance: NULL
       EventSource: NULL
         EventUser: NULL
     EventCategory: NULL
           EventID: NULL
   EventBinaryData: NULL
      MaxAvailable: NULL
         CurrUsage: NULL
          MinUsage: NULL
          MaxUsage: NULL
        InfoUnitID: 1
         SysLogTag: rsyslogd:
      EventLogType: NULL
   GenericFileName: NULL
          SystemID: NULL
1 row in set (0.00 sec)
mysql>



2013年3月8日金曜日

SSH パスフレーズの変更

暗号鍵にアクセスする為のパスフレーズを変更する方法です。


元のパスフレーズがわかる場合

$ ssh-keygen -p
Enter file in which the key is (/home/user01/.ssh/id_rsa):   <-鍵の場所が聞かれる
Enter old passphrase:  <-古いパスフレーズを入力
Key has comment '/home/user01/.ssh/id_rsa'
Enter new passphrase (empty for no passphrase): <-新しいパスフレーズを入力
Enter same passphrase again:               <-新しいパスフレーズを入力
Your identification has been saved with the new passphrase.
$


元のパスフレーズがわからない場合

$ ssh-keygen -t rsa -f .ssh/id_rsa -N "新しいパスフレーズ"
Generating public/private rsa key pair.
.ssh/id_rsa already exists.
Overwrite (y/n)? y  <-上書きについて確認される
Your identification has been saved in .ssh/id_rsa.
Your public key has been saved in .ssh/id_rsa.pub.
The key fingerprint is:
b9:6e:a5:bc:6d:db:80:ed:56:cd:bb:d9:73:39:e4:c8 root@pm02
The key's randomart image is:
+--[ RSA 2048]----+
|                 |
|                 |
|                 |
|         .       |
|        S    o   |
|         +. . o. |
|       .ooo.. +..|
|       .+ooo E.B.|
|       .o++.. o.=|
+-----------------+
$

※こちらのケースでは、秘密鍵と共に公開鍵も更新されるので、..ssh/authorized_keysの更新も忘れずに。

2013年3月4日月曜日

postfwd (postfix用ポリシーサーバ)

2013/03/04: 初版
2013/04/07: 「起動スクリプト」の赤字部分の箇所を追加(というか修正)



postfwdは、perlで書かれたpostfix用ポリシーサーバーで、複数の条件を組み合わせたACLが作成可能なので、postfix単体では実現できなかったSPAM対策に利用する事ができます。

今回は、そのセットアップについて記載します(といっても難しいところはないのですが)。
#評価環境はCentOS6.3
#postfwdには、version1 と version2が存在しますが、今回はversion2を利用


事前準備

動作に必要な下記のperlモジュールをインストールしておく。
・Net::Server::Daemonize
・Net::Server::Multiplex
・Net::Server::PreFork
 →上記3つはEPELリポジトリのperl-Net-Server含まれるので、そちらをインストールした
・Net::DNS
 →Baseリポジトリからperl-Net-DNSをインストールした


インストール

tarボールの中に、version1とversion2の両方のものが含まれているが、今回はversion2用のものを任意のディレクトリにコピーする形でインストールを実施する。
※なお、設定ファイルpostfwd.cfはひとまず空ファイルで用意しています

[root@cent6 ~]# wget http://postfwd.org/postfwd.tar.gz
[root@cent6 ~]# tar zxf postfwd.tar.gz
[root@cent6 ~]# cd postfwd-1.34/
[root@cent6 postfwd-1.34]# cp sbin/postfwd2 /usr/local/sbin
[root@cent6 postfwd-1.34]# cp bin/postfwd-script.sh /etc/rc.d/init.d/postfwd2
[root@cent6 postfwd-1.34]# touch /etc/postfix/postfwd.cf


起動スクリプトの修正

インストールした状況に合わせて、以下のように修正する。
→修正後のdiff出力を参照ください

[root@cent6 postfwd-1.34]# vi /etc/rc.d/init.d/postfwd2
[root@cent6 postfwd-1.34]# diff -u bin/postfwd-script.sh /etc/rc.d/init.d/postfwd2
--- bin/postfwd-script.sh       2010-11-15 07:03:42.000000000 +0900
+++ /etc/rc.d/init.d/postfwd2   2013-04-07 21:45:26.224068602 +0900
@@ -3,22 +3,27 @@
 # Startscript for the postfwd daemon
 #
 # by JPK
+# chkconfig: 35 75 25
+# description: postfwd is written in perl to combine complex postfix restrictions
+#              in a ruleset similar to those of the most firewalls.
 PATH=/bin:/usr/bin:/usr/local/bin
 # path to program
-#PFWCMD=/usr/local/postfwd/sbin/postfwd2
-PFWCMD=/usr/local/postfwd/sbin/postfwd
+#PFWCMD=/usr/local/postfwd/sbin/postfwd
+PFWCMD=/usr/local/sbin/postfwd2
 # rulesetconfig file
 PFWCFG=/etc/postfix/postfwd.cf
 # pidfile
-PFWPID=/var/tmp/postfwd.pid
+#PFWPID=/var/tmp/postfwd.pid
+PFWPID=/var/tmp/postfwd2-master.pid
 # daemon settings
 PFWUSER=nobody
 PFWGROUP=nobody
 PFWINET=127.0.0.1
-PFWPORT=10040
+#PFWPORT=10040
+PFWPORT=10045
 # recommended extra arguments
 PFWARG="--shortlog --summary=600 --cache=600 --cache-rbl-timeout=3600 --cleanup-requests=1200 --cleanup-rbls=1800 --cleanup-rates=1200"
@@ -40,7 +45,7 @@
        stop*)          ${PFWCMD} --interface=${PFWINET} --port=${PFWPORT} --pidfile=${PFWPID} --kill;
                        ;;
-       reload*)        ${PFWCMD} --interface=${PFWINET} --port=${PFWPORT} --pidfile=${PFWPID} -- reload;
+       reload*)        ${PFWCMD} --interface=${PFWINET} --port=${PFWPORT} --pidfile=${PFWPID} --reload;
                        ;;
        restart*)       $0 stop;
[root@cent6 postfwd-1.34]#




自動起動の設定と起動

[root@cent6 postfwd-1.34]# chkconfig --add postfwd2
[root@cent6 postfwd-1.34]# chkconfig postfwd2 on
[root@cent6 postfwd-1.34]# /etc/rc.d/init.d/postfwd2 start
Starting postfwd2...
[root@cent6 postfwd-1.34]#



状態確認


[root@cent6 ~]# ps -fC postfwd2
UID        PID  PPID  C STIME TTY          TIME CMD
nobody    1201     1  0 23:51 ?        00:00:00 /usr/local/sbin/postfwd2 --shortlog --summary=600 --cache=600 --cache-rbl-timeout=3600
nobody    1202  1201  0 23:51 ?        00:00:00  postfwd2::cache
nobody    1203  1201  0 23:51 ?        00:00:00  postfwd2::policy
nobody    1204  1203  0 23:51 ?        00:00:00  postfwd2::policy::child
nobody    1205  1203  0 23:51 ?        00:00:00  postfwd2::policy::child
nobody    1206  1203  0 23:51 ?        00:00:00  postfwd2::policy::child
nobody    1207  1203  0 23:51 ?        00:00:00  postfwd2::policy::child
nobody    1208  1203  0 23:51 ?        00:00:00  postfwd2::policy::child
nobody    1209  1203  0 23:51 ?        00:00:00  postfwd2::policy::child
nobody    1210  1203  0 23:51 ?        00:00:00  postfwd2::policy::child
nobody    1211  1203  0 23:51 ?        00:00:00  postfwd2::policy::child
nobody    1212  1203  0 23:51 ?        00:00:00  postfwd2::policy::child
nobody    1213  1203  0 23:51 ?        00:00:00  postfwd2::policy::child
[root@cent6 ~]#

[root@cent6 postfwd-1.34]# netstat -antp | grep postfwd2
tcp        0      0 127.0.0.1:10045             0.0.0.0:*                   LISTEN      1288/ postfwd2:
[root@cent6 postfwd-1.34]#



また、メールログには以下のように出力されている。

Mar  3 23:51:13 cent6 postfwd2/master[1200]: postfwd2 1.34 starting
Mar  3 23:51:13 cent6 postfwd2/master[1201]: Started cache at pid 1202
Mar  3 23:51:13 cent6 postfwd2/master[1202]: 2013/03/03-23:51:13 postfwd2::cache (type Net::Server::Multiplex) starting! pid(1202)
Mar  3 23:51:13 cent6 postfwd2/master[1201]: Started server at pid 1203
Mar  3 23:51:13 cent6 postfwd2/master[1203]: 2013/03/03-23:51:13 postfwd2::server (type Net::Server::PreFork) starting! pid(1203)
Mar  3 23:51:13 cent6 postfwd2/master[1202]: Binding to UNIX socket file /var/tmp/postfwd2-cache.socket using SOCK_STREAM#012
Mar  3 23:51:13 cent6 postfwd2/cache[1202]: ready for input
Mar  3 23:51:13 cent6 postfwd2/master[1203]: Binding to TCP port 10045 on host 127.0.0.1#012
Mar  3 23:51:13 cent6 postfwd2/policy[1203]: critical: no rules found - i feel useless (have you set -f or -r?)
Mar  3 23:51:13 cent6 postfwd2/policy[1203]: ready for input



以上で、セットアップは完了です。
設定ファイルpostfwd.cfが空ファイルなので、実際は何の役にも立ってませんが.....
#設定は、次回書こうと思います。