Class: SystemdMon::StateChange

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/systemd_mon/state_change.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(original_state = nil) ⇒ StateChange

Returns a new instance of StateChange.



7
8
9
10
# File 'lib/systemd_mon/state_change.rb', line 7

def initialize(original_state = nil)
  self.states = []
  states << original_state if original_state
end

Instance Attribute Details

#statesObject

Returns the value of attribute states.



5
6
7
# File 'lib/systemd_mon/state_change.rb', line 5

def states
  @states
end

Instance Method Details

#<<(state) ⇒ Object



20
21
22
23
# File 'lib/systemd_mon/state_change.rb', line 20

def <<(state)
  self.states << state
  @diff = nil
end

#auto_restart?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/systemd_mon/state_change.rb', line 51

def auto_restart?
  first.ok? && last.ok? && changes.any? { |s| s.sub == "auto-restart" }
end

#changesObject



31
32
33
# File 'lib/systemd_mon/state_change.rb', line 31

def changes
  states[1..-1]
end

#diffObject



89
90
91
92
93
94
# File 'lib/systemd_mon/state_change.rb', line 89

def diff
  @diff ||= zipped.reject { |states|
    match = states.first.value
    states.all? { |s| s.value == match }
  }
end

#eachObject



25
26
27
28
29
# File 'lib/systemd_mon/state_change.rb', line 25

def each
  states.each do |state|
    yield state
  end
end

#fail?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/systemd_mon/state_change.rb', line 43

def fail?
  last.fail?
end

#important?Boolean

Returns:

  • (Boolean)


81
82
83
84
85
86
87
# File 'lib/systemd_mon/state_change.rb', line 81

def important?
  if length == 1
    first.fail?
  else
    diff.map(&:last).any?(&:important?)
  end
end

#lastObject



12
13
14
# File 'lib/systemd_mon/state_change.rb', line 12

def last
  states.last
end

#lengthObject



16
17
18
# File 'lib/systemd_mon/state_change.rb', line 16

def length
  states.length
end

#ok?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/systemd_mon/state_change.rb', line 39

def ok?
  last.ok?
end

#recovery?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/systemd_mon/state_change.rb', line 35

def recovery?
  first.fail? && last.ok?
end

#reload?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/systemd_mon/state_change.rb', line 55

def reload?
  first.ok? && last.ok? && changes.any? { |s| s.active == "reloading" }
end

#restart?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/systemd_mon/state_change.rb', line 47

def restart?
  first.ok? && last.ok? && changes.any? { |s| s.active == "deactivating" }
end

#status_textObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/systemd_mon/state_change.rb', line 63

def status_text
  if recovery?
    "recovered"
  elsif auto_restart?
    "automatically restarted"
  elsif restart?
    "restarted"
  elsif reload?
    "reloaded"
  elsif still_fail?
    "still failed"
  elsif fail?
    "failed"
  else
    "started"
  end
end

#still_fail?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/systemd_mon/state_change.rb', line 59

def still_fail?
  length > 1 && first.fail? && last.fail?
end

#to_sObject



104
105
106
107
108
109
110
111
112
# File 'lib/systemd_mon/state_change.rb', line 104

def to_s
  diff.inject("") { |s, (*states)|
    first = states.shift
    s << "#{first.name} state changed from #{first.value} to "
    s << states.map(&:value).join(" then ")
    s << "\n"
    s
  }
end

#zippedObject



96
97
98
99
100
101
102
# File 'lib/systemd_mon/state_change.rb', line 96

def zipped
  if length == 1
    first.all_states
  else
    first.all_states.zip(*changes.map(&:all_states))
  end
end