Logstash不同服务日志分索引收集
一般我们使用elk的时候,都是多个服务一起使用,那我们该怎么不同服务之间创建出不同的索引呢?我们就可以利用Logstash提供的多个input配置,type配置类型,根据监听的端口和type在es中创建不同的索引。
input {
tcp {
mode => "server"
host => "0.0.0.0" # 允许任意主机发送日志
port => 5044 #监听的端口号
codec => json_lines # 数据格式
type => consumer
}
}
input{
tcp{
mode => "server"
host => "0.0.0.0"
port => 5045
codec => json_lines # 数据格式
type => product
}
}
filter {
grok {
match => {
"message" => '%{IPORHOST:clientip} %{USER:ident} %{USER:auth} \[%{HTTPDATE:timestamp}\] "%{WORD:verb} %{DATA:request} HTTP/%{NUMBER:httpversion}" %{NUMBER:response:int} (?:-|%{NUMBER:bytes:int}) %{QS:referrer} %{QS:agent}'
}
}
if[type] == "consumer" {
mutate {
add_tag => ["consumer"]
}
}
if [type] == "product" {
mutate {
add_tag => ["product"]
}
}
}
output {
if "consumer" in [tags] {
elasticsearch {
hosts => ["http://10.211.55.3:9200"] # ElasticSearch 的地址和端口
index => "consumer-%{+YYYY.MM.dd}" # 指定索引名,可以根据自己的需求指定命名
codec => "json"
}
}
if "product" in [tags] {
elasticsearch {
hosts => ["http://10.211.55.3:9200"] # ElasticSearch 的地址和端口
index => "product-%{+YYYY.MM.dd}" # 指定索引名,可以根据自己的需求指定命名
codec => "json"
}
}
stdout {
codec => rubydebug
}
}
下面是Logstash的打印的情况
一个是consumer另一个是product,目前的话就实现了不同服务之间日志文件不同的监听情况。