RHEL 6.5 搭建Rsyslog日誌伺服器和Loganalyzer日誌分析工具

parakai發表於2014-10-31

1. 簡介

LogAnalyzer 是一款syslog日誌和其他網路事件資料的Web前端。它提供了對日誌的簡單瀏覽、搜尋、基本分析和一些圖表報告的功能。資料可以從資料庫或一般的syslog文字檔案中獲取,所以LogAnalyzer不需要改變現有的記錄架構。基於當前的日誌資料,它可以處理syslog日誌訊息,Windows事件日誌記錄,支援故障排除,使使用者能夠快速查詢日誌資料中看出問題的解決方案。

LogAnalyzer 獲取客戶端日誌會有兩種儲存模式,一種是直接讀取客戶端/var/log/目錄下的日誌並儲存到服務端該目錄下,一種是讀取後儲存到日誌伺服器資料庫中,推薦使用後者。

LogAnalyzer 採用php開發,所以日誌伺服器需要php的執行環境,以下采用LAMP。

2.系統環境

Rsyslog Server OS:CentOS 6.5

Rsyslog 版本:rsyslog-5.8.10

LogAnalyzer 版本:LogAnalyzer 3.6.6 (v3-stable)

LAMP 版本:httpd-2.2.15 + mysql-5.1.73 + php-5.3.3

防火牆已關閉/iptables: Firewall is not running.

SELINUX=disabled

3.安裝並設定Mysql、rsyslog環境

3.1. 新建yum源

[root@redhat6 ~]# cd /etc/yum.repos.d/
[root@redhat6 yum.repos.d]# mv rhel-source.repo rhel-source.repo.bak
[root@redhat6 yum.repos.d]# mount /dev/cdrom /mnt/
[root@redhat6 yum.repos.d]# ls /mnt/
[root@redhat6 yum.repos.d]# cd ~
[root@redhat6 ~]# cat >>/etc/yum.repos.d/yumserver.repo<<EOF 
[yumserver]
name=yum server
baseurl=file:///mnt
enabled=1 
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release
EOF
[root@redhat6 ~]# yum list

3.2. 檢查rsyslog軟體

[root@redhat6 ~]# rpm -qa | grep rsyslog
rsyslog-5.8.10-2.el6.x86_64   #rsyslog預設已經安裝
[root@redhat6 ~]# rpm -ql rsyslog    
/etc/logrotate.d/syslog
/etc/pki/rsyslog
/etc/rc.d/init.d/rsyslog
/etc/rsyslog.conf            #rsyslog的配置檔案
/etc/rsyslog.d
/etc/sysconfig/rsyslog
/lib64/rsyslog
/lib64/rsyslog/imfile.so
/lib64/rsyslog/imklog.so
/lib64/rsyslog/immark.so
/lib64/rsyslog/impstats.so
/lib64/rsyslog/imptcp.so
/lib64/rsyslog/imtcp.so
/lib64/rsyslog/imudp.so
/lib64/rsyslog/imuxsock.so
/lib64/rsyslog/lmnet.so
/lib64/rsyslog/lmnetstrms.so
/lib64/rsyslog/lmnsd_ptcp.so
/lib64/rsyslog/lmregexp.so
/lib64/rsyslog/lmstrmsrv.so
/lib64/rsyslog/lmtcpclt.so
/lib64/rsyslog/lmtcpsrv.so
/lib64/rsyslog/lmzlibw.so
/lib64/rsyslog/ommail.so
/lib64/rsyslog/omprog.so
/lib64/rsyslog/omruleset.so
/lib64/rsyslog/omtesting.so
/lib64/rsyslog/omuxsock.so
/lib64/rsyslog/pmlastmsg.so
/sbin/rsyslogd
/var/lib/rsyslog

3.3 mysql伺服器安裝配置

[root@redhat6 ~]#yum -y install mysql-server rsyslog-mysql mysql
[root@redhat6 ~]#chkconfig mysqld on
[root@redhat6 ~]#service mysqld start

3.4 設定Mysql密碼

[root@redhat6 ~]# mysqladmin -u root password mysql123

3.5 匯入日誌檔案的sql指令碼,生成日誌檔案的資料庫

[root@redhat6 ~]# cd /usr/share/doc/rsyslog-mysql-5.8.10/
[root@redhat6 rsyslog-mysql-5.8.10]# ls
createDB.sql
[root@redhat6 rsyslog-mysql-5.8.10]# mysql -uroot -p < createDB.sql
Enter password: 
[root@redhat6 rsyslog-mysql-5.8.10]# cd ~
[root@redhat6 ~]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.1.61 Source distribution

Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| Syslog             |
| mysql              |
| test               |
+--------------------+
4 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)

匯入資料庫操作建立了Syslog 庫並在該庫中建立了兩張空表SystemEventsSystemEventsProperties

3.6. 建立rsyslog 使用者在mysql下的相關許可權

mysql> grant all on Syslog.* to 'rsyslog'@'localhost' identified by 'syslog';
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

3.7. 配置服務端支援rsyslog-mysql 模組,並開啟UDP服務埠獲取網內其他系統日誌

# vi /etc/rsyslog.conf
$ModLoad ommysql
*.* :ommysql:localhost,Syslog,rsyslog,syslog
在 #### MODULES #### 下新增上面兩行。
說明:localhost 表示本地主機,Syslog為資料庫名,rsyslog為資料庫的使用者,syslog為該使用者密碼。

