アセンブリ詳細logstash(入力、コーデック、フィルタ、出力)

詳細な組立logstash

概念とlogstashの特性。

概念:logstashは、データ収集、処理及び送信手段(出力)です。
特長:

  -集中データ処理のすべての種類
  -異なるモデル及びデータのフォーマットの標準化
  -カスタムログ形式の急速な拡大から
  -には容易にカスタムデータソースプラグインを追加します

logstashインストール構成。

①.下载安装
[root@node1 ~]# wget https://download.elastic.co/logstash/logstash/packages/centos/logstash-7.4.0-1.noarch.rpm
[root@node1 ~]# rpm -ivh logstash-7.4.0-1.noarch.rpm

②.简单测试
logstash,启动后输入"hello,xkops"。
[root@node1 ~]# /opt/logstash/bin/logstash -e 'input{ stdin{} } output{ stdout{} }'
*提示:如果输出"hello,xkops",则证明logstash启动成功。

③.以服务启动方式。
[root@node1 ~]# service logstash start
总结:logstash三种启动方式,-e sting类型启动,-f 指定配置文件启动,服务启动。

詳細な設定文をlogstash。

三つの部分を含むlogstashプロファイル設定、すなわち:入力{}、フィルタ{} 、出力{}。
{}エリアの定義、領域は、収集、処理のために、一つ以上のインサートを定義し、プラグを介してデータを出力することができます。

データの種類:

  • ブール型:ssl_enable =>真
  • バイトタイプ:バイト=> "1MiB"
  • 文字列型:名前=> "xkops"
  • 値の種類:ポート=> 22
  • 数组:マッチ=> [ "日時"、 "UNIX"]
  • 哈希:オプション=> {キー1 => "値1"、KEY2 => "値2"}
  • コーデック:コーデック=> "JSON"
  • 路径:FILE_PATH => "を/ tmp /ファイル名"

条件:

  等于: ==
  不等于: !=
  小于: <
  大于: >
  小于等于: <=
  大于等于: >=
  匹配正则: =~
  不匹配正则: !~
  包含: in
  不包含: not in 
  与:    and
  或:    or
  非与: nand
  非或:   xor
  复合表达式: ()
  取反符合: !()

一般的なプラグインをlogstash

タイプ:

  • 入力クラス:即ち入力領域内のプラグインを定義します。
  • コーデックの種類:スケジュールされたデータ処理フォーマット、例えば普通、JSON、json_lines形式として。これは、入力、出力領域で定義することができます。
  • フィルタークラス:即ちフィルタ領域内のプラグインを定義します。
  • 出力クラス:即ち出力領域内のプラグインを定義します。
  • プラグアドレスの様々なタイプ:HTTPS://github.com/logstash-plugins

入力タイププラグ

ファイル、TCP、UDP、syslogの、ビートなど:入力タイプのプラグは、一般プラグインを使用します。

①.fileプラグイン:

file插件字段解释:
codec =>             #可选项,默认是plain,可设置其他编码方式。

discover_interval => #可选项,logstash多久检查一下path下有新文件,默认15s。

exclude =>           #可选项,排除path下不想监听的文件。

sincedb_path =>      #可选项,记录文件以及文件读取信息位置的数据文件。~/.sincedb_xxxx

sincedb_write_interval => #可选项,logstash多久写一次sincedb文件,默认15s.

stat_interval =>          #可选项,logstash多久检查一次被监听文件的变化,默认1s。

start_position =>         #可选项,logstash从哪个位置读取文件数据,默认从尾部,值为:end。初次导入,设置为:beginning。

path =>                   #必选项,配置文件路径,可定义多个。

tags =>                   #可选项,在数据处理过程中,由具体的插件来添加或者删除的标记。

type =>                   #可选项,自定义处理时间类型。比如nginxlog。

例:

[root@node1 conf.d]# cat /tmp/file{1,2}.log
This is test file-plugin in file1.log
This is test file-plugin in file2.log
[root@node1 conf.d]# pwd
/etc/logstash/conf.d
[root@node1 conf.d]# cat file.conf
input{
    file{
        start_position => "beginning"
        path => ["/tmp/file1.log","/tmp/file2.log"]
        type => 'filelog'
    }
}
output{
    stdout{}
}
[root@node1 conf.d]# /opt/logstash/bin/logstash -f file.conf -t
Configuration OK
[root@node1 conf.d]# /opt/logstash/bin/logstash -f file.conf 
Settings: Default pipeline workers: 1
Pipeline main started
2019-07-16T02:50:07.410Z node1 This is test file-plugin in file1.log
2019-07-16T02:50:07.428Z node1 This is test file-plugin in file2.log
*提示:能够输出内容则证明file插件正常工作。

②.tcpプラグイン:

