• logstash插件配置-codec插件说明json和multiline


    编码插件(Codec)

    Codec 是 logstash 从 1.3.0 版开始新引入的概念(Codec 来自 Coder/decoder 两个单词的首字母缩写)。

    在此之前,logstash 只支持纯文本形式输入,然后以过滤器处理它。但现在,我们可以在输入 期处理不同类型的数据,这全是因为有了 codec 设置。

    所以,这里需要纠正之前的一个概念。Logstash 不只是一个input | filter | output 的数据流,而是一个 input | decode | filter | encode | output 的数据流!codec 就是用来 decode、encode 事件的。

    codec 的引入,使得 logstash 可以更好更方便的与其他有自定义数据格式的运维产品共存,比如 graphite、fluent、netflow、collectd,以及使用 msgpack、json、edn 等通用数据格式的其他产品等。

    事实上,我们在第一个 “hello world” 用例中就已经用过 codec 了 —— rubydebug 就是一种 codec!虽然它一般只会用在 stdout 插件中,作为配置测试或者调试的工具。

    1,采用json编码

    这里通过nginx日志作为案例测试学习使用。修改nginx配置文件,将日志以json格式存储:

    1. log_format json '{"@timestamp":"$time_iso8601",'
    2. '"@version":"1",'
    3. '"host":"$server_addr",'
    4. '"client":"$remote_addr",'
    5. '"size":$body_bytes_sent,'
    6. '"responsetime":$request_time,'
    7. '"domain":"$host",'
    8. '"url":"$uri",'
    9. '"status":"$status"}';
    10. access_log /logs/nginx/access.log json;

    重启nginx使其生效。

    设置测试使用的logstash配置文件webnginx.conf:

    1. input {
    2. file {
    3. path => "/logs/nginx/access.log"
    4. type => "nginx"
    5. start_position => "beginning"
    6. add_field => { "key"=>"value"}
    7. codec => "json"
    8. }
    9.  
    10. }
    11. output {
    12. stdout{
    13. codec => rubydebug{ }
    14. }
    15. }

    logstash加载启动测试:

    1. logstash -f webnginx.conf

    访问测试地址,显示如下(这里我将www.elk.com定向到了nginx所在的虚拟机)

    1. {
    2. "type" => "nginx",
    3. "url" => "/app/panels/timepicker/module.html",
    4. "tags" => [],
    5. "path" => "/logs/nginx/access.log",
    6. "@timestamp" => 2017-02-13T17:02:47.000Z,
    7. "size" => 2397,
    8. "domain" => "www.elk.com",
    9. "@version" => "1",
    10. "host" => "192.168.1.104",
    11. "client" => "192.168.2.16",
    12. "responsetime" => 0.0,
    13. "key" => "value",
    14. "status" => "200"
    15. }
    16.  

    补充说明:
    日志格式中统一记录为字符串格式(即都带上双引号 “),然后再在 logstash 中用 filter/mutate 插件来变更应该是数值类型的字符字段的值类型。

    2,合并多行数据(Multiline)

    有些时候,应用程序调试日志会包含非常丰富的内容,为一个事件打印出很多行内容。这种日志通常都很难通过命令行解析的方式做分析。 logstash 正为此准备好了 codec/multiline 插件。测试:

    1. input {
    2. stdin {
    3. codec => multiline {
    4. pattern => "^["
    5. negate => true
    6. what => "previous"
    7. }
    8. }
    9. }
    10. output {
    11. stdout {
    12. codec => rubydebug{ }
    13. }
    14. }
    15.  

    加载:logstash -f multiline.conf
    效果如下:

    1. hello world
    2. hello logstash
    3. hello multiline
    4. [
    5. {
    6. "@timestamp" => 2017-02-13T17:29:47.658Z,
    7. "@version" => "1",
    8. "host" => "0.0.0.0",
    9. "message" => "[ hello world hello logstash hello multiline",
    10. "tags" => [
    11. [0] "multiline"
    12. ]
    13. }
    14.  

    这个插件的原理很简单,就是把当前行的数据添加到前面一行后面,直到新进的当前行匹配 ^[ 正则为止。这个正则还可以用 grok 表达式。

    参考文章:
    http://kibana.logstash.es/content/logstash/plugins/codec/multiline.html

  • 相关阅读:
    2019年计划书单
    redis 分布式锁实现
    filter-grok,dissect匹配数据
    nohup-长期运行进程
    filter
    kill
    watch
    free
    jar
    tree
  • 原文地址:https://www.cnblogs.com/zhengchunyuan/p/11225678.html
Copyright © 2020-2023  润新知