如何在logstash配置文件中引用环境变量? [英] How to reference environment variables in logstash configuration file?

查看:1381
本文介绍了如何在logstash配置文件中引用环境变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在logstash配置中引用环境变量?

Is it possible to reference environment variables in logstash configuration?

就我而言,我想使我在环境中设置的elasticsearch地址可配置.

In my case, i want to make my elasticsearch address configurable that i have set in the environment.

推荐答案

使用Logstash 2.3,您可以使用$ {var}或$ var将环境变量引用设置为Logstash插件配置. https://www.elastic.co/guide/zh-CN/logstash/current/environment- variables.html

With logstash 2.3, you can set environment variable references into Logstash plugins configuration using ${var} or $var. https://www.elastic.co/guide/en/logstash/current/environment-variables.html

在logstash 2.3之前,您可以使用由社区维护的环境"过滤器插件.

Before logstash 2.3, you can use the "environment" filter plugin which is community maintained.

文档位于: https://www.elastic.co/guide/zh-CN/logstash/current/plugins-filters-environment.html#plugins-filters-environment-add_field_from_env

如何安装此插件:

$LOGSTASH_HOME/bin/plugin install logstash-filter-environment

位于以下位置的源代码: https://github.com/logstash-plugins/logstash-filter-environment

Source code at : https://github.com/logstash-plugins/logstash-filter-environment

主要部分是:

# encoding: utf-8
require "logstash/filters/base"
require "logstash/namespace"


# Set fields from environment variables
class LogStash::Filters::Environment < LogStash::Filters::Base
  config_name "environment"

  # Specify a hash of fields to the environment variable
  # A hash of matches of `field => environment` variable
  config :add_field_from_env, :validate => :hash, :default => {}

  public
  def register
    # Nothing
  end # def register

  public
  def filter(event)
    return unless filter?(event)
    @add_field_from_env.each do |field, env|
      event[field] = ENV[env]
    end
    filter_matched(event)
  end # def filter
end # class LogStash::Filters::Environment

这篇关于如何在logstash配置文件中引用环境变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