插件介紹
Http插件是2.0版本才出現的新插件,1.x是沒有這個插件的。這個插件可以幫助logstash接收其他主機或者本機發送的http報文。
插件的原理很簡單,它自己啟動了一個ruby的服務器,用於接收Http請求。然后會把host(IP地址)和header相關的信息添加到event中。
下面就看看這個插件如何使用吧!
基本配置
先看看默認的配置吧!
http {}
簡單到心碎啊!其實有很多參數都是默認的...
上面的配置其實相當於:
http{
host => "0.0.0.0"
port => 8080
additional_codecs => {"application/json"=>"json"}
codec => "plain"
threads => 4
ssl => false
}
參數詳解
最主要的幾個參數,也是Http中常見的屬性:
host
默認是0.0.0.0,即所有的地址都可以發送到本機,從而接收Http信息。
port
是http插件中服務器運行的端口號。只要發送到“本機IP”:"該端口號"
的數據都可以被http插件接收到。
additional_codecs
配置文本類型和codec的映射,如上面所示,默認配置了json文本對應使用json的codec。
codec
如果上面的映射集合中找不到文本類型對應的codec,那么會默認按照這個屬性配置的codec解析。
ssl
是否開啟SSL。
threads
ruby插件中服務器的啟動線程,這里默認是4個。
user、password、keystore、keystore_password
這些都與http的認證有關系了,就不多說了。如果想要使用,再去參考文檔吧!
源碼初探
閱讀插件的源碼是為了更好的理解插件的使用,並且在出錯的時候知道哪里出現了問題。Logstash的插件往往都有固定的書寫格式,因此很容易看到插件的核心代碼。
在Input插件中,主要包含兩個方法:
public
def register
# register方法相當於初始化的構造方法
end # def register
# 主要的核心業務方法都在run中
def run(queue)
Stud.interval(@interval) do
# 創建事件
event = LogStash::Event.new("message" => @message, "host" => @host)
# 裝飾event對象
decorate(event)
# 放入隊列中
queue << event
end # loop
end # def run
下面看看http的插件內容吧!
首先看看register都做了什么吧!
def register
require "logstash/util/http_compressed_requests"
# 創建Puma服務器
@server = ::Puma::Server.new(nil) # we'll set the rack handler later
# 下面的都是跟認證相關的....
if @user && @password then
token = Base64.strict_encode64("#{@user}:#{@password.value}")
@auth_token = "Basic #{token}"
end
if @ssl
if @keystore.nil? || @keystore_password.nil?
raise(LogStash::ConfigurationError, "Settings :keystore and :keystore_password are required because :ssl is enabled.")
end
ctx = Puma::MiniSSL::Context.new
ctx.keystore = @keystore
ctx.keystore_pass = @keystore_password.value
ctx.verify_mode = case @verify_mode
when 'peer'
Puma::MiniSSL::VERIFY_PEER
when 'force_peer'
Puma::MiniSSL::VERIFY_PEER | Puma::MiniSSL::VERIFY_FAIL_IF_NO_PEER_CERT
when 'none'
Puma::MiniSSL::VERIFY_NONE
end
@server.add_ssl_listener(@host, @port, ctx)
else
@server.add_tcp_listener(@host, @port)
end
# 設置線程的數量
@server.min_threads = 0
@server.max_threads = @threads
@codecs = Hash.new
# 創建文本類型對應的codecs映射
@additional_codecs.each do |content_type, codec|
@codecs[content_type] = LogStash::Plugin.lookup("codec", codec).new
end
end # def register
可以簡單的把上面的代碼歸納為:
- 1 創建Puma服務器,Puma是一款ruby的高性能服務器。
- 2 進行用戶身份和密碼的驗證授權
- 3 設置基本的線程信息
- 4 創建codecs映射
再看看run方法
def run(queue)
p = Proc.new do |req|
begin
remote_host = req['puma.socket'].peeraddr[3]
REJECTED_HEADERS.each {|k| req.delete(k) }
req = lowercase_keys(req)
body = req.delete("rack.input")
# 這里使用相應的codec解析對應的Body信息
@codecs.fetch(req["content_type"], @codec).decode(body.read) do |event|
# 這里遍歷每個事件,然后添加host和headers信息
event["host"] = remote_host
event["headers"] = req
decorate(event)
queue << event
end
# 如果正常處理,則返回ok
['200', @response_headers, ['ok']]
rescue => e
@logger.error("unable to process event #{req.inspect}. exception => #{e.inspect}")
['500', @response_headers, ['internal error']]
end
end
auth = Proc.new do |username, password|
username == @user && password == @password.value
end if (@user && @password)
@server.app = Rack::Builder.new do
use(Rack::Auth::Basic, &auth) if auth
use CompressedRequests
run(p)
end
@server.run.join
end
private
def lowercase_keys(hash)
new_hash = {}
hash.each_pair do |k,v|
new_hash[k.downcase] = v
end
new_hash
end
看了上面的代碼,基本對http的原理有了一定了解吧!