第二章 logstash - 输出插件之redis与es

时间:2023-03-09 00:52:12
第二章 logstash - 输出插件之redis与es

最常用的两个输出插件:

  • redis
  • es

一、redis

1、用法

 output {
redis{
batch => false
batch_events => 50
batch_timeout => 5
codec => plain
congestion_interval => 1
congestion_threshold => 0
data_type => list
db => 0
host => ["127.0.0.1:6379"]
key => xxx
password => xxx
port => 6379
reconnect_interval => 1
shuffle_hosts => true
timeout => 5
workers => 1
}
}

2、配置项

以上所有配置项都是可选的,没有必须的。(以下4个红色配置是最重要的4个配置

  • 批处理类(仅用于data_type为list)
    • batch:设为true,通过发送一条rpush命令,存储一批的数据
      • 默认为false:1条rpush命令,存储1条数据
      • 设为true之后,1条rpush会发送batch_events条数据或发送batch_timeout秒(取决于哪一个先到达)
    • batch_events:一次rpush多少条
      • 默认50条
    • batch_timeout:一次rpush最多消耗多少s
      • 默认5s
  • 编码类
    • codec:对输出数据进行codec,避免使用logstash的separate filter
  • 拥塞保护(仅用于data_type为list)
    • congestion_interval:每多长时间进行一次拥塞检查
      • 默认1s
      • 设为0,表示对每rpush一个,都进行检测
    • congestion_threshold:list中最多可以存在多少个item数据
      • 默认是0:表示禁用拥塞检测
      • 当list中的数据量达到congestion_threshold,会阻塞直到有其他消费者消费list中的数据
      • 作用:防止OOM
  • data_type
    • list:使用rpush
    • channel:使用publish
  • db:使用redis的数据库,默认使用0号
  • host:数组
    • eg.["127.0.0.1:6380", "127.0.0.1"]
    • 可以指定port,会覆盖全局port
  • port:全局port,默认6379
  • key:list或channel的名字
    • 支持动态key,例如:logstash-%{type}
  • password:redis密码,默认不使用密码
  • reconnect_interval:失败重连的间隔,默认为1s
  • timeout:连接超时,默认5s

二、es

1、使用方式

 output {
elasticsearch {
hosts => ["127.0.0.1:9200"]
action => index
cacert => /xxx
codec => plain
doc_as_upsert => false
document_id => 1
document_type => xxx
flush_size => 500
idle_flush_time => 1
index => logstash-%{+YYYY.MM.dd}
keystore => /xxx
keystore_password => xxx
manage_template => true
max_retries => 3
parent => nil
password => xxx
path => /
proxy => xxx
retry_max_interval => 2
routing => xxx
script => xxx
script_lang => xxx
script_type => inline
script_var_name => event
scripted_upsert => false
sniffing => false
sniffing_delay => 5
ssl => false
ssl_certificate_verification => true
template => /xxx
template_name => logstash
template_overwrite => false
timeout => 5
truststore => /xxx
truststore_password => xxx
upsert => xxx
user => xxx
workers => 1
}
}

2、基本配置

以上配置全部都是可选的,没有必须的。以下列出最重要的几个。

  • hosts:["127.0.0.1:9200","127.0.0.2:9200"]
  • action:指定es的行为,indexdeletecreateupdate
    • 默认为index:index a document(该document就是一个来自于logstash的event)
    • delete:通过id删除一个document(需要指定document_id)
    • create:index a document(如果该document已经在index中存在,则失败)
    • update:通过id更新一个document
  • cacert:验证server合法性的.cer或.pem文件路径
  • codec
  • document_id
  • document_type
  • index:默认值:logstash-%{+YYYY.MM.dd}
    • 便于删除老数据
    • 在语法解析的时候,看到+号开头的,会自动认为后面是时间格式,尝试用时间格式来解析后续字符串。所以,之前处理过程中不要给自定义的字段起一个+号开头的名字
    • 索引名中不能有大写字母
    • 有时也会自定义为:logstash-%{servicename}-%{+YYYY.MM.dd}
  • user:进入es cluster的用户
  • password:进入es cluster的密码
  • timeout:Set the timeout for network operations and requests sent Elasticsearch. If a timeout occurs, the request will be retried.
  • flush_size:默认500,logstash攒够500条数据再一次性向es发送
  • idle_flush_time:默认1s,如果1s内没攒够500条还是会一次性将攒的数据发出去给es