Class: MonitorTypeThreshold

Inherits:
MonitorType show all
Defined in:
lib/monitor_type/threshold.rb

Overview

A base class for checking a number against a min and/or max value

Instance Method Summary collapse

Methods inherited from MonitorType

#alert, #extract_params, #run, #setup, #teardown

Constructor Details

#initialize(params) ⇒ MonitorTypeThreshold

See super method for generic description



6
7
8
9
10
# File 'lib/monitor_type/threshold.rb', line 6

def initialize(params)
  @min = params[:min] ||= 0
  @max = params[:max]
  super(params)
end

Instance Method Details

#check(value) ⇒ Object

Provides the means to check a value against thresholds



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/monitor_type/threshold.rb', line 30

def check(value)
  context_sentence = @context_sentence.nil? ? '' : "#{@context_sentence}\n"
  url = @url.nil? ? '' : "\n\n#{@url}\n"

  value = value.to_i

  alert("#{context_sentence}Minimum threshold exceeded. " \
        "Minimum: #{@min}, " \
        "Actual: #{value}#{url}") if !@min.nil? && value < @min

  alert("#{context_sentence}Maximum threshold exceeded. " \
        "Maximum: #{@max}, " \
        "Actual: #{value}#{url}") if !@max.nil? && value > @max
end

#derived_valueObject

Get the context dependent value which is to be checked



13
14
15
# File 'lib/monitor_type/threshold.rb', line 13

def derived_value
  fail 'Method needs to be overridden'
end

#processObject



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/monitor_type/threshold.rb', line 17

def process
  if @block.nil?
    value = derived_value
  else
    value = @block.call(@params)
    string = "value: #{value}\n"
    puts string
  end

  check(value)
end