用LINUX+SYSLOG-NG+PHP-SYSLOG-NG搭建日志服務器


轉自:http://blog.sina.com.cn/s/blog_4a071ed80100cssu.html

日志服務器安裝指南

 

一、希望達到的目的
       1.服務器日志集中存放到日志服務器和MySQL數據庫中;
       2.每天發送一封E-MAIL, 報告異常日志條目;
       3.實時報告異常系統事件;
       4.WEB界面查詢日志;
二、日志主機安裝和配置

1.安裝syslog-ng:
安裝前准備:(操作系統為:centos 4.7)
#cd /usr/local/src

#wget http://www.balabit.com/downloads/files/syslog-ng/sources/pkgs/dists/rhel-4/syslog-ng-ose-2.1.3/i386/RPMS.syslog-ng/syslog-ng-2.1.3-1.i386.rpm

#wget http://www.balabit.com/downloads/files/syslog-ng/sources/pkgs/dists/rhel-4/syslog-ng-ose-2.1.3/i386/RPMS.syslog-ng/libevtlog0-0.2.8-1.i386.rpm

#wget http://www.balabit.com/downloads/files/syslog-ng/sources/pkgs/dists/rhel-4/syslog-ng-ose-2.1.3/i386/RPMS.syslog-ng/libdbi8-dev-0.8.2bb2-3.rhel4.i386.rpm

#wget http://www.balabit.com/downloads/files/syslog-ng/sources/pkgs/dists/rhel-4/syslog-ng-ose-2.1.3/i386/RPMS.syslog-ng/libdbi8-0.8.2bb2-3.rhel4.i386.rpm

#wget http://www.balabit.com/downloads/files/syslog-ng/sources/pkgs/dists/rhel-4/syslog-ng-ose-2.1.3/i386/RPMS.syslog-ng/libdbd8-mysql-0.8.2bb5-3.rhel4.i386.rpm

 

配置/etc/yum.repos.d/CentOS-Base.repo

添加:

[dag]

name=Dag RPM Repostory for Red Hat Enterprise Linux

baseurl=http://apt.sw.be/redhat/el$releasever/en/$basearch/dag

gpgcheck=1

enabled=1

gpgkey=http://apt.sw.be/packages/RPM-GPG-KEY.dag.txt

 

開始安裝syslog-ng

#rpm -ivh libdb*

#rpm -ivh libevtlog0-0.2.8-1.i386.rpm

#rpm -ivh syslog-ng-2.1.3-1.i386.rpm

#rpm -ivh msttcorefonts-2.0-1.noarch.rpm

# mkdir -p /usr/share/fonts/truetype/msttcorefonts/

# cp /usr/X11R6/lib/X11/fonts/truetype/verdana* /usr/share/fonts/truetype/msttcorefonts/.

 

配置 syslog-ng:
# vi /etc/syslog-ng/syslog-ng.conf      
options {

long_hostnames(off);

log_msg_size(8192);

sync(1);

log_fifo_size(20480);

time_reopen(10);

use_dns(yes);

dns_cache(yes);

use_fqdn(yes);

keep_hostname(yes);

chain_hostnames(no);

perm(0644);

stats(43200);

};

source s_internal { internal(); };

destination d_syslognglog { file("/var/log/syslog-ng.log"); };

log { source(s_internal); destination(d_syslognglog); };

 

source s_local {

        unix-dgram("/dev/log");

        file("/proc/kmsg" log_prefix("kernel:"));

};

 

filter f_messages { level(info..emerg); };

filter f_secure { facility(authpriv); };

filter f_mail { facility(mail); };

filter f_cron { facility(cron); };

filter f_emerg { level(emerg); };

filter f_spooler { level(crit..emerg) and facility(uucp, news); };

filter f_local7 { facility(local7); };

 

destination d_messages { file("/var/log/messages"); };

destination d_secure { file("/var/log/secure"); };

destination d_maillog { file("/var/log/maillog"); };

destination d_cron { file("/var/log/cron"); };

destination d_console { usertty("root"); };

destination d_spooler { file("/var/log/spooler"); };

destination d_bootlog { file("/var/log/boot.log"); };

 

log { source(s_local); filter(f_emerg); destination(d_console); };

log { source(s_local); filter(f_secure); destination(d_secure); flags(final); };

log { source(s_local); filter(f_mail); destination(d_maillog); flags(final); };

log { source(s_local); filter(f_cron); destination(d_cron); flags(final); };

log { source(s_local); filter(f_spooler); destination(d_spooler); };

log { source(s_local); filter(f_local7); destination(d_bootlog); };

log { source(s_local); filter(f_messages); destination(d_messages); };

 

# Remote logging