3.8. 開啟相關日誌模組

# vi /etc/rsyslog.conf
$ModLoad immark       #immark是模組名,支援日誌標記
$ModLoad imudp        #imupd是模組名,支援udp協議
$UDPServerRun 514     #允許514埠接收使用UDP和TCP協議轉發過來的日誌
#**如沒有下列欄位,新增:**
$template SpiceTmpl,"%TIMESTAMP%.%TIMESTAMP:::date-subseconds% %syslogtag% %syslogseverity-text%:%msg:::sp-if-no-1st-sp%%msg:::drop-last-lf%\n"
:programname, startswith, "spice-vdagent"   /var/log/spice-vdagent.log;SpiceTmpl

4.驗證日誌檔案存放位置

4.1. 驗證是否存放在伺服器的/var/log/messages裡面

[root@redhat6 ~]# logger -p info "load message"
[root@redhat6 ~]# tail -f /var/log/messages 
Oct 31 10:16:05 redhat6 kernel: Kernel logging (proc) stopped.
Oct 31 10:16:05 redhat6 rsyslogd: [origin software="rsyslogd" swVersion="5.8.10" x-pid="1204" x-info="http://www.rsyslog.com"] exiting on signal 15.

4.2. 驗證是否存放在伺服器的mysql資料庫中

[root@redhat6 ~]# mysql -u root -p
mysql> use Syslog;
mysql> select * from SystemEvents\G
*************************** 4. row ***************************
                ID: 4
        CustomerID: NULL
        ReceivedAt: 2014-10-31 10:22:08
DeviceReportedTime: 2014-10-31 10:22:08
          Facility: 1
          Priority: 6
          FromHost: redhat6
           Message:  load message
        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: root:
      EventLogType: NULL
   GenericFileName: NULL
          SystemID: NULL

5.基於web的loganalyzer日誌分析工具的搭建

5.1 安裝httpd,php,php-gd,php-mysql

httpd用來提供web服務

php使apache支援php,因為loganalyzer是用php編寫

php-mysql用於loganalyzer連線資料庫

php-gd用於繪圖

[root@redhat6 ~]# yum -y install httpd php php-mysql php-gd

新建資料夾存放loganalyzer的網頁檔案

[root@redhat6 ~]# mkdir -p /web/syslog

5.2 安裝LogAnalyzer

[root@redhat6 ~]# wget http://download.adiscon.com/loganalyzer/loganalyzer-3.6.6.tar.gz

或上傳已下載好的loganalyzer-3.6.6.tar.gz檔案

[root@redhat6 ~]# tar -zxf loganalyzer-3.6.6.tar.gz 
[root@redhat6 ~]# ls
anaconda-ks.cfg  install.log  install.log.syslog  loganalyzer-3.6.6  loganalyzer-3.6.6.tar.gz
[root@redhat6 ~]# cd loganalyzer-3.6.6
[root@redhat6 loganalyzer-3.6.6]# cp -r src/* /web/syslog/
[root@redhat6 loganalyzer-3.6.6]# cp -r contrib/* /web/syslog/
[root@redhat6 loganalyzer-3.6.6]# cd /web/syslog/
[root@redhat6 syslog]# chmod +x configure.sh 
[root@redhat6 syslog]# chmod +x secure.sh 
[root@redhat6 syslog]# sh configure.sh 

5.3 修改httpd的配置檔案

[root@redhat6 syslog]# vim /etc/httpd/conf/httpd.conf
ServerName 192.168.1.157:80
DocumentRoot "/web"
<Directory "/web">
DirectoryIndex index.php index.html index.html.var
[root@redhat6 syslog]# service httpd restart
Stopping httpd:                                            [  OK  ]
Starting httpd:                                            [  OK  ]

5.4 建立loganalyzer的資料庫,並授權

6.安裝嚮導中安裝LogAnalyzer

6.1. 開啟瀏覽器訪問:http://192.168.1.157/syslog/ enter image description here

提示沒有配置檔案,點選 here 利用嚮導生成。

6.2 第一步,測試系統環境 enter image description here

點選"Next",進入第二步。 enter image description here 提示錯誤:config.php許可權要設定為666,可以使用contrib目錄下的configure.sh 指令碼生成。

[root@redhat6 syslog]# ll config.php 
-rwxr-xr-x. 1 root root 0 Oct 31 10:42 config.php
[root@redhat6 syslog]# 
[root@redhat6 syslog]# 
[root@redhat6 syslog]# chmod 666 config.php 
[root@redhat6 syslog]# ll config.php 
-rw-rw-rw-. 1 root root 0 Oct 31 10:42 config.php

做完上面的操作之後,執行 ReCheck 操作,config.php 檔案可寫,點選 Next 進入下一步。 enter image description here

6.3 第三步,基礎配置 enter image description here

在User Database Options 中,填入上面設定的引數,然後點選 Next。 注意資料庫名大小寫

6.4 第四步,建立表 enter image description here

點選 Next 開始建立表。

6.5 第五步,檢查SQL結果 enter image description here

6.6 第六步,建立管理使用者 enter image description here

6.7 第七步,建立第一個系統日誌source enter image description here

6.8 第八步,完成 enter image description here

7.測試

LogAnalyzer 首頁 enter image description here

Rsyslog + LogAnalyzer 日誌伺服器部署完畢。

相關文章