paulwong

          用Kibana和logstash快速搭建實(shí)時(shí)日志查詢、收集與分析系統(tǒng)

            Logstash是一個(gè)完全開(kāi)源的工具,他可以對(duì)你的日志進(jìn)行收集、分析,并將其存儲(chǔ)供以后使用(如,搜索),您可以使用它。說(shuō)到搜索,logstash帶有一個(gè)web界面,搜索和展示所有日志。
          kibana 也是一個(gè)開(kāi)源和免費(fèi)的工具,他可以幫助您匯總、分析和搜索重要數(shù)據(jù)日志并提供友好的web界面。他可以為 Logstash 和 ElasticSearch 提供的日志分析的 Web 界面
          說(shuō)到這里,我們看看 kibana 和 logstash到底能為我們做些什么呢?下面是kibana的界面


            簡(jiǎn)單來(lái)講他具體的工作流程就是 logstash agent 監(jiān)控并過(guò)濾日志,將過(guò)濾后的日志內(nèi)容發(fā)給redis(這里的redis只處理隊(duì)列不做存儲(chǔ)),logstash index將日志收集在一起交給
          全文搜索服務(wù)ElasticSearch 可以用ElasticSearch進(jìn)行自定義搜索 通過(guò)Kibana 來(lái)結(jié)合 自定義搜索進(jìn)行頁(yè)面展示,下圖是 Kibana官網(wǎng)上的流程圖



          好了 讓我們一步步的把這套環(huán)境搭建起來(lái)吧,先看看都需要安裝什么軟件包
          ruby 運(yùn)行Kibana 必須,
          rubygems 安裝ruby擴(kuò)展必須
          bundler 功能類(lèi)似于yum
          JDK 運(yùn)行java程序必須 
          redis 用來(lái)處理日志隊(duì)列
          logstash 收集、過(guò)濾日志
          ElasticSearch 全文搜索服務(wù)(logstash集成了一個(gè))
          kibana 頁(yè)面展示

          這里有三臺(tái)服務(wù)器
          192.168.233.128 logstash index,ElasticSearch,kibana,JDK
          192.168.233.129 logstash agent,JDK
          192.168.233.130 redis


          首先到 logstash index服務(wù)器上面,logstash分為 index和aget ,agent負(fù)責(zé)監(jiān)控、過(guò)濾日志,index負(fù)責(zé)收集日志并將日志交給ElasticSearch 做搜索

          此外 logstash 的收集方式分為 standalone 和 centralized。
          standalone 是所有功能都在一個(gè)服務(wù)器上面,自發(fā)自收,centralized 就是集中收集,一臺(tái)服務(wù)器接收所有shipper(個(gè)人理解就是logstash agent)的日志。
          其實(shí) logstash本身不分 什么 shipper 和 collector ,只不過(guò)就是配置文件不同而已,我們這次按照集中的方式來(lái)測(cè)試


          在 logstash index上安裝基礎(chǔ)的軟件環(huán)境

          1. [192.168.233.128 root@nodec:~] 
          2. # cd /soft/ 
          3. [192.168.233.128 root@nodec:/soft] 
          4. # wget http://down1.chinaunix.net/distfiles/jdk-6u13-dlj-linux-i586.bin 
          5. 從oracle下載實(shí)在是太慢了,從CU下載會(huì)快一些,如果需要最新版本請(qǐng)?jiān)L問(wèn)這里 
          6. http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html 
          7. [192.168.233.128 root@nodec:/soft] 
          8. # sh jdk-6u13-dlj-linux-i586.bin 
          9. 輸入yes 便開(kāi)始安裝了 
          10. 安裝完成后設(shè)置一下 JAVA_HOME 
          11. [192.168.233.128 root@nodec:/soft/Kibana-0.2.0] 
          12. # vim /etc/profile 
          13. export JAVA_HOME=/usr/java 
          14. export PATH=$JAVA_HOME/bin:$PATH 
          15. export CLASSPATH=.:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar:$CLASSPATH 
          16.  
          17. 安裝ruby 就比較簡(jiǎn)單了(Kibana需要ruby 1.8.7以上版本) 
          18. [192.168.233.128 root@nodec:/soft] 
          19. # yum install ruby rubygems 
          20. ..... 安裝內(nèi)容省略 
          21. 安裝完成后用 rubygems 來(lái)安裝bundler 
          22. [192.168.233.128 root@nodec:/soft] 
          23. # /usr/bin/gem install bundler 
          24. ..... 
          25.  
          26. ok 這樣基本的環(huán)境就已經(jīng)有了,下面就是安裝kibana 和 logstash 
          27. 其實(shí)logstash 就是一個(gè)java腳本,不需要安裝... 下載即用 
          28. [192.168.233.128 root@nodec:/soft] 
          29. # wget http://logstash.objects.dreamhost.com/release/logstash-1.1.0-monolithic.jar 
          30. 現(xiàn)在看看 這個(gè)腳本應(yīng)該怎么去執(zhí)行 
          31. [192.168.233.128 root@nodec:/soft] 
          32. # java -jar /soft/logstash-1.1.0-monolithic.jar -h 
          33. No such command "-h" 
          34. Available commands: 
          35.   -v 
          36.   -V 
          37.   --version 
          38.   agent 
          39.   web 
          40.   test 
          41. 顯然沒(méi)有 -h 參數(shù),不過(guò)列出了能用的參數(shù),但是logstash的參數(shù)可不止這些, 
          42. java -jar /soft/logstash-1.1.0-monolithic.jar agent --help 
          43. 這些是在agent模式下的命令參數(shù) 
          44. -f, --config CONFIGFILE 
          45.     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. 
          46. -e CONFIGSTRING 
          47.     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
          48. -w, --filterworks COUNT 
          49.     Run COUNT filter workers (default: 1) 
          50. --watchdog-timeout TIMEOUT 
          51.     Set watchdog timeout value. 
          52. -l, --log FILE 
          53.     Log to a given path. Default is to log to stdout 
          54. -v 
          55.     Increase verbosity. There are multiple levels of verbosity available with '-vv' currently being the highest 
          56. --pluginpath PLUGIN_PATH 
          57.     A colon-delimted path to find other logstash plugins in 
          58. java -jar /soft/logstash-1.1.0-monolithic.jar web --help 
          59. 下面的是在web界面的參數(shù) 
          60. --log FILE 
          61.     Log to a given path. Default is stdout. 
          62. --address ADDRESS 
          63.     Address on which to start webserver. Default is 0.0.0.0. 
          64. --port PORT 
          65.     Port on which to start webserver. Default is 9292. 
          66. -B, --elasticsearch-bind-host ADDRESS 
          67.     Address on which to bind elastic search node. 
          68. -b, --backend URL 
          69.     The backend URL to use. Default is elasticsearch:/// (assumes multicast discovery). You can specify elasticsearch://[host][:port]/[clustername] 
          如果上面的這些命令都能執(zhí)行正常的話就表示 logstash可以使用了,但要讓他啟動(dòng)還需要一個(gè)配置文件
          1. [192.168.233.128 root@nodec:/soft] 
          2.  
          3. # vim redis.conf 
          4.  
          5. input {
          6. redis { 
          7.   host => '192.168.233.130' 
          8.   data_type => 'list' 
          9.   port => "6379" 
          10.   key => 'logstash:redis' 
          11.   type => 'redis-input' 
          12.        } 
          13.        }
          14.  
          15. output { 
          16.     elasticsearch { 
          17.     embedded => true 
          18.                   } 
          19.        } 
          解釋一下 logstash的配置文件由 input filter output 等幾個(gè)基本的部分組成,顧名思義 input 就是在那收集數(shù)據(jù),output就是輸出到哪,filter代表一個(gè)過(guò)濾規(guī)則意思是什么內(nèi)容 會(huì)被收集。 上面這段是讓 logstash 去192.168.233.130 這個(gè)redis服務(wù)器上去收集日志 redis端口為6379,key是 logstash:redis 類(lèi)型為 redis-input ,(注意:這幾個(gè)值必須跟logstash agent的 output 所對(duì)應(yīng)),收集完成后輸出到 elasticsearch ,embedded => true 的意思是使用logstash 內(nèi)嵌的 elasticsearch。如果有獨(dú)立的elasticsearch服務(wù)器,需要將 這條改為 host => 'elasticsearch的ip' port => 端口 好了,這個(gè)簡(jiǎn)單的配置文件可以讓logstash開(kāi)始啟動(dòng)了  
          1. [192.168.233.128 root@nodec:/soft] 
          2. # java -jar /soft/logstash-1.1.0-monolithic.jar agent -f /soft/redis.conf -- web --backend elasticsearch:///?local & 
          3. [1] 5205 
          4. ...這里要等待約5秒鐘... 為什么?去問(wèn)開(kāi)發(fā)者吧 
          5. [192.168.233.128 root@nodec:/soft] 
          6. # 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"} 
          7. 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_ 
          8. Mizuno 0.5.0 (Jetty 8.0.y.z-SNAPSHOT) listening on 0.0.0.0:9292 
          9. 解釋一下 上面的命令 agent 代理模式 -f 指定配置文件 --web 其實(shí)是個(gè)分隔符等于又啟動(dòng)了一個(gè)命令,后面的參數(shù)就是開(kāi)啟一個(gè)web頁(yè)面默認(rèn)端口是9292,這個(gè)命令如果拆成兩個(gè)就是這個(gè)樣子 
          10. java -jar /soft/logstash-1.1.0-monolithic.jar agent -f /soft/redis.conf & 
          11. java -jar /soft/logstash-1.1.0-monolithic.jar web --backend elasticsearch:///?local & (其實(shí)如果用kibana來(lái)做web界面的話這一步完全可以省掉了)
          好了,看到9292 端口啟動(dòng)就代表 啟動(dòng)成功了,檢查一下  
          1. [192.168.233.128 root@nodec:/soft] 
          2. # lsof -i:9292 
          3. COMMAND  PID USER   FD   TYPE DEVICE SIZE NODE NAME 
          4. java    5205 root  465u  IPv4 130805       TCP *:armtechdaemon (LISTEN) 
          5. 其實(shí)logstash還啟動(dòng)了一個(gè)端口9200,因?yàn)閱?dòng)了內(nèi)嵌的 elasticsearch,這個(gè)9200是 elasticsearch在監(jiān)聽(tīng) 
          6. [192.168.233.128 root@nodec:/soft] 
          7. # lsof -i:9200 
          8. COMMAND  PID USER   FD   TYPE DEVICE SIZE NODE NAME 
          9. java    5205 root  160u  IPv4 130682       TCP *:wap-wsp (LISTEN) 
          現(xiàn)在可以通過(guò)瀏覽器訪問(wèn)一下 http://192.168.233.128:9292 看看logstash是的頁(yè)面是個(gè)什么樣子  

          現(xiàn)在還不能搜索因?yàn)楝F(xiàn)在還沒(méi)有數(shù)據(jù),其實(shí)這個(gè)時(shí)候 http://192.168.233.128:9200 也是可以訪問(wèn)的,
          很多開(kāi)發(fā)自己寫(xiě)代碼來(lái)調(diào)用elasticsearch 來(lái)實(shí)現(xiàn)他們自己的需要,這里就不多說(shuō)了
          192.168.233.128 這臺(tái)logstash index的操作暫時(shí)告一段落,下面開(kāi)始配置logstash的agent
          登錄到 服務(wù)器 192.168.233.129 安裝基本軟件包和logstash

          1. [192.168.233.129 root@noded:~] 
          2. # cd /soft/ 
          3. [192.168.233.129 root@noded:/soft] 
          4. # wget http://down1.chinaunix.net/distfiles/jdk-6u13-dlj-linux-i586.bin 
          5. [192.168.233.129 root@noded:/soft] 
          6. # sh jdk-6u13-dlj-linux-i586.bin 
          7. 設(shè)置 JAVA_HOME 
          8. [192.168.233.129 root@noded:/soft] 
          9. # vim /etc/profile 
          10. export JAVA_HOME=/usr/java 
          11. export PATH=$JAVA_HOME/bin:$PATH 
          12. export CLASSPATH=.:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar:$CLASSPATH 
          13. [192.168.233.129 root@noded:/soft] 
          14. # yum install ruby 
          15. 192.168.233.129 root@noded:/soft] 
          16. # wget http://logstash.objects.dreamhost.com/release/logstash-1.1.0-monolithic.jar 
          17. [192.168.233.129 root@noded:/soft] 
          18. # vim redis.conf 
          19. input { 
          20.     file { 
          21.     type => "producer" 
          22.     path => "/soft/apache.log" 
          23.     } 
          24.     file { 
          25.     type => "php-log" 
          26.     path => "/soft/php.log" 
          27.     } 
          28. filter { 
          29.        grep { 
          30.        match => [ "@message""mysql|GET|error" ] 
          31.             } 
          32.        } 
          33.  
          34. output { 
          35.       redis { 
          36.       host => '192.168.233.130' 
          37.       data_type => 'list' 
          38.       key => 'logstash:redis' 
          39.        } 
          40.        } 


             大概說(shuō)一下這個(gè)配置文件 input 里的file就是要監(jiān)視的文件了 這里我監(jiān)視了兩個(gè)文件,如果這兩個(gè)文件有追加的內(nèi)容就會(huì)通過(guò)下面的output設(shè)置發(fā)給 redis服務(wù)器
          filter 里的grep 意思就是 grep...  后面這段就是 日志內(nèi)容里面只要有匹配 mysql或GET或error的內(nèi)容就會(huì)被過(guò)濾出來(lái),發(fā)送到 logstash index
          以上就是一個(gè)比較簡(jiǎn)單的配置文件了,讓我們啟動(dòng)他

          1. [192.168.233.129 root@noded:/soft] 
          2. # java -jar /soft/logstash-1.1.0-monolithic.jar agent -f /soft/redis.conf & 
          3. 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"} 
          4. 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"} 
          5. 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"} 
          6. 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"} 


          只要沒(méi)有 warning 和 error就算是正常啟動(dòng)了
          啟動(dòng)之前請(qǐng)確定 192.168.233.130的 redis服務(wù)器已經(jīng)啟動(dòng),不然會(huì)報(bào)錯(cuò)
          下面登錄到 192.168.233.130 上看看 redis服務(wù)的狀態(tài)

          1. [192.168.233.130 root@nodea:/data/redis/etc] 
          2. # lsof -i:6379 
          3. COMMAND    PID USER   FD   TYPE DEVICE SIZE NODE NAME 
          4. redis-ser 2732 root    4u  IPv4   7946       TCP *:6379 (LISTEN) 
          5. redis-ser 2732 root    5u  IPv4   7963       TCP localhost.localdomain:6379->localhost.localdomain:19214 (ESTABLISHED) 
          6. java      2733 root    9u  IPv4   7959       TCP localhost.localdomain:19214->localhost.localdomain:6379 (ESTABLISHED) 
          7. 狀態(tài)正常,端口處于監(jiān)聽(tīng)狀態(tài),我用的是最簡(jiǎn)單的 配置, 
          8. [192.168.233.130 root@nodea:/data/redis/etc] 
          9. # vim redis.conf 
          10. #this is the config file for redis 
          11. pidfile /var/run/redis.pid 
          12. port 6379 
          13. timeout 0 
          14. loglevel verbose 
          15. logfile /data/redis/log/redis.log 
          16. dbfilename dump.rdb 
          17. dir /data/redis/db/ 
          18. vm-swap-file /tmp/redis.swap 
          19. activerehashing yes 
          20. 啟動(dòng)命令如下 
          21. [192.168.233.130 root@nodea:/data/redis/etc] 
          22. # redis-server /data/redis/etc/redis.conf & 


          下載安裝就比較簡(jiǎn)單了

          1. [192.168.233.130 root@nodea:/soft] 
          2. # wget http://redis.googlecode.com/files/redis-2.4.14.tar.gz 
          3. [192.168.233.130 root@nodea:/data/redis/etc] 
          4. # make –j24 
          5. [192.168.233.130 root@nodea:/data/redis/etc] 
          6. # make install 


          配置文件里的那幾個(gè)路徑要提前建好

          最后我們回到 logstash agent 上面測(cè)試一下

          1. [192.168.233.129 root@noded:/soft] 
          2. # echo GET12313 >> apache.log 
          3. [192.168.233.129 root@noded:/soft] 
          4. # echo errorabcd >> apache.log 


          ok 到 http://192.168.233.128:9292 去搜索一下 剛才的兩個(gè)內(nèi)容


          嗯,就是這樣了,我現(xiàn)在找個(gè)php的錯(cuò)誤日志給他追加到php.log文件里 
          [192.168.233.129 root@noded:/soft]
          # cat php-error.log >> php.log
          在看看 logstash的頁(yè)面 搜索一下 error


          OK,最后就是 Kibana了 ,我把Kibana裝在了 logstash index上面
          下載地址為 http://kibana.org/intro.html

          1. [192.168.233.128 root@nodec:/soft] 
          2. # tar xf Kibana-0.2.0.tar.gz 
          3. [192.168.233.128 root@nodec:/soft] 
          4. # cd Kibana-0.2.0 
          5. [192.168.233.128 root@nodec:/soft/Kibana-0.2.0] 
          6. # bundle install 
          7. 直接安裝就好了,非常簡(jiǎn)單,因?yàn)橹霸蹅円呀?jīng)安裝好了 bundle 
          8. 編輯配置文件,指定 elasticsearch 的位置 
          9. [192.168.233.128 root@nodec:/soft/Kibana-0.2.0] 
          10. # vim KibanaConfig.rb 
          11. ..... 
          12.   Elasticsearch = "localhost:9200" 
          13.   KibanaPort = 5601 
          14.   KibanaHost = '0.0.0.0' 
          15. ..... 
          16. 主要是這幾個(gè)參數(shù) 
          17. 啟動(dòng)的話需要ruby 
          18. [192.168.233.128 root@nodec:/soft/Kibana-0.2.0] 
          19. # /usr/bin/ruby kibana.rb & 
          20. [192.168.233.128 root@nodec:/soft/Kibana-0.2.0] 
          21. # == Sinatra/1.3.5 has taken the stage on 5601 for development with backup from Thin 
          22. >> Thin web server (v1.5.0 codename Knife) 
          23. >> Maximum connections set to 1024 
          24. >> Listening on 0.0.0.0:5601, CTRL+C to stop 
          25. 如果ruby的東西都不缺的話,啟動(dòng)會(huì)很順利,ok 現(xiàn)在看看5601端口的狀態(tài) 
          26. [192.168.233.128 root@nodec:/soft/Kibana-0.2.0] 
          27. # lsof -i:5601 
          28. COMMAND  PID USER   FD   TYPE DEVICE SIZE NODE NAME 
          29. ruby    3116 root    5u  IPv4  28947       TCP *:esmagent (LISTEN) 


          訪問(wèn)一下 試試看 http://192.168.233.128:5601 嘗試搜索一下php的錯(cuò)誤日志,比如mysql


          呵呵,要的就是這個(gè)效果,日志會(huì)實(shí)時(shí)的匯總到 logstash index 上供我們查詢,當(dāng)然這只是開(kāi)始使用logstash的第一步而已,更多的高級(jí)功能可以看看官方文檔
          http://logstash.net/docs/1.1.9/
          如果有問(wèn)題大家可以一起探討,我也是剛開(kāi)始接觸這個(gè)東東,收集日志是相當(dāng)方便啊,據(jù)說(shuō)還能跟nagios結(jié)合. 呵呵

          posted on 2014-09-30 13:14 paulwong 閱讀(8578) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): LOG ANALYST BIG DATA SYSTEM

          主站蜘蛛池模板: 布尔津县| 瑞昌市| 黄陵县| 阿坝| 太白县| 米易县| 金平| 鄱阳县| 台东市| 大关县| 哈密市| 新巴尔虎左旗| 多伦县| 铁岭市| 徐州市| 苍山县| 栾川县| 芜湖市| 永济市| 定州市| 高台县| 汕头市| 大安市| 定结县| 阳谷县| 务川| 龙口市| 镇巴县| 桐城市| 炉霍县| 大厂| 永丰县| 年辖:市辖区| 梓潼县| 明星| 英超| 石河子市| 藁城市| 时尚| 乌鲁木齐县| 文成县|