用logstash,elasticSearch,kibana實現數據收集和統計分析工做

原文連接:http://www.open-open.com/lib/view/open1448799635720.htmlhtml

世界上的軟件80%是運行在內網的,爲了使得運行在客戶端的軟件有良好的體驗,而且獲得有用的數據,咱們須要對這些系統產生的數據,進行統計和分析,這個過程一般包括數據採集,清洗,建模,分析,報表等。接下來在本篇文章中,將會構建一個基於logstash,elasticSearch,kibana的一套數據收集分析的系統java

1、框架概要

用logstash,elasticSearch,kibana實現數據收集和統計分析工做

logstash實時的從業務日誌中數據數據(能夠進行響應的過濾),而後把數據輸入到redis中,redis只作消息隊列不對消息作處理和存儲,而後redis會把數據轉給elasticSearch,elasticSearch會對數據作存儲,索引(基於Lunce),再kibana中創建對elasticSearch的連接,實時的抓取索索引後的數據,這樣數據就能夠實時的進行展現,經過一些數據組裝,查詢條件,獲得咱們想要的結果(能夠經過各類方式例如圖表,表格,折線圖等顯示)node

接下來的第二章節講述所需軟件以及安裝linux

2、所需軟件

Redis(https://www.elastic.co/)nginx

kibana(https://download.elastic.co/kibana/kibana/kibana-4.1.3-linux-x64.tar.gz)redis

logstash(https://download.elastic.co/logstash/logstash/logstash-2.1.0.zip)shell

Elasticsearchruby

https://download.elasticsearch.org/elasticsearch/release/org/elasticsearch/distribution/tar/elasticsearch/2.1.0/elasticsearch-2.1.0.tar.gz服務器

對於kibana,logstash,Elasticsearch 這三件套,也能夠是直接去下載套裝(https://www.elastic.co/downloadsapp

用logstash,elasticSearch,kibana實現數據收集和統計分析工做

把軟件包均上傳到/opt/tools文件夾下,軟件均安裝在/opt文件夾下

 

3、安裝

軟件的安裝和運行,最好不要使用root用戶,由於裏面有些軟件,必需要求非root用於才能夠正常的運行。

一、 卸載自帶的jdk 安裝jdk

上傳下載的jdk rpm包

安裝:

rpm –ivh  
jdk-8u65-linux-x64.rpm

經過命令查看jdk版本是否安裝成功,安裝完成它會自動的配置環境變量等相關信息

java -version
java version "1.8.0_65"
Java(TM) SE Runtime Environment (build 1.8.0_65-b17)
Java HotSpot(TM) 64-Bit Server VM (build 25.65-b01, mixed mode)

 

二、 安裝Redis

安裝:

tar xzf redis-3.0.5.tar.gz /opt
$ cd redis-3.0.5
$ make && make install

 

啓動

$ src/redis-server

用logstash,elasticSearch,kibana實現數據收集和統計分析工做

 

出現以上提示,redis啓動成功

客戶端測試

$ src/redis-cli
redis> set foo bar
OK
redis> get foo
"bar"

因爲安裝完成後,redis只能窗口化運行,當關閉server的那個shell窗口之後,服務就終端了,因此須要修改相關配置

Vi redis.conf

 

修改:

daemonize yes 

appendonly yes

 

這樣redis能夠支持後臺運行

其餘:
一樣,咱們能夠把Redis做爲Linux服務開機啓動
這裏只提供一種最簡單的方式,最好的是經過編寫開機啓動腳原本作。
若是要開機啓動redis,咱們須要把redis設置爲daemon後臺啓動(若是不設置爲後臺啓動,則linux啓動後圖形界面會卡在一個空白的頁面),而redis只有1個啓動參數,就是redis的配置文件路徑。redis的默認配置文件redis.conf位於redis的安裝目錄下。咱們能夠把該文件copy到/etc目錄下
Shell代碼 
1[root@localhost redis-2.6.14]# cp redis.conf /etc/ 
redis的默認配置文件中daemonize參數的值爲no,表明爲非後臺啓動,因此咱們須要把該參數的值修改成yes。至於其它的參數在這裏就不詳細說了,具體能夠參見:http://blog.csdn.net/htofly/article/details/7686436
修改完daemonize參數以後,redis就可以經過daemon方式啓動了,那麼下一步就是把redis加入到linux開機啓動服務配置中了,具體步驟以下:
使用VI編輯器打開Linux開機啓動服務配置文件/etc/rc.local,並在其中加入下面的一行代碼:
Shell代碼 
2/usr/local/redis-2.6.14/src/redis-server /etc/redis.conf 
編輯完後保存,而後重啓系統就OK了。
中止Redis服務:
Shell代碼 
src/redis-cli shutdown

 

三、安裝elasticsearch

tar zxvf elasticsearch-2.1.0.tar.gz

進入elasticsearch-2.1.0文件夾,須要注意的是,elasticsearch不容許用root帳戶進行啓動、

./bin/elasticsearch

elasticSearch建議用一個專用的用戶進行操做,若是用root用戶啓動的話,會報錯,

會報以下錯誤:

 Don’t run Elasticsearch as root

在如下網址文檔中有詳細的說明

(https://www.elastic.co/blog/scripting-security)

curl -X GET http://localhost:9200/
打印出如下信息:
{
  "name" : "Witchfire",
  "cluster_name" : "elasticsearch",
  "version" : {
    "number" : "2.1.0",
    "build_hash" : "72cd1f1a3eee09505e036106146dc1949dc5dc87",
    "build_timestamp" : "2015-11-18T22:40:03Z",
    "build_snapshot" : false,
    "lucene_version" : "5.3.1"
  },
  "tagline" : "You Know, for Search"
}

ElasticSearch安裝運行成功

 

 

四、 安裝Logstash

cd /opt/tools
unzip logstash-2.1.0.zip
mv logstash-2.1.0 /opt

 

創建服務器啓動的配置文件

cd logstash-2.10.0/
mkdir conf log
cd conf
vi server.conf
 
input { stdin { }
         redis{
              type => "redis-input"
              data_type => "list"
              key => "key_count"
         }
 }
output {
  elasticsearch { hosts => ["localhost:9200"] }
  stdout { codec => rubydebug }
}

 

//reids地址是127.0.0.1.默認端口是6379 因爲我都安裝在了本機,因此這個就沒有進行再配置,相關配置能夠參考如下官網網址:

https://www.elastic.co/guide/en/logstash/current/plugins-inputs-redis.html

啓動服務端

#bin/logstash -f  conf/server.conf --log logs/stdout &   # "&"爲在後臺執行
#bin/logstash -f  conf/client.conf --log logs/stdout &   # "&"爲在後臺執行

五、安裝kibana

cd /opt/tools
tar -zxvf kibana-4.1.3-linux-x64.tar.gz -C /opt
cd /opt

 

能夠在kibana/conf/文件夾中,修改kibana.yml配置文件,修改 elasticsearch的訪問路徑爲相關的訪問路徑,默認爲:

elasticsearch_url: http://localhost:9200

因爲個人elasticearc安裝在了本機,這個地址和端口就暫時不作修改了

默認會有.kibana索引,若是沒有的話再進行創建

執行命令

curl -XPUT localhost:9200/.kibana

用logstash,elasticSearch,kibana實現數據收集和統計分析工做

剛開始下載的是kibana 的版本是4.1.3,而後啓動的時候報錯,

{"name":"Kibana","hostname":"test-dev1","pid":15605,"level":30,"msg":"Found kibana index","time":"2015-11-26T07:49:48.672Z","v":0}
{"name":"Kibana","hostname":"test-dev1","pid":15605,"level":50,"err":{"message":{"root_cause":[{"type":"search_parse_exception","reason":"No mapping found for [buildNum] in order to sort on"}],"type":"search_phase_execution_exception","reason":"all shards failed","phase":"query","grouped":true,"failed_shards":[{"shard":0,"index":".kibana","node":"uj6DUqLOSZOWghUpRlegmw","reason":{"type":"search_parse_exception","reason":"No mapping found for [buildNum] in order to sort on"}}]},"name":"Error","stack":"Error: [object Object]\n    at respond (/opt/kibana-4.1.3-linux-x64/src/node_modules/elasticsearch/src/lib/transport.js:235:15)\n    at checkRespForFailure (/opt/kibana-4.1.3-linux-x64/src/node_modules/elasticsearch/src/lib/transport.js:203:7)\n    at HttpConnector.<anonymous> (/opt/kibana-4.1.3-linux-x64/src/node_modules/elasticsearch/src/lib/connectors/http.js:156:7)\n    at IncomingMessage.bound (/opt/kibana-4.1.3-linux-x64/src/node_modules/elasticsearch/node_modules/lodash-node/modern/internals/baseBind.js:56:17)\n    at IncomingMessage.emit (events.js:117:20)\n    at _stream_readable.js:944:16\n    at process._tickCallback (node.js:442:13)"},"msg":"","time":"2015-11-26T07:49:48.683Z","v":0}
{"name":"Kibana","hostname":"test-dev1","pid":15605,"level":60,"err":{"message":{"root_cause":[{"type":"search_parse_exception","reason":"No mapping found for [buildNum] in order to sort on"}],"type":"search_phase_execution_exception","reason":"all shards failed","phase":"query","grouped":true,"failed_shards":[{"shard":0,"index":".kibana","node":"uj6DUqLOSZOWghUpRlegmw","reason":{"type":"search_parse_exception","reason":"No mapping found for [buildNum] in order to sort on"}}]},"name":"Error","stack":"Error: [object Object]\n    at respond (/opt/kibana-4.1.3-linux-x64/src/node_modules/elasticsearch/src/lib/transport.js:235:15)\n    at checkRespForFailure (/opt/kibana-4.1.3-linux-x64/src/node_modules/elasticsearch/src/lib/transport.js:203:7)\n    at HttpConnector.<anonymous> (/opt/kibana-4.1.3-linux-x64/src/node_modules/elasticsearch/src/lib/connectors/http.js:156:7)\n    at IncomingMessage.bound (/opt/kibana-4.1.3-linux-x64/src/node_modules/elasticsearch/node_modules/lodash-node/modern/internals/baseBind.js:56:17)\n    at IncomingMessage.emit (events.js:117:20)\n    at _stream_readable.js:944:16\n    at process._tickCallback (node.js:442:13)"},"msg":"","time":"2015-11-26T07:49:48.683Z","v":0}

 

通過搜索得知,原來是版本問題,從新下載了4.3版本。從新啓動,正常

啓動kibana,默認端口爲5601

用logstash,elasticSearch,kibana實現數據收集和統計分析工做

默認是沒有任何日誌的,當咱們訪問nginx的時候,會產生一些訪問日誌,回頭再看kibana,日誌將會實時的顯示到kibana中,深刻的去學習kibana的使用,將會作出來咱們想要的數據。

用logstash,elasticSearch,kibana實現數據收集和統計分析工做

用logstash,elasticSearch,kibana實現數據收集和統計分析工做

來自:http://my.oschina.net/u/2457218/blog/536893

相關文章
相關標籤/搜索