post_agent.rb 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. module Agents
  2. class PostAgent < Agent
  3. cannot_be_scheduled!
  4. cannot_create_events!
  5. description <<-MD
  6. Post Agent receives events from other agents and send those events as the contents of a post request to a specified url. `post_url` field must specify where you would like to receive post requests and do not forget to include URI scheme (`http` or `https`)
  7. MD
  8. event_description "Does not produce events."
  9. def default_options
  10. {
  11. :post_url => "http://www.example.com",
  12. :expected_receive_period_in_days => 1
  13. }
  14. end
  15. def working?
  16. last_receive_at && last_receive_at > options[:expected_receive_period_in_days].to_i.days.ago && !recent_error_logs?
  17. end
  18. def validate_options
  19. unless options[:post_url].present? && options[:expected_receive_period_in_days].present?
  20. errors.add(:base, "post_url and expected_receive_period_in_days are required fields")
  21. end
  22. end
  23. def post_event(uri, event)
  24. req = Net::HTTP::Post.new(uri.request_uri)
  25. req.form_data = event
  26. Net::HTTP.start(uri.hostname, uri.port, :use_ssl => uri.scheme == "https") { |http| http.request(req) }
  27. end
  28. def receive(incoming_events)
  29. incoming_events.each do |event|
  30. uri = URI options[:post_url]
  31. post_event uri, event.payload
  32. end
  33. end
  34. end
  35. end