Logstash是一個徹底開源的工具,他能夠對你的日誌進行收集、分析,並將其存儲供之後使用(如,搜索),您可使用它。說到搜索,logstash帶有一個web界面,搜索和展現全部日誌。
kibana 也是一個開源和免費的工具,他能夠幫助您彙總、分析和搜索重要數據日誌並提供友好的web界面。他能夠爲 Logstash 和 ElasticSearch 提供的日誌分析的 Web 界面
說到這裏,咱們看看 kibana 和 logstash到底能爲咱們作些什麼呢?下面是kibana的界面php
簡單來說他具體的工做流程就是 logstash agent 監控並過濾日誌,將過濾後的日誌內容發給redis(這裏的redis只處理隊列不作存儲),logstash index將日誌收集在一塊兒交給
全文搜索服務ElasticSearch 能夠用ElasticSearch進行自定義搜索 經過Kibana 來結合 自定義搜索進行頁面展現,下圖是 Kibana官網上的流程圖html
好了 讓咱們一步步的把這套環境搭建起來吧,先看看都須要安裝什麼軟件包
ruby 運行Kibana 必須,
rubygems 安裝ruby擴展必須
bundler 功能相似於yum
JDK 運行java程序必須
redis 用來處理日誌隊列
logstash 收集、過濾日誌
ElasticSearch 全文搜索服務(logstash集成了一個)
kibana 頁面展現
這裏有三臺服務器
192.168.233.128 logstash index,ElasticSearch,kibana,JDK
192.168.233.129 logstash agent,JDK
192.168.233.130 redis
首先到 logstash index服務器上面,logstash分爲 index和aget ,agent負責監控、過濾日誌,index負責收集日誌並將日誌交給ElasticSearch 作搜索java
此外 logstash 的收集方式分爲 standalone 和 centralized。
standalone 是全部功能都在一個服務器上面,自發自收,centralized 就是集中收集,一臺服務器接收全部shipper(我的理解就是logstash agent)的日誌。
其實 logstash自己不分 什麼 shipper 和 collector ,只不過就是配置文件不一樣而已,咱們此次按照集中的方式來測試node
在 logstash index上安裝基礎的軟件環境mysql
若是上面的這些命令都能執行正常的話就表示 logstash可使用了,但要讓他啓動還須要一個配置文件
- [192.168.233.128 root@nodec:~]
- # cd /soft/
- [192.168.233.128 root@nodec:/soft]
- # wget http://down1.chinaunix.net/distfiles/jdk-6u13-dlj-linux-i586.bin
- 從oracle下載實在是太慢了,從CU下載會快一些,若是須要最新版本請訪問這裏
- http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html
- [192.168.233.128 root@nodec:/soft]
- # sh jdk-6u13-dlj-linux-i586.bin
- 輸入yes 便開始安裝了
- 安裝完成後設置一下 JAVA_HOME
- [192.168.233.128 root@nodec:/soft/Kibana-0.2.0]
- # vim /etc/profile
- export JAVA_HOME=/usr/java
- export PATH=$JAVA_HOME/bin:$PATH
- export CLASSPATH=.:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar:$CLASSPATH
- 安裝ruby 就比較簡單了(Kibana須要ruby 1.8.7以上版本)
- [192.168.233.128 root@nodec:/soft]
- # yum install ruby rubygems
- ..... 安裝內容省略
- 安裝完成後用 rubygems 來安裝bundler
- [192.168.233.128 root@nodec:/soft]
- # /usr/bin/gem install bundler
- .....
- ok 這樣基本的環境就已經有了,下面就是安裝kibana 和 logstash
- 其實logstash 就是一個java腳本,不須要安裝... 下載即用
- [192.168.233.128 root@nodec:/soft]
- # wget http://logstash.objects.dreamhost.com/release/logstash-1.1.0-monolithic.jar
- 如今看看 這個腳本應該怎麼去執行
- [192.168.233.128 root@nodec:/soft]
- # java -jar /soft/logstash-1.1.0-monolithic.jar -h
- No such command "-h"
- Available commands:
- -v
- -V
- --version
- agent
- web
- test
- 顯然沒有 -h 參數,不過列出了能用的參數,可是logstash的參數可不止這些,
- java -jar /soft/logstash-1.1.0-monolithic.jar agent --help
- 這些是在agent模式下的命令參數
- -f, --config CONFIGFILE
- Load the logstash config from a specific file, directory, or a wildcard. If given a directory or wildcard, config files will be read in order lexigraphically.
- -e CONFIGSTRING
- Use the given string as the configuration data. Same syntax as the config file. If not input is specified, 'stdin { type => stdin }' is default. If no output is specified, 'stdout { debug => true }}' is default.
- -w, --filterworks COUNT
- Run COUNT filter workers (default: 1)
- --watchdog-timeout TIMEOUT
- Set watchdog timeout value.
- -l, --log FILE
- Log to a given path. Default is to log to stdout
- -v
- Increase verbosity. There are multiple levels of verbosity available with '-vv' currently being the highest
- --pluginpath PLUGIN_PATH
- A colon-delimted path to find other logstash plugins in
- java -jar /soft/logstash-1.1.0-monolithic.jar web --help
- 下面的是在web界面的參數
- --log FILE
- Log to a given path. Default is stdout.
- --address ADDRESS
- Address on which to start webserver. Default is 0.0.0.0.
- --port PORT
- Port on which to start webserver. Default is 9292.
- -B, --elasticsearch-bind-host ADDRESS
- Address on which to bind elastic search node.
- -b, --backend URL
- The backend URL to use. Default is elasticsearch:/// (assumes multicast discovery). You can specify elasticsearch://[host][:port]/[clustername]
- [192.168.233.128 root@nodec:/soft]
- # vim redis.conf
- input {
- redis {
- host => '192.168.233.130'
- data_type => 'list'
- port => "6379"
- key => 'logstash:redis'
- type => 'redis-input'
- }
- }
- output {
- elasticsearch {
- embedded => true
- }
- }
解釋一下 logstash的配置文件由 input filter output 等幾個基本的部分組成,顧名思義 input 就是在那收集數據,output就是輸出到哪,filter表明一個過濾規則意思是什麼內容 會被收集。 上面這段是讓 logstash 去192.168.233.130 這個redis服務器上去收集日誌 redis端口爲6379,key是 logstash:redis 類型爲 redis-input ,(注意:這幾個值必須跟logstash agent的 output 所對應),收集完成後輸出到 elasticsearch ,embedded => true 的意思是使用logstash 內嵌的 elasticsearch。若是有獨立的elasticsearch服務器,須要將 這條改成 host => 'elasticsearch的ip' port => 端口 好了,這個簡單的配置文件可讓logstash開始啓動了
- [192.168.233.128 root@nodec:/soft]
- # java -jar /soft/logstash-1.1.0-monolithic.jar agent -f /soft/redis.conf -- web --backend elasticsearch:///?local &
- [1] 5205
- ...這裏要等待約5秒鐘... 爲何?去問開發者吧
- [192.168.233.128 root@nodec:/soft]
- # I, [2013-03-19T03:23:10.749000 #5205] INFO -- : Using beta plugin 'redis'. For more information about plugin statuses, see http://logstash.net/docs/1.1.0/plugin-status {"timestamp":"2013-03-19T03:23:10.732000 -0700","message":"Using beta plugin 'redis'. For more information about plugin statuses, see http://logstash.net/docs/1.1.0/plugin-status ","level":"info"}
- file:/soft/logstash-1.1.0-monolithic.jar!/gems/rack-1.3.4/lib/rack/backports/uri/common_192.rb:53 warning: already initialized constant WFKV_
- Mizuno 0.5.0 (Jetty 8.0.y.z-SNAPSHOT) listening on 0.0.0.0:9292
- 解釋一下 上面的命令 agent 代理模式 -f 指定配置文件 --web 實際上是個分隔符等於又啓動了一個命令,後面的參數就是開啓一個web頁面默認端口是9292,這個命令若是拆成兩個就是這個樣子
- java -jar /soft/logstash-1.1.0-monolithic.jar agent -f /soft/redis.conf &
- java -jar /soft/logstash-1.1.0-monolithic.jar web --backend elasticsearch:///?local & (其實若是用kibana來作web界面的話這一步徹底能夠省掉了)
好了,看到9292 端口啓動就表明 啓動成功了,檢查一下
- [192.168.233.128 root@nodec:/soft]
- # lsof -i:9292
- COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
- java 5205 root 465u IPv4 130805 TCP *:armtechdaemon (LISTEN)
- 其實logstash還啓動了一個端口9200,由於啓動了內嵌的 elasticsearch,這個9200是 elasticsearch在監聽
- [192.168.233.128 root@nodec:/soft]
- # lsof -i:9200
- COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
- java 5205 root 160u IPv4 130682 TCP *:wap-wsp (LISTEN)
如今能夠經過瀏覽器訪問一下 http://192.168.233.128:9292 看看logstash是的頁面是個什麼樣子
如今還不能搜索由於如今尚未數據,其實這個時候 http://192.168.233.128:9200 也是能夠訪問的,
不少開發本身寫代碼來調用elasticsearch 來實現他們本身的須要,這裏就很少說了
192.168.233.128 這臺logstash index的操做暫時告一段落,下面開始配置logstash的agent
登陸到 服務器 192.168.233.129 安裝基本軟件包和logstashlinux
- [192.168.233.129 root@noded:~]
- # cd /soft/
- [192.168.233.129 root@noded:/soft]
- # wget http://down1.chinaunix.net/distfiles/jdk-6u13-dlj-linux-i586.bin
- [192.168.233.129 root@noded:/soft]
- # sh jdk-6u13-dlj-linux-i586.bin
- 設置 JAVA_HOME
- [192.168.233.129 root@noded:/soft]
- # vim /etc/profile
- export JAVA_HOME=/usr/java
- export PATH=$JAVA_HOME/bin:$PATH
- export CLASSPATH=.:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar:$CLASSPATH
- [192.168.233.129 root@noded:/soft]
- # yum install ruby
- 192.168.233.129 root@noded:/soft]
- # wget http://logstash.objects.dreamhost.com/release/logstash-1.1.0-monolithic.jar
- [192.168.233.129 root@noded:/soft]
- # vim redis.conf
- input {
- file {
- type => "producer"
- path => "/soft/apache.log"
- }
- file {
- type => "php-log"
- path => "/soft/php.log"
- }
- }
- filter {
- grep {
- match => [ "@message", "mysql|GET|error" ]
- }
- }
- output {
- redis {
- host => '192.168.233.130'
- data_type => 'list'
- key => 'logstash:redis'
- }
- }
大概說一下這個配置文件 input 裏的file就是要監視的文件了 這裏我監視了兩個文件,若是這兩個文件有追加的內容就會經過下面的output設置發給 redis服務器
filter 裏的grep 意思就是 grep... 後面這段就是 日誌內容裏面只要有匹配 mysql或GET或error的內容就會被過濾出來,發送到 logstash index
以上就是一個比較簡單的配置文件了,讓咱們啓動他ios
- [192.168.233.129 root@noded:/soft]
- # java -jar /soft/logstash-1.1.0-monolithic.jar agent -f /soft/redis.conf &
- I, [2013-03-19T19:45:35.762000 #2721] INFO -- : Using beta plugin 'file'. For more information about plugin statuses, see http://logstash.net/docs/1.1.0/plugin-status {"timestamp":"2013-03-19T19:45:35.752000 -0700","message":"Using beta plugin 'file'. For more information about plugin statuses, see http://logstash.net/docs/1.1.0/plugin-status ","level":"info"}
- I, [2013-03-19T19:45:35.778000 #2721] INFO -- : Using beta plugin 'file'. For more information about plugin statuses, see http://logstash.net/docs/1.1.0/plugin-status {"timestamp":"2013-03-19T19:45:35.778000 -0700","message":"Using beta plugin 'file'. For more information about plugin statuses, see http://logstash.net/docs/1.1.0/plugin-status ","level":"info"}
- I, [2013-03-19T19:45:35.804000 #2721] INFO -- : Using beta plugin 'grep'. For more information about plugin statuses, see http://logstash.net/docs/1.1.0/plugin-status {"timestamp":"2013-03-19T19:45:35.803000 -0700","message":"Using beta plugin 'grep'. For more information about plugin statuses, see http://logstash.net/docs/1.1.0/plugin-status ","level":"info"}
- I, [2013-03-19T19:45:35.854000 #2721] INFO -- : Using beta plugin 'redis'. For more information about plugin statuses, see http://logstash.net/docs/1.1.0/plugin-status {"timestamp":"2013-03-19T19:45:35.853000 -0700","message":"Using beta plugin 'redis'. For more information about plugin statuses, see http://logstash.net/docs/1.1.0/plugin-status ","level":"info"}
只要沒有 warning 和 error就算是正常啓動了
啓動以前請肯定 192.168.233.130的 redis服務器已經啓動,否則會報錯
下面登陸到 192.168.233.130 上看看 redis服務的狀態web
- [192.168.233.130 root@nodea:/data/redis/etc]
- # lsof -i:6379
- COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
- redis-ser 2732 root 4u IPv4 7946 TCP *:6379 (LISTEN)
- redis-ser 2732 root 5u IPv4 7963 TCP localhost.localdomain:6379->localhost.localdomain:19214 (ESTABLISHED)
- java 2733 root 9u IPv4 7959 TCP localhost.localdomain:19214->localhost.localdomain:6379 (ESTABLISHED)
- 狀態正常,端口處於監聽狀態,我用的是最簡單的 配置,
- [192.168.233.130 root@nodea:/data/redis/etc]
- # vim redis.conf
- #this is the config file for redis
- pidfile /var/run/redis.pid
- port 6379
- timeout 0
- loglevel verbose
- logfile /data/redis/log/redis.log
- dbfilename dump.rdb
- dir /data/redis/db/
- vm-swap-file /tmp/redis.swap
- activerehashing yes
- 啓動命令以下
- [192.168.233.130 root@nodea:/data/redis/etc]
- # redis-server /data/redis/etc/redis.conf &
下載安裝就比較簡單了redis
- [192.168.233.130 root@nodea:/soft]
- # wget http://redis.googlecode.com/files/redis-2.4.14.tar.gz
- [192.168.233.130 root@nodea:/data/redis/etc]
- # make –j24
- [192.168.233.130 root@nodea:/data/redis/etc]
- # make install
配置文件裏的那幾個路徑要提早建好
最後咱們回到 logstash agent 上面測試一下sql
- [192.168.233.129 root@noded:/soft]
- # echo GET12313 >> apache.log
- [192.168.233.129 root@noded:/soft]
- # echo errorabcd >> apache.log
ok 到 http://192.168.233.128:9292 去搜索一下 剛纔的兩個內容
嗯,就是這樣了,我如今找個php的錯誤日誌給他追加到php.log文件裏
[192.168.233.129 root@noded:/soft]
# cat php-error.log >> php.log
在看看 logstash的頁面 搜索一下 error
OK,最後就是 Kibana了 ,我把Kibana裝在了 logstash index上面
下載地址爲 http://kibana.org/intro.html
- [192.168.233.128 root@nodec:/soft]
- # tar xf Kibana-0.2.0.tar.gz
- [192.168.233.128 root@nodec:/soft]
- # cd Kibana-0.2.0
- [192.168.233.128 root@nodec:/soft/Kibana-0.2.0]
- # bundle install
- 直接安裝就行了,很是簡單,由於以前我們已經安裝好了 bundle
- 編輯配置文件,指定 elasticsearch 的位置
- [192.168.233.128 root@nodec:/soft/Kibana-0.2.0]
- # vim KibanaConfig.rb
- .....
- Elasticsearch = "localhost:9200"
- KibanaPort = 5601
- KibanaHost = '0.0.0.0'
- .....
- 主要是這幾個參數
- 啓動的話須要ruby
- [192.168.233.128 root@nodec:/soft/Kibana-0.2.0]
- # /usr/bin/ruby kibana.rb &
- [192.168.233.128 root@nodec:/soft/Kibana-0.2.0]
- # == Sinatra/1.3.5 has taken the stage on 5601 for development with backup from Thin
- >> Thin web server (v1.5.0 codename Knife)
- >> Maximum connections set to 1024
- >> Listening on 0.0.0.0:5601, CTRL+C to stop
- 若是ruby的東西都不缺的話,啓動會很順利,ok 如今看看5601端口的狀態
- [192.168.233.128 root@nodec:/soft/Kibana-0.2.0]
- # lsof -i:5601
- COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
- ruby 3116 root 5u IPv4 28947 TCP *:esmagent (LISTEN)
訪問一下 試試看 http://192.168.233.128:5601 嘗試搜索一下php的錯誤日誌,好比mysql
呵呵,要的就是這個效果,日誌會實時的彙總到 logstash index 上供咱們查詢,固然這只是開始使用logstash的第一步而已,更多的高級功能能夠看看官方文檔 http://logstash.net/docs/1.1.9/ 若是有問題你們能夠一塊兒探討,我也是剛開始接觸這個東東,收集日誌是至關方便啊,聽說還能跟nagios結合. 呵呵