growl_agent.rb 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. require 'ruby-growl'
  2. module Agents
  3. class GrowlAgent < Agent
  4. cannot_be_scheduled!
  5. cannot_create_events!
  6. description <<-MD
  7. The GrowlAgent sends any events it receives to a Growl GNTP server immediately.
  8. It is assumed that events have a `message` or `text` key, which will hold the body of the growl notification, and a `subject` key, which will have the headline of the Growl notification. You can use Event Formatting Agent if your event does not provide these keys.
  9. Set `expected_receive_period_in_days` to the maximum amount of time that you'd expect to pass between Events being received by this Agent.
  10. MD
  11. def default_options
  12. {
  13. 'growlserver' => 'localhost',
  14. 'growlpassword' => '',
  15. 'growlappname' => 'HuginnGrowl',
  16. 'growlnotificationname' => 'Notification',
  17. 'expected_receive_period_in_days' => "2"
  18. }
  19. end
  20. def working?
  21. last_receive_at && last_receive_at > options['expected_receive_period_in_days'].to_i.days.ago && !recent_error_logs?
  22. end
  23. def validate_options
  24. unless options['growlserver'].present? && options['expected_receive_period_in_days'].present?
  25. errors.add(:base, "growlserver and expected_receive_period_in_days are required fields")
  26. end
  27. end
  28. def register_growl
  29. @growler = Growl.new options['growlserver'], options['growlappname'], "GNTP"
  30. @growler.password = options['growlpassword']
  31. @growler.add_notification options['growlnotificationname']
  32. end
  33. def notify_growl(subject, message)
  34. @growler.notify(options['growlnotificationname'],subject,message)
  35. end
  36. def receive(incoming_events)
  37. incoming_events.each do |event|
  38. register_growl
  39. message = (event.payload['message'] || event.payload['text']).to_s
  40. subject = event.payload['subject'].to_s
  41. if message != "" && subject != ""
  42. log "Sending Growl notification '#{subject}': '#{message}' to #{options['growlserver']} with event #{event.id}"
  43. notify_growl(subject,message)
  44. else
  45. log "Event #{event.id} not sent, message and subject expected"
  46. end
  47. end
  48. end
  49. end
  50. end