Logstash基本概念:html
logstash收集日誌基本流程: input-->codec-->filter-->codec-->outputjava
input:從哪裏收集日誌。web
filter:發出去前進行過濾vim
output:輸出至Elasticsearch或Redis消息隊列centos
codec:輸出至前臺,方便邊實踐邊測試ruby
數據量不大日誌按照月來進行收集markdown
若是經過logstash來採集日誌,那麼每一個客戶端都須要安裝logstashapp
安裝須要前置系統環境elasticsearch
安裝javaide
下載並安裝GPG key [root@master_agent yum.repos.d]# rpm --import https://packages.elastic.co/GPG-KEY-elasticsearch
配置yum倉庫
[root@master_agent yum.repos.d]# echo '[logstash-2.3]> name=Logstash repository for 2.3.x packages > baseurl=https://packages.elastic.co/logstash/2.3/centos> gpgcheck=1> gpgkey=https://packages.elastic.co/GPG-KEY-elasticsearch > enabled=1' >>/etc/yum.repos.d/logstash.repo
安裝logstash
[root@master_agent yum.repos.d]# yum install -y logstash
查看多種輸出輸出方式
[root@master_agent yum.repos.d]# /opt/logstash/bin/logstash -e 'input { stdin{} } output { stdout{} }'
輸出內容:
[root@master_agent yum.repos.d]# /opt/logstash/bin/logstash -e 'input { stdin{} } output { stdout{} }'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 Settings: Default pipeline workers: 1Pipeline main started hello2017-06-27T04:10:21.010Z master_agent hello hello word 2017-06-27T04:10:48.513Z master_agent hello wordquit2017-06-27T04:11:12.959Z master_agent quitexit2017-06-27T04:11:19.064Z master_agent exit
[root@master_agent yum.repos.d]# /opt/logstash/bin/logstash -e 'input { stdin{} } output { stdout{ codec => rubydebug} }'
[root@master_agent yum.repos.d]# /opt/logstash/bin/logstash -e 'input { stdin{} } output { stdout{ codec => rubydebug} }'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 Settings: Default pipeline workers: 1Pipeline main started hello word { "message" => "hello word", "@version" => "1", "@timestamp" => "2017-06-27T04:13:14.560Z", "host" => "master_agent"}
[root@master_agent yum.repos.d]# /opt/logstash/bin/logstash -e 'input { stdin{} } output { elasticsearch { hosts => ["172.16.1.201:9200"] } }'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 Settings: Default pipeline workers: 1Pipeline main started hello word!! Pipeline main has been shutdown
記錄兩份,屏幕輸出一份、es記錄一份 [root@master_agent yum.repos.d]# /opt/logstash/bin/logstash -e 'input { stdin{} } output { elasticsearch { hosts => ["172.16.1.201:9200"] } stdout{ codec => rubydebug } }'
[root@master_agent yum.repos.d]# /opt/logstash/bin/logstash -e 'input { stdin{} } output { elasticsearch { hosts => ["172.16.1.201:9200"] } stdout{ codec => rubydebug } }'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 Settings: Default pipeline workers: 1Pipeline main started hello fly salt { "message" => "hello fly salt", "@version" => "1", "@timestamp" => "2017-06-27T04:32:50.297Z", "host" => "master_agent"}
配置Logstash,將建立一個配置文件,指定您想要使用的插件和設置爲每一個插件。 你能夠參考事件字段在配置和使用條件符合必定時處理事件標準;當您運行logstash,你使用-f指定您的配置文件。 參考連接
實例以下:
[root@master_agent yum.repos.d]# vim /etc/logstash/conf.d/logstash.conf ##新建一個配置語法文件,讓輸入輸出標準化
input { stdin { } }output { elasticsearch { hosts => ["localhost:9200"] } stdout { codec => rubydebug } }
[root@master_agent yum.repos.d]# /opt/logstash/bin/logstash -f /etc/logstash/conf.d/logstash.conf ### 執行命令配置語法文件 手動輸入hao123.com,查看結果
[root@master_agent yum.repos.d]# /opt/logstash/bin/logstash -f /etc/logstash/conf.d/logstash.conf 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 Settings: Default pipeline workers: 1Pipeline main started hao123.com { "message" => "hao123.com", "@version" => "1", "@timestamp" => "2017-06-27T05:06:02.695Z", "host" => "master_agent"}
建立一個文件夾用於專門存放logfile配置文件 存放的路徑
[root@master_agent yum.repos.d]# mkdir -p /home/elk/logfile/
Input plugins file 收集實例:
編輯一個新的配置文件
[root@master_agent yum.repos.d]# mkdir -p /home/elk/logfile/[root@master_agent yum.repos.d]# cd /home/elk/logfile/[root@master_agent logfile]# ls[root@master_agent logfile]# vim file.conf
編寫配置文件內容
input { file { path => "/var/log/messages" ###讀取log日誌文件路徑 type => "system" ### 設置個類型 start_position => "beginning" ### 從日誌最開始地方讀取 } }output { elasticsearch { hosts => ["172.16.1.201:9200"] index => "system-%{+YYYY.MM.dd}" ##設置個索引天天建立 } }
注意:若是採集的日誌文件日誌量不是很大的狀況下,採用每個月建一從索引,日誌量很大的狀況下能夠按照天天來創建索引