Class: CloudFlock::Remote::SSH::Watchdog

Inherits:
Object
  • Object
show all
Defined in:
lib/cloudflock/remote/ssh/watchdog.rb

Overview

The Watchdog Class allows for the creation of custom watchdogs to allow the status of an ongoing migration as well as the health of the hosts involved to be monitored.

Examples

# Create a Watchdog to monitor system load, the state will be tracked as
a float and updated every 15 seconds (roughly 3 refreshes by default.)
# The state of the Watchdog can be accessed via the Watchdog#state method.
system_load = Watchdog.new(ssh, 'uptime', 15) do |wait|
  wait.gsub(/^.*(\d+\.\d+).*$/, '\\1').to_f
end

# Alerts can be created, so that action can be taken automatically.
system_load.create_alarm('high_load') { |wait| wait > 10 }
system_load.on_alarm('high_load') { |wait| puts "Load is #{wait}!"; exit }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, ssh, command, interval = 30, &block) ⇒ Watchdog

Public: Create a new Watchdog to keep track of some aspect of a given host’s state.

name - String containing the watchdog’s name. ssh - SSH session which the Watchdog should monitor. command - String to run periodically on the target SSH session to

determine the host's state.

interval - Number of seconds to wait between command invocations.

(default: 30)

block - Optional block to be passed the results of the command to

transform the data and make it more easily consumable.
(default: identity function)


38
39
40
41
42
43
44
45
46
47
# File 'lib/cloudflock/remote/ssh/watchdog.rb', line 38

def initialize(name, ssh, command, interval = 30, &block)
  @name      = name
  @ssh       = ssh
  @command   = command
  @interval  = interval
  @transform = block
  @thread    = start_thread
  @alarms    = {}
  @actions   = {}
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



24
25
26
# File 'lib/cloudflock/remote/ssh/watchdog.rb', line 24

def name
  @name
end

#stateObject

Returns the value of attribute state.



23
24
25
# File 'lib/cloudflock/remote/ssh/watchdog.rb', line 23

def state
  @state
end

Instance Method Details

#alarm_active?(name) ⇒ Boolean

Public: Determine whether a given alarm is presently active.

name - Name of the alarm.

Returns false if the alarm is not defined, or the result of the alarm predicate otherwise.

Returns:

  • (Boolean)


88
89
90
# File 'lib/cloudflock/remote/ssh/watchdog.rb', line 88

def alarm_active?(name)
  triggered = alarms[name].nil? ? false : alarms[name]
end

#create_alarm(name, &block) ⇒ Object

Public: Create a new named alarm, providing a predicate to indicate that the alarm should be considered active.

name - Name for the alarm. block - Block to be evaluated in order to determine if the alarm is

active.  The block should accept one argument (the current state
of the Watchdog).

Returns nothing.



65
66
67
# File 'lib/cloudflock/remote/ssh/watchdog.rb', line 65

def create_alarm(name, &block)
  alarms[name] = block
end

#on_alarm(name, &block) ⇒ Object

Public: Define the action which should be taken when an alarm is triggered.

name - Name of the alarm. block - Block to be executed when an alarm is determined to be triggered.

The block should accept one argument (the current state of the
Watchdog).

Returns nothing.



78
79
80
# File 'lib/cloudflock/remote/ssh/watchdog.rb', line 78

def on_alarm(name, &block)
  actions[name] = block
end

#stopObject

Public: Stop the Watchdog from running.

Returns nothing.



52
53
54
# File 'lib/cloudflock/remote/ssh/watchdog.rb', line 52

def stop
  thread.kill
end

#triggered_alarmsObject

Public: Return the state of all active alarms.

Returns an Array of active alarms.



95
96
97
# File 'lib/cloudflock/remote/ssh/watchdog.rb', line 95

def triggered_alarms
  alarms.select { |k,v| v[state] }.map(&:first)
end