class DbMon::Monitor

Public Class Methods

new(options = {}) click to toggle source
# File lib/db_mon/monitor.rb, line 8
def initialize(options = {})
  @interval  = options['interval']     
  @threshold = options['threshold']
  @database  = options['database']
  @username  = options['username']
  @password  = options['password']
  @table     = options['table']
  @notify    = options['notify']
  @time_column = options['time_column'] || 'created_at'


  Mail.defaults do 
    delivery_method options['delivery_method'].to_sym
  end
end

Public Instance Methods

monitor_table() click to toggle source
# File lib/db_mon/monitor.rb, line 25
def monitor_table
  while true
    sleep @interval.to_i
    result = query_table 
    
    unless result.count >= @threshold
      send_alert_email
    end
  end
end
query_table() click to toggle source
# File lib/db_mon/monitor.rb, line 36
def query_table
  raise "Must be implemented on child classes" 
end
send_alert_email() click to toggle source
# File lib/db_mon/monitor.rb, line 40
    def send_alert_email
      table     = @table
      database  = @database
      threshold = @threshold
      interval  = @interval
      notify    = @notify
      
      mail = Mail.deliver do 
        from 'database_monitor@modustools.com'
        to notify
        subject 'Database Monitor Alert'

        html_part do
          content_type 'text/html; charset=UTF-8'
          body <<-EOM
            <h2>Database Alert</h2>

            <p>
              The #{table} table on #{database} has had less than #{threshold} record(s) 
              added to it in the last #{interval} seconds. This may be an indication that 
              there is a problem with the service(s) responsible for creating these records.
            </p>
            <p>
             #{interval} seconds is:
            </p>
            #{interval/60.0} minutes<br />
            #{(interval/60.0)/60.0} hours<br />
            #{((interval/60.0)/60.0)/24.0} days<br />
          EOM
        end
      end

      p 

    end