ELKStack 實戰之 Logstash

一、Logstash介紹

Logstash是一個完全開源的工具,他可以對你的日誌進行收集、分析,並將其存儲供以後使用(如,搜索),您可以使用它。說到搜索,logstash帶有一個web界面,搜索和展示所有日誌。 
  kibana也是一個開源和免費的工具,他可以幫助您彙總、分析和搜索重要數據日誌並提供友好的web界面。他可以爲 Logstash 和 ElasticSearch 提供的日誌分析的 Web 界面。

提示: logstash與es沒有任何關係 
image_1bdc0c7fn1ir6179p18n01bffgs19.png-69.5kB

ELK幾個概念

INPUT

OUTPUT

首先LogStash是收集日誌,它收集完日誌就需要把日誌存儲下來,所以我們可以用運輸者的身份來表示LogStash(INPUT or OUTPUT)LogStash可以在日誌發送之前做一個過濾(OUTPUT之前)

二、LogStash部署與配置

和Elasticsearch一樣,在開始部署LogStash之前也需要你的環境中正確的安裝的JDK。可以下載安裝Oracle的JDK或者使用 yum安裝openjdk。

LogStash三大功能

  1. INPUT
  2. FILTER (支持過濾的功能)
  3. OUTPUT

image_1bdc0cn4v14l1gcj19mq10ncfdtm.png-59.6kB

1.設置時區

  1. [root@abcdocker ~]# yum install ntpdate y
  2. [root@abcdocker ~]# ntpdate time1.aliyun.com
  3. [root@abcdocker ~]# timedatectl set-timezone Asia/Shanghai #設置時區
  4. 另一種設置時區方法
  5. [root@abcdocker ~]# cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

像我這樣高逼格的玩家一般都把這種命令用一條顯示

  1. [root@abcdocker ~]# yum install ntpdate -y && ntpdate time1.aliyun.com && timedatectl set-timezone Asia/Shanghai

2.安裝JDK

  1. [root@abcdocker ~]# yum install -y java
  2. [root@abcdocker ~]# java -version
  3. openjdk version "1.8.0_65"
  4. OpenJDK Runtime Environment (build 1.8.0_65-b17)
  5. OpenJDK 64-Bit Server VM (build 25.65-b01, mixed mode)

3.YUM部署LogStash 
下載並安裝GPG key

  1. [root@abcdocker ~]# rpm --import https://packages.elastic.co/GPG-KEY-elasticsearch

我們可以查詢是否導入成功,可能有很多key

  1. [root@abcdocker ~]# rpm -qa gpg-pubkey*
  2. gpg-pubkey-f4a80eb5-53a7ff4b
  3. gpg-pubkey-d88e42b4-52371eca

4.添加yum倉庫

  1. [root@abcdocker ~]# cat /etc/yum.repos.d/logstash.repo
  2. [logstash-2.3]
  3. name=Logstash repository for 2.3.x packages
  4. baseurl=https://packages.elastic.co/logstash/2.3/centos
  5. gpgcheck=1
  6. gpgkey=https://packages.elastic.co/GPG-KEY-elasticsearch
  7. enabled=1

5.安裝logstash

  1. [root@abcdocker ~]# yum install -y logstash
  2. [root@abcdocker ~]# rpm -qa |grep logst
  3. logstash-2.3.4-1.noarch
  4. #請在2臺服務器都進行安裝

6. 配置logstash

提示:logstash實現input和ouput主要是依賴於logstash有需多的插件

  1. [root@abcdocker ~]# /opt/logstash/bin/logstash -e 'input { stdin{} } output { stdout{} }'
  2. OpenJDK 64-Bit Server VM warning: If the number of processors is expected to increase from one, then you should configure the number of parallel GC threads appropriately using -XX:ParallelGCThreads=N
  3. Settings: Default pipeline workers: 1
  4. Pipeline main started
  5. www.abcdocker.com
  6. 2017-03-12T04:13:31.347Z abcdocker.com www.abcdocker.com
  7. input插件stdin(標準輸入)
  8. output插件 stdout(標準輸出)
  9. /opt/logstash/bin/logstash 前臺啓動
  10. 提示:標準輸入和標準輸出的意思就是我們輸入什麼就會輸出什麼
  11. 輸入:www.abcdocker.com
  12. 輸入:時間戳、節點名稱、內容

