Class: MonitorType
- Inherits:
-
Object
- Object
- MonitorType
- Defined in:
- lib/MonitorType.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
Instance Method Summary collapse
-
#alert(string) ⇒ Object
Called when a monitor has been tripped.
-
#initialize(params) ⇒ MonitorType
constructor
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.
-
#process ⇒ Object
Check if the monitor has tripped.
-
#run ⇒ Object
An extention of the main run loop.
-
#sanitise ⇒ Object
Overload this method if any parameters should be checked in context.
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
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/MonitorType.rb', line 21 def initialize( params ) if params[:name].nil? then puts "*** Monitor parameter missing, name" puts "*** :name => <name of monitor>" abort end @name = params[:name] @email = params[:email] if !@email.nil? then if params[:email_sender].nil? then if ENV["EMAIL_SENDER"].nil? then puts "*** Alert parameter missing, email_sender" puts "*** An email recipient has been specified for monitor, #{@name}, but no email sender has been specified" puts "*** :email_sender => <email of sender>" puts "*** or, a catch all environment variable" puts "*** EMAIL_SENDER=<email of sender>" abort else @admin_email = ENV["EMAIL_SENDER"] end else @admin_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
82 83 84 85 86 87 88 89 90 91 |
# File 'lib/MonitorType.rb', line 82 def alert( string ) body = "#{@name} tripped.\n#{string}" puts "*** " if !@email.nil? then Alert_Email.new( @email, body ).Send puts "Emailed, #{@email}" else puts body end end |
#process ⇒ Object
Check if the monitor has tripped
58 59 60 |
# File 'lib/MonitorType.rb', line 58 def process raise "Method needs to be overridden" end |
#run ⇒ Object
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.
66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/MonitorType.rb', line 66 def run if Time.now > @next then begin @next = @cron.next( Time.now ) log "Monitor, #{@name}, next run time, #{@next}" self.sanitise self.process rescue MonitorTypeExceptionHandled => e self.alert( e. ) end end end |
#sanitise ⇒ Object
Overload this method if any parameters should be checked in context
54 55 |
# File 'lib/MonitorType.rb', line 54 def sanitise end |