graylog2 是一個開源的日誌存儲系統,是由java語言編寫的server,可以接收TCP,UDP,AMQP的協議發送的日誌信息,而且基於mongodb數據庫服務器快速存儲,可以經過一個基於ruby編寫的web管理界面,讓輕鬆管理你的日誌。下面將在一臺默認安裝的rhel5.x的系統上來實現graylog2+syslog-ng+mongodb構建集中管理日誌服務器html
一.源碼包下載java
- cd /usr/src
- #mongodb數據庫,在此的主要做用是存儲日誌信息
- wget http://fastdl.mongodb.org/linux/mongodb-linux-i686-2.2.1.tgz
- #graylog2日誌服務器
- wget http://cloud.github.com/downloads/Graylog2/graylog2-server/graylog2-server-0.9.5.tar.gz
- #yaml是一種編程語言,
- wget http://pyyaml.org/download/libyaml/yaml-0.1.4.tar.gz
- #ruby腳步語言
- wget ftp://ftp.ruby-lang.org//pub/ruby/1.9/ruby-1.9.2-p0.tar.gz
- #graylog2-web界面
- wget http://cloud.github.com/downloads/Graylog2/graylog2-web-interface/graylog2-web-interface-0.9.5p2.tar.gz
- #eventlog,安裝syslog-ng須要先安裝eventlog
- wget http://www.balabit.com/downloads/files/syslog-ng/sources/3.2.4/source/eventlog_0.2.12.tar.gz
- #syslog-ng日誌服務器,
- wget http://www.balabit.com/downloads/files/syslog-ng/open-source-edition/3.3.5/source/syslog-ng_3.3.5.tar.gz
- #JDK,java環境
- JDK下載地址, http://www.oracle.com/technetwork/java/javase/downloads/index.html 打開此連接下載相應的jdk版本,這裏以jdk-6u21-linux-i586.bin
二.安裝相關依賴包linux
- yum -y install gcc* openssl-devel glib2-devel
三.安裝並配置mongodb數據庫
1.安裝mongodbgit
- useradd mongodb
- mkdir -pv /data/db
- chown -R mongodb:mongodb /data
- tar xvf /usr/src/mongodb-linux-i686-2.2.1.tgz -C /usr/local/
- cd /usr/local/
- ln -sv mongodb-linux-i686-2.2.1 mongodb
2.爲mongodb提供服務啓動腳本,並啓動github
- vim /etc/init.d/mongod #內容以下
- #!/bin/bash
- # chkconfig: - 90 11
- # description: mongodb server SysV script
- . /etc/rc.d/init.d/functions
- if [ -f /etc/sysconfig/mongod ]; then
- . /etc/sysconfig/mongod
- fi
- mongod=/usr/local/mongodb/bin/mongod
- prog=mongod
- lockfile=/data/db/mongod.lock
- RETVAL=0
- OPTIONS="--fork --logpath=/data/mongod.log"
- start() {
- echo -n $"Starting $prog: "
- daemon $mongod $OPTIONS
- RETVAL=$?
- echo
- [ $RETVAL = 0 ]
- return $RETVAL
- }
- stop() {
- echo -n $"Stopping $prog: "
- killproc -p ${lockfile} $mongod
- RETVAL=$?
- echo
- [ $RETVAL = 0 ] && rm -f ${lockfile}
- }
- case "$1" in
- start)
- start
- ;;
- stop)
- stop
- ;;
- restart)
- stop
- start
- ;;
- status)
- status -p ${lockfile} $mongod
- RETVAL=$?
- ;;
- *)
- echo $"Usage: $prog {start|stop|restart|status|}"
- exit 1
- esac
- exit $RETVAL
- ----------------------------------------------------
- chmod a+x /etc/init.d/mongod
- chkconfig --add mongod
- chkconfig mongod on
- service mongod start
3.建立graylog2所需數據庫實例web
- /usr/local/mongodb/bin/mongo
- >use admin
- >db.addUser('admin','password')
- >use graylog2
- >db.addUser('graylog','redhat')
- >exit
四.安裝graylog2-server
1.安裝JDK環境mongodb
- cd /usr/src
- chmod a+x jdk-6u21-linux-i586.bin
- ./jdk-6u21-linux-i586.bin
- mv jdk1.6.0_21/ /usr/local/
- cd /usr/local/
- ln -sv jdk1.6.0_21 jdk
- 添加java環境變量,
- vim /etc/profile
- JAVA_HOME=/usr/local/jdk
- PATH=$JAVA_HOME/bin:$PATH
- export JAVA_HOME PATH
- source /etc/profile
2.安裝graylog-server數據庫
- cd /usr/src
- tar -xvf graylog2-server-0.9.5.tar.gz -C /usr/local/
- useradd graylog
- chown -R graylog:graylog /usr/local/graylog2-server-0.9.5/
- cd /usr/local
- ln -sv graylog2-server-0.9.5 graylog2
- cp /usr/local/graylog2/graylog2.conf.example /etc/graylog2.conf
3.配置garylog2-server編程
- vim /etc/graylog2.conf 修改內容以下
- syslog_listen_port = 515 #將默認的514端口修改成其餘未使用端口,由於514端口是syslog-ng默認的端口,這裏須要用syslog-ng的514端口來接收其餘服務器發來的日誌,並進行處理的,
- syslog_protocol = udp
- mongodb_useauth = true
- mongodb_user = graylog2 #訪問mongodb的用戶名
- mongodb_password = redhat #訪問mongodb用戶名的密碼
- mongodb_host = localhost #mogodb的主機
- mongodb_database = graylog2 #存儲日誌的數據庫
- mongodb_port = 27017 #訪問mongodb的端口,默認是27017
- ......
- ......
- 爲了方便起見這裏修改下graylog-server服務啓動腳本配置文件
- sed -i 's/java/$JAVA_CMD/' /usr/local/graylog2/bin/graylog2ctl
- sed -i '2 aJAVA_CMD=/usr/local/jdk/bin/java' /usr/local/graylog2/bin/graylog2ctl
4.將graylog2-server添加爲系統服務並啓動vim
- vim /etc/init.d/graylog2 #內容以下
- #!/bin/bash
- #
- # chkconfig: - 83 19
- # description: graylog2-server SysV script
- #
- GRAYLOG_BIN=/usr/local/graylog2/bin
- cd $GRAYLOG_BIN && ./graylog2ctl $1
- -------------------------------------------
- chmod a+x /etc/init.d/graylog2
- chkconfig --add graylog2
- chkconfig graylog2 on
- service graylog2 start
五.安裝graylog2-web-interface
1.安裝ruby腳本語言環境
- cd /usr/src
- tar xvf yaml-0.1.4.tar.gz
- cd yaml-0.1.4
- ./configure --prefix=/usr/local/yaml
- make
- make install
- cd /usr/src
- tar xvf ruby-1.9.2-p0.tar.gz
- cd ruby-1.9.2-p0
- ./configure --prefix=/usr/local/ruby --enable-shared --disable-install-doc --with-opt-dir=/usr/local/yaml
- make
- make install
- vim /etc/profile
- 在PATH環境變量中添加
- PATH=......:/usr/local/ruby/bin
- source /etc/profile
2.安裝graylog2-web-interface
- cd /usr/src
- tar xvf graylog2-web-interface-0.9.5p2.tar.gz -C /usr/local/
- cd /usr/local/
- chown -R graylog:graylog /usr/local/graylog2-web-interface-0.9.5p2/
- ln -sv graylog2-web-interface-0.9.5p2/ graylog2-web-interface
- cd graylog2-web-interface
- gem install bundler #須要連網
- bundle install #須要聯網
3.配置graylog2-web-interface
- vim /usr/local/graylog2-web-interface/config/mongoid.yml
- production:
- host: localhost #mongodb的主機
- port: 27017 #mongodb的端口號
- username: graylog #mongodb的用戶名
- password: redhat #mongodb的用戶名密碼
- database: graylog2 #mongodb的數據庫名
- --------------------------------------------------
- vim /usr/local/graylog2-web-interface/config/general.yml #根據本身的須要修改,不作任何修改也不要緊,
- vim /usr/local/graylog2-web-interface/config/email.yml #根據本身的須要修改,不作任何修改也不要緊,
- 配置日誌切割 cat > /etc/logrotate.d/graylog2-web-interface <<EOF /usr/local/graylog2-web-interface/log/*log { size=512M rotate 90 copytruncate delaycompress compress notifempty missingok } EOF
4.啓動graylog2-web-interface,本身能夠照着上面的服務啓動腳本的例子寫一個SysV風格的腳本
- cd /usr/local/graylog2-web-interface && ./script/rails server -e production &
5.首次訪問graylog2-web界面的配置
- 打開瀏覽器輸入http://服務器IP:3000就打開graylog2的web界面了.
- 首次使用須要作一些配置,只須要添加一個管理員賬號便可,見如下圖,
圖1.
圖2輸入用戶信息
圖3 登陸界面
圖4 點擊messages
圖5
能夠看到,此時尚未日誌信息,這是由於日誌的收集程序,syslog-ng尚未配置,以下
六.安裝並配置syslog-ng服務,
其實也能夠不用將syslog-ng配置成服務,graylog2也能夠經過網絡來接收日誌信息,這裏將syslog-ng配置爲服務的目的是,經過syslog-ng統一收集日誌並經過syslog-ng的強大過濾功能,能夠對本身感興趣的日誌進行過濾處理
1.安裝eventlog
- cd /usr/src/
- tar xvf eventlog_0.2.12.tar.gz
- cd eventlog-0.2.12
- ./configure --prefix=/usr/local/eventlog
- make
- make install
2.安裝syslog-ng
- cd /usr/src
- tar xvf syslog-ng_3.3.5.tar.gz
- cd syslog-ng-3.3.5
- export PKG_CONFIG_PATH=/usr/local/eventlog/lib/pkgconfig
- ./configure --prefix=/usr/local/syslog-ng
- make
- make install
3.配置syslog-ng,
- cat > /usr/local/syslog-ng/etc/syslog-ng.conf <<EOF
- @version: 3.3
- @include "scl.conf"
- source s_local {
- system();
- internal();
- };
- #設置日誌的來源爲本機udp的514端口
- source s_network { udp(ip(0.0.0.0) port(514)); };
- #將日誌發送到本機的udp515端口進行處理,515端口是graylog2-server的端口,
- destination d_local { udp("127.0.0.1" port(515) ); };
- log {
- source(s_local);
- source(s_network);
- destination(d_local);
- };
- EOF
4.添加爲系統服務,並啓動
- vim /etc/init.d/syslog-ng
- #!/bin/bash
- #
- # chkconfig: - 60 27
- # description: syslog-ng SysV script.
- . /etc/rc.d/init.d/functions
- syslog_ng=/usr/local/syslog-ng/sbin/syslog-ng
- prog=syslog-ng
- pidfile=/usr/local/syslog-ng/var/syslog-ng.pid
- lockfile=/usr/local/syslog-ng/var/syslog-ng.lock
- RETVAL=0
- STOP_TIMEOUT=${STOP_TIMEOUT-10}
- start() {
- echo -n $"Starting $prog: "
- daemon --pidfile=$pidfile $syslog_ng $OPTIONS
- RETVAL=$?
- echo
- [ $RETVAL = 0 ] && touch ${lockfile}
- return $RETVAL
- }
- stop() {
- echo -n $"Stopping $prog: "
- killproc -p $pidfile -d $STOP_TIMEOUT $syslog_ng
- RETVAL=$?
- echo
- [ $RETVAL = 0 ] && rm -f $lockfile $pidfile
- }
- case "$1" in
- start)
- start
- ;;
- stop)
- stop
- ;;
- status)
- status -p $pidfile $syslog_ng
- RETVAL=$?
- ;;
- restart)
- stop
- start
- ;;
- *)
- echo $"Usage: $prog {start|stop|restart|status}"
- RETVAL=2
- esac
- exit $RETVAL
- ---------------------------------------
- chmod a+x /etc/init.d/syslog-ng
- killall syslogd
- chkconfig --add syslog-ng
- chkconfig syslog-ng on
- service syslog-ng start
七.日誌客戶端配置
1.syslog客戶端配置
- vim /etc/syslog.conf
- *.* @服務器IP
2.syslog-ng客戶端配置
- vim "syslog-ng的配置文件"
- destination d_local { udp("127.0.0.1" port(514) ); };
八.測試
- 到這裏基本已經配置完成來,下面來測試下,在客戶端上建立一用戶,
- useradd test2
- 而後打開瀏覽器輸入,http://服務器IP:3000
圖一 用戶登陸
能夠看到上面建立test2用戶時的日誌已經顯示出來了。到這裏配置已經結束了。
syslog及syslog-ng的詳細配置見 http://ant595.blog.51cto.com/5074217/1080922