CentOS 5.5搭建日誌服務器

一、Rsyslog服務器的安裝與配置

1、清空iptabels, 關閉selinux避免安裝過中報錯

清空iptables

iptables -F
service iptables save

關閉selinux

setenforce 0
cd /etc/selinux/
vi conifg#由vim改爲vi

將配置文件中的  SELINUX=enforcing  修改爲:  SELINUX=disabled

2、yum安裝LAMP環境及rsyslog、rsyslog mysql支持模塊
yum install -y mysql-server mysql-devel curl-devel net-snmp-devel php php-gd php-xml php-mysql httpd rsyslog rsyslog-mysql
#此處將libcurl改爲curl
3、設置mysqld和httpd開機啓動,並啓動服務
chkconfig mysqld on
chkconfig httpd on
service mysqld start
service httpd start
4、修改mysql的root密碼
mysqladmin -u root password "你的密碼"

修改密碼後就可以使用如下命令登錄mysql

mysql -u root -p
5、修改mysql的配置文件,支持uft8

因爲通過 yum安裝的mysql是不支持uft8的,在收集Windows服務器的日誌和某些支持中文的設備的情況下,收集到的日誌無法正確顯示,因此需要修改mysql配置文件支持中文顯示。

配置文件全文如下:

vi /etc/my.cnf#新增內容
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
character_set_server=utf8
init_connect='SET NAMES utf8'
default-character-set=utf8

[mysql]
default-character-set=utf8

[mysql.server]
default-character-set=utf8

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
default-character-set=utf8

[client]
default-character-set=utf8

重啓mysqld服務

service mysqld restart
  • 1

6、導入Rsyslog數據庫到mysql

Rsyslog的mysql數據庫默認文件是:

/usr/share/doc/rsyslog-mysql-5.8.10/createDB.sql
  • 1

在rsyslog-mysql相應版本的文件夾中  你可以使用下列命令中查詢到:

echo  $(rpm -ql rsyslog-mysql | grep sql$)

使用如下命令將文件導入到mysql:

mysql -u root -p < $(rpm -ql rsyslog-mysql | grep sql$)

隨後輸入你在第3步中設置的mysql密碼就可以導入數據庫了,數據庫名爲:Syslog

7、設置Rsyslog數據庫的字符集

上一步導入的數據庫只有二個表: SystemEvents 和 SystemEventsProperties  Syslog數據庫及這二個表的默認字符集也不是uft8,需要進行修改

#修改Syslog數據庫的字符集
mysql> ALTER DATABASE `Syslog` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
#修改相應數據表的字符集
mysql> use Syslog;
mysql> alter table SystemEvents  default character set =utf8;

mysql> alter table  SystemEventsProperties  default character set =utf8;

查看修改後的效果:

#查看數據庫的字符集設置
mysql> show variables like 'character%';
#查看Systlog數據庫所有表的字符集設置
mysql> use Syslog;
mysql> show table status from Syslog\G;
8、創建Rsyslog數據庫用戶

登錄mysql創建Rsyslog的數據庫用戶logger

#登錄
mysqlmysql -u root -p
#創建用戶 
mysql> grant all privileges on Syslog.* to 'rsyslog'@'localhost' identified by '147258'; 
mysql> flush privileges; 
mysql> exit;
9、修改rsyslog配置文件
vi /etc/sysconfig/rsyslog
#由vim改爲vi

配置文件如下:

# Options for rsyslogd
# Syslogd options are deprecated since rsyslog v3
# If you want to use them, switch to compatibility mode 2 by "-c 2"
# See rsyslogd(8) for more 
detailsSYSLOGD_OPTIONS="-c 2 -r -x -m 180"
KLOGD_OPTIONS="-x"

各參數詳解:  -c 指定運行兼容模式。  -r 指定監聽端口。 默認514  -x 在接收客戶端消息時,禁用DNS查找。需和-r參數配合使用。  -m 標記時間戳。單位是分鐘,爲0時,表示禁用該功能。

10、配置服務端支持rsyslog-mysql模塊,並開啓UDP服務端口
vi /etc/rsyslog.conf
#由vim改爲vi

在#### MODULES ####下添加這兩行

$ModLoad ommysql.so
*.* :ommysql:localhost,Syslog,rsyslog,147258

注:localhost表示本地主機,Syslog爲數據庫名,rsyslog爲數據庫的用戶,147258爲該用戶密碼

取消下面三行註釋

$ModLoad immark
$ModLoad imudp
$UDPServerRun 514

重啓rsyslog服務:

service syslog stop//此處爲增加內容
service rsyslog restart
service syslog restart//此處爲增加內容
11、添加服務端iptables防火牆規則

如果服務端有需要配置iptables規則的話,執行如下命令開放TCP和UDP的514端口

iptables -I INPUT -p tcp --dport 514 -j ACCEPT
iptables -I INPUT -p udp --dport 514 -j ACCEPT
service iptables save

