官方文檔地址:https://docs.fluentd.org/output/elasticsearch
td-agent的v3.0.1版本以后自帶包含out_elasticsearch插件,不用再安裝了,可以直接使用。
若是使用的是Fluentd,則需要安裝這個插件:
$ fluent-gem install fluent-plugin-elasticsearch
配置示例
<match my.logs>
@type elasticsearch
host localhost
port 9200
logstash_format true
</match>
參數說明
- @type:必填,elasticsearch
- host:可選,elasticsearch連接地址,默認是localhost
- port:可選,elasticsearch使用的端口,默認是9200
- hosts:可選,連接多個elasticsearch時使用,若是使用這個,host和port配置的則會被忽略,則用法如下:
hosts host1:port1,host2:port2,host3:port3
# or
hosts https://customhost.com:443/path,https://username:password@host-failover.com:443
- user:可選,默認nil
- password:可選,默認nil
- scheme:可選,連接協議,默認http
- path: 可選,Elasticsearch的REST API端點,用於發布寫請求,默認nil
- index_name,可選,索引名稱,默認fluentd,用法示例:
# index by tags
index_name fluentd.${tag}
# by tags and timestamps
# 這種形式的還需要在chunk_keys中設置tag和time,如下所示:
index_name fluentd.${tag}.%Y%m%d
<match my.logs>
@type elasticsearch
host localhost
port 9200
index_name fluentd.${tag}.%Y%m%d => fluentd.my.logs.20201105
<buffer tag,time>
timekey 1m
</buffer>
</match>
- logstash_format:可選,默認false,若為true,則索引名稱格式是logstash-%Y.%m.%d,比index_name優先級高
- logstash_prefix:可選,logstash前綴索引名,用於在logstash_format為true時,默認logstash
- @log_level:可選,日志等級,參數有fatal, error, warn, info, debug, trace
其他
可以使用%{}
樣式占位符來轉義URL編碼所需的字符
比如:
# 有效配置
user %{demo+}
password %{@secret}
hosts https://%{j+hn}:%{passw@rd}@host1:443/elastic/,http://host2
# 無效配置
user demo+
password @secret
實際使用案例
收集openresty(nginx)日志
# cat /etc/td-agent/td-agent.conf
<source>
@type tail
@id input_tail
<parse>
@type nginx
</parse>
path /usr/local/openresty/nginx/logs/host.access.log
tag td.nginx.access
</source>
<match td.nginx.access>
@type elasticsearch
host localhost
port 9200
index_name fluentd.${tag}.%Y%m%d
<buffer tag,time>
timekey 1m
</buffer>
</match>
關於@type nginx日志過濾的內容
官方文檔地址:https://docs.fluentd.org/parser/nginx
使用的正則表達式:
expression /^(?<remote>[^ ]*) (?<host>[^ ]*) (?<user>[^ ]*) \[(?<time>[^\]]*)\] "(?<method>\S+)(?: +(?<path>[^\"]*?)(?: +\S*)?)?" (?<code>[^ ]*) (?<size>[^ ]*)(?: "(?<referer>[^\"]*)" "(?<agent>[^\"]*)"(?:\s+(?<http_x_forwarded_for>[^ ]+))?)?$/
time_format %d/%b/%Y:%H:%M:%S %z
remote, user, method, path, code, size, referer, agent and http_x_forwarded_for 都包含在record中,時間用於事件時間
# 日志內容
127.0.0.1 192.168.0.1 - [28/Feb/2013:12:00:00 +0900] "GET / HTTP/1.1" 200 777 "-" "Opera/12.0" -
# 過濾后的結果
time:
1362020400 (28/Feb/2013:12:00:00 +0900)
record:
{
"remote" : "127.0.0.1",
"host" : "192.168.0.1",
"user" : "-",
"method" : "GET",
"path" : "/",
"code" : "200",
"size" : "777",
"referer" : "-",
"agent" : "Opera/12.0",
"http_x_forwarded_for": "-"
}
假設不用這個參數的話,假若刪除
<parse>
@type nginx
</parse>
啟動后則會報錯:
<parse> section is required
只得使用none替換:
<parse>
@type none
</parse>