class MonitorType

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

Public Class Methods

new(params) click to toggle source

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

# 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?
  @slack_url = params[:slack_url]
  @slack_channel = params[:slack_channel]

  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

Public Instance Methods

alert(string) click to toggle source

Called when a monitor has been tripped

@param [String] string A description of the trip that occurred

# File lib/monitor_type.rb, line 106
def alert(string)
  sent = false
  body = "#{@name} tripped.\n#{string}"
  puts '*** '
  if !@email.nil?
    AlertEmail.new(@sender_email, @email, body).send
    puts "Emailed, #{@email}"
    sent = true
  end

  unless @slack_url.nil?
    AlertSlack.new.send(@slack_url, @slack_channel, body)
    puts "Slacked, #{@slack_channel}"
    sent = true
  end


  puts body unless sent
end
extract_params() click to toggle source

Overload this method if any parameters should be checked in context

# File lib/monitor_type.rb, line 71
def extract_params
end
process() click to toggle source

Check if the monitor has tripped

# File lib/monitor_type.rb, line 83
def process
  fail 'Method needs to be overridden'
end
run() click to toggle source

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.

# File lib/monitor_type.rb, line 90
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
setup() click to toggle source

Overload this method if any parameters should be checked in context

# File lib/monitor_type.rb, line 75
def setup
end
teardown() click to toggle source

Overload this method if any parameters should be checked in context

# File lib/monitor_type.rb, line 79
def teardown
end