RHEL6でrsyslogが標準になるそうですね。syslog-ng派の自分としてはさみしい限りです。
というわけでひさしぶりにsyslog-ng使ってみました。
通常のsyslogサーバ機能に加えて以下のリモートログ受信機能を追加してます。
・リモートからのログを/var/log/remote/<ホスト名>/<ホスト名>.<日付>に出力
・エラーログは別途、/var/log/remote/all_error/all_error.<日付>に出力&通知メール飛ばす
インストール
EPELのRPM使います。
http://download.fedora.redhat.com/pub/epel/5/x86_64/repoview/syslog-ng.html
3.x系もあるのですが何が違うのか忘れたので気にしないことにします。
[root@log ~]# rpm -ivh http://download.fedora.redhat.com/pub/epel/5/x86_64/syslog-ng-2.1.4-1.el5.x86_64.rpm
[root@log ~]# /etc/init.d/syslog stop
カーネルロガーを停止中: [ OK ]
システムロガーを停止中: [ OK ]
[root@log ~]# /etc/init.d/syslog-ng start
Starting syslog-ng: [ OK ]
[root@log ~]# logger test
[root@log ~]# tail /var/log/messages
...
Nov 15 18:56:24 log mikeda: test
[root@log ~]# chkconfig syslog off
[root@log ~]# chkconfig syslog-ng on
[root@log ~]# vi /etc/logrotate.d/syslog
/var/log/messages /var/log/secure /var/log/maillog /var/log/spooler /var/log/boot.log /var/log/cron {
sharedscripts
postrotate
/bin/kill -HUP `cat /var/run/syslog-ng.pid 2> /dev/null` 2> /dev/null || true
# /bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true
# /bin/kill -HUP `cat /var/run/rsyslogd.pid 2> /dev/null` 2> /dev/null || true
endscript
}
設定
optinosをちょっといじってリモートログの設定追加しただけです。
options {
sync (0);
time_reopen (10);
log_fifo_size (1000);
long_hostnames (off);
use_dns (yes);
use_fqdn (no);
create_dirs (yes);
keep_hostname (no);
stats(86400);
dir_perm(0750);
perm(0640);
owner(root);
group(root);
};
source s_sys {
file ("/proc/kmsg" log_prefix("kernel: "));
unix-stream ("/dev/log");
internal();
# udp(ip(0.0.0.0) port(514));
};
destination d_cons { file("/dev/console"); };
destination d_mesg { file("/var/log/messages"); };
destination d_auth { file("/var/log/secure"); };
destination d_mail { file("/var/log/maillog" sync(10)); };
destination d_spol { file("/var/log/spooler"); };
destination d_boot { file("/var/log/boot.log"); };
destination d_cron { file("/var/log/cron"); };
destination d_kern { file("/var/log/kern"); };
destination d_mlal { usertty("*"); };
filter f_kernel { facility(kern); };
filter f_default { level(info..emerg) and
not (facility(mail)
or facility(authpriv)
or facility(cron)); };
filter f_auth { facility(authpriv); };
filter f_mail { facility(mail); };
filter f_emergency { level(emerg); };
filter f_news { facility(uucp) or
(facility(news)
and level(crit..emerg)); };
filter f_boot { facility(local7); };
filter f_cron { facility(cron); };
#log { source(s_sys); filter(f_kernel); destination(d_cons); };
log { source(s_sys); filter(f_kernel); destination(d_kern); };
log { source(s_sys); filter(f_default); destination(d_mesg); };
log { source(s_sys); filter(f_auth); destination(d_auth); };
log { source(s_sys); filter(f_mail); destination(d_mail); };
log { source(s_sys); filter(f_emergency); destination(d_mlal); };
log { source(s_sys); filter(f_news); destination(d_spol); };
log { source(s_sys); filter(f_boot); destination(d_boot); };
log { source(s_sys); filter(f_cron); destination(d_cron); };
### リモートログ
source s_remote { udp(ip(0.0.0.0) port(514)); };
destination d_remote { file("/var/log/remote/$HOST/$HOST.$YEAR$MONTH$DAY"); };
destination d_remote_err {
file("/var/log/remote/all_error/all_error.$YEAR$MONTH$DAY");
# 実際はもっとちゃんとした外部プログラムを書く
program("while read l;do echo $l | mail -s alert mikeda@sample.jp;done");
};
# level(err..emerg)でも。お好みで
filter f_remote_err { match("(?i)(error|fail)"); };
log { source(s_remote); destination(d_remote); };
log { source(s_remote); filter(f_remote_err); destination(d_remote_err); };
ちょっとハマったのはリモート機器側でホスト名つけて送ってくるとその名前でディレクトリができちゃうことでした。
それを無視してサーバ側でIPからDNSを引かせるにはkeep_hostname (no);とuse_dns (yes);を併用すればいけました。
(@kjmさんありがとうございました。)