天天看點

Graylog2+mongdb+rsyslog中央日志伺服器對syslog的web管理

Graylog2+mongdb+rsyslog中央日志伺服器對syslog的web管理

1、下載下傳mongodb,可以通過下面2中方法安裝。

(1)官網下載下傳yum源後直接安裝或下載下傳tar包進行安裝。http://www.mongodb.org/ 。

(2)通過EPEL(企業linux額外軟體包)安裝。http://fedoraproject.org/wiki/EPEL 。

友善起見,這裡選擇用yum源直接安裝。

<1>在*.repo中加入

-------------------------------------------------------------------------------------

[10gen]

name=10gen Repository

baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/i686

gpgcheck=0

-------------------------------------------------------------------------------------

<2>yum install mongo-10gen-server

2、啟動、配置mongodb

<1>vi /etc/mongod.conf

-------------------------------------------------------------------------------------

port = 27017#運作端口27017

auth = true #開啟認證

nohttpinterface = false

-------------------------------------------------------------------------------------

<2>/etc/init.d/mongod start

<3>mongo

    >use graylog2

>db.addUser("login","passwd")#graylog2庫的user,graylog2這個user和mongo相連

>db.auth("login","passwd")#打開認證

>db.system.users.find()#檢視存在的使用者

>exit

<4>/etc/init.d/mongod restart#mongodb預設是開機自啟動的

用ps aux | grep mongo檢視是否存在程序;

用tail -f /var/log/mongo/mongod.log跟蹤mongodb資訊。

3、下載下傳graylog2-server(需要JAVA環境)

為友善起見,我們使用系統自帶的openjdk

<1>yum, install java-1.6.0-openjdk

<2>tar zxvf graylog2-server-0.9.5p1.tar.gz -C /opt

<3>cd /opt; ln -s graylog2-server-0.9.5p1 graylog2

<4>cd graylog2; cp graylog2.conf.example /etc/graylog2.conf

4、配置、啟動graylog2-server,連接配接mongodb。

<1>vi /etc/graylog2.conf

-------------------------------------------------------------------------------------------

# On which port (UDP) should we listen for Syslog messages? (Standard: 514)

syslog_listen_port = 514

syslog_protocol = udp

# MongoDB Configuration

mongodb_useauth = true

mongodb_user = login

mongodb_password = passwd

mongodb_host = localhost

#mongodb_replica_set = localhost:27017,localhost:27018,localhost:27019

mongodb_database = graylog2

mongodb_port = 27017

-------------------------------------------------------------------------------------------

<2>cd /opt/graylog2/bin

<3>./graylog2ctl start

(注:其log日志就存在目前目錄下的nohup.out中。後面會修改并放入/var/log/下,這樣便于日志管理,排錯)

<4>./graylog2ctl stop

<5>vi /etc/init.d/graylog2-server(仿照graylog2ctl寫此腳本)

-------------------------------------------------------------------------------------------

#!/bin/sh

#

# graylog2-server:   graylog2 message collector

# chkconfig: - 98 02

# description:  This daemon listens for syslog and GELF messages and stores them in mongodb

#

CMD=$1

NOHUP=`which nohup`

#JAVA_HOME=/usr/java/latest

#JAVA_CMD=$JAVA_HOME/bin/java

GRAYLOG2_SERVER_HOME=/opt/graylog2

start() {

    echo "Starting graylog2-server ..."

    $NOHUP java -jar $GRAYLOG2_SERVER_HOME/graylog2-server.jar > /var/log/graylog2.log 2>&1 &

}

stop() {

        PID=`cat /tmp/graylog2.pid`

    echo "Stopping graylog2-server ($PID) ..."

        kill $PID

}

restart() {

    echo "Restarting graylog2-server ..."

        stop

        start

}

case "$CMD" in

    start)

        start

        ;;

    stop)

        stop

        ;;

    restart)

        restart

        ;;

    *)

        echo "Usage $0 {start|stop|restart}"

        RETVAL=1

esac

-------------------------------------------------------------------------------------------

<6>chmod +x /etc/init.d/graylog2-server

<7>chkconfig --add graylog2-server

<8>chkconfig graylog2-server on

<9>/etc/init.d/graylog2-server start

