Skip to content

Instantly share code, notes, and snippets.

@itmustbejj
Created March 6, 2018 18:34
Show Gist options
  • Save itmustbejj/0ae90817d918a55d1ad6bc769332150f to your computer and use it in GitHub Desktop.
Save itmustbejj/0ae90817d918a55d1ad6bc769332150f to your computer and use it in GitHub Desktop.
A logstash filter that posts raw documents to A2 using inline ruby.
filter {
ruby {
init => "
require 'net/http'
require 'uri'
require 'json'
require 'openssl'
"
code => "
automate_url = 'https://a2.mychef.co/data-collector/v0'
automate_token = 'MY_TOKEN_HERE'
#Max size of rabbitmq queues in bytes, if exceeded stop sending data to a2
rabbit_max_queue = 1000000 #1GB
uri = URI.parse(automate_url)
# Verify Automate ingest is not backed up
queue_size = `du -s /var/opt/delivery/rabbitmq/db/`.split.first.to_i
if (queue_size > rabbit_max_queue) then
puts %{queue_size: #{queue_size} exceeds rabbit_max_queue: #{rabbit_max_queue} disabling a2 forwarding}
else
# Create the HTTP objects
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.open_timeout = 5
http.read_timeout = 10
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
headers = {'X-Data-Collector-Token'=> automate_token, 'X-Data-Collector-Auth'=> 'version=1.0', 'Content-Type'=> 'application/json'}
request = Net::HTTP::Post.new(uri.request_uri, headers)
request.body = event.to_json
# Send the request
response = http.request(request)
puts %{#{response.body}\n#{response.code}} unless response.code.match(/2[0-9]{2}/)
end
"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment