Class: MonitorType

Inherits:
Object
  • Object
show all
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

MonitorType_Threshold

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



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
# File 'lib/MonitorType.rb', line 30

def initialize( params )
    if params[:name].nil? then
        string = "*** Monitor parameter missing, name"
        string = "#{string}*** :name => <name of monitor>"
        raise MonitorTypeMustHaveNameError.new(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? then
        #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? then
            if ENV["EMAIL_SENDER"].nil? then
                string = "*** Alert parameter missing, email_sender"
                string = "#[string}*** An email recipient has been specified for monitor, #{@name}, but no email sender has been specified"
                string = "#[string}*** :email_sender => <email of sender>"
                string = "#[string}*** or, a catch all environment variable"
                string = "#[string}*** EMAIL_SENDER=<email of sender>"

                raise MonitorTypeMustHaveSenderEmailAddressForEmailAlertError.new(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



108
109
110
111
112
113
114
115
116
117
# File 'lib/MonitorType.rb', line 108

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

#extractParamsObject

Overload this method if any parameters should be checked in context



70
71
# File 'lib/MonitorType.rb', line 70

def extractParams
end

#processObject

Check if the monitor has tripped



82
83
84
# File 'lib/MonitorType.rb', line 82

def process
    raise "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
102
103
# File 'lib/MonitorType.rb', line 90

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

#setupObject

Overload this method if any parameters should be checked in context



74
75
# File 'lib/MonitorType.rb', line 74

def setup
end

#teardownObject

Overload this method if any parameters should be checked in context



78
79
# File 'lib/MonitorType.rb', line 78

def teardown
end