热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

添加logstash过滤器

2019独角兽企业重金招聘Python工程师标准Introduction介绍Logstashisapowerfultoolforcentralizingandanalyzin

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

Introduction介绍

Logstash is a powerful tool for centralizing and analyzing logs, which can help to provide and overview of your environment, and to identify issues with your servers. One way to increase the effectiveness of your ELK Stack (Elasticsearch, Logstash, and Kibana) setup is to collect important application logs and structure the log data by employing filters, so the data can be readily analyzed and query-able. We will build our filters around "grok" patterns, that will parse the data in the logs into useful bits of information.

本教程是“How To Install Elasticsearch, Logstash, and Kibana 4 on Ubuntu 14.04”的后续,专注于为多种多样的应用日志添加logstash过滤器。

前置条件

要学习本教程,需要有一个运行的Logstash服务,且接收来自Filebeat等日志传输工具的日志。如果你没有,按照这篇教程“How To Install Elasticsearch, Logstash, and Kibana 4 on Ubuntu 14.04”搭建这样的环境。

 

ELK服务端设定

  • Logstash安装在/opt/logstash
  • Logstash配置文件目录/etc/logstash/conf.d
  • 输入文件名称为02-beats-input.conf
  • 输出文件名称为30-elasticsearch-output.conf

使用如下命令创建patterns目录:

  • sudo mkdir -p /opt/logstash/patterns
  • sudo chown logstash: /opt/logstash/patterns

Client Server Assumptions客户端服务器设定

  • 每个客户端应用服务器上Filebeat已经配置好,将syslog/auth.log发送到Logstash服务器。

如果你的配置与此不同,将本教程适当调整即可。

Grok介绍

Grok使用正则匹配,解析文本模式,并将其分配给一个标识符。

Grok模式的语法是%{PATTERN:IDENTIFIER}。Logstash过滤器包含了一系列grok模式进行匹配,并将匹配的日志片段分配给相应的标识符,将日志输出进行格式化。

更多grok相关内容,查看 Logstash grok page和 Logstash Default Patterns listing

如何学习本教程

本文下面的每一主要章节都包含了收集和过滤特定应用日志的详细配置。如果你想收集过滤某个应用的日志,你需要在客户端(filebeat)和Logstash服务端都进行配置。

Logstash模式子章节

Logstash模式子章节包含了grok模式,你可以添加到Logstash服务器的/opt/logstash/patterns目录下,你可以在Logstash过滤器部分使用新的模式。

过滤器子章节

Logstash过滤器子章节The Logstash Filter subsections will include a filter that can can be added to a new file, between the input and output configuration files, in /etc/logstash/conf.d on the Logstash Server. The filter determine how the Logstash server parses the relevant log files. Remember to restart the Logstash service after adding a new filter, to load your changes.

Filebeat Prospector 子章节

Filebeat Prospectors are used specify which logs to send to Logstash. Additional prospector configurations should be added to the /etc/filebeat/filebeat.yml file directly after existing prospectors in the prospectors section:

Prospector Examples

filebeat:# List of prospectors to fetch data.prospectors:-- /var/log/secure- /var/log/messagesdocument_type: syslog-paths:- /var/log/app/*.logdocument_type: app-access
...

In the above example, the red highlighted lines represent a Prospector that sends all of the .log files in /var/log/app/ to Logstash with the app-access type. After any changes are made, Filebeat must be reloaded to put any changes into effect.

Now that you know how to use this guide, the rest of the guide will show you how to gather and filter application logs!

Application: Nginx

Logstash Patterns: Nginx

Nginx log patterns are not included in Logstash's default patterns, so we will add Nginx patterns manually.

On your ELK server, create a new pattern file called nginx:

 

  • sudo vi /opt/logstash/patterns/nginx

Then insert the following lines:

Nginx Grok Pattern

NGUSERNAME [a-zA-Z\.\@\-\+_%]+
NGUSER %{NGUSERNAME}
NGINXACCESS %{IPORHOST:clientip} %{NGUSER:ident} %{NGUSER:auth} \[%{HTTPDATE:timestamp}\] "%{WORD:verb} %{URIPATHPARAM:request} HTTP/%{NUMBER:httpversion}" %{NUMBER:response} (?:%{NUMBER:bytes}|-) (?:"(?:%{URI:referrer}|-)"|%{QS:referrer}) %{QS:agent}

Save and exit. The NGINXACCESS pattern parses, and assigns the data to various identifiers (e.g. clientipidentauth, etc.).

Next, change the ownership of the pattern file to logstash:

 

  • sudo chown logstash: /opt/logstash/patterns/nginx

Logstash Filter: Nginx

On your ELK server, create a new filter configuration file called 11-nginx-filter.conf:

 

  • sudo vi /etc/logstash/conf.d/11-nginx-filter.conf

Then add the following filter:

Nginx Filter

filter {if [type] == "nginx-access" {grok {match => { "message" => "%{NGINXACCESS}" }}}
}

Save and exit. Note that this filter will attempt to match messages of nginx-access type with the NGINXACCESS pattern, defined above.

Now restart Logstash to reload the configuration:

 

  • sudo service logstash restart

Filebeat Prospector: Nginx

On your Nginx servers, open the filebeat.yml configuration file for editing:

 

  • sudo vi /etc/filebeat/filebeat.yml

Add the following Prospector in the filebeat section to send the Nginx access logs as type nginx-accessto your Logstash server:

Nginx Prospector

-paths:- /var/log/nginx/access.logdocument_type: nginx-access

