Class: Bosh::Monitor::Events::Alert

Inherits:
Base
  • Object
show all
Defined in:
lib/bosh/monitor/events/alert.rb

Constant Summary collapse

SEVERITY_MAP =

Considering Bosh::Agent::Alert

{
  1 => :alert,
  2 => :critical,
  3 => :error,
  4 => :warning,
  -1 => :ignored
}

Instance Attribute Summary collapse

Attributes inherited from Base

#attributes, #errors, #id, #kind, #logger

Instance Method Summary collapse

Methods inherited from Base

#add_error, create, create!, #error_message, #valid?

Constructor Details

#initialize(attributes = {}) ⇒ Alert

Returns a new instance of Alert.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/bosh/monitor/events/alert.rb', line 16

def initialize(attributes = {})
  super
  @kind = :alert

  @id         = @attributes["id"]
  @severity   = @attributes["severity"]
  @title      = @attributes["title"]
  @summary    = @attributes["summary"] || @title
  @source     = @attributes["source"]

  # This rescue is just to preserve existing test behavior. However, this
  # seems like a pretty wacky way to handle errors - wouldn't we rather
  # have a nice exception?
  @created_at = Time.at(@attributes["created_at"]) rescue @attributes["created_at"]
end

Instance Attribute Details

#created_atObject (readonly)

Returns the value of attribute created_at.



14
15
16
# File 'lib/bosh/monitor/events/alert.rb', line 14

def created_at
  @created_at
end

#sourceObject (readonly)

Returns the value of attribute source.



14
15
16
# File 'lib/bosh/monitor/events/alert.rb', line 14

def source
  @source
end

#titleObject (readonly)

Returns the value of attribute title.



14
15
16
# File 'lib/bosh/monitor/events/alert.rb', line 14

def title
  @title
end

Instance Method Details

#metricsObject



86
87
88
# File 'lib/bosh/monitor/events/alert.rb', line 86

def metrics
  [ ]
end

#severityObject



52
53
54
# File 'lib/bosh/monitor/events/alert.rb', line 52

def severity
  SEVERITY_MAP[@severity] || @severity
end

#short_descriptionObject



48
49
50
# File 'lib/bosh/monitor/events/alert.rb', line 48

def short_description
  "Severity #{@severity}: #{@source} #{@title}"
end

#to_hashObject



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/bosh/monitor/events/alert.rb', line 56

def to_hash
  {
    :kind       => "alert",
    :id         => @id,
    :severity   => @severity,
    :title      => @title,
    :summary    => @summary,
    :source     => @source,
    :created_at => @created_at.to_i
  }
end

#to_jsonObject



68
69
70
# File 'lib/bosh/monitor/events/alert.rb', line 68

def to_json
  Yajl::Encoder.encode(self.to_hash)
end

#to_plain_textObject



76
77
78
79
80
81
82
83
84
# File 'lib/bosh/monitor/events/alert.rb', line 76

def to_plain_text
  result = ""
  result << "#{@source}\n" unless @source.nil?
  result << (@title || "Unknown Alert") << "\n"
  result << "Severity: #{@severity}\n"
  result << "Summary: #{@summary}\n" unless @summary.nil?
  result << "Time: #{@created_at.utc}\n"
  result
end

#to_sObject



72
73
74
# File 'lib/bosh/monitor/events/alert.rb', line 72

def to_s
  "Alert @ #{@created_at.utc}, severity #{@severity}: #{@summary}"
end

#validateObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/bosh/monitor/events/alert.rb', line 32

def validate
  add_error("id is missing") if @id.nil?
  add_error("severity is missing") if @severity.nil?

  if @severity && (!@severity.kind_of?(Integer) || @severity < 0)
    add_error("severity is invalid (non-negative integer expected)")
  end

  add_error("title is missing") if @title.nil?
  add_error("timestamp is missing") if @created_at.nil?

  if @created_at && !@created_at.kind_of?(Time)
    add_error('created_at is invalid UNIX timestamp')
  end
end