source s_remote {

        tcp(ip(0.0.0.0) port(514));

        udp(ip(0.0.0.0) port(514));

};

 

destination r_console {file("/var/log/syslog-ng/$YEAR$MONTH$DAY/$HOST/console" owner("root") group("root") perm(0640) dir_perm(0750) create_dirs(yes));};

destination r_secure {file("/var/log/syslog-ng/$YEAR$MONTH$DAY/$HOST/secure" owner("root") group("root") perm(0640) dir_perm(0750) create_dirs(yes));};

destination r_cron {file("/var/log/syslog-ng/$YEAR$MONTH$DAY/$HOST/cron" owner("root") group("root") perm(0640) dir_perm(0750) create_dirs(yes));};

destination r_spooler {file("/var/log/syslog-ng/$YEAR$MONTH$DAY/$HOST/spooler" owner("root") group("root") perm(0640) dir_perm(0750) create_dirs(yes));};

destination r_bootlog {file("/var/log/syslog-ng/$YEAR$MONTH$DAY/$HOST/bootlog" owner("root") group("root") perm(0640) dir_perm(0750) create_dirs(yes));};

destination r_messages {file("/var/log/syslog-ng/$YEAR$MONTH$DAY/$HOST/messages" owner("root") group("root") perm(0640) dir_perm(0750) create_dirs(yes));};

 

log { source(s_remote); filter(f_emerg); destination(r_console); };

log { source(s_remote); filter(f_secure); destination(r_secure); flags(final); };

log { source(s_remote); filter(f_cron); destination(r_cron); flags(final); };

log { source(s_remote); filter(f_spooler); destination(r_spooler); };

log { source(s_remote); filter(f_local7); destination(r_bootlog); };

log { source(s_remote); filter(f_messages); destination(r_messages); };

 

啟動 syslog-ng:
# /etc/rc.d/init.d/syslog-ng restart

驗證:#netstat -an |grep 514

tcp        0      0 0.0.0.0:514                 0.0.0.0:*                   LISTEN     

udp        0      0 0.0.0.0:514                 0.0.0.0:*           

查看文件:
#tail –f /var/log/syslog-ng/日期/主機名(或IP地址)/message

2. 安裝MySQL:
#yum install -y mysql-server

#/etc/rc.d/init.d/mysqld start


3. 安裝php-syslog-ng,以便將日志寫入 MySQL 數據庫中:
下載最新版php-syslog-ng

#yum install php-gd php-mysql

#wget http://php-syslog-ng.gdd.net/current.tgz

#tar zxvf php-syslog-ng-2.9.8l.tgz –C /var/www/html/.

#cd /var/www/html/

#mv php-syslog-ng html

#chown –R apache:apache html

#cd scripts

 

替換腳本中的文件實際路徑(方法:perl -i -pe 's/\/www\/php-syslog-ng/\<newpath>/g' *)

#perl -i -pe 's/\/www\/php-syslog-ng/\/var\/www\/html/g' *

