Class: What::Modules::Base

Inherits:
Object
  • Object
show all
Includes:
Celluloid
Defined in:
lib/what/modules/base.rb

Direct Known Subclasses

Disk, Existence, Http, Memory, Process, Unicorn, What

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params, output) ⇒ Base

Returns a new instance of Base.



7
8
9
10
11
12
13
14
15
16
# File 'lib/what/modules/base.rb', line 7

def initialize(params, output)
  defaults = (self.class)::DEFAULTS rescue {}
  @name = params['name']
  @config = defaults.merge(params['config'] || {})
  @max = params['max'] || 'alert'
  @interval = params['interval'] || Config['interval']
  @output = output
  @failures = 0
  initialize_module
end

Instance Attribute Details

#intervalObject (readonly)

Returns the value of attribute interval.



5
6
7
# File 'lib/what/modules/base.rb', line 5

def interval
  @interval
end

Instance Method Details

#checkObject

This method should be overridden. The implementation here is for backwards compatibility with older modules that implement check! instead of check.



54
55
56
# File 'lib/what/modules/base.rb', line 54

def check
  check!
end

#detailsObject

This method may be overridden, to provide extra details based on the results of the check.



65
66
67
# File 'lib/what/modules/base.rb', line 65

def details
  {}
end

#healthObject

This method must be overridden.



59
60
61
# File 'lib/what/modules/base.rb', line 59

def health
  raise "Module #{self.class.name} doesn't override 'health'"
end

#identifierObject

A unique identifier that we can use to match results up with this specific instance. This is necessary because Celluloid weirds identity.



47
48
49
# File 'lib/what/modules/base.rb', line 47

def identifier
  object_id
end

#initialize_moduleObject



18
19
# File 'lib/what/modules/base.rb', line 18

def initialize_module
end

#nameObject



41
42
43
# File 'lib/what/modules/base.rb', line 41

def name
  Helpers.underscore(self.class.name.split('::').last)
end

#shared_statusObject



82
83
84
85
86
87
# File 'lib/what/modules/base.rb', line 82

def shared_status
  status = {}
  status['name'] = @name if @name
  status['type'] = name # FIXME
  status
end

#start_monitoringObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/what/modules/base.rb', line 21

def start_monitoring
  @output[identifier] = nil

  loop do
    check
    @output[identifier] = status
    sleep interval
  end
rescue Exception => e
  # stop looping -- the Monitor will restart if necessary
  @failures += 1
  output = shared_status.merge(
    "health" => "alert",
    "error" => "#{e.class}: #{e.message}",
    "failures" => @failures
  )
  puts "Error in module:\n#{YAML.dump(output)}"
  @output[identifier] = output
end

#statusObject



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/what/modules/base.rb', line 69

def status
  status = shared_status
  status['health'] = if @max == 'ok' || health == 'ok'
                       'ok'
                     elsif @max == 'warning' || health == 'warning'
                       'warning'
                     else
                       'alert'
                     end
  status['details'] = details
  status
end