Module: Mysql_Checks
- Defined in:
- lib/apprentice/checks/mysql.rb
Overview
Contains MariaDB/MySQL specific methods for checking on slave health
Instance Method Summary collapse
-
#check_seconds_behind ⇒ Object
Get the value of
'Seconds_Behind_Master', which indicates the amount of time in seconds the slave is behind the master’s instruction set received via the replication thread. -
#check_slave_io ⇒ Object
Get the value of
'Slave_IO_Running', which, obviously, should beYessince otherwise it would mean the slave is not replicated properly and/or has stopped because of an error. -
#get_mysql_status ⇒ Object
Gets the actual status from the MariaDB/MySQL slave using the Mysql2 gem.
-
#run_checks ⇒ Object
Returns the relevant status HTTP code accompanied by a useful user feedback text.
Instance Method Details
#check_seconds_behind ⇒ Object
Get the value of 'Seconds_Behind_Master', which indicates the amount of time in seconds the slave is behind the master’s instruction set received via the replication thread. This should always be as close to zero as possible (or even zero). If this value is beyond @threshold constantly you will need to think about changing your setup to accommodate the traffic coming in from the master.
Attributes
-
@status- Uses the'Seconds_Behind_Master'key inside the hash -
@threshold- The globally defined threshold after which the slave is considered to be too far behind to still be an active member. The default is 120 seconds.
Return values
true or false - depending on whether or not the slave’s replication thread is behind @threshold
Examples
@status = Hash.new
@status['Slave_IO_Running'] = 'Yes'
r = check_slave_io
r.inspect # => true
@status['Slave_IO_Running'] = 'No'
r = check_slave_io
r.inspect # => false
91 92 93 |
# File 'lib/apprentice/checks/mysql.rb', line 91 def check_seconds_behind return true if Integer(@status['Seconds_Behind_Master']) < @threshold end |
#check_slave_io ⇒ Object
Get the value of 'Slave_IO_Running', which, obviously, should be Yes since otherwise it would mean the slave is not replicated properly and/or has stopped because of an error.
Attributes
-
@status- Uses the'Slave_IO_Running'key inside the hash.
Return values
true or false - depending on whether or not the slave’s replication thread is running.
Examples
@status = Hash.new
@status['Slave_IO_Running'] = 'Yes'
r = check_slave_io
r.inspect # => true
@status['Slave_IO_Running'] = 'No'
r = check_slave_io
r.inspect # => false
59 60 61 62 |
# File 'lib/apprentice/checks/mysql.rb', line 59 def check_slave_io return true if @status['Slave_IO_Running'] == 'Yes' false end |
#get_mysql_status ⇒ Object
Gets the actual status from the MariaDB/MySQL slave using the Mysql2 gem. Notice that we’re using the EventMachine-enabled Mysql2::Client.
Right now it only returns the relevant error output and continues working afterwards.
Nothing is mentioned about explicitly closing a client connection in the Mysql2 docs, however, we need to be careful with the amount of connections we’re using since we might find ourselves in an environment where the number of connections is constraint for a very few.
Return values
-
@status - Contains a hash of all the relevant replication related variables to be examined by #run_checks
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/apprentice/checks/mysql.rb', line 16 def get_mysql_status begin client = Mysql2::Client.new( host: @server, port: @sql_port, username: @user, password: @password ) result = client.query 'SHOW SLAVE STATUS;' if result.count > 0 result.each do |key, state| @status[key] = state end end client.close rescue Exception => puts end end |
#run_checks ⇒ Object
Returns the relevant status HTTP code accompanied by a useful user feedback text
Attributes
-
@status - Should contain a hash with the relevant information to determine the the cluster member status. Also see #get_mysql_status.
Return values
-
response- A hash containing a HTTP:codeand a:textto return to the user
Example
@status = {'Seconds_Behind_Master' => 140 }
response = self.run_checks
response.inspect # => {:code => 503, :text => 'Some text'}
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/apprentice/checks/mysql.rb', line 111 def run_checks get_mysql_status unless @status.empty? response = {code: 200, text: []} if !check_slave_io response[:text] << 'Slave IO is not running.' end if !check_seconds_behind response[:text] << "Slave is #{@status['Seconds_Behind_Master']} seconds behind. Threshold is #{@threshold}" end response[:code] = 503 unless response[:text].empty? return response else return {code: 503, text: ['Unable to determine slave status']} end end |