ELK中Logstash的基本配置和用法

文章目录

《Elasticsearch搜索引擎系统入门》中简单描述了Logstah的安装,本篇文章将较为详细的讲解Logstash的基本配置和用法。

Logstash的条件判断

比较操作符

复制代码
相等: ==, !=, <, >, <=, >=
正则: =~(匹配正则), !~(不匹配正则)
包含: in(包含), not in(不包含)

布尔操作符

复制代码
and(与), or(或), nand(非与), xor(非或)

一元运算符:

复制代码
!(取反)
()(复合表达式), !()(对复合表达式结果取反)

Logstash的输入插件

Stdin输入

Stdin输入就是把输入的内容直接输出到控制台,不做存储。配置示例:

json 复制代码
input {
	stdin {
	}
}
filter {
}
output {
	stdout {
		codec => rubydebug
	}
}

进入 logstash的安装目录:

bash 复制代码
mkdir config/input_config/
cd config/input_config/
vim Stdin.conf #然后写入上面的配置内容

再次回到logstash的安装目录:

bash 复制代码
sudo chmod -R 777 data/
# 使用刚才的配置文件启动
./bin/logstash -r -f ./config/input_config/Stdin.conf

启动后,在控制台输入内容就可以收集到:

文件内容输入

配置示例:

json 复制代码
input {
	file {
		path => "/home/rx/tmp/test.log"
		tags => "123"
		type => "syslog"
	}
}
filter {
}
output {
	stdout {
		codec => rubydebug
	}
}

path里面支持日志路径的通配符,比如:/tmp/log*/*.log

将上面的内容保存到 logstash目录下面的 config/input_config/File.conf

然后启动:./bin/logstash -r -f ./config/input_config/File.conf

接下来,给 /home/rx/tmp/test.log 里面写入内容,查看logstash控制台:

filter过滤器

配置示例:

json 复制代码
input {
	stdin {
	}
}
filter {
	json {
		source => "message"
		target => "content"
	}
}
output {
	stdout {
		codec => rubydebug
	}
}

以上内容 filter 中的 source表示对 message 部分进行解析,target 表示目标字段,可以将 message里面的内容解析出来后放到 content 中。

保存上面配置文件到 Stdin_filter.conf 中,然后运行:./bin/logstash -r -f ./config/input_config/Stdin_filter.conf

在控制台输入一个json,示例:{"ip":"127.0.0.1","method":"POST","url":"/user/info?id=1"}

可以看到 json的内容被解析到了 content 里面,而message 里面是原始的json字符串内容。

Logstash的输出插件

可以支持很多输出方式,在 https://www.elastic.co/guide/en/logstash/current/output-plugins.html 查看:

在这里介绍输出到ES的操作步骤。

测试收集日志

下面测试对系统日志/var/log/syslog/var/log/auth.log 进行收集,配置logstash如下的配置文件:
vim /home/rx/soft/elastic/logstash-7.1.0/config/output_config/logstash-to-es.conf 写入内容:

json 复制代码
input {
    file {
        path => ["/var/log/syslog"]
        type => "system"
        tags => ["syslog","test"]
        start_position => "beginning"
    }
    file {
        path => ["/var/log/auth.log"]
        type => "system"
        tags => ["auth","test"]
        start_position => "beginning"
    }
}
filter {
}
output {
    if [type] == "system" {
        if [tags][0] == "syslog" {
            elasticsearch {
                hosts  => ["http://192.168.0.211:9200","http://192.168.0.212:9200","http://192.168.0.213:9200"]
                index  => "logstash-system-syslog-%{+YYYY.MM.dd}"
            }
            stdout { codec=> rubydebug }
        }
        else if [tags][0] == "auth" {
            elasticsearch {
                hosts  => ["http://192.168.0.211:9200","http://192.168.0.212:9200","http://192.168.0.213:9200"]
                index  => "logstash-system-auth-%{+YYYY.MM.dd}"
            }
            stdout { codec=> rubydebug }
        }
    }
}
  • 以上配置的hosts部分使用了集群配置,如果是单个ES服务,直接使用单个IP和端口即可,例如:hosts => ["http://127.0.0.1:9200"]
  • start_position => "beginning" 表示从文件头部开始收集

启动 logstash:./logstash-7.1.0/bin/logstash -r -f ./logstash-7.1.0/config/output_config/logstash-to-es.conf

启动后就开始收集日志了:

启动kibana

修改 kibana/config/kibana.yml 里面关于ES的内容:

复制代码
elasticsearch.hosts: ["http://localhost:9200"]

然后启动ES 和 kibana:

复制代码
./elasticsearch-7.1.0/bin/elasticsearch #启动ES
./kibana-7.1.0-linux-x86_64/bin/kibana #启动kibana

浏览器访问 http://localhost:5601/ 打开kibana的页面,进入 Management > Index management 即可看到最新收集到的日志索引:

在kibana中配置索引数据

进入 kibana 管理页面的 Management > Index Patterns > Create index pattern:

下一步:

创建完成:

然后在 Discover 中就可以看到最新收集的日志了:

相关推荐
error:(24 分钟前
【Linux命令从入门到精通系列指南】export 命令详解:环境变量管理的核心利器
linux·运维·服务器
Yeats_Liao44 分钟前
遗留系统微服务改造(四):从单体到微服务的演进之路
运维·微服务·架构
2301_793167991 小时前
网络管理部分
linux·运维·服务器·网络·php
序属秋秋秋1 小时前
《Linux系统编程之入门基础》【Linux的前世今生】
linux·运维·服务器·开源·unix·gnu
qiuiuiu4131 小时前
正点原子RK3568学习日记-GIT
linux·c语言·开发语言·单片机
搬砖的小码农_Sky1 小时前
Windows操作系统上`ping`命令的用法详解
运维·网络·windows
Janspran3 小时前
监控系统4 - LVGL | sqlite3 | mqtt
linux·sqlite3·嵌入式实时数据库
敲上瘾3 小时前
Docker镜像构建指南:Dockerfile语法与docker build命令全解析
linux·服务器·docker·微服务·容器
YC运维7 小时前
Dockerfile实战案例详解
运维·docker·容器
一个响当当的名号7 小时前
一些主要应用和NAT
运维·服务器·网络