Save and exit. Reload Filebeat to put the changes into effect:

 

  • sudo service filebeat restart

Now your Nginx logs will be gathered and filtered!

Application: Apache HTTP Web Server

Apache's log patterns are included in the default Logstash patterns, so it is fairly easy to set up a filter for it.

Note: If you are using a RedHat variant, such as CentOS, the logs are located at /var/log/httpd instead of /var/log/apache2, which is used in the examples.

Logstash Filter: Apache

On your ELK server, create a new filter configuration file called 12-apache.conf:

 

  • sudo vi /etc/logstash/conf.d/12-apache.conf

Then add the following filter:

Apache Filter

filter {if [type] == "apache-access" {grok {match => { "message" => "%{COMBINEDAPACHELOG}" }}}
}

Save and exit. Note that this filter will attempt to match messages of apache-access type with the COMBINEDAPACHELOG pattern, one the default Logstash patterns.

Now restart Logstash to reload the configuration:

 

  • sudo service logstash restart

Filebeat Prospector: Apache

On your Apache servers, open the filebeat.yml configuration file for editing:

 

  • sudo vi /etc/filebeat/filebeat.yml

Add the following Prospector in the filebeat section to send the Apache logs as type apache-access to your Logstash server:

Apache Prospector

-paths:- /var/log/apache2/access.logdocument_type: apache-access

Save and exit. Reload Filebeat to put the changes into effect:

 

  • sudo service filebeat restart

Now your Apache logs will be gathered and filtered!

Conclusion

It is possible to collect and parse logs of pretty much any type. Try and write your own filters and patterns for other log files.

Feel free to comment with filters that you would like to see, or with patterns of your own!

If you aren't familiar with using Kibana, check out this tutorial: How To Use Kibana Visualizations and Dashboards.

https://www.digitalocean.com/community/tutorials/adding-logstash-filters-to-improve-centralized-logging


转:https://my.oschina.net/u/259976/blog/831518



推荐阅读
  • PyCharm下载与安装指南
    本文详细介绍如何从官方渠道下载并安装PyCharm集成开发环境(IDE),涵盖Windows、macOS和Linux系统,同时提供详细的安装步骤及配置建议。 ... [详细]
  • 本文详细记录了在基于Debian的Deepin 20操作系统上安装MySQL 5.7的具体步骤,包括软件包的选择、依赖项的处理及远程访问权限的配置。 ... [详细]
  • Explore how Matterverse is redefining the metaverse experience, creating immersive and meaningful virtual environments that foster genuine connections and economic opportunities. ... [详细]
  • 本文详细介绍了如何在Linux系统上安装和配置Smokeping,以实现对网络链路质量的实时监控。通过详细的步骤和必要的依赖包安装,确保用户能够顺利完成部署并优化其网络性能监控。 ... [详细]
  • 深入理解 SQL 视图、存储过程与事务
    本文详细介绍了SQL中的视图、存储过程和事务的概念及应用。视图为用户提供了一种灵活的数据查询方式,存储过程则封装了复杂的SQL逻辑,而事务确保了数据库操作的完整性和一致性。 ... [详细]
  • 本文详细介绍了如何使用 Yii2 的 GridView 组件在列表页面实现数据的直接编辑功能。通过具体的代码示例和步骤,帮助开发者快速掌握这一实用技巧。 ... [详细]
  • DNN Community 和 Professional 版本的主要差异
    本文详细解析了 DotNetNuke (DNN) 的两种主要版本:Community 和 Professional。通过对比两者的功能和附加组件,帮助用户选择最适合其需求的版本。 ... [详细]
  • 1.如何在运行状态查看源代码?查看函数的源代码,我们通常会使用IDE来完成。比如在PyCharm中,你可以Ctrl+鼠标点击进入函数的源代码。那如果没有IDE呢?当我们想使用一个函 ... [详细]
  • CentOS7源码编译安装MySQL5.6
    2019独角兽企业重金招聘Python工程师标准一、先在cmake官网下个最新的cmake源码包cmake官网:https:www.cmake.org如此时最新 ... [详细]
  • 本文详细介绍了 Dockerfile 的编写方法及其在网络配置中的应用,涵盖基础指令、镜像构建与发布流程,并深入探讨了 Docker 的默认网络、容器互联及自定义网络的实现。 ... [详细]
  • c# – UWP:BrightnessOverride StartOverride逻辑 ... [详细]
  • 深入解析Spring Cloud Ribbon负载均衡机制
    本文详细介绍了Spring Cloud中的Ribbon组件如何实现服务调用的负载均衡。通过分析其工作原理、源码结构及配置方式,帮助读者理解Ribbon在分布式系统中的重要作用。 ... [详细]
  • Android 渐变圆环加载控件实现
    本文介绍了如何在 Android 中创建一个自定义的渐变圆环加载控件,该控件已在多个知名应用中使用。我们将详细探讨其工作原理和实现方法。 ... [详细]
  • Android LED 数字字体的应用与实现
    本文介绍了一种适用于 Android 应用的 LED 数字字体(digital font),并详细描述了其在 UI 设计中的应用场景及其实现方法。这种字体常用于视频、广告倒计时等场景,能够增强视觉效果。 ... [详细]
  • 作为一名新手,您可能会在初次尝试使用Eclipse进行Struts开发时遇到一些挑战。本文将为您提供详细的指导和解决方案,帮助您克服常见的配置和操作难题。 ... [详细]
author-avatar
罗kowalske
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有