Class: DbMon::Monitor

Inherits:
Object
  • Object
show all
Defined in:
lib/db_mon/monitor.rb

Direct Known Subclasses

MysqlMonitor, PgMonitor

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Monitor

Returns a new instance of Monitor.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 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

Instance Method Details

#monitor_tableObject



25
26
27
28
29
30
31
32
33
34
# 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_tableObject



36
37
38
# File 'lib/db_mon/monitor.rb', line 36

def query_table
  raise "Must be implemented on child classes" 
end

#send_alert_emailObject



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
69
70
71
72
73
74
# 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 '[email protected]'
    to notify
    subject 'Database Monitor Alert'

    html_part do
      content_type 'text/html; charset=UTF-8'
      body "        <h2>Database Alert</h2>\n\n        <p>\n          The \#{table} table on \#{database} has had less than \#{threshold} record(s) \n          added to it in the last \#{interval} seconds. This may be an indication that \n          there is a problem with the service(s) responsible for creating these records.\n        </p>\n        <p>\n         \#{interval} seconds is:\n        </p>\n        \#{interval/60.0} minutes<br />\n        \#{(interval/60.0)/60.0} hours<br />\n        \#{((interval/60.0)/60.0)/24.0} days<br />\n      EOM\n    end\n  end\n\n  p \n\nend\n"