ELK filebeat和logstash使用:配置单个文件来源、配置多个文件来源

一、Windows下载安装

1、filebeat

cd D:\ProgramFiles\ELK\filebeat-7.2.0-windows-x86
filebeat.exe -e -c filebeat.yml

2、logstash

cd D:\ProgramFiles\ELK\logstash-7.2.0\bin
logstash.bat -f ../logstash_test.conf
  • 配置文件 logstash_test.conf写哪都可以,只要你能够正确指定。我的配置文件写在logstash-7.2.0 根目录,也是bin 的同级目录

二、配置

1、梳理下流程

[ filebeat ] 配置

  • 1、把指定位置paths的日志文件input(输入)到 filebeat
  • 2、从 filebeat 把数据 output(输出)到 logstash

[ logstash ] 配置

  • 3、在logstashinput(输入)filebeat 传过来的数据
  • 4、进行filter数据过滤匹配
  • 5、把数据output(输入)到es

总结:利用 Filebeat 去读取日志发送到 Logstash ,再由 Logstash 对日志分析处理,提取我们需要的项后发送给 Elasticsearch 存储起来, 然后利用 Kibana 去分析获得的日志。

2、filebeat配置

  • filebeat.inputs:- type: log 配置
- type: log
  enabled: true #设置为true
  paths:
    - E:\test\log\*\[0-9][0-9].log #日志文件地址
  fields:
    logtype: testlog # 日志类型,与 logstash中的字段 [fields][logtype] 相呼应
  multiline.pattern:  '^\[' #我们的日志一条记录可能有多行,在这里要做好正则,把多行日志分割成一条条的记录
  multiline.negate: true
  multiline.match: after
  • Outputs中默认把数据output(输出)到Es,我们注释掉这行hosts: ["localhost:9200"],改成把数据output(输出)到 logstash
#-------------------------- Elasticsearch output ------------------------------
#output.elasticsearch:
  # Array of hosts to connect to.
  #hosts: ["localhost:9200"]

  # Optional protocol and basic auth credentials.
  #protocol: "https"
  #username: "elastic"
  #password: "changeme"

#----------------------------- Logstash output --------------------------------
output.logstash:
  # The Logstash hosts
  hosts: ["localhost:9601"] #自定义一个未被占用的端口,与logstash input中的 beats port 相呼应

3、logstash配置

# 数据输入:logstash单独使用的话的直接从我们的log文件中输入数据,这里我们从filebeat中输入数据 port 与 Logstash output 中我们配置的端口号相呼应        
input {
    
    
	beats {
    
    
		port => 9601
	}
  
	#file {
    
    
		#path => "E:/test/log/*/*.log"
		#start_position => "beginning"
		#stat_interval => 3
	#}
}
# 数据过滤
filter {
    
    
	if [fields][logtype] == "testlog" {
    
     # [fields][logtype] 等于 filebeat中的logtype字段
		if ([message]=~ "正则表达式") {
    
     # message就是指原始消息:如果原始消息满足这个正则表达式,则丢弃该记录
			drop{
    
    }
		}
		grok {
    
    
			match => {
    
    
			    # 尝试匹配message的数据,把匹配到的数据存入es表,字段名如下 为自定义的 timestamp,client_ip,...
				# message配置:string or array
				"message" => [
					"\[%{TIMESTAMP_ISO8601:timestamp}\] %{IP:client_ip} %{USERNAME:method} %{URL:url}%{CUSTOMURIPARAM:param} %{NUMBER:duration}"
				]
			}
		}
		# 把我们的时间格式 YYYY-mm-dd HH:ii:ss 转化为时间戳的形式,存入到es:替换es的@timestamp字段
		date {
    
    
			match => ["timestamp", "ISO8601"]
			target => "@timestamp"
		}
		# 由于我们匹配出来了一个timestamp字段,并且替换了es默认的@timestamp字段;所以es的字段timestamp和@timestamp值是想等的,我们删除自己匹配出来的字段timestamp
		mutate{
    
    
           remove_field => ["timestamp"]
        }
	}
    
}
# 数据输出
output {
    
    
	if [fields][logtype] == "testlog" {
    
     #文件来源是 testlog,对应filebeat的 logtype 字段
		if "_grokparsefailure" not in [tags]{
    
     #匹配成功,存es的success-log 表
			elasticsearch {
    
    
			    # hosts配置:string or array
				hosts => ["localhost:9200"]
				index => "success-log-%{+YYYY.MM.dd}"
			}
		} else {
    
     #匹配失败,存es的error-log 表
			elasticsearch {
    
    
				hosts => ["localhost:9200"]
				index => "error-log-%{+YYYY.MM.dd}"
			}
		}
	} #else { #当filebeat有多个日志文件来源时,根据filebeat的 logtype 字段判断要存入哪张es表
		#elasticsearch {
    
    
				#hosts => ["localhost:9200"]
				#index => "other-log-%{+YYYY.MM.dd}"
			#}
	#}
}

三、filebeat配置多个文件来源

filebeat.inputs:配置多个- type: log即可,然后在logstash中根据字段[fields][logtype]判断文件来源,对不同来源进行不同的操作

[ 示例 ]

# 文件来源1-START
- type: log
  enabled: true
  paths:
    E:\test\log\*\[0-9][0-9]_test1.log #日志文件地址
  fields:
    logtype: testlog1
  multiline.pattern:  '日志切割正则'
  multiline.negate: true
  multiline.match: after
# 文件来源1-END

# 文件来源2-START
- type: log
  enabled: true
  paths:
    - E:\test\log\*\??_test2.log #日志文件地址
  fields:
    logtype: testlog2
  multiline.pattern:  '日志切割正则'
  multiline.negate: true
  multiline.match: after
# 文件来源2-END
  • 如果你想知道你写的文件路径是否正确,filebeat是否读取到了你的文件地址,请前往文件:filebeat-7.2.0-windows-x86\data\registry\filebeat\data.json查看

四、相关地址

猜你喜欢

转载自blog.csdn.net/qq_36025814/article/details/108845153