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

p4cf 9年前發布 | 132K 次閱讀 日志處理 ElasticSearch

世界上的軟件80%是運行在內網的,為了使得運行在客戶端的軟件有良好的體驗,并且得到有用的數據,我們需要對這些系統產生的數據,進行統計和分析,這個過程通常包括數據采集,清洗,建模,分析,報表等。接下來在本篇文章中,將會構建一個基于logstash,elasticSearchkibana的一套數據收集分析的系統

一、框架概要

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

logstash實時的從業務日志中數據數據(可以進行響應的過濾),然后把數據輸入到redis中,redis只做消息隊列不對消息做處理和存儲,然后redis會把數據轉給elasticSearchelasticSearch會對數據做存儲,索引(基于Lunce),再kibana中建立對elasticSearch的鏈接,實時的抓取索索引后的數據,這樣數據就可以實時的進行展示,通過一些數據組裝,查詢條件,得到我們想要的結果(可以通過各種方式例如圖表,表格,折線圖等顯示)

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

二、所需軟件

Redishttps://www.elastic.co/

kibanahttps://download.elastic.co/kibana/kibana/kibana-4.1.3-linux-x64.tar.gz

logstashhttps://download.elastic.co/logstash/logstash/logstash-2.1.0.zip

Elasticsearch

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

對于kibanalogstashElasticsearch 這三件套,也可以是直接去下載套裝(https://www.elastic.co/downloads

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

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

三、安裝

軟件的安裝和運行,最好不要使用root用戶,因為里面有些軟件,必須要求非root用于才可以正常的運行。

1、 卸載自帶的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)

2、 安裝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


3、安裝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安裝運行成功


4、 安裝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 &   # "&"為在后臺執行

5、安裝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

 本文由用戶 p4cf 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!