Logstash在导入历史日志的时候,默认时间戳是现在实时的时间,后期搜索的时候会产生诸多不便。本文介绍在导入日志的时候如何实现修改@timestamp时间为日志的产生时间。
同时,顺便简单介绍下如何从文本文件导入日志。
一、准备
今年是2021年,但是我们准备了一份2019年的日志文件,大概长这样。
2019-11-08 20:26:11 Local7.Info 10.153.0.10 Apr 14 2000 19:20:57 CSDN-BJ-08F-LAN-SW-01 %%01POE/6/PORTPW(l)[19]:Port GigabitEthernet0/0/3 current power is 2295.
2019-11-08 20:26:23 Local7.Info 10.152.20.1 Nov 8 20:26:21 2019 CSDN-SZ-10F-MDF-New-H3C-CORE-SW %%10SHELL/6/SHELL_CMD: -Line=vty0-IPAddr=10.152.128.222-User=admin; Command is dis ip int brief
2019-11-08 20:26:31 Local7.Warning 10.152.250.3 Nov 8 2019 12:26:31 CSDN-SZ01-8F-MDF-01-AggSW-01 %%01IFPDT/4/IF_STATE(l)[73]:Interface GigabitEthernet1/0/8 has turned into DOWN state.
2019-11-08 20:26:31 Local7.Warning 10.152.250.3 Nov 8 2019 12:26:31 CSDN-SZ01-8F-MDF-01-AggSW-01 %%01IFADP/4/PORTDOWNINFO(l)[74]:Interface GigabitEthernet1/0/8 has turned into DOWN state.
这个文件是以tab为分隔符,总共四列,对于Logstash而言,只要文件是以固定的分隔符分割就可以,逗号,tab,空格都是支持的。
把这个文件保存成txt文件,保存到任意目录下,待会能找到就行。例如逗老师把这个文件保存在/root下
二、编写Logstash conf文件
直接贴个例子,详细的解释看注释。
input {
file {
path => ["/root/SyslogCatchAll-2019-11-08.txt"]#日志文件
start_position => "beginning"#从头开始读,默认是从结尾网上读
sincedb_path => "/dev/null"#导入记录数据库,调试期间可以配置为null,这样同一个源文件可以重复导入
}
}
filter {
csv {
separator => " "#配置分割字符,本文是tab,那这里就敲一个真实的制表符
columns => ["timestamp","severity_label","logsource","message"]#定义四列的标题
}
date {
#date模块来处理时间戳相关的信息
match => ["timestamp", "yyyy-MM-dd HH:mm:ss"]
#匹配日志源文件中时间格式为2019-11-08 20:26:31(yyyy-MM-dd HH:mm:ss)
target => "@timestamp"
#将匹配到的时间戳传递给系统变量,@timestamp
}
mutate {
#根据需要自行选择提出某些字段
remove_field => ["@version","severity","facility","facility_label","priority","host"]
}
}
output {
#输出到ES
elasticsearch {
hosts => ["http://localhost:9200"]
index => "syslog-message-history-data"
#user => "elastic"
#password => "changeme"
}
}
编辑完成后将文件保存为/root/logstash_import.conf
三、运行导入
使用-f参数手动运行logstash,并附带刚才编辑好的conf文件。
/usr/share/logstash/bin/logstash -f /root/logstash_import.conf
系统哗啦啦哗啦啦的跑完之后,去kibana里看效果。
可以看到,现在是2021年,但导入日志的时间戳为日志产生的时间,2019年