修改 syslog-ng.conf 文件,在最后添加下面幾行:
destination d_mysql {

program("/usr/bin/mysql -usyslogadmin -psyslogadmin syslog"

template("INSERT INTO logs (host, facility, priority, level, tag, datetime, program, msg)

VALUES ( '$HOST', '$FACILITY', '$PRIORITY', '$LEVEL', '$TAG', '$YEAR-$MONTH-$DAY $HOUR:$MIN:$SEC', '$PROGRAM', '$MSG' );\n")

template-escape(yes));

};

log {

source(s_remote);

destination(d_mysql);

};

 

修改/etc/php.ini文件

將display_errors = Off更改為display_errors = On ;

將magic_quotes_gpc = Off更改為magic_quotes_gpc = On ;

將memory_limit = 8M更改為memory_limit = 256M ;

將max_execution_time = 30更改為max_execution_time = 90

然后重啟httpd

# /etc/rc.d/init.d/httpd restart

 

在瀏覽器輸入http://日志服務器ip地址/html

Screen 1: 點擊next開始安裝;

Screen 2: 選擇接受協議后點擊next繼續;

Screen 3: 輸入數據庫ROOT用戶密碼,其它可以保持默認 (你可以不選擇 "install sample data" box) ,點擊Next 繼續;

點擊ok繼續...

Screen 4:輸入站點名稱后點擊next繼續;

Screen 5: 輸入email地址和admin的密碼外,其它可以保持默認,點擊next繼續;

Screen 6: 如果選擇安裝用於收集Cisco ERROR TABLE的數據,將會彈出如下安裝對話框;

點擊Install CEMDB繼續... (如果點擊install CEMDB不工作,請使用Firefox進行安裝。)
你將看到如下對話框: 點擊 "Start Import" 導入 CEMDB數據到數據庫.

一旦完成點擊"Continue"按鈕.

一旦點擊“continue”完成后, 你將看到主登陸窗口:

重啟 syslog-ng:
# /etc/rc.d/init.d/syslog-ng restart

驗證:現在你應該可以通過 MySQL 客戶端軟件查看日志了。

配置計划任務

使用php-syslog-ng對日志進行集中管理,其數據量可能是相當巨大的,使用計划任務實現日志循環的問題.

配置計划任務使用crontab -e加入

@daily php /var/www/html/scripts/logrotate.php >> /var/log/syslog-ng/logrotate.log

@daily find /var/www/html/html/jpcache/ -atime 1 -exec rm -f '{}' ';'

*/10 * * * * php /var/www/html/scripts/reloadcache.php >> /var/log/syslog-ng/reloadcache.log

 然后重啟計划任務

# /etc/rc.d/init.d/crond restart

(1)logrotate.php

對php-syslog-ng中的表進行日志循環,其與系統的logrotate不同。該腳本的執行結果,是把每天的日志表備份起來,並創建一個新的日志表供讀寫。在crontab下,每天自動執行一次。

(2)reloadcache.php

該腳本的作用是,當有新的host加入syslog-ng后,更新mysql中的信息。在crontab下,每5分鍾運行一次。


4.安裝 logcheck:
從 http://sourceforge.net/project/showfiles.php?group_id=100960 下載 logcheck
安裝:
# 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
修改logcheck.sh文件
將其中:
1).
SYSADMIN=root
改為:
SYSADMIN=root,***@163l.com
2).
$LOGTAIL /var/log/messages > $TMPDIR/check.$$
$LOGTAIL /var/log/secure >> $TMPDIR/check.$$
$LOGTAIL /var/log/maillog >> $TMPDIR/check.$$
改為:
$LOGTAIL /var/log/syslog-ng/all-messages > $TMPDIR/check.$$
3).
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

配置系統定時去監測發送郵件
#crontab -e
……
0 0 * * */usr/local/logcheck/etc/logcheck.sh > /dev/null 2>&1

5.安裝 swatch:
按順序安裝下面的perl 模塊:Carp-Clan, Bit-Vector, Date-Calc,Time-HiRes, File-Tail,TimeDate, swatch
配置 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.配置啟動:

#vi /etc/rc.local
/usr/bin/swatch –config-file=/etc/swatch.conf –tail-file=/var/syslog-ng/all-messages &

三、日志客戶機配置
1、LINUX客戶端配置

linux使用的syslog守護進程主要有兩種,syslog和syslog-ng

(1)syslog

#vi /etc/syslog.conf

*.info                                           @loghost

loghost為日志服務器的IP或者主機名,主機明必須能正確解析到日志服務器IP。

解析方法:

#vi /etc/hosts

X.X.X.X     loghost

保存、退出后重新啟動syslog服務:

#/etc/init.d/syslog restart

 

(2)syslog-ng

在配置文件syslog-ng.conf中加入兩行:

destination d_udp { udp("loghost" port(514)); };

log { source(src); destination(d_udp); };

重新啟動syslog-ng服務

#/etc/init.d/syslog-ng restart

 

2、winodws服務器的配置

因為windows服務器不支持日志服務器,因此需要安裝一個轉換軟件:

下載地址為:https://engineering.purdue.edu/ECN/Resources/Documents/UNIX/evtsys/

根據系統的版本下載32位和64位的程序。

解壓后是兩個文件evtsys.dll和evtsys.exe
把這兩個文件拷貝到 c:\windows\system32目錄下。

打開Windows命令提示符(開始->運行 輸入CMD)
C:\>evtsys –i –h 192.168.10.100   #(日志服務器的IP地址)
-i 表示安裝成系統服務
-h 指定log服務器的IP地址
如果要卸載evtsys,則:
net stop evtsys
evtsys -u

啟動該服務:
C:\>net start evtsys

 

配置完成:)

 

 

附錄:――mysql 數據備份和恢復方法

1、mysqldump命令常用實例

備份整個數據庫

mysqldump -u root -ppassword databasename  >data.sql

備份某個或多個表

mysqldump -u root -ppassword databasename table1name table2name >data.sql

只備份數據結構

mysqldump -u root -ppassword databasename --no-data >data.sql

恢復

mysql -u root -ppassword --database=databasename <data.sql

 

2、忘了mysql的root口令怎么辦

#/etc/init.d/mysql stop

#mysqld_safe --skip-grant-tables &

#mysqladmin -u user –p password 'newpassword''

#mysqladmin flush privileges



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM