Class: Deadpool::StateSnapshot

Inherits:
Object
  • Object
show all
Defined in:
lib/deadpool/state_snapshot.rb

Instance Method Summary collapse

Constructor Details

#initialize(state) ⇒ StateSnapshot

Returns a new instance of StateSnapshot.



8
9
10
11
12
13
14
15
# File 'lib/deadpool/state_snapshot.rb', line 8

def initialize(state)
  @name           = state.name
  @timestamp      = state.timestamp
  @status_code    = state.status_code
  @all_messages   = state.all_messages
  @error_messages = state.error_messages
  @children       = []
end

Instance Method Details

#add_child(child) ⇒ Object



17
18
19
# File 'lib/deadpool/state_snapshot.rb', line 17

def add_child(child)
  @children << child
end

#all_error_messagesObject



25
26
27
28
29
# File 'lib/deadpool/state_snapshot.rb', line 25

def all_error_messages
  @children.inject(@error_messages) do |arr, child|
    arr + child.all_error_messages
  end
end

#full_reportObject



42
43
44
45
46
47
# File 'lib/deadpool/state_snapshot.rb', line 42

def full_report
  output = "System Status: #{status_code_to_s(overall_status)}\n\n"
  output += self.to_s

  return output
end

#nagios_reportObject



31
32
33
34
35
36
37
38
39
40
# File 'lib/deadpool/state_snapshot.rb', line 31

def nagios_report
  message = ''
  if overall_status != OK
    message += all_error_messages.join(' | ')
  end

  message += " last checked #{(Time.now - @timestamp).round} seconds ago."

  "#{status_code_to_s(overall_status)} - #{message}\n"
end

#overall_statusObject



21
22
23
# File 'lib/deadpool/state_snapshot.rb', line 21

def overall_status
  @children.map { |child| child.overall_status }.push(@status_code).max
end

#status_code_to_s(code) ⇒ Object



69
70
71
72
73
74
75
76
77
# File 'lib/deadpool/state_snapshot.rb', line 69

def status_code_to_s(code)
  case code
  when OK       then 'OK'
  when WARNING  then 'WARNING'
  when CRITICAL then 'CRITICAL'
  else
    'UNKNOWN'
  end
end

#to_s(indent = 0) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/deadpool/state_snapshot.rb', line 49

def to_s(indent=0)
  indent_space = '  ' * indent

  output = "#{indent_space}#{@name}\n"
  output += "#{indent_space}#{status_code_to_s(@status_code)} - checked #{(Time.now - @timestamp).round} seconds ago.\n"
  unless @error_messages.empty?
    output += "#{indent_space}!!! #{@error_messages.join("\n#{indent_space}!!! ")}\n"
  end
  unless @all_messages.empty?
    output += "#{indent_space}#{@all_messages.join("\n#{indent_space}")}\n"
  end
  output += "\n"
  
  @children.each do |child|
    output += child.to_s(indent+1)
  end

  return output
end