PageRenderTime 38ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/backup/notifier/datadog.rb

https://bitbucket.org/ToadJamb/forks_backup
Ruby | 116 lines | 54 code | 20 blank | 42 comment | 9 complexity | 54a5823ec703d6ae2e71aa581e83eba9 MD5 | raw file
  1. # encoding: utf-8
  2. require 'dogapi'
  3. module Backup
  4. module Notifier
  5. class DataDog < Base
  6. ##
  7. # The DataDog API key
  8. attr_accessor :api_key
  9. ##
  10. # The title of the event
  11. attr_accessor :title
  12. ##
  13. # The text information for the event
  14. attr_accessor :text
  15. ##
  16. # The timestamp for the event
  17. attr_accessor :date_happened
  18. ##
  19. # The priority of the event (low/normal)
  20. attr_accessor :priority
  21. ##
  22. # The host that generated the event
  23. attr_accessor :host
  24. ##
  25. # The tags for this host (should be an array)
  26. attr_accessor :tags
  27. ##
  28. # The alert_type of the event (error/warning/info/success)
  29. attr_accessor :alert_type
  30. ##
  31. # The aggregation_key for the event
  32. attr_accessor :aggregation_key
  33. ##
  34. # The source_type for the event (nagios, hudson, jenkins, user, my apps, feed, chef, puppet, git, bitbucket, fabric, capistrano)
  35. attr_accessor :source_type_name
  36. def initialize(model, &block)
  37. super
  38. instance_eval(&block) if block_given?
  39. @title ||= default_title
  40. @text ||= default_text
  41. end
  42. private
  43. ##
  44. # Notify the user of the backup operation results.
  45. #
  46. # `status` indicates one of the following:
  47. #
  48. # `:success`
  49. # : The backup completed successfully.
  50. # : Notification will be sent if `on_success` is `true`.
  51. #
  52. # `:warning`
  53. # : The backup completed successfully, but warnings were logged.
  54. # : Notification will be sent if `on_warning` or `on_success` is `true`.
  55. #
  56. # `:failure`
  57. # : The backup operation failed.
  58. # : Notification will be sent if `on_warning` or `on_success` is `true`.
  59. #
  60. def notify!(status)
  61. hash = {alert_type: default_alert_type(status)}
  62. hash.store(:msg_title, @title)
  63. hash.store(:date_happened, @date_happened) if @date_happened
  64. hash.store(:priority, @priority) if @priority
  65. hash.store(:host, @host) if @host
  66. hash.store(:tags, @tags) if @tags
  67. hash.store(:aggregation_key, @aggregation_key) if @aggregation_key
  68. hash.store(:source_type_name, @source_type_name) if @source_type_name
  69. hash.store(:alert_type, @alert_type) if @alert_type
  70. send_event(hash)
  71. end
  72. # Dogapi::Client will raise an error if unsuccessful.
  73. def send_event(hash)
  74. client = Dogapi::Client.new(@api_key)
  75. event = Dogapi::Event.new(@text, hash)
  76. client.emit_event(event)
  77. end
  78. # set alert type
  79. def default_alert_type(status)
  80. case status
  81. when :success then 'success'
  82. when :warning then 'warning'
  83. when :failure then 'error'
  84. end
  85. end
  86. # set default title
  87. def default_title
  88. "Backup #{ model.label }"
  89. end
  90. # set default text
  91. def default_text
  92. "Backup Notification for #{ model.label }"
  93. end
  94. end
  95. end
  96. end