一、Logstash介紹
Logstash
是一個完全開源的工具,他可以對你的日誌進行收集、分析,並將其存儲供以後使用(如,搜索),您可以使用它。說到搜索,logstash帶有一個web界面,搜索和展示所有日誌。
kibana也是一個開源和免費的工具,他可以幫助您彙總、分析和搜索重要數據日誌並提供友好的web界面。他可以爲 Logstash 和 ElasticSearch 提供的日誌分析的 Web 界面。
提示: logstash與es沒有任何關係
ELK幾個概念
INPUT
OUTPUT
首先LogStash是收集日誌,它收集完日誌就需要把日誌存儲下來,所以我們可以用運輸者的身份來表示LogStash(INPUT or OUTPUT)LogStash可以在日誌發送之前做一個過濾(OUTPUT之前)
二、LogStash部署與配置
和Elasticsearch一樣,在開始部署LogStash之前也需要你的環境中正確的安裝的JDK。可以下載安裝Oracle的JDK或者使用 yum安裝openjdk。
LogStash三大功能
INPUT 進
FILTER (支持過濾的功能)
OUTPUT 出
1.設置時區
[root@abcdocker ~]# yum install ntpdate –y
[root@abcdocker ~]# ntpdate time1.aliyun.com
[root@abcdocker ~]# timedatectl set-timezone Asia/Shanghai #設置時區
另一種設置時區方法
[root@abcdocker ~]# cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
像我這樣高逼格的玩家一般都把這種命令用一條顯示
[root@abcdocker ~]# yum install ntpdate -y && ntpdate time1.aliyun.com && timedatectl set-timezone Asia/Shanghai
2.安裝JDK
[root@abcdocker ~]# yum install -y java
[root@abcdocker ~]# java -version
openjdk version "1.8.0_65"
OpenJDK Runtime Environment (build 1.8.0_65-b17)
OpenJDK 64-Bit Server VM (build 25.65-b01, mixed mode)
3.YUM部署LogStash
下載並安裝GPG key
[root@abcdocker ~]# rpm --import https://packages.elastic.co/GPG-KEY-elasticsearch
我們可以查詢是否導入成功,可能有很多key
[root@abcdocker ~]# rpm -qa gpg-pubkey*
gpg-pubkey-f4a80eb5-53a7ff4b
gpg-pubkey-d88e42b4-52371eca
4.添加yum倉庫
[root@abcdocker ~]# cat /etc/yum.repos.d/logstash.repo
[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
5.安裝logstash
[root@abcdocker ~]# yum install -y logstash
[root@abcdocker ~]# rpm -qa |grep logst
logstash-2.3.4-1.noarch
#請在2臺服務器都進行安裝
6. 配置logstash
提示:logstash實現input和ouput主要是依賴於logstash有需多的插件
[root@abcdocker ~]# /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: 1
Pipeline main started
www.abcdocker.com
2017-03-12T04:13:31.347Z abcdocker.com www.abcdocker.com
input插件stdin(標準輸入)
output插件 stdout(標準輸出)
/opt/logstash/bin/logstash 前臺啓動
提示:標準輸入和標準輸出的意思就是我們輸入什麼就會輸出什麼
輸入:www.abcdocker.com
輸入:時間戳、節點名稱、內容
使用codec rubydebug功能
[root@abcdocker ~]# /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: 1
Pipeline main started
www.abcdocker.com
{
"message" => "www.abcdocker.com", #內容
"@version" => "1", #版本
"@timestamp" => "2017-03-12T04:18:06.702Z", #時間戳
"host" => "abcdocker.com" #主機
}
=> 代表等號
這樣就可以讓我們的輸出內容顯示的美觀一些
7. 將日誌寫入es裏面
官方文檔地址: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
配置主機及索引(生產場景都是寫入配置文件中)
[root@abcdocker ~]# /opt/logstash/bin/logstash -e 'input { stdin{} } output { elasticsearch { hosts => ["192.168.56.11:9200"] index => "logstash-%{+YYYY.MM.dd}" } }'
#使用output插件將數據寫入es
# hosts 定義主機名稱
# index 定義日期格式
[root@abcdocker ~]# /opt/logstash/bin/logstash -e 'input { stdin{} } output { elasticsearch { hosts => ["192.168.56.11:9200"] index => "logstash-%{+YYYY.MM.dd}" } }'
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: 1
Pipeline main started
www.abcdocker.com --- time
這時候信息已經不進行輸出而是寫入到ES中,我們可以到es上進行查看
訪問地址: http://ip:9200/_plugin/head/
ES搭建文檔:https://www.abcdocker.com/abcdocker/2234
這樣我們就可以看到我們輸出的www.abcdocker.com ---time
的信息,以及日誌的時間等。我們寫入完之後直接刷新就可以顯示。
我們可以將信息打印到es裏面,又進行輸出
[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}" } }'
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: 1
Pipeline main started
123456789000
3{
"message" => "123456789000",
"@version" => "1",
"@timestamp" => "2017-04-05T08:19:52.469Z",
"host" => "abcdocker.com"
}
33333333333
{
"message" => "333333333333",
"@version" => "1",
"@timestamp" => "2017-04-05T08:19:56.412Z",
"host" => "abcdocker.com"
}
111111111
{
"message" => "111111111",
"@version" => "1",
"@timestamp" => "2017-04-05T08:19:59.885Z",
"host" => "abcdocker.com"
}
提示:我們學習logstash需要靈活的運用本身的插件,來實現我們想要的任何的功能
我們現在把logstash
放在前臺運行,是爲了更好的學習插件。現在我們需要把logstash運行在後臺
提示:在收集的時候所有的機器都需要安裝一個logstash(通過網絡可以不需要)因爲在INPUT和OUTPUT插件有一個通過http的插件,這樣可以通過網絡進行傳輸。如果是文件我所有的節點都需要安裝一個logstash
yum安裝的目錄需要將配置文件放在/etc/logstash/conf.d
目錄下,因爲腳本會在這個目錄進行讀取
[root@abcdocker ~]# vim /etc/init.d/logstash
腳本啓動的時候會去這個目錄下找配置文件
編寫配置文件
我們進入/etc/logstash/conf.d
下進行編輯配置文件
[root@abcdocker ~]# cd /etc/logstash/conf.d/
[root@abcdocker conf.d]# cat demo.conf
input{
stdin{}
}
filter{
}
output{
elasticsearch {
hosts => ["192.168.56.11:9200"]
index => "logstash-%{+YYYY.MM.dd}"
}
stdout{
codec => rubydebug
}
}
#配置文件的寫法和命令行的區別並不大
配置文件小提示:
- 配置文件的語法需要包含INPUT or OUTPUT,其中filter可以沒有。
- INPUT和OUTPUT中的{}不可以搞錯,其中INPUT存放INPUT插件,OUTPUT存放OUTPUT插件
- (其中插件也需要有{花括號}) “=>”代表等於,寫一個數組需要使用[中括號]多個可以使用逗號分隔。
- 字符串需要使用“雙引號”
- #號代表註釋
- 日誌中信息都是一行一行的,在logstash中叫做事件(因爲logstash可以將多行進行合併)
- 流程: 事件 --> input --> codec --> filter --> codec --> output
指定配置文件啓動
[root@abcdocker ~]# /opt/logstash/bin/logstash -f /etc/logstash/conf.d/demo.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: 1
Pipeline main started
wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
{
"message" => "wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww",
"@version" => "1",
"@timestamp" => "2017-04-05T09:36:19.770Z",
"host" => "abcdocker.com"
}
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
{
"message" => "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
"@version" => "1",
"@timestamp" => "2017-04-05T09:36:23.651Z",
"host" => "abcdocker.com"
}
ccccccccccccccccccccccccccccccccccccccc
{
"message" => "ccccccccccccccccccccccccccccccccccccccc",
"@version" => "1",
"@timestamp" => "2017-04-05T09:36:27.317Z",
"host" => "abcdocker.com"
}
我們在查看head插件
這時候我們寫的配置文件已經可以實現簡單的輸出和將數據寫入ES中
三、插件介紹
INPUT插件介紹
file插件地址:https://www.elastic.co/guide/en/logstash/current/plugins-inputs-file.html
1.收集mesassage錯誤日誌
[root@abcdocker ~]# vim /etc/logstash/conf.d/file.conf
input {
file{
path => ["/var/log/messages","/var/log/secure"]
type => "system-log"
start_position => "beginning"
}
}
filter{
}
output{
elasticsearch {
hosts => ["192.168.56.11:9200"]
index => "system-log-%{+YYYY.MM}"
}
}
path 定義日誌路徑
start_position 記錄日誌從頭讀取數據還是從尾讀取數據
type 可以設置類型,進行if判斷(簡單來說可以將日誌進行分類,錯誤放在一起,正常放在一起)
提示:關於hosts後面端口可寫可不寫,不寫默認是9200
[root@abcdocker ~]# /opt/logstash/bin/logstash -f /etc/logstash/conf.d/file.conf
啓動
我們可以訪問head進行查看