1.什么是Logstash?
- Logstash是一个开源的数据收集引擎,它具有实时数据传输能力。它可以统一过滤来自不同源的数据,并按照开发者制定的规范输出到目的地,支持正则表达式
- 由于日志文件来源多(如:系统日志、服务器日志、tomcat日志、nginx日志等),且内容杂乱,不便于人类进行观察。因此我们可以使用Logstash对日志文件进行收集和统一过滤,变成可读性高的内容
2.下载Logstash和安装部署
访问Logstash下载地址 进行下载本文章采用(7.12.1)版本进行演示
注意:Logstash需要使用Java环境 请提前配置好java环境
java -version #查看java版本
tar -zxvf logstash-7.12.1.tar.gz #将下载好的压缩包进行解压
3.运行Logstash
3.1 简单案例
cd /elk/logstash-7.12.1
bin/logstash -e 'input {stdin{}} output {stdout{}}' # 简单案例 大致如下:图Logstash.3-1
图:Logstash.3-1
3.2 使用配置文件进行启动
cd config/
cp logstash-sample.conf logstash.conf
vim logstash.conf
######################################
在配置文件中添加以下配置
input {
file {
path=> "/var/log/messages"
type=> "system"
start_position=> "beginning"
}
}
filter{
}
output{
#Standard output
stdout{}
}
bin/logstash -f config/logstash.conf
#该日志为liunx系统操作日志详情可见图:Logstash.3-2
图:Logstash.3-2
3.3 Logstash的input和filter 插件
插件详情地址:https://www.elastic.co/guide/en/logstash/7.12/index.html
- inputs输入
- codecs解码
- filters 过滤
- outputs 输出
查看插件
bin/logstash-plugin list
3.4数据处理流程
input->解码->filter->编码->output
4.Logstash的数据使用Kibana进行展示
4.1 Liunx相关操作
#新建一个logstash-elk.conf
input{
file{
path => "/var/log/messages"
type => "system"
start_position => "beginning"
}
}
filter {
}
output {
elasticsearch {
hosts => ["127.0.0.1:9200"]
index => "msg-%{+YYYY.MM.dd}"
}
}
#使用该命令进行后台运行
nohup /elk/logstash-7.12.1/bin/logstash -f /elk/logstash-7.12.1/config/logstash-elk.conf >> /elk/log/output.log 2>&1&
4.2 Kibana界面相关操作
展开列表-> Management->Stack Management
Data->Index Management
Next step->Time field(@timestamp)->Create index pattern 即可
在同一界面配置
Kibana->Index Patterns->Create index pattern
4.3 查看Logs
Home->Observability->Logs
Settings->Indices->Log indices (将其中default的内容改为msg-*)->Apply
4.4 查看日志视图信息
Home->Analytics->Discover
5.Logstash常见的报错信息
5.1[2021-05-10T13:42:21,424][ERROR][logstash.agent ] Failed to execute action
问题:
- 可能是conf内容书写有误
- 编码格式不是UTF-8
解决方案:请使用VSCODE 进行编辑 删除不必要的空格和换行符然后在Win下保存成UTF-8的形式 重新上传到Liunx上进行运行
5.2 A plugin had an unrecoverable error. Will restart this plugin.
问题:插件有不可修复的错误,请重新启动该插件
解决方案:将该logstash进程杀死重新运行即可
ps aux|grep logstash
kill -9/kill 进程ID
6.使用logstash收集Nginx日志展示
Liunx安装教程:https://www.runoob.com/linux/nginx-install-setup.html
6.1 nginx安装
tar zxvf nginx-1.18.0.tar.gz
./configure --prefix=/usr/local/nginx --withpcre=/usr/local/pcre-8.38 --withzlib=/usr/local/zlib-1.2.11
make
make install
- 启动Nginx
sbin/nginx -c conf/nginx.conf
6.2 Logstash启动和配置
编写logstash-nginx.conf
input {
file{
path => "/usr.local/nginx/logs/access.log"
type => "nginxaccess"
start_position => "beginning"
}
}
filter {
grok {
match => {"message"=> "%{HTTPD_COMBINEDLOG}"}
}
}
output{
stdout{
}
}
#后台启动Logstash
nohup /elk/logstash-7.12.1/bin/logstash -f /elk/logstash-7.12.1/config/logstash-nginx.conf >> /elk/log/output.log 2>&1 &
6.3 Kibana上查看Logstash获取到的日志信息
配置与前面4.2和 4.3 相同
6.4 使用timestamp时间格式处理
注意@timestamp
字段在Elasticsearch内部,对时间类型字段,统一采用UTC时间,存成long长整型。
编写logstash-nginx-time.conf 配置文件
input{
file{
path=>"/usr/local/nginx/logs/access.log"
type=>"nginxaccess"
start_position=>"beginning"
}
}
filter{
grok{
match=>{"message"=>"%{HTTPD_COMBINEDLOG}"}
}
date{
match=>["timestamp","dd/MMM/yyyy:HH:mm:ss Z"]
target =>"@timestamp"
}
}
output{
elasticsearch{
hosts=>["127.0.0.1:9200"]
index=>"nginx-time-%{+YYYY.MM.dd}"
}
}