Class: MysqlHealth::Health

Inherits:
Object
  • Object
show all
Defined in:
lib/mysql_health/health.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Health

Returns a new instance of Health.



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
68
69
70
# File 'lib/mysql_health/health.rb', line 32

def initialize(options = {})
  @options = options

  @mutex = Mutex.new
  @scheduler = Rufus::Scheduler.start_new
  def @scheduler.handle_exception(job, e)
    MysqlHealth.log.error "job #{job.job_id} caught #{e.class} exception '#{e}' #{e.backtrace.join("\n")}"
  end

  if options[:master]
    master_status = {}
    master_status[:status] = 503
    master_status[:content] = "Health of master not yet determined\n"
    self.master_status=(master_status)
    @scheduler.every options[:interval], :allow_overlapping => options[:allow_overlapping], :first_in => options[:delay] do 
      check_master
    end
  else
    master_status = {}
    master_status[:status] = '501 Not Enabled'
    master_status[:content] = "Health of master not enabled\n"
    self.master_status=(master_status)
  end

  if options[:slave]
    slave_status = {}
    slave_status[:status] = '501 Not Enabled'
    slave_status[:content] = "Health of slave not yet determined\n"
    self.slave_status=(slave_status)
    @scheduler.every options[:interval], :allow_overlapping => options[:allow_overlapping], :first_in => options[:delay] do 
      check_slave
    end
  else
    slave_status = {}
    slave_status[:status] = '501 Not Enabled'
    slave_status[:content] = "Health of slave not enabled\n"
    self.slave_status=(slave_status)
  end
end

Class Method Details

.handle_exception(job, e) ⇒ Object



37
38
39
# File 'lib/mysql_health/health.rb', line 37

def @scheduler.handle_exception(job, e)
  MysqlHealth.log.error "job #{job.job_id} caught #{e.class} exception '#{e}' #{e.backtrace.join("\n")}"
end

Instance Method Details

#check_masterObject



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/mysql_health/health.rb', line 107

def check_master
  MysqlHealth.log.debug("check_master")
  response = {}
  response[:content_type] = 'text/plain'

  begin
    # connect to the MySQL server
    dbh = DBI.connect(@options[:dsn], @options[:username], @options[:password])

    status = {}
    dbh.select_all('SHOW STATUS') do |row|
      status[row[0].downcase.to_sym] = row[1]
    end
    mysqladmin_status = "Uptime: %s  Threads: %s  Questions: %s  Slow queries: %s  Opens: %s  Flush tables: %s  Open tables: %s  Queries per second avg: %.3f\n" %
              [ status[:uptime], status[:threads_running], status[:questions], status[:slow_queries], status[:opened_tables], status[:flush_commands], status[:open_tables], status[:queries].to_i/status[:uptime].to_i]
    if status.length > 0
      if read_only?(dbh)
        response[:status] = '503 Service Read Only'
        response[:content] = mysqladmin_status
      else
        response[:status] = '200 OK'
        response[:content] = mysqladmin_status
      end
    else
      response[:status] = '503 Service Unavailable'
      response[:content] = mysqladmin_status
    end
  rescue Exception => e
    response[:status] = '500 Server Error'
    response[:content] = e.message
  end
  self.master_status=(response)
end

#check_slaveObject



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/mysql_health/health.rb', line 141

def check_slave
  MysqlHealth.log.debug("check_slave")
  response = {}
  response[:content_type] = 'text/plain'

  begin
    # connect to the MySQL server
    dbh = DBI.connect(@options[:dsn], @options[:username], @options[:password])

    show_slave_status = []
    status = {}
    dbh.execute('SHOW SLAVE STATUS') do |sth|
      sth.fetch_hash() do |row|
        row.each_pair do |k,v|
          status[k.downcase.to_sym] = v
          show_slave_status << "#{k}: #{v}"
        end
      end
    end

    if status.length > 0
      seconds_behind_master = status[:seconds_behind_master]

      # We return a "203 Non-Authoritative Information" when replication is shot. We don't want to reduce site performance, but still want to track that something is awry.
      if seconds_behind_master.eql?('NULL')
        response[:status] = '203 Slave Stopped'
        response[:content] = status.to_json
        response[:content_type] = 'application/json'
      elsif seconds_behind_master.to_i > 60*30
        response[:status] = '203 Slave Behind'
        response[:content] = status.to_json
        response[:content_type] = 'application/json'
      elsif read_only?(dbh)
        response[:status] = '200 OK ' + seconds_behind_master  + ' Seconds Behind Master'
        response[:content] = status.to_json
        response[:content_type] = 'application/json'
      else
        response[:status] = '503 Service Unavailable'
        response[:content] = status.to_json
        response[:content_type] = 'application/json'
      end
    else
      response[:status] = '503 Slave Not Configured'
      response[:content] = show_slave_status.join("\n")
    end
  rescue Exception => e
    response[:status] = '500 Server Error'
    response[:content] = e.message
  end
  self.slave_status=(response)
end

#master_statusObject



79
80
81
82
83
84
85
# File 'lib/mysql_health/health.rb', line 79

def master_status 
  master_status = nil
  @mutex.synchronize do
    master_status = @master_status
  end
  return master_status
end

#master_status=(response) ⇒ Object



72
73
74
75
76
77
# File 'lib/mysql_health/health.rb', line 72

def master_status=(response)
  @mutex.synchronize do
    MysqlHealth.log.info("master status: #{response[:status]}")
    @master_status = response
  end
end

#read_only?(dbh) ⇒ Boolean

Returns:

  • (Boolean)


102
103
104
105
# File 'lib/mysql_health/health.rb', line 102

def read_only?(dbh)
  variables = dbh.select_all("SHOW VARIABLES WHERE Variable_name = 'read_only' AND Value = 'ON'")
  return (variables.length == 1)
end

#slave_statusObject



94
95
96
97
98
99
100
# File 'lib/mysql_health/health.rb', line 94

def slave_status
  slave_status = nil
  @mutex.synchronize do
    slave_status = @slave_status
  end
  return slave_status
end

#slave_status=(response) ⇒ Object



87
88
89
90
91
92
# File 'lib/mysql_health/health.rb', line 87

def slave_status=(response)
  @mutex.synchronize do 
    MysqlHealth.log.info("slave status: #{response[:status]}")
    @slave_status = response
  end
end