此時,日志就存在了/var/log/graylog2.log。

    用“ps aux | grep graylog2”檢視是否存在java -jar ../graylog2-server.jar這個程序。

    用cat /var/log/graylog2.log檢視是否有錯誤資訊。(這裡首先要注意主機的域名解析問題,再然後是可能會報錯,說514端口沒有權限,可以先暫時不管它,後面會處理這個問題)

當然在mongodb中用db.messages.find()指令檢視graylog2庫的messages表,這時應該已經插入了這個關于514端口沒有權限的錯誤日志資訊。

5、下載下傳、配置graylog2-web-interface需要的RUBY環境

<1>先解除安裝舊版本的ruby(它不支援graylog2)

yum erase/remove ruby ruby-libs ruby-mode ruby-rdoc ruby-irb ruby-ri ruby-docs

<2>yum install gcc gcc-c++ openssl-devel zlib-devel autoconf readline-devel curl-devel expat-devel gettext-devel

<3>下載下傳新的ruby。http://www.ruby-lang.org/en/downloads/ 。

tar jxvf ruby-1.9.2-p0.tar.bz2

<4>cd ruby-1.9.2-p0

<5>./configure -enable-shared -enable-pthread -prefix=/usr

<6>make

<7>make install

注:Ruby1.9.2以上包含了rubygems

<8>ruby -v 檢視到rybu版本号1.9.2p0

<9>gem -v 檢視到1.3.7

由<8><9>兩步的測試結果,說明ruby安裝成功。

<10>gem update 

<11>gem install rake

6、下載下傳、配置graylog2-web-interface

<1>tar zxvf graylog2-web-interface-0.9.5p2.tar.gz -C /opt

<2>cd /opt; ln -s graylog2-web-interface-0.9.5p2 graylog2-web-interface;

useradd -m graylog;chown -R graylog /opt/graylog2-web-interface-0.9.5p2

<3>cd graylog2-web-interface

<4>gem install bundler

<5>bundle install