二 、Loganalyzer安裝與配置

1、下載Loganalyzer

從Loganalyzer官網下載安裝文件到/usr/local/src目錄下  最新版本是4.1.3 穩定版本是:3.6.6

2、解壓文件並複製源代碼到apache的loganalyzer目錄
cd /usr/local/src
tar -zxvf loganalyzer-4.1.3.tar.gz
cd loganalyzer-4.1.3
mkdir -p /var/www/html/loganalyzer/
cp -r src/* /var/www/html/loganalyzer/
cp -r contrib/* /var/www/html/loganalyzer/
3、生成空的配置文件並設置權限
cd /var/www/html/loganalyzer/
touch config.php
chmod 666 config.php
4、修改php環境

爲配合LogAnalyzer對php環境的要求,請修改/etc/php.ini中的內容爲:

memory_limit = 512M  
max_execution_time = 120
5、創建apache日誌目錄
mkdir -p  /var/log/httpd/loganalyzer
6、配置apache

這部分,請根據apache實際情況操作。以默認系統爲例,虛擬主機配置文件都放在/etc/httpd/conf/httpd.conf

配置文件修改如下:

Listen 80
ServerAdmin root@localhost
ServerName log_server:80
DocumentRoot "/var/www/html/loganalyzer"
<Directory />
    Options FollowSymLinks
    AllowOverride All
</Directory>
<Directory "/var/www/html/loganalyzer">
    Options Indexes FollowSymLinks
    AllowOverride All
    Order allow,deny
    Allow from all
</Directory>

ErrorLog /var/log/httpd/loganalyzer/error.log
LogLevel warn
CustomLog /var/log/httpd/loganalyzer/access_log combined
ServerSignature On

重啓httpd服務

service httpd restart
7、Loganalyzer初始化配置

在瀏覽器輸入網址,進入安裝嚮導  訪問http://serverip:80  一共8個步驟,這裏只將需要修改的步驟列出,其餘只需要”Next”即可

第3步:”Enable User Database”選擇“Yes”;

第4、5二個步驟是指定用戶數據庫信息(數據庫指定爲:Syslog)和創建用戶

第7步:”Source Type” 選擇 “MYSQL Native”並在下載配置數據庫信息 

第8步Finish後,完成安裝就可登錄並查看日誌信息了。

三 、Linux客戶端配置

Centos默認都會安裝rsyslog。

1、確認rsyslog服務是否啓動
chkconfig | grep rsyslog

如果沒有設置爲開機啓動,執行如下命令:

chkconfig rsyslog on
service rsyslog start
2、配置Rsyslog客戶端發送本地日誌到服務端
vi /etc/rsyslog.conf
#由vim改爲vi

在末行添加一行:

*.* @192.168.7.201

注:192.168.7.201 爲日誌服務器端IP地址

3、重啓Rsyslog服務
service rsyslog restart
  • 1

這時在服務端刷新就可以看到相關日誌信息了。

四 、Windows客戶端配置

Loganalyzer官網推薦在Winodws上使用Winsyslog來實現日誌數據的收集和發送,但是這個軟件是收費的。這裏用一個開源的軟件Evtsys,但這個軟件也有個問題無法在Server 2012以上的系統中使用,在Server 2008中是沒有問題的

Evtsy官網由於是佈署在google服務器上的因此現在無法訪問,可以到網上去搜索下載。  下載後的文件解壓將裏面的evtsys.exe文件放到c:\windows\System32目錄下,然後運行cmd執行下面的命令:

evtsys -i -s 10 -h log-server-ip -p 514 
net start evtsys

只要看到最後的安裝成功即可。  以下附上命令的詳細參數

只要看到最後的安裝成功即可。  以下附上命令的詳細參數

Version: 4.4 (32-bit) 
Usage: evtsys.exe -i|-u|-d [-h host] [-b host] [-f facility] [-p port] 
       [-s minutes] [-l level] [-n] 
  -i           Install service 
  -u           Uninstall service 
  -d           Debug: run as console program 
  -h host      Name of log host 
  -b host      Name of secondary log host (optional) 
  -f facility  Facility level of syslog message 
  -l level     Minimum level to send to syslog.\n", stderr); 
           0=All/Verbose, 1=Critical, 2=Error, 3=Warning, 4=Info 
  -n           Include only those events specified in the config file. 
  -p port      Port number of syslogd 
  -q bool      Query the Dhcp server to obtain the syslog/port to log to 
               (0/1 = disable/enable) 
  -s minutes   Optional interval between status messages. 0 = Disabled 

Default port: 514 
Default facility: daemon 
Default status interval: 0 
Host (-h) required if installing.

這時在刷新Loganalyzer就可以看到相關日誌了.


內容轉自  中文站 http://www.centoscn.com/CentosServer/log/2015/1230/6588.html

爲了貼合自己的實際情況,做出部分修改.


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章