search
HomeSystem TutorialLINUXDetailed explanation of Elasticsearch's basic friend Logstash
Detailed explanation of Elasticsearch's basic friend LogstashJul 18, 2024 am 06:56 AM
linuxlinux tutorialRed Hatlinux systemlinux commandlinux certificationred hat linuxlinux video

Detailed explanation of Elasticsearchs basic friend Logstash

Logstash is a powerful data processing tool that can realize data transmission, format processing, formatted output, and has powerful plug-in functions, which are often used for log processing.

1. Principle

Input

Data can be extracted from files, storage, and databases. Input has two options. One is to hand it over to Filter for filtering and pruning. The other one is given directly to Output

Filter

Ability to dynamically transform and parse data. Data information can be filtered and pruned in a customized way

Output

Providing numerous output options, you can send data where you want it, with the flexibility to unlock numerous downstream use cases.

Detailed explanation of Elasticsearchs basic friend Logstash

2. Installation and use
1.Installation
wget https://artifacts.elastic.co/downloads/logstash/logstash-6.0.1.rpm
yum install -y ./logstash-6.0.1.rpm
2.Logstash configuration file
vim /etc/logstash/logstash.yml
path.data: /var/lib/logstash                                    # 数据存放路径
path.config: /etc/logstash/conf.d/*.conf                        # 其他插件的配置文件,输入输出过滤等等
path.logs: /var/log/logstash                                    # 日志存放路径
3.JVM configuration file in Logstash

Logstash is a program developed based on Java and needs to run in the JVM. It can be set for the JVM by configuring jvm.options. For example, the maximum and minimum memory, garbage cleaning mechanism, etc. Here are just two of the most commonly used ones.

The memory allocation of JVM cannot be too large or too small. If it is too large, it will slow down the operating system. Too small to start.

vim /etc/logstash/jvm.options                               # logstash有关JVM的配置
-Xms256m                                                    # logstash最大最小使用内存
-Xmx1g
4. The simplest log collection configuration

Install an httpd for testing and configure Logstash to collect Apache’s accless.log log file

yum install httpd
echo "Hello world" > /var/www/html/index.html               # 安装httpd,创建首页用于测试
vim /etc/logstash/conf.d/test.conf
input {
    file {                                                  # 使用file作为数据输入
        path => ['/var/log/httpd/access_log']               # 设定读入数据的路径
        start_position => beginning                         # 从文件的开始处读取,end从文件末尾开始读取
    }
}
output {                                                    # 设定输出的位置
    stdout {
        codec => rubydebug                                  # 输出至屏幕
    }
}
5. Test configuration file

Logstash is a built-in command but it is not included in the environment variables, so you can only use the absolute path to use this command.

/usr/share/logstash/bin/logstash -t  -f  /etc/logstash/conf.d/test.conf   # 测试执行配置文件,-t要在-f前面
Configuration OK                                                          # 表示测试OK
6. Start logstash

After running logstash in the current session, do not close this session. Temporarily call it Session 1, and then open a new window as Session 2

/usr/share/logstash/bin/logstash  -f  /etc/logstash/conf.d/test.conf

After starting, use the curl command in session 2 to test

curl 172.18.68.14

Then you can see the output information when you return to the previous session 1

{
      "@version" => "1",
          "host" => "logstash.shuaiguoxia.com",
          "path" => "/var/log/httpd/access_log",
    "@timestamp" => 2017-12-10T14:07:07.682Z,
       "message" => "172.18.68.14 - - [10/Dec/2017:22:04:44 +0800] \"GET / HTTP/1.1\" 200 12 \"-\" \"curl/7.29.0\""
}

At this point, the simplest Logstash configuration has been completed. Here, the collected direct output is just collected without filtering or pruning.

3. Elasticsearch and Logstash

上面的配置时Logsatsh从日志文件中抽取数据,然后输出至屏幕。那么在生产中往往是将抽取的数据过滤后输出到Elasticsearch中。下面讲解Elasticsearch结合Logstash

Logstash抽取httpd的access.log文件,然后经过过滤(结构化)之后输出给Elasticsearch Cluster,在使用Head插件就可以看到抽取到的数据。(Elasticsearch Cluster与Head插件搭建请查看前两篇文章)

Detailed explanation of Elasticsearchs basic friend Logstash

配置Logstash

    vim /etc/logstash/conf.d/test.conf
    input {
    file {
        path => ['/var/log/httpd/access_log']
        start_position => "beginning"
    }
    }
    filter {
    grok {
        match => {
            "message" => "%{COMBINEDAPACHELOG}"
        }

        remove_field => "message"   
    }
    }
    output {
    elasticsearch {
        hosts => ["http://172.18.68.11:9200","http://172.18.68.12:9200","http://172.18.68.13:9200"]
        index => "logstash-%{+YYYY.MM.dd}"
        action => "index"
        document_type => "apache_logs"
    }
    }

启动Logstash

     /usr/share/logstash/bin/logstash -t -f /etc/logstash/conf.d/test.conf       # 测试配置文件
    Configuration OK
     /usr/share/logstash/bin/logstash  -f /etc/logstash/conf.d/test.conf         # 启动Logstash

测试

每个执行10次172.18.68.14,位Logstash的地址

    curl 127.0.0.1
    curl 172.18.68.14

验证数据

使用浏览器访问172.18.68.11:9100(Elastisearch 安装Head地址,前面文章有讲)

选择今天的日期,就能看到一天内访问的所有数据。

Detailed explanation of Elasticsearchs basic friend Logstash

四、监控其他

监控Nginx日志

仅仅列了filter配置块,input与output参考上一个配置

    filter {
        grok {
                match => {
                        "message" => "%{HTTPD_COMBINEDLOG} \"%{DATA:realclient}\""
                }
                remove_field => "message"
        }
        date {
                match => ["timestamp","dd/MMM/YYYY:H:m:s Z"]
                remove_field => "timestamp"
        }
    }

监控Tomcat

仅仅列了filter配置块,input与output参考上一个配置

    filter {
        grok {
                match => {
                        "message" => "%{HTTPD_COMMONLOG}"
                }
                remove_field => "message"
        }
        date {
                match => ["timestamp","dd/MMM/YYYY:H:m:s Z"]
                remove_field => "timestamp"
        }
    } 
五、Filebeat

现在已经搭建成在节点安装Logstash并发送到Elasticsearch中去,但是Logstash是基于Java开发需要运行在JVM中,所以是一个重量级采集工具,仅仅对于一个日志采集节点来说使用Logstash太过重量级,那么就可以使用一个轻量级日志收集工具Filebeat来收集日志信息,Filebeat同一交给Logstash进行过滤后再Elasticsearch。这些在接下来的文章在进行讲解,先放一张架构图吧。

Detailed explanation of Elasticsearchs basic friend Logstash

The above is the detailed content of Detailed explanation of Elasticsearch's basic friend Logstash. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
什么是linux设备节点什么是linux设备节点Apr 18, 2022 pm 08:10 PM

linux设备节点是应用程序和设备驱动程序沟通的一个桥梁;设备节点被创建在“/dev”,是连接内核与用户层的枢纽,相当于硬盘的inode一样的东西,记录了硬件设备的位置和信息。设备节点使用户可以与内核进行硬件的沟通,读写设备以及其他的操作。

Linux中open和fopen的区别有哪些Linux中open和fopen的区别有哪些Apr 29, 2022 pm 06:57 PM

区别:1、open是UNIX系统调用函数,而fopen是ANSIC标准中的C语言库函数;2、open的移植性没fopen好;3、fopen只能操纵普通正规文件,而open可以操作普通文件、网络套接字等;4、open无缓冲,fopen有缓冲。

linux中什么叫端口映射linux中什么叫端口映射May 09, 2022 pm 01:49 PM

端口映射又称端口转发,是指将外部主机的IP地址的端口映射到Intranet中的一台计算机,当用户访问外网IP的这个端口时,服务器自动将请求映射到对应局域网内部的机器上;可以通过使用动态或固定的公共网络IP路由ADSL宽带路由器来实现。

linux中eof是什么linux中eof是什么May 07, 2022 pm 04:26 PM

在linux中,eof是自定义终止符,是“END Of File”的缩写;因为是自定义的终止符,所以eof就不是固定的,可以随意的设置别名,linux中按“ctrl+d”就代表eof,eof一般会配合cat命令用于多行文本输出,指文件末尾。

linux怎么判断pcre是否安装linux怎么判断pcre是否安装May 09, 2022 pm 04:14 PM

在linux中,可以利用“rpm -qa pcre”命令判断pcre是否安装;rpm命令专门用于管理各项套件,使用该命令后,若结果中出现pcre的版本信息,则表示pcre已经安装,若没有出现版本信息,则表示没有安装pcre。

linux怎么查询mac地址linux怎么查询mac地址Apr 24, 2022 pm 08:01 PM

linux查询mac地址的方法:1、打开系统,在桌面中点击鼠标右键,选择“打开终端”;2、在终端中,执行“ifconfig”命令,查看输出结果,在输出信息第四行中紧跟“ether”单词后的字符串就是mac地址。

手机远程linux工具有哪些手机远程linux工具有哪些Apr 29, 2022 pm 05:30 PM

手机远程linux工具有:1、JuiceSSH,是一款功能强大的安卓SSH客户端应用,可直接对linux服务进行管理;2、Termius,可以利用手机来连接Linux服务器;3、Termux,一个强大的远程终端工具;4、向日葵远程控制等等。

linux中rpc是什么意思linux中rpc是什么意思May 07, 2022 pm 04:48 PM

在linux中,rpc是远程过程调用的意思,是Reomote Procedure Call的缩写,特指一种隐藏了过程调用时实际通信细节的IPC方法;linux中通过RPC可以充分利用非共享内存的多处理器环境,提高系统资源的利用率。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),