<6>Edit all ./config/*.yml。這裡可以先隻編輯mongoid.yml

vi /opt/graylog2-web-interface/config/mongoid.yml

--------------------------------------------------------------------------------------------

production:

  host: localhost#注意從此行起,前面不能頂格,有2個空格

  port: 27017

  database: graylog2

  username: login

  password: passwd

--------------------------------------------------------------------------------------------

<7>RAILS_ENV=production script/rails server

通路http://127.0.0.1:3000測試,若出現頁面讓你建立第一個使用者,則成功。

<8>按Ctrl C,結束它。

<9>gem install passenger        #下載下傳後,用passenger管理控制graylog2-web

<10>gem install file-tail

<11>chmod -R 777 tmp log

<12>passenger start

Passenger自動下載下傳、配置、組建好一切所需要的東西後,按Ctrl C,結束它。

<13> vi /etc/init.d/graylog2-web-interface (原本是沒有這個檔案的)

-------------------------------------------------------------------------------------------

#!/bin/bash

#

# graylog2-web-interface:   graylog2 web interface

#

# chkconfig: - 98 02

# description:  Starts graylog2-web-interface using passenger-standalone. 

USER=graylog

APP_DIR=/opt/graylog2-web-interface

ADDR=127.0.0.1

PORT=3000

ENVIRONMENT=production

#LOG_FILE=/var/log/graylog2-web-interface.log

CMD_START="cd $APP_DIR; passenger start -d \

                    -a $ADDR \

                    -p $PORT \

                    -e $ENVIRONMENT \

                    --user $USER"

CMD_STOP="cd $APP_DIR; passenger stop -p $PORT"

CMD_STATUS="cd $APP_DIR; passenger status -p $PORT"

. /lib/lsb/init-functions

case "$1" in

  start)

    echo "Starting graylog2-web-interface"

    su - $USER -c "$CMD_START"

    ;;

  stop)

    echo "Stopping graylog2-web-interface"

     su - $USER -c "$CMD_STOP"

    ;;

  status)

   su - $USER -c "$CMD_STATUS"

   ;;

  *)

    echo "Usage: $0 start|stop|status" >&2

    exit 3

    ;;

esac                                                                                              

-------------------------------------------------------------------------------------------

<14>chmod +x /etc/init.d/graylog2-web-interface 

<15>chkconfig --add graylog2-web-interface 

<16>chkconfig graylog2-web-interface on

<17>/etc/init.d/graylog2-web-interface start

通路http://127.0.0.1:3000,看!出現頁面讓你建立第一個使用者!搞定!

Graylog2+mongdb+rsyslog中央日志伺服器對syslog的web管理

在這裡你先建立你的first user(我的是admin、adminpasswd,記得填個Email),然後就可以用剛建立的user進入web界面了。

Graylog2+mongdb+rsyslog中央日志伺服器對syslog的web管理

7、用ryslog代替本機syslog,使graylog2正常開啟、運作

此時,你的graylog2伺服器還沒有真正搭建好,日志并不能被收集進mongodb中存儲,并通過graylog2-web頁面顯示出來。你會發現web上會一直報告錯誤說你的graylog2 server好像沒有運作,通過個log資訊可以知道是原因是graylog2(server)對514端口沒有權限。這個也就是我們之前遺留下來的問題。

解決辦法如下:

<1>yum install rsyslog

<2>vi /etc/sysconfig/rsyslog

----------------------------------------------------------------------------------------

SYSLOGD_OPTIONS="-m 0 -r10514"

----------------------------------------------------------------------------------------

<3> vi /etc/rsyslog.conf

----------------------------------------------------------------------------------------

*.*                 @127.0.0.1:514

----------------------------------------------------------------------------------------

<4>/etc/init.d/syslog stop

<5>chkconfig  syslog off

<6>/etc/init.d/rsyslog start

<7>chkconfig  rsyslog on

<8>netstat -anltpue | grep 514

可以看見

udp   0   0 0.0.0.0:10514    0.0.0.0:*    0    124239    4766/rsyslogd

<9>/etc/init.d/graylog2-server restart

<10>netstat -anltpue | grep 514

可以看見有

udp   0   0 0.0.0.0:10514    0.0.0.0:*    0    124239    4766/rsyslogd

udp   0   0 :::514           :::*        0    111125    957/java 

然後,你就可以測試你的graylog2是否正常運作了。Web界面有了messages資料,檢視mongodb中的graylog2庫也有了messages表的資料。Graylog2正常運作。

8、增加遠端客戶機

    我們來為這台graylog2中央日志伺服器增加客戶機:

<1>vi /etc/syslog.conf

----------------------------------------------------------------------------------------------

*.*                          @graylog2's IP

----------------------------------------------------------------------------------------------

<2>/etc/init.d/syslog restart

好了,大功告成!可以用logger指令進行測試,并在graylog2 web界面中檢視、管理遠端log日志了。

Graylog2+mongdb+rsyslog中央日志伺服器對syslog的web管理

9、下載下傳、配置graylog2虛拟主機、啟動apache

<1>yum install httpd -y

<2>vi /etc/httpd/conf/httpd.conf 

-------------------------------------------------------------------------------------------

NameVirtualHost *:80

<VirtualHost *:80>

ServerName log.example.com

ServerAlias graylog2.example.com

    ProxyPreserveHost On

    ProxyPass        / http://127.0.0.1:3000/

    ProxyPassReverse / http://127.0.0.1:3000/

    CustomLog /var/log/httpd/graylog2.example.com-access_log common

</VirtualHost>

-------------------------------------------------------------------------------------------

<3> service httpd configtest

顯示“Syntax OK”證明配置正确。

<4>/etc/init.d/httpd start

Finally,通過web頁面通路log.example.com或者graylog2.example.com進入graylog2日志管理系統。

10、web界面

<1>通過

Graylog2+mongdb+rsyslog中央日志伺服器對syslog的web管理

選項中的

Graylog2+mongdb+rsyslog中央日志伺服器對syslog的web管理

設定(這裡設定為1,即1min),可調整、檢視最近N(這裡為1)min裡graylog2(mongodb)接收到的日志資訊(即messages)數量為多少

Graylog2+mongdb+rsyslog中央日志伺服器對syslog的web管理

,提示資訊

Graylog2+mongdb+rsyslog中央日志伺服器對syslog的web管理

<2>通過

Graylog2+mongdb+rsyslog中央日志伺服器對syslog的web管理

選項建立一個組【這裡建立的組名為(r)syslog】後,你還可以将主機加入組,便于分類、管理。

Graylog2+mongdb+rsyslog中央日志伺服器對syslog的web管理

當然,還有更多的功能等待你去發現,最後,再送上2張截圖,祝你好運:)

Graylog2+mongdb+rsyslog中央日志伺服器對syslog的web管理
Graylog2+mongdb+rsyslog中央日志伺服器對syslog的web管理

繼續閱讀