tcp插件字段解释:
add_field =>              #可选项,默认{}。
codec =>                  #可选项,默认plain。
data_timeout =>           #可选项,默认-1。
host =>                   #可选项,默认0.0.0.0。
mode =>                   #可选项,值为["server","client"]之一,默认为server。
port =>                   #必选,端口。
ssl_cacert =>             #可选项,定义相关路径。
ssl_cert =>               #可选项,定义相关路径。
ssl_enable =>             #可选项,默认为false。
ssl_key =>                #可选项,定义相关路径。
ssl_key_passphrase =>     #可选项,默认nil
ssl_verify =>             #可选项,默认false。
tags =>                   #可选项
type =>                   #可选项

例:

[root@node1 conf.d]# cat /tmp/tcp.log 
this is test tcp-plugin in tcp.log
send tcplog data
output tcplog data
[root@node1 conf.d]# pwd
/etc/logstash/conf.d
[root@node1 conf.d]# cat tcp.conf
input{
    tcp{
        host => "127.0.0.1"
        port => 8888
        type => "tcplog"
    }
}
output{
    stdout{}
}
[root@node1 conf.d]# /opt/logstash/bin/logstash -f tcp.conf
# 此时开启另个终端,使用nc开启一个tcp端口8888,并将数据推送到8888端口。
[root@node1 conf.d]# nc 127.0.0.1 8888 < /tmp/tcp.log 
# 提示:前一个终端如果出现数据,则证明tcp插件工作正常。

③udpプラグイン:

udp插件字段解释:
add_field =>         #可选项,默认{}。
host =>              #可选项,默认0.0.0.0。
queue_size =>        #默认2000
tags =>              #可选项
type =>              #可选项
workers =>           #默认为2

例:

[root@node1 conf.d]# pwd
/etc/logstash/conf.d
[root@node1 conf.d]# cat udp.conf
input{
    udp{
        host => "127.0.0.1"
        port => 9999
    }
}
output{
    stdout{}
}
[root@node1 conf.d]# /opt/logstash/bin/logstash -f udp.conf
# 打开另一终端执行如下脚本,并输入内容:"hello,udplog"。
[root@node1 conf.d]# cat /tmp/udpclient.py 
#/usr/bin/env python
import socket

host = "127.0.0.1"
port = 9999
file_input = raw_input("Please input udp log: ")
s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
s.sendto(file_input,(host,port))

*ヒント:端末は、ログの前に受信された場合には、仕事へのプラグインを適切にUDP証明します。

④.syslogプラグイン:

例:

[root@node1 conf.d]# pwd
/etc/logstash/conf.d
[root@node1 conf.d]# cat syslog.conf
input{
    syslog{
        host => "127.0.0.1"
        port => 518
        type => "syslog"
    }
}
output{
    stdout{}
}
[root@node1 conf.d]# echo '*.* @@127.0.0.1:518' >> /etc/rsyslog.conf
[root@node1 conf.d]# /etc/init.d/rsyslog restart
关闭系统日志记录器: [确定]
启动系统日志记录器: [确定]
[root@node1 conf.d]# /opt/logstash/bin/logstash -f syslog.conf 
使用logger命令向系统写入日志:
[root@node1 conf.d]# logger

# 提示:此处随便输入内容,查看前一个终端是否会有内容输出,如果输出则证明syslog插件工作正常。

コーデッククラスのプラグイン

コーデックタイプのプラグ、プラグ使用:普通、JSON、json_lines、rubydebug 、マルチなど。
①.plainプラグイン:

实例:
[root@node1 conf.d]# pwd
/etc/logstash/conf.d
[root@node1 conf.d]# cat plain.conf
input{
    stdin{
        codec => "plain"
    }
}
output{
    stdout{}
}
[root@node1 conf.d]# /opt/logstash/bin/logstash -f plain.conf 
输入信息查看输出。

②.jsonプラグイン:

实例:
[root@node1 conf.d]# pwd
/etc/logstash/conf.d
[root@node1 conf.d]# cat json.conf
input{
    stdin{}
}
output{
    stdout{
        codec => "json"
    }
}
[root@node1 conf.d]# /opt/logstash/bin/logstash -f json.conf 
输入信息查看输出。

③.json_linesはプラグイン:(長すぎるJSONテキストを使用した場合)

实例:
[root@node1 conf.d]# pwd
/etc/logstash/conf.d
[root@node1 conf.d]# cat jsonlines.conf
input{
    tcp{
        host => "127.0.0.1"
        port => 8888
        codec => "json_lines"
    }
}
output{
    stdout{}
}
[root@node1 conf.d]# /opt/logstash/bin/logstash -f jsonlines.conf 
启动一个新的终端,执行如下命令。
[root@node1 conf.d]# cat /tmp/jsonlines.txt 
You run a price alerting platform which allows price-savvy customers to specify a rule like "I am interested in buying a specific electronic gadget and I want to be notified if the price of gadget falls below $X from any vendor within the next month". In this case you can scrape vendor prices, push them into Elasticsearch and use its reverse-search (Percolator) capability to match price movements against customer queries and eventually push the alerts out to the customer once matches are found.
[root@node1 conf.d]# nc 127.0.0.1 8888 < /tmp/jsonlines.txt 
# 提示:观察前一个终端的输出,如果正常输出,则json_lines插件工作正常。

