logstash http_poller 第一个 URL 请求的响应应该输入到第二个 URL 的请求参数
logstash http_poller first URL request's response should be input to second URL's request param
我有两个网址(出于安全考虑,我将使用虚拟网址进行解释)
a> https://xyz.company.com/ui/api/token
b> https://xyz.company.com/request/transaction?date=2016-01-21&token=<tokeninfo>
当你点击 'a' 中提到的 url 时,它会生成一个令牌,让它成为一个 16 个字符的字符串
然后该令牌应该用于在令牌参数
中对点 'b' 进行第二次请求
已更新
The second url response is important to me i.e is a JSON response, I need
to filter the json data and extract required data and output it to standard
output and elastic search.
有没有办法在 logstash 中使用插件 "http_poller" 或任何其他插件来做到这一点。
注意:这些请求url要依次执行,即先执行'a'url点,再执行'b'url点应该在收到新令牌后接下来执行。
求推荐。
是的,可以混合使用 http_poller
输入和 http
输出。
这是我想出的配置:
input {
# 1. trigger new token requests every hour
http_poller {
urls => {
token => "https://xyz.company.com/ui/api/token"
}
interval => 3600
add_field => {"token" => "%{message}"}
}
}
filter {
}
output {
# 2. call the API
http {
http_method => "get"
url => "https://xyz.company.com/request/transaction?date=2016-01-21&token=%{token}"
}
}
更新
如果希望能够获取到API调用的内容并存储到ES中,就需要一个混合方案。您需要设置一个 cron 来调用运行两个 HTTP 调用的脚本并将结果存储在一个文件中,然后您可以让 logstash 跟踪该文件并将结果转发给 ES。
Shell 用于 cron 的脚本:
#!/bin/sh
# 1. Get the token
TOKEN=$(curl -s -XGET https://xyz.company.com/ui/api/token)
# 2. Call the API with the token and append JSON to file
curl -s -XGET "https://xyz.company.com/request/transaction?date=2016-01-21&token=$TOKEN" >> api_calls.log
可以使用 crontab(或类似的)在 cron 上设置上面的脚本,plenty of examples 那里有关于如何实现这个的。
那么logstash的配置就可以很简单了。它只需要拖尾 api_calls.log
文件并将文件发送到 ES
input {
file {
path => "api_calls.log"
start_position => "beginning"
}
}
filter {
json {
source => "message"
}
}
output {
elasticsearch {
hosts => ["localhost:9200"]
index => "my_index"
document_type" => "my_type"
}
stdout {
codec => "rubydebug"
}
}
我有两个网址(出于安全考虑,我将使用虚拟网址进行解释)
a> https://xyz.company.com/ui/api/token
b> https://xyz.company.com/request/transaction?date=2016-01-21&token=<tokeninfo>
当你点击 'a' 中提到的 url 时,它会生成一个令牌,让它成为一个 16 个字符的字符串
然后该令牌应该用于在令牌参数
中对点 'b' 进行第二次请求已更新
The second url response is important to me i.e is a JSON response, I need
to filter the json data and extract required data and output it to standard
output and elastic search.
有没有办法在 logstash 中使用插件 "http_poller" 或任何其他插件来做到这一点。
注意:这些请求url要依次执行,即先执行'a'url点,再执行'b'url点应该在收到新令牌后接下来执行。
求推荐。
是的,可以混合使用 http_poller
输入和 http
输出。
这是我想出的配置:
input {
# 1. trigger new token requests every hour
http_poller {
urls => {
token => "https://xyz.company.com/ui/api/token"
}
interval => 3600
add_field => {"token" => "%{message}"}
}
}
filter {
}
output {
# 2. call the API
http {
http_method => "get"
url => "https://xyz.company.com/request/transaction?date=2016-01-21&token=%{token}"
}
}
更新
如果希望能够获取到API调用的内容并存储到ES中,就需要一个混合方案。您需要设置一个 cron 来调用运行两个 HTTP 调用的脚本并将结果存储在一个文件中,然后您可以让 logstash 跟踪该文件并将结果转发给 ES。
Shell 用于 cron 的脚本:
#!/bin/sh
# 1. Get the token
TOKEN=$(curl -s -XGET https://xyz.company.com/ui/api/token)
# 2. Call the API with the token and append JSON to file
curl -s -XGET "https://xyz.company.com/request/transaction?date=2016-01-21&token=$TOKEN" >> api_calls.log
可以使用 crontab(或类似的)在 cron 上设置上面的脚本,plenty of examples 那里有关于如何实现这个的。
那么logstash的配置就可以很简单了。它只需要拖尾 api_calls.log
文件并将文件发送到 ES
input {
file {
path => "api_calls.log"
start_position => "beginning"
}
}
filter {
json {
source => "message"
}
}
output {
elasticsearch {
hosts => ["localhost:9200"]
index => "my_index"
document_type" => "my_type"
}
stdout {
codec => "rubydebug"
}
}