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
67
68
# 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

Instance Method Details

#alert(string) ⇒ Object

Called when a monitor has been tripped

Parameters:

  • string (String)

    A description of the trip that occurred



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# 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_paramsObject

Overload this method if any parameters should be checked in context



71
72
# File 'lib/monitor_type.rb', line 71

def extract_params
end

#processObject

Check if the monitor has tripped



83
84
85
# File 'lib/monitor_type.rb', line 83

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.



90
91
92
93
94
95
96
97
98
99
100
101
# 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

#setupObject

Overload this method if any parameters should be checked in context



75
76
# File 'lib/monitor_type.rb', line 75

def setup
end

#teardownObject

Overload this method if any parameters should be checked in context



79
80
# File 'lib/monitor_type.rb', line 79

def teardown
end