使用codec rubydebug功能

  1. [root@abcdocker ~]# /opt/logstash/bin/logstash -e 'input { stdin{} } output { stdout{ codec => rubydebug} }'
  2. OpenJDK 64-Bit Server VM warning: If the number of processors is expected to increase from one, then you should configure the number of parallel GC threads appropriately using -XX:ParallelGCThreads=N
  3. Settings: Default pipeline workers: 1
  4. Pipeline main started
  5. www.abcdocker.com
  6. {
  7. "message" => "www.abcdocker.com", #內容
  8. "@version" => "1", #版本
  9. "@timestamp" => "2017-03-12T04:18:06.702Z", #時間戳
  10. "host" => "abcdocker.com" #主機
  11. }
  12. => 代表等號
  13. 這樣就可以讓我們的輸出內容顯示的美觀一些

7. 將日誌寫入es裏面 
image_1bdc0d7uji05126f1f9g142210ml13.png-60kB

官方文檔地址:https://www.elastic.co/guide/en/logstash/current/first-event.html 
INPUT插件:https://www.elastic.co/guide/en/logstash/current/input-plugins.html 
OUTPUT插件:https://www.elastic.co/guide/en/logstash/current/output-plugins.html

我們將日誌寫入到es需要查看output插件,同樣我們也可以INPUT es日誌,OUTPUT es日誌 
寫入到elasticsearch: 
https://www.elastic.co/guide/en/logstash/current/plugins-outputs-elasticsearch.html

配置主機及索引(生產場景都是寫入配置文件中)

  1. [root@abcdocker ~]# /opt/logstash/bin/logstash -e 'input { stdin{} } output { elasticsearch { hosts => ["192.168.56.11:9200"] index => "logstash-%{+YYYY.MM.dd}" } }'
  2. #使用output插件將數據寫入es
  3. # hosts 定義主機名稱
  4. # index 定義日期格式
  5. [root@abcdocker ~]# /opt/logstash/bin/logstash -e 'input { stdin{} } output { elasticsearch { hosts => ["192.168.56.11:9200"] index => "logstash-%{+YYYY.MM.dd}" } }'
  6. OpenJDK 64-Bit Server VM warning: If the number of processors is expected to increase from one, then you should configure the number of parallel GC threads appropriately using -XX:ParallelGCThreads=N
  7. Settings: Default pipeline workers: 1
  8. Pipeline main started
  9. www.abcdocker.com --- time

這時候信息已經不進行輸出而是寫入到ES中,我們可以到es上進行查看

訪問地址: http://ip:9200/_plugin/head/

ES搭建文檔:https://www.abcdocker.com/abcdocker/2234 
image_1bdc0dl3i122d68p1h1tjcb12dm1g.png-126.4kB

image_1bdc0e2c8ms819698vq1vm113a01t.png-167.2kB
這樣我們就可以看到我們輸出的www.abcdocker.com ---time的信息,以及日誌的時間等。我們寫入完之後直接刷新就可以顯示。

我們可以將信息打印到es裏面,又進行輸出

  1. [root@abcdocker ~]# /opt/logstash/bin/logstash -e 'input { stdin{} } output { stdout{ codec => rubydebug } elasticsearch { hosts => ["192.168.56.11:9200"] index => "logstash-%{+YYYY.MM.dd}" } }'
  2. OpenJDK 64-Bit Server VM warning: If the number of processors is expected to increase from one, then you should configure the number of parallel GC threads appropriately using -XX:ParallelGCThreads=N
  3. Settings: Default pipeline workers: 1
  4. Pipeline main started
  5. 123456789000
  6. 3{
  7. "message" => "123456789000",
  8. "@version" => "1",
  9. "@timestamp" => "2017-04-05T08:19:52.469Z",
  10. "host" => "abcdocker.com"
  11. }
  12. 33333333333
  13. {
  14. "message" => "333333333333",
  15. "@version" => "1",
  16. "@timestamp" => "2017-04-05T08:19:56.412Z",
  17. "host" => "abcdocker.com"
  18. }
  19. 111111111
  20. {
  21. "message" => "111111111",
  22. "@version" => "1",
  23. "@timestamp" => "2017-04-05T08:19:59.885Z",
  24. "host" => "abcdocker.com"
  25. }

image_1bdc0ejp91gbn1kll10rd106e1k5b2a.png-208.4kB

提示:我們學習logstash需要靈活的運用本身的插件,來實現我們想要的任何的功能

我們現在把logstash放在前臺運行,是爲了更好的學習插件。現在我們需要把logstash運行在後臺

