filebeat:7.6.2
logstash:7.6.2
1、logstash配置
网上看了很多文章介绍logstash的输出模式rubydebug,好像都没说这个东西是怎么用的,很多都只是说这样的写法:
output
stdout
codec => rubydebug
rubydebug是一种编码格式,类似json,我们完全可以使用json来替代,用法相同,在新版的logstash中codec可以省略不写,类似这样
bin/logstash -e 'input{stdin{}}output{stdout{}}'
这种方式启动的logstash输出的信息会显示在终端界面,方便调试,logstash常用参数:
-e 立即执行,使用命令行指定的参数启动
-f 指定启动的配置文件
-t 测试配置文件是否正确
-l 指定日志文件
-w 指定filter线程数量,默认是5个
2、filebeat配置
如果默认字段无法满足需求时可以添加自定义字段,添加方法如下:
filebeat.inputs
input_type log
enabledtrue
paths
/data/mysql/data/mysql_slow.log exclude_lines'^\# Time|^/data/mysql/bin/mysqld|^Tcp port|^Time' #排除行
multiline.negate true #多行合并
multiline.match after
multiline.pattern'^\# User|^\# Time'
tags"mysql-slow-log" #打一个tags
#自定义字段方法,这里自定义字段indexname
fields
indexname"mysql"
#如果为true,那么fields自定义字段放在文档的父栏目中,并且如果和filebeat中字段冲突,自定义字段会覆盖其他字段
#如果为false或者未设置,那么将放在子栏目中
fields_under_roottrue
output.logstash#输出至logstash
hosts"172.2.2.10:55043"
processors
add_host_metadata ~
add_cloud_metadata ~
drop_fields#删除无用的字段
fields"beat" "input" "source" "offset" "prospector"
3、测试输出内容
输出的内容格式如下,我们自定义的字段名字是indexname
[root@centos bin]# ./logstash -e
{
"ecs" => {
"version" => "1.4.0"
},
"host" => {
"id" => "5ac8c7f6d6fa45a4b3586ddeb6010436",
"architecture" => "x86_64",
"os" => {
"family" => "redhat",
"name" => "CentOS Linux",
"kernel" => "3.10.0-957.el7.x86_64",
"codename" => "Core",
"platform" => "centos",
"version" => "7 (Core)"
},
"name" => "rockettest-163",
"containerized" => false,
"hostname" => "rockettest-163"
},
"lock_time" => 0.000253,
"sql" => "select * from cashier_info_202305 where id>1000000 and id <1200000;",
"indexname" => "mysql",
"log" => {
"flags" => [
[0] "multiline"
],
"offset" => 2482,
"file" => {
"path" => "/data/mysql/data/mysql_slow.log"
}
},
"action" => "select",
"rows_sent" => 199974,
"row_id" => 23,
"tags" => [
[0] "mysql-slow-log",
[1] "beats_input_codec_plain_applied",
[2] "_jsonparsefailure"
],
"timestamp" => "1691001872",
"@timestamp" => 2023-08-02T18:44:33.859Z,
"user" => "root",
"clienthost" => "localhost",
"rows_examined" => 199974,
"agent" => {
"type" => "filebeat",
"id" => "62a73aae-a6f7-4b46-86ef-bbefdd5d70ad",
"ephemeral_id" => "c238172d-bedd-456d-aa59-095e81721912",
"hostname" => "rockettest-163",
"version" => "7.6.2"
},
"query_time" => 1.098111
}
filebeat自定义字段在logstash中调用方法
output
elasticsearch
hosts => "172.2.2.10:9200"
index => "%{indexname}-%{+YYYY-MM-dd}"