④.rubedebugプラグイン:

例:

[root@node1 conf.d]# cat rubydebug.conf
input{
    stdin{
        codec => "json"
    }
}
output{
    stdout{
        codec => "rubydebug"
    }
}
[root@node1 conf.d]# /opt/logstash/bin/logstash -f rubydebug.conf
输入json串查看输出效果。
json串:{"name":"xkops","age":"25"}

⑤.multilineプラグ:(処理エラーログ)

multiline插件字段:
charset =>           #字符编码,可选
max_bytes =>         #bytes类型,设置最大的字节数,可选
max_lines =>         #number类型,设置最大的行数,默认是500行,可选
multiline_tag =>     #string类型,设置一个事件标签,默认是"multiline" ,可选
pattern =>           #string 类型,设置匹配的正则表达式 ,必选 
patterns_dir =>      #array类型,可以设置多个正则表达式,可选
negate =>            #boolean类型,设置正向匹配还是反向匹配,默认是false,可选
what =>              #设置未匹配的内容是向前合并还是向后合并,previous, next 两个值选择,必选
错误日志:
[16-07-2016 22:54:01] PHP warning: unknown exception in /xxx/test/index.php:99
111111111111111111
222222222222222222
[16-07-2016 23:19:43] PHP warning: unknown exception in /xxx/test/index.php:93
[root@node1 conf.d]# pwd
/etc/logstash/conf.d
[root@node1 conf.d]# cat codecmultilines.conf
input{
    stdin{
        codec => multiline{
        pattern => "^\["
        negate => true
        what => "previous"
        }
    }
}
output{
    stdout{}
}

[root@node1 conf.d]# /opt/logstash/bin/logstash -f codecmultilines.conf 
# 提示:输入上述错误日志查看输出。

フィルタプラグインクラス

フィルタプラグ、一般的に使用されるフィルタプラグイン:JSON、GROKなど。

①.jsonプラグイン:

add_field =>     #hash(可选项),默认{}
add_tag =>       #array(可选项),默认[]
remove_field =>  #array(可选项),默认[]
remove_tag =>    #array(可选项),默认[]
source =>        #string(必选项)
target =>        #string(可选项)

例:

[root@node1 conf.d]# pwd
/etc/logstash/conf.d
[root@node1 conf.d]# cat filterjson.conf

input{
    stdin{}
}
filter{
    json{
        source => "message"
        #target => "content"
    }
}
output{
    stdout{
        codec => "rubydebug"
    }
}

[root@node1 conf.d]# /opt/logstash/bin/logstash -f filterjson.conf 
输入两个串,查看输出。
{"name":"xkops","age":"25"}
name xkops

②.grokプラグ

GROKさまざまなプラグインを解決するために、非構造化ログデータを差し込みます。
GROKは、表示パターンが豊富です。

cat /opt/logstash/vendor/bundle/jruby/1.9/gems/logstash-patterns-core-2.0.5/patterns/grok-patterns
https://github.com/logstash-plugins/logstash-patterns-core/blob/master/patterns/grok-patterns

例:

输入的元数据内容:
20.3.1.3 GET /xkops/index.html 8838 1.323
[root@node1 conf.d]# pwd
/etc/logstash/conf.d
[root@node1 conf.d]# cat filtergrok.conf
input{
    stdin{}
}
filter{
    grok{
        match => ["message","%{IP:ip} %{WORD:method} %{URIPATH:uri} %{NUMBER:bytes} %{NUMBER:duration}"]
    }
}
output{
    stdout{
        codec => "rubydebug"
    }
}
[root@node1 conf.d]# /opt/logstash/bin/logstash -f filtergrok.conf
# 提示:输入上述元数据查看输出。

ヒント:GROKオンラインツールます。https://grokdebug.herokuapp.com/

③.kvプラグ

鍵データの処理KVプラグのキーと値の分析

プラグインクラスの出力

出力プラグイン:

  • ①.fileプラグ
  • ②.tcp/ UDPプラグ
  • ③.redis/ kfaka
  • ④.elasticsearch

付録:
Redisの構成:

input{
    redis{
        host => 'redis-server'
        port => '6379'
        data_type => 'list'
        key => 'lb'
        codec => 'json'
    }
}

おすすめ

転載: www.cnblogs.com/passzhang/p/12059284.html