提示:在收集的時候所有的機器都需要安裝一個logstash(通過網絡可以不需要)因爲在INPUT和OUTPUT插件有一個通過http的插件,這樣可以通過網絡進行傳輸。如果是文件我所有的節點都需要安裝一個logstash

yum安裝的目錄需要將配置文件放在/etc/logstash/conf.d目錄下,因爲腳本會在這個目錄進行讀取

  1. [root@abcdocker ~]# vim /etc/init.d/logstash

image_1bdc0euk318mnbi419g51g70jfb2n.png-145.1kB

腳本啓動的時候會去這個目錄下找配置文件

編寫配置文件 
我們進入/etc/logstash/conf.d下進行編輯配置文件

  1. [root@abcdocker ~]# cd /etc/logstash/conf.d/
  2. [root@abcdocker conf.d]# cat demo.conf
  3. input{
  4. stdin{}
  5. }
  6. filter{
  7. }
  8. output{
  9. elasticsearch {
  10. hosts => ["192.168.56.11:9200"]
  11. index => "logstash-%{+YYYY.MM.dd}"
  12. }
  13. stdout{
  14. codec => rubydebug
  15. }
  16. }
  17. #配置文件的寫法和命令行的區別並不大

配置文件小提示:

  1. 配置文件的語法需要包含INPUT or OUTPUT,其中filter可以沒有。
  2. INPUT和OUTPUT中的{}不可以搞錯,其中INPUT存放INPUT插件,OUTPUT存放OUTPUT插件
  3. (其中插件也需要有{花括號}) “=>”代表等於,寫一個數組需要使用[中括號]多個可以使用逗號分隔。
  4. 字符串需要使用“雙引號”
  5. #號代表註釋
  6. 日誌中信息都是一行一行的,在logstash中叫做事件(因爲logstash可以將多行進行合併)
  7. 流程: 事件 --> input --> codec --> filter --> codec --> output

指定配置文件啓動

  1. [root@abcdocker ~]# /opt/logstash/bin/logstash -f /etc/logstash/conf.d/demo.conf
  2. OpenJDK 64-Bit Server VM warning: If the number of processors is expected to increase from one, then you should configure the number of parallel GC threads appropriately using -XX:ParallelGCThreads=N
  3. Settings: Default pipeline workers: 1
  4. Pipeline main started
  5. wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
  6. {
  7. "message" => "wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww",
  8. "@version" => "1",
  9. "@timestamp" => "2017-04-05T09:36:19.770Z",
  10. "host" => "abcdocker.com"
  11. }
  12. aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
  13. {
  14. "message" => "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  15. "@version" => "1",
  16. "@timestamp" => "2017-04-05T09:36:23.651Z",
  17. "host" => "abcdocker.com"
  18. }
  19. ccccccccccccccccccccccccccccccccccccccc
  20. {
  21. "message" => "ccccccccccccccccccccccccccccccccccccccc",
  22. "@version" => "1",
  23. "@timestamp" => "2017-04-05T09:36:27.317Z",
  24. "host" => "abcdocker.com"
  25. }

我們在查看head插件 
image_1bdc0fe481qdg5a31d8k14mvht334.png-292.3kB
這時候我們寫的配置文件已經可以實現簡單的輸出和將數據寫入ES中

三、插件介紹

INPUT插件介紹

file插件地址:https://www.elastic.co/guide/en/logstash/current/plugins-inputs-file.html

1.收集mesassage錯誤日誌

  1. [root@abcdocker ~]# vim /etc/logstash/conf.d/file.conf
  2. input {
  3. file{
  4. path => ["/var/log/messages","/var/log/secure"]
  5. type => "system-log"
  6. start_position => "beginning"
  7. }
  8. }
  9. filter{
  10. }
  11. output{
  12. elasticsearch {
  13. hosts => ["192.168.56.11:9200"]
  14. index => "system-log-%{+YYYY.MM}"
  15. }
  16. }
  17. path 定義日誌路徑
  18. start_position 記錄日誌從頭讀取數據還是從尾讀取數據
  19. type 可以設置類型,進行if判斷(簡單來說可以將日誌進行分類,錯誤放在一起,正常放在一起)
  20. 提示:關於hosts後面端口可寫可不寫,不寫默認是9200
  21. [root@abcdocker ~]# /opt/logstash/bin/logstash -f /etc/logstash/conf.d/file.conf
  22. 啓動

我們可以訪問head進行查看

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章