Class: Reptile::Heartbeat

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/reptile/heartbeat.rb

Overview

MySQL DTD for setting up Heartbeats. Change HEARTBEAT_USER, HEARTBEAT_PASS, and MONITORING_BOX to ip of the monitoring server.

GRANT SELECT, INSERT, UPDATE, ALTER ON replication_monitor.* TO ‘HEARTBEAT_USER’@“localhost” IDENTIFIED BY ‘HEARTBEAT_PASS’; GRANT SELECT, INSERT, UPDATE, ALTER ON replication_monitor.* TO ‘HEARTBEAT_USER’@“MONITORING_BOX” IDENTIFIED BY ‘HEARTBEAT_PASS’;

CREATE DATABASE replication_monitor;

CREATE TABLE replication_monitor.heartbeats (

unix_time INTEGER NOT NULL,
db_time TIMESTAMP NOT NULL,
INDEX time_idx(unix_time)

)

Class Method Summary collapse

Class Method Details

.connect(configs) ⇒ Object



32
33
34
# File 'lib/reptile/heartbeat.rb', line 32

def self.connect(configs)
  Databases.connect(configs.merge(user).merge("database" => 'replication_monitor'))
end

.read(name, configs) ⇒ Object

Read the most recent heartbeat and return the delay in seconds, or nil if no heartbeat are found.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/reptile/heartbeat.rb', line 45

def self.read(name, configs)
  self.connect(configs)

  current_time = Time.now

  delay = nil
  heartbeat = Heartbeat.find(:first, :order => 'db_time DESC')

   # No heartbeats at all!
  if heartbeat.nil?
    Log.info "No heartbeats found on #{name} at #{Time.now}"
    return nil;
  end

  delay = (Time.now - Time.at(heartbeat.unix_time)).round

  Log.info "The slave delay for '#{name}' is #{strfdelay(delay)}"

  delay
end

.userObject

The default connection settings which override per-database settings.



28
29
30
# File 'lib/reptile/heartbeat.rb', line 28

def self.user
  @user ||= {}
end

.user=(default_configs) ⇒ Object

Set the default connection settings for writing/reading heartbeats. These will be merged with the per-database settings passed to connect.



23
24
25
# File 'lib/reptile/heartbeat.rb', line 23

def self.user=(default_configs)
  @user = default_configs
end

.write(name, configs) ⇒ Object

Write a heartbeat. Thump thump.



38
39
40
41
42
# File 'lib/reptile/heartbeat.rb', line 38

def self.write(name, configs)
  self.connect(configs)
  heartbeat = Heartbeat.create(:unix_time => Time.now.to_i, :db_time => "NOW()")
  Log.debug "Wrote heartbeat to #{name} at #{Time.at(heartbeat.unix_time)}"
end