Logstash 只将 Java 错误日志写入 es 索引

由于研发即想要只包含 java 错误日志的信息,又想要其它正常日志的信息,所以就需要通过 logstash input 消费一个 kafka 主题,然后通过 output 写入两个 elasticsearch 的索引。

刚开始想法是通过条件判断将包含 “ERROR” 的日志写入一个索引,然后将其它信息写入一个索引。output 的配置如下:

output{
    if [message] =~ "ERROR" {
        elasticsearch {
            hosts => ["10.0.30.34:9200","10.0.30.35:9200","10.0.30.36:9200"]
            index => "errorlog-test"
            user => "elastic"
            password => "vlan30-elastic"
        }
    }

   elasticsearch {
       hosts => ["10.0.30.34:9200","10.0.30.35:9200","10.0.30.36:9200"]
       index => "alllog-test"
       user => "elastic"
       password => "vlan30-elastic"
   }
}

经过测试,发现这种情况下 logstash 向 elasticsearch 写入数据的速度很慢。

所以最后决定开启两个 logstash pipelines 来分别加载输出所有日志的配置文件和只输出 java 错误日志的配置文件。

·

配置文件如下所示:

PS: 我是通过 filter 将包含 “INFO” 的日志删除来实现的。

# 只输出 java 错误日志的配置文件
input{
    kafka {
        bootstrap_servers => "10.0.30.31:9092,10.0.30.32:9092,10.0.30.33:9092"
        topics => "wpf-test-topic"
        group_id => "vlan30-errorlog"
        decorate_events => true
        consumer_threads => 3
        auto_offset_reset => "earliest"
        codec => "json"
    }
}

filter {
    # 删除包含 "INFO" 的日志
    if [message] =~ "INFO" {
        drop {}
    }

    #增加一个字段,计算 timestamp 8小时
    ruby {
        code => "event.set('timestamp', event.get('@timestamp').time.utc+8*60*60)"
    }

    #用 mutate 插件先转换为 string 类型,gsub只处理string类型的数据
    #再用正则匹配,最终得到想要的日期
    mutate {
        convert => ["timestamp", "string"]
        gsub => ["timestamp", "T([\S\s]*?)Z", ""]
        gsub => ["timestamp", "-", "."]
   }
}

output{
    if [fields][project] == "wpf-test"{
        elasticsearch {
            hosts => ["10.0.30.34:9200","10.0.30.35:9200","10.0.30.36:9200"]
            index => "errorlog-wpf-test-%{timestamp}"
            user => "elastic"
            password => "vlan30-elastic"
        }
    }
}

·

# 输出所有日志的配置文件
input{
    kafka {
        bootstrap_servers => "10.0.30.31:9092,10.0.30.32:9092,10.0.30.33:9092"
        topics => "wpf-test-topic"
        group_id => "vlan30-logstash"
        decorate_events => true
        consumer_threads => 3
        auto_offset_reset => "earliest"
        codec => "json"
    }
}

filter {
    #增加一个字段,计算 timestamp 8小时
    ruby {
        code => "event.set('timestamp', event.get('@timestamp').time.utc+8*60*60)"
    }

    #用 mutate 插件先转换为 string 类型,gsub只处理string类型的数据
    #再用正则匹配,最终得到想要的日期
    mutate {
        convert => ["timestamp", "string"]
        gsub => ["timestamp", "T([\S\s]*?)Z", ""]
        gsub => ["timestamp", "-", "."]
   }
}

output{
    if [fields][project] == "wpf-test"{
        elasticsearch {
            hosts => ["10.0.30.34:9200","10.0.30.35:9200","10.0.30.36:9200"]
            index => "log-wpf-test-%{timestamp}"
            user => "elastic"
            password => "vlan30-elastic"
        }
    }
}

·

附加:一个 Logstash 加载多个配置文件

参考的是 Elastic 中国社区官方博客的文章。在 Logstash 的 config 目录下有一个 pipelines.yml 配置文件,可以通过它来实现。关于它的一些信息可以参考官方文档的解释

PS: 一个 pipeline 含有一个逻辑的数据流,它从 input 接收数据,并把它们传入到队列里,经过 worker 的处理,最后输出到 output。

#如下是一个 pipleline 加载一个配置文件
- pipeline.id: wpf-test
  pipeline.workers: 1
  pipeline.batch.size: 1
  path.config: /usr/local/logstash-7.9.1/config/conf.d/wpf-test-logstash.conf

- pipeline.id: wpf-test-errorlog
  pipeline.workers: 1
  pipeline.batch.size: 1
  path.config: /usr/local/logstash-7.9.1/config/conf.d/wpf-test-errorlog.conf

# pipeline.id:管道名称
# pipeline.workers:此管道的工作线程数
# pipeline.batch.size:单个工作线程从输入中收集的最大事件数
# path.config:此管道要加载的配置文件路径

·

启动时要注意:当不带参数启动 Logstash 时,它将读取 pipelines.yml 文件并实例化文件中指定的所有管道。当您使用 -e or -f 时,Logstash 会忽略该 pipelines.yml 文件并记录有关它的警告。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值