logstash 入门篇

场景介绍

基于分布式集群海量日志数据,且分布在不同的服务器上,日志的采集以及可视化是需要我们解决的问题。ELK就是这么一个方案,当然我们这里主要讲解logstash安装配置和基础语法。

ELK帮我们解决了什么问题?

  • 错误日志分散在大量服务器上, 且错误类型众多, 需要有一个工具能够将所有错误日志汇总, 帮助代码的开发调试.
  • 不需要频繁登陆服务器, 节省时间.
  • 根据日志, 实时掌握整个系统的运行状况
  • 及时发现问题进行排查
  • 事后对系统进行分析, 消除黑盒

让我们先来看一下logstash的组成结构,我们可以看到核心分为三部分,input,filter,output
logstash结构


安装配置

这里当然采用docker

$ docker pull docker.elastic.co/logstash/logstash:7.1.1
$ docker run --rm -it -v ~/pipeline/:/usr/share/logstash/pipeline/ docker.elastic.co/logstash/logstash:7.1.1

Every file in the host directory ~/pipeline/ will then be parsed by Logstash as pipeline configuration,
举个小栗子,自定义~/pipeline/logstash.conf配置

基础语法

Logstash 设计了自己的 DSL —— 有点像 Puppet 的 DSL,或许因为都是用 Ruby 语言写的吧 —— 包括有区域,注释,数据类型(布尔值,字符串,数值,数组,哈希),条件判断,字段引用等。

区段

# This is a comment. You should use comments to describe
# parts of your configuration.
input {
  ...
}

filter {
  ...
}

output {
  ...
}

数据类型

  • bool
debug => true
  • string
host => "hostname"
  • number
port => 514
  • array
match => ["datetime", "UNIX", "ISO8601"]
  • hash
options => {
    key1 => "value1",
    key2 => "value2"
}

字段引用

如果你想在 Logstash 配置中使用字段的值,只需要把字段的名字写在中括号 [] 里就行了,这就叫字段引用。
对于 嵌套字段(也就是多维哈希表,或者叫哈希的哈希)的写法

[geoip][location][0]

Logstash 还支持变量内插

"the longitude is %{[geoip][location][0]}"

条件判断

支持的操作符

equality, etc: ==, !=, <, >, <=, >=
regexp: =~, !~
inclusion: in, not in
boolean: and, or, nand, xor
unary: !()
posted @ 2019-06-26 19:45  家迪的家  阅读(796)  评论(0编辑  收藏  举报