filebeat + logstash 对message提取指定字段

说明

filebeat中message要么是一段字符串,要么在日志生成的时候拼接成json然后在filebeat中指定为json。但是大部分系统日志无法去修改日志格式,filebeat则无法通过正则去匹配出对应的field,这时需要结合logstash的grok来过滤,架构如下:

实例说明:

以系统登录日志格式为例:

登录成功日志
Jan  6 17:11:47 localhost sshd[3324]: Received disconnect from 172.16.0.13: 11: disconnected by user
Jan  6 17:11:47 localhost sshd[3324]: pam_unix(sshd:session): session closed for user root
Jan  6 17:11:48 localhost sshd[3358]: Address 172.16.0.13 maps to localhost, but this does not map back to the address - POSSIBLE BREAK-IN ATTEMPT!
Jan  6 17:11:51 localhost sshd[3358]: Accepted password for root from 172.16.0.13 port 38604 ssh2
Jan  6 17:11:51 localhost sshd[3358]: pam_unix(sshd:session): session opened for user root by (uid=0)
登录失败日志
Jan  6 17:13:10 localhost sshd[3380]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=172.16.0.39  user=root
Jan  6 17:13:12 localhost sshd[3380]: Failed password for root from 172.16.0.39 port 58481 ssh2

这里需要定义两个field,Status和ClientIP来获取某个IP登录服务器的频率和状态
而单filebeat输出信息为:

{"@timestamp":"2017-01-12T03:12:46.772Z","beat":{"hostname":"localhost","name":"localhost","version":"5.1.1"},"input_type":"log","message":"Jan 12 11:11:40 localhost sshd[1564]: Accepted password for root from 172.16.11.239 port 65278 ssh2","offset":8548,"source":"/var/log/secure","type":"log"}

message为字符串,且filebeat无法通过正则匹配出想要的数据,所以filebeat只负责在服务器上收索转发日志数据,过滤功能则交给logstash来处理,配置如下:

filebeat_ssh.yaml
filebeat.prospectors:
- input_type: log
  paths: /var/log/secure
  include_lines: [".*Failed.*",".*Accepted.*"]

output.logstash:
  hosts: ["localhost:5044"]
logstash_ssh.conf
input {
    beats {
        port => 5044
    }
}

filter {
    grok {
        match => {
            "message" => ".* sshd\[\d+\]: (?<status>\S+) .* (?<ClientIP>(?:\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})?) .*"
        }
        overwrite => ["message"]
    }
}

output {
    stdout {
        codec=>rubydebug
    }
}
配置解释

filebeat_ssh.yaml

  • include_lines:filebeat只过滤出包含该字符串的行,列表形式
  • output.logstash中指定logstash服务器和logstash监听filebeat的端口,这里为了测试方便,将filebeat和logstash装在同一台机器
  • 更多参数请查看** filebeat.full.yml **文件

logstash_ssh.conf

  • input beats来指定logstash监听filebeat的端口
  • filter 过滤插件,详情查看Grok正则过滤Linux系统登录日志
  • output 这里为了测试输出到控制台,实际生产中可输出到elasticserach
输出结果
{
    "@timestamp" => 2017-01-12T04:00:16.325Z,
        "offset" => 9538,
      "@version" => "1",
    "input_type" => "log",
          "beat" => {
        "hostname" => "localhost",
            "name" => "localhost",
         "version" => "5.1.1"
    },
          "host" => "localhost",
        "source" => "/var/log/secure",
       "message" => "Jan 12 12:00:08 localhost sshd[2043]: Accepted password for root from 172.16.11.239 port 51763 ssh2",
          "type" => "log",
      "ClientIP" => "172.16.11.239",
          "tags" => [
        [0] "beats_input_codec_plain_applied"
    ],
        "status" => "Accepted"
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,120评论 19 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 47,151评论 6 342
  • 前言 调研了ELK技术栈,发现新一代的logstash-forward即Filebeat,使用了golang,性能...
    大数据之心阅读 14,543评论 10 40
  • 没有太多的铺垫 更没有所谓的剧情剧本 我就这样 和妈妈一点点的依赖 相依为命 在记事起 我就和她有着特殊的交流 ...
    HEILEE阅读 1,765评论 0 1
  • 浓绿枝头数点红, 无华本色透丰容。 不愁秋深寒霜至, 只缘腹藏晶华盈。 (2015.09.24)
    正谊明道阅读 6,218评论 2 3

友情链接更多精彩内容