[TOC]
### 多個輸入和輸出插件的混合使用
通常你管理的信息會來自于不同的地方,你的用例也可能要求將數據存儲到不同的目的地。你的Logstash管道可以使用多個input和output插件來滿足這些需求。
在本章節,你將創建一個Logstash管道同時從Twitter和Filebeat客戶端獲取信息,然后將這些信息存儲到Elasticsearch集群的同時直接寫入文件。
#### 從Twitter讀取
要添加一個Twitter輸入,你可以使用[twitter](http://www.elastic.co/guide/en/logstash/6.4/plugins-inputs-twitter.html)input插件。要配置這個插件,你需要下面幾條信息:
+ 一個用來唯一標示您的Twitter應用程序的用戶密鑰。
+ 一個用來充當Twitter應用密碼的秘密。(不知道怎么翻譯這一句,原文:A consumer secret, which serves as the password for your Twitter app.)
+ 要在傳入的Feed中搜索的一個或多個關鍵字。在這個例子中使用“Cloud”作為關鍵字,你可以自定義你想要搜索的任何東西。
+ 一個認證令牌,使此應用可以識別一個Twitter帳號。
+ 一個認證令牌秘密,用作Twitter帳號密碼。(原文:An oauth token secret, which serves as the password of the Twitter account.)
訪問[https://dev.twitter.com/apps](https://dev.twitter.com/apps)來設置Twitter帳號和生成你的用戶密鑰和秘密,以及你的認證令牌和秘密。如果你不確定如何生成這些信息,查看[twitter](http://www.elastic.co/guide/en/logstash/6.4/plugins-inputs-twitter.html)input插件的文檔來獲取更多信息。
> <font color=#1E90FF size=4>注</font>:這個Twitter app似乎有些類似在微信或者其他什么開發平臺申請的一個自己創建的應用,并非Twitter應用本身,至于那什么用戶密鑰,秘密,認證令牌什么的應該于此有關。鑒于Twitter屬于404網站,不好研究。自己感受下吧。
和之前一樣創建一個配置文件(命名為`second-pipeline.conf`)并且使用同樣的配置框架。如果你懶,還可以復制一份剛才的,只是運行的時候確保使用的是正確的配置文件。
將下面的內容填寫到`input`配置段中,并根據自己的情況替換其中的內容:
```json
twitter {
consumer_key => "enter_your_consumer_key_here"
consumer_secret => "enter_your_secret_here"
keywords => ["cloud"]
oauth_token => "enter_your_access_token_here"
oauth_token_secret => "enter_your_access_token_secret_here"
}
```
#### 配置Filebeat將日志發送到Logstash
正如之前了解的,Filebeat是一個輕量級,資源友好的工具用來從服務器文件上收集日志并將其轉發給Logstash實例進行處理。
在安裝Filebeat之后,你需要進行一些配置。在本地安裝目錄中打開`filebeat.yml`文件,并用下面的內容替換其中內容。確保`path`指向你的系統日志:
```yaml
filebeat.prospectors:
- type: log
paths:
- /var/log/*.log ①
fields:
type: syslog ②
output.logstash:
hosts: ["localhost:5044"]
```
① 指定一個或多個文件的絕對路徑
***
② 向事件添加一個`type`字段,值為`syslog`
保存設置。
同樣,為了簡單,這里你不需要配置TLS/SSL,但在生產中可能并非如此。
通過向`second-pipeline.conf`文件的`input`配置段添加以下信息來使用Filebeat input 插件。
```json
beats {
port => "5044"
}
```
#### 把Logstash數據寫入文件
使用`file`output插件,可以配置你的Logstash管道直接將數據寫入文件。
通過向`second-pipeline.conf`文件的`output`配置段添加如下信息來使用`file`output插件:
```json
file {
path => "/path/to/target/file"
}
```
#### 寫入多個Elasticsearch節點
寫入多個Elasticsearch節點可以減輕特定節點的負載,并且可以在主節點不可用時提供冗余入口。
要配置此功能,在`second-pipeline.conf`文件的`output`配置段中添加如下信息:
```json
output {
elasticsearch {
hosts => ["IP Address 1:port1", "IP Address 2:port2", "IP Address 3"]
}
}
```
在hosts行配置三個非主節點的Elasticsearch節點IP地址。當hosts中配置多個IP地址的時候,Logstash會在地址列表中進行負載均衡,如果Elasticsearch使用的是默認的9200端口,你可以在上面的配置中省略它。
#### 測試
通過上面的配置,`second-pipeline.conf`內容應該如下:
```json
input {
twitter {
consumer_key => "enter_your_consumer_key_here"
consumer_secret => "enter_your_secret_here"
keywords => ["cloud"]
oauth_token => "enter_your_access_token_here"
oauth_token_secret => "enter_your_access_token_secret_here"
}
beats {
port => "5044"
}
}
output {
elasticsearch {
hosts => ["IP Address 1:port1", "IP Address 2:port2", "IP Address 3"]
}
file {
path => "/path/to/target/file"
}
}
```
Logstash現在同時從你配置的Twitter feed和Filebeat中接收數據,將數據存儲到Elasticsearch集群的三個節點的同時將數據寫入到一個文件。
在數據源主機上使用下面的命令啟動Filebeat:
```shell
sudo ./filebeat -e -c filebeat.yml -d "publish"
```
Logstash啟動之前,Filebeat會收到連接5044失敗的錯誤信息,這是正常的。
要驗證你的配置,使用下面的命令:
```shell
bin/logstash -f second-pipeline.conf --config.test_and_exit
```
`--config.test_and_exit`選項檢查你的配置文件并向你報告其中的錯誤。當配置文件通過檢查之后,使用下面的命令啟動Logstash:
```shell
bin/logstash -f second-pipeline.conf
```
使用`grep`工具在目標文件中搜索來驗證是否存在信息:
```shell
grep syslog /path/to/target/file
```
然后運行一個Elasticsearch查詢來搜索同樣的內容:
```shell
curl -XGET 'localhost:9200/logstash-$DATE/_search?pretty&q=fields.type:syslog'
```
用當前的時間替換其中的$DATE,時間使用YYYY.MM.DD格式。
使用下面的查詢語句從Twitter feed,中查詢數據:
```shell
curl -XGET 'localhost:9200/logstash-$DATE/_search?pretty&q=fields.type:syslog'
```
同樣的,記得用當前的時間替換其中的$DATE變量,格式依然是YYYY.MM.DD
- Emmm
- Logstash簡介
- 開始使用Logstash
- 安裝Logstash
- 儲存你的第一個事件
- 通過Logstash解析日志
- 多個輸入和輸出插件的混合使用
- Logstash是如何工作的
- 執行模型Execution Model
- 設置并運行Logstash
- Logstash目錄布局
- Logstash配置文件
- logstash.yml
- Secrets keystore for secure settings
- 從命令行運行Logstash
- 以服務的方式運行Logstash
- 在Docker中運行Logstash
- 配置容器版Logstash
- Logging
- 關閉Logstash
- 安裝X-Pack
- 設置X-Pack
- 升級Logstash
- 使用包管理升級
- 直接下載進行升級
- 升級至6.0
- Upgrading with the Persistent Queue Enabled
- 配置Logstash
- 管道配置文件的結構
- 訪問配置中的事件數據和字段
- 在配置中使用環境變量
- Logstash配置示例
- 多管道
- 管道間通信(beta)
- 重載配置文件
- 管理多行事件
- Glob Pattern Support
- Converting Ingest Node Pipelines
- Logstash間通信
- 配置集中式管道管理
- X-Pack Monitoring
- X-Pack Security
- X-Pack Settings
- Field References Deep Dive(深入字段引用)
- 管理Logstash
- 集中式管道管理
- 使用Logstash模塊
- 使用Elastic Cloud
- Logstash ArcSight模塊