一.希望达到的目的
1.服务器日志集中存放到MySQL数据库中;
2.每天发送一封E-MAIL, 报告异常日志条目;
3.实时报告异常系统事件;
4.WEB界面查询日志;
二.日志主机安装和配置
各个软件的功能及交互图
1.安装syslog-ng:
安装前准备:
#yum -y install openssl*
#rpm -qa|grep openssl
openssl097a-0.9.7a-9.el5_2.1
- openssl-0.9.8b-10.el5_2.1
- openssl-devel-0.9.8b-10.el5_2.1
- openssl-perl-0.9.8b-10.el5_2.1
复制代码
安装glib:
注:编译syslog-ng的时候会用到glib库,而系统自带的库版本太低所以要安装高的版本
(抓狂
:我用的syslog-ng是 3.0.1版本他只认glib-2.12.3,所以大家下载时不知道下载什么版本时先编译一下就知道了……)
下载地址: http://ftp.gnome.org/pub/gnome/sources/glib/
#./configure --prefix=/usr && make && make install
安装eventlog:
下载地址: http://www.balabit.com/downloads/files/eventlog/0.2/
# ./configure --prefix=/usr/local/eventlog && make && make install
安装 libol:
下载地址: http://www.balabit.com/downloads/files/libol/
# ./configure ?prefix=/usr/local/libol &&make && make install
安装 syslog-ng:
下载地址: http://www.balabit.com/downloads/files/syslog-ng/sources/
# export PKG_CONFIG_PATH=/usr/local/eventlog/lib/pkgconfig/
# ./configure ?prefix=/usr/local/syslog-ng --with-libol=/usr/local/libol/bin
# make && make install
配置 syslog-ng:
# vi /usr/local/syslog-ng/etc/syslog-ng/syslog-ng.conf
- options {
- keep_hostname(yes);
- long_hostnames(off);
- sync(1);
- log_fifo_size(1024);
- create_dirs(yes); # if a dir does not exist create it
- owner(root); # owner of created files
- group(root); # group of created files
- perm(0600); # permissions of created files
- dir_perm(0700); # permissions of created dirs
- };
- source s_all {
- udp(); # remote logs # arriving at 514/udp
- unix-stream("/dev/log"); # local system logs
- file("/proc/kmsg"); # local kernel logs
- internal(); # internal syslog-ng logs
- };
- destination single-file {
- file("/var/log/syslog-ng/all-messages");
- };
- log {
- source(s_all);
- destination(single-file);
- };
复制代码
启动 syslog-ng:
# /etc/init.d/syslog stop
# /usr/local/syslog-ng/sbin/syslog-ng
验证:
#ps ?ef|grep syslog-ng
查看文件:
#tail /var/log/syslog-ng/all-messages
2. 安装MySQL到 /usr/local/mysql 下,以存放日志。
3. 安装 SQLSyslogd, 以便将日志写入 MySQL 数据库中:
SQLSyslogd附件:
sqlsyslogd.tgz (2.36 KB, 下载次数: 77)
# ln ?s /usr/local/mysql/lib/mysql /usr/bin
# ln ?s /usr/local/mysql/include/mysql /usr/local/include/lib
# find / -name libmysqlclient*
/usr/local/src/mysql-5.0.51b/debian/libmysqlclientSLIB-dev.files.in
- /usr/local/mysql/lib/mysql/libmysqlclient.so
- /usr/local/mysql/lib/mysql/libmysqlclient_r.so.15
- /usr/local/mysql/lib/mysql/libmysqlclient.so.15.0.0
- /usr/local/mysql/lib/mysql/libmysqlclient.so.15
- /usr/local/mysql/lib/mysql/libmysqlclient_r.la
- /usr/local/mysql/lib/mysql/libmysqlclient_r.so.15.0.0
- /usr/local/mysql/lib/mysql/libmysqlclient_r.a
- /usr/local/mysql/lib/mysql/libmysqlclient.a
- /usr/local/mysql/lib/mysql/libmysqlclient_r.so
- /usr/local/mysql/lib/mysql/libmysqlclient.la
复制代码
#ln -s /usr/local/mysql/lib/mysql/libmysqlclient.so.15 /usr/lib
# make && cp sqlsyslogd /usr/local/sbin/
# cat “/usr/local/lib/mysql” >> /etc/ld.so.conf && ldconfig
验证 sqlsyslogd 能正常运行:# sqlsyslogd //如果正常,你可以看到 sqlsyslogd 的帮助信息。
修改 sqlsyslogd.sql 文件:
# vi sqlsyslogd.sql
create database sqlsyslogd;
- use sqlsyslogd;
- create table logs (
- Id int(10) NOT NULL auto_increment,
- Timestamp varchar(16),
- Host varchar(50),
- Prog varchar(50),
- Mesg text,
- PRIMARY KEY (id)
- );
- use mysql;
- create user sqlsyslogd@localhost identified by ‘foo’;
- grant all on sqlsyslogd.* to sqlsyslogd;
- flush privileges;
复制代码
# sql ?u root ?p < sqlsyslogd.sql
# vi /usr/local/etc/sqlsyslogd.conf
foo
修改 syslog-ng.conf 文件,在最后添加下面几行:
destination mysql {
- program(“/usr/local/sbin/sqlsyslogd ?u sqlsyslogd \
- ?t logs sqlsyslogd ?p”);
- };
- log {
- source(all);
- destination(mysql);
- };
复制代码
重启 syslog-ng:
# pkill ?SIGHUP syslog-ng
#/usr/local/syslog-ng/sbin/syslog-ng
验证:现在你应该可以通过 MySQL 客户端软件查看日志了。
4.安装 logcheck 和 newlogcheck:
从 http://sourceforge.net/project/showfiles.php?group_id=100960 下载 logcheck,
从 http://www.campin.net/download/newlogcheck.tgz 下载 newlogcheck.tgz
安装:
# mkdir ?p /usr/local/logcheck/bin /usr/local/logcheck/etc /usr/local/logcheck/tmp
#tar zvxf logcheck-1.1.2.tar.gz
# cd logcheck-1.1.2
修改 Makefile 文件,将其中:
INSTALLDIR = /usr/local/etc
- INSTALLDIR_BIN = /usr/local/bin
- INSTALLDIR_SH = /usr/local/etc
- TMPDIR = /usr/local/etc/tmp
复制代码
改为:
INSTALLDIR = /usr/local/logcheck/etc
- INSTALLDIR_BIN = /usr/local/logcheck/bin
- INSTALLDIR_SH = /usr/local/logcheck/etc
- TMPDIR = /usr/local/logcheck/etc/tmp
复制代码
# make linux
修改logcheck.sh文件:
LOGTAIL=/usr/local/bin/logtail
- TMPDIR=/usr/local/etc/tmp
- HACKING_FILE=/usr/local/etc/logcheck.hacking
- VIOLATIONS_FILE=/usr/local/etc/logcheck.violations
- VIOLATIONS_IGNORE_FILE=/usr/local/etc/logcheck.violations.ignore
- IGNORE_FILE=/usr/local/etc/logcheck.ignore
复制代码
改为:
LOGTAIL=/usr/local/bin/logtail
- TMPDIR=/usr/local/etc/tmp
- HACKING_FILE=/usr/local/etc/logcheck.hacking
- VIOLATIONS_FILE=/usr/local/etc/logcheck.violations
- VIOLATIONS_IGNORE_FILE=/usr/local/etc/logcheck.violations.ignore
- IGNORE_FILE=/usr/local/etc/logcheck.ignore
复制代码
#tar zvxf newlogcheck.tgz
# cd newlogcheck
#cp *\.* /usr/local/logcheck/etc/
配置:根据logcheck各个文件的位置修改 /usr/local/logcheck/etc/目录下的 newlogcheck.sh 和 sort_logs.pl
#vi /usr/local/logcheck/etc/newlogcheck.sh
SYSADMIN=root,***@***.com //多个邮箱地址用逗号隔开
- LOGTAIL=/usr/local/bin/logtail
- BASEDIR=/usr/local/logcheck
复制代码
# vi /usr/local/logcheck/etc/sort_logs.pl
my $LOGCHECK_DIR = "/usr/local/logcheck";
复制代码
# mkdir /usr/local/logcheck/tmp/host
测试:
# /usr/local/logcheck/etc/newlogcheck.sh
如果安装正常,你应该收到一封e-mail, 现在你可以添加一个crontab 来自动化logcheck日志
# crontab ?e
…………
0 0 * * * /usr/local/logcheck/etc/newlogcheck.sh
5.安装 swatch:
按顺序安装下面的perl 模块:Carp-Clan, Bit-Vector, Date-Calc,Time-HiRes,File-Tail,TimeDate, swatch(使用3.0.8版本,不要用3.0.12)
配置 swatch:
#vi /etc/swatch.conf
watchfor /Failed password/
-
- mail address=root, subject=warning: Failed password
-
- throttle 01:00
-
- watchfor /Invalid user/
-
- mail address=root, subject=warning: Invalid user
-
- throttle 01:00
-
- watchfor /authentication failure/
-
- mail address=root, subject=warning: authentication failure
-
- throttle 01:00
-
- watchfor /iptables:/
-
- mail address=root, subject=warning: iptables operation
-
- throttle 01:00
-
- watchfor /Duplicate address/
-
- mail address=root, subject=warning: Duplicate address
-
- throttle 01:00
-
- watchfor /file system full/
-
- mail address=root, subject=warning: file system full
-
- throttle 01:00
-
- watchfor /(panic|halt)/
-
- mail address=root, subject=warning: panic or halt happened
-
- throttle 01:00
-
- watchfor /Media Error/
-
- mail address=root, subject=warning: disk error happened
-
- throttle 01:00
-
- ……
复制代码
运行swatch:
# /usr/bin/swatch --config-file=/etc/swatch.conf --tail-file=/var/log/syslog-ng/all-messages &
验证:
#ps ?ef|grep swatch | grep ?v grep
如果正常,你应该看到2个进程:
root ...... /usr/bin/perl /usr/bin/swatch --config-file=/etc/swatch.conf --tail-file=/var/log/syslog-ng/all-messages
- root ...... /usr/bin/perl /root/.swatch_script.17374 ß //你看到的将是其他数字
复制代码
6.安装 splunk:
从 http://download.splunk.com/relea ... 1113-Linux-i686.tgz 下载 splunk
解压 splunk并将其移动到 /usr/local/splunk
修改 /usr/local/splunk/bin/setSplunkEvn 和 /usr/local/splunk/etc/init.d/redhat/splunk
将其中的 $SPLUNK_HOME 设置为 /usr/local/splunk
- 将/usr/local/splunk/etc/init.d/redhat/splunk 拷贝到/etc/init.d/目录下
复制代码
# chmod 700 /etc/init.d/splunk && chkconfig splunk on
启动splunk:
#/etc/init.d/splunk start
现在你可以通过 WEB 浏览器访问 splunk 了:http://localhost:8000/
配置:
现在你可以搜索日志了.
7.配置启动:vi /etc/rc.local
……
- /usr/local/syslog-ng/sbin/syslog-ng
- /usr/bin/swatch ?config-file=/etc/swatch.conf ?tail-file=/var/syslog-ng/all-messages &
复制代码
三.日志客户机配置
这里所说的日志客户机指需要把自己的日志传送给日志主机的服务器。
我们在日志客户机上的配置很简单,只需要两步:
1. 在 /etc/syslog.conf 文件添加一项,使日志客户机把自己的严重程度在.info以上的日志也发送给日志主机:
# vi /etc/syslog.conf
2. 在 /etc/hosts 文件里面添加一项,使日志客户机能解析上面的 log_host:
# cat /etc/hosts
总结,配置一个日志主机,需要花费较大的功夫。但是,一旦你配置好了,它给你带来的益处也很大,比如,你能够随时查阅很久以前的日志,你能及时知道某台服务器的硬盘有问题了,如果有黑客入侵了你的服务器,不管他如何销毁自己的踪迹,你都能够他在入侵过程中留下的痕迹,等等等等。所以,花大力气建立这样一个日志主机还是明智的。
转自: http://bbs.linuxtone.org/thread-1874-1-1.html