Class: MonitorType

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

Overview

Base class for Monitors

Given that this is a DSL, it extracts named parameters from a hash in order to provide more precise reporting of errors, without spewing syntax errors at a user

Direct Known Subclasses

MonitorTypeThreshold

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ MonitorType

Check that all required parameters have been passed in Make sure that any errors encountered are reported in a way that fixing the error is made easier



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/monitor_type.rb', line 28

def initialize(params)
  if params[:name].nil?
    string = '*** Monitor parameter missing, name' \
             '*** :name => <name of monitor>'
    fail MonitorTypeMustHaveNameError, string
  end
  @params = params
  @block = params[:block] unless params[:block].nil?
  @name = params[:name]
  @email = params[:email]
  @url = params[:url] unless params[:url].nil?

  if !@email.nil?
    # Sender email address from ENV allows for a single sender across all alerts
    # Checking params before ENV allows a particular entry to be different
    if params[:email_sender].nil?
      if ENV['EMAIL_SENDER'].nil?
        string = '*** Alert parameter missing, email_sender' \
                  '*** An email recipient has been specified for monitor, ' \
                  "#{@name}, but no email sender has been specified" \
                  '*** :email_sender => <email of sender>' \
                  '*** or, a catch all environment variable' \
                  '*** EMAIL_SENDER=<email of sender>'

        fail MonitorTypeMustHaveSenderEmailAddressForEmailAlertError, string
      else
        @sender_email = ENV['EMAIL_SENDER']
      end
    else
      @sender_email = params[:admin_email]
    end
  end

  cron_string = params[:cron] || '0 1 * * *'
  @cron = CronParser.new(cron_string)
  @next = Time.now - 1

  log "Loaded Monitor, #{@name}."
end

Instance Method Details

#alert(string) ⇒ Object

Called when a monitor has been tripped

Parameters:

  • string (String)

    A description of the trip that occurred



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

def alert(string)
  body = "#{@name} tripped.\n#{string}"
  puts '*** '
  if !@email.nil?
    AlertEmail.new(@sender_email, @email, body).send
    puts "Emailed, #{@email}"
  else
    puts body
  end
end

#extract_paramsObject

Overload this method if any parameters should be checked in context



69
70
# File 'lib/monitor_type.rb', line 69

def extract_params
end

#processObject

Check if the monitor has tripped



81
82
83
# File 'lib/monitor_type.rb', line 81

def process
  fail 'Method needs to be overridden'
end

#runObject

An extention of the main run loop. Each monitor is responsible for knowing when it should run, so this function is called pretty much continuosuly.



88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/monitor_type.rb', line 88

def run
  return unless Time.now > @next

  @next = @cron.next(Time.now)
  log "Monitor, #{@name}, next run time, #{@next}"
  extract_params
  setup
  process
  teardown
rescue MonitorTypeExceptionHandled => e
  alert(e.message)
end

#setupObject

Overload this method if any parameters should be checked in context



73
74
# File 'lib/monitor_type.rb', line 73

def setup
end

#teardownObject

Overload this method if any parameters should be checked in context



77
78
# File 'lib/monitor_type.rb', line 77

def teardown
end