Class: Maatkit::Heartbeat

Inherits:
Object
  • Object
show all
Defined in:
lib/maatkit-ruby/mk-heartbeat.rb

Overview

Monitor MySQL replication delay.

Maatkit::Heartbeat.new( array, str, array)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHeartbeat

Returns a new Heartbeat Object



243
244
# File 'lib/maatkit-ruby/mk-heartbeat.rb', line 243

def initialize()
end

Instance Attribute Details

#ask_passObject

Prompt for a password when connecting to MySQL.



17
18
19
# File 'lib/maatkit-ruby/mk-heartbeat.rb', line 17

def ask_pass
  @ask_pass
end

#charsetObject

short form: -A; type: string Default character set. If the value is utf8, sets Perl’s binmode on STDOUT to utf8, passes the mysql_enable_utf8 option to DBD::mysql, and runs SET NAMES UTF8 after connecting to MySQL. Any other value sets binmode on STDOUT without the utf8 layer, and runs SET NAMES after connecting to MySQL.



24
25
26
# File 'lib/maatkit-ruby/mk-heartbeat.rb', line 24

def charset
  @charset
end

#checkObject

Check slave delay once and exit.



28
29
30
# File 'lib/maatkit-ruby/mk-heartbeat.rb', line 28

def check
  @check
end

#configObject

type: Array Read this comma-separated list of config files; if specified, this must be the first option on the command line.



34
35
36
# File 'lib/maatkit-ruby/mk-heartbeat.rb', line 34

def config
  @config
end

#create_tableObject

Create the heartbeat “–table” if it does not exist. This option causes the table specified by “–database” and “–table” to be created with the following MAGIC_create_heartbeat table definition:

CREATE TABLE heartbeat (
# id int NOT NULL PRIMARY KEY,
# ts datetime NOT NULL
);

The heartbeat table requires at least one row. If you manually create the heartbeat table, then you must insert a row by doing:

INSERT INTO heartbeat (id) VALUES (1);

This is done automatically by –create-table.



48
49
50
# File 'lib/maatkit-ruby/mk-heartbeat.rb', line 48

def create_table
  @create_table
end

#daemonizeObject

Fork to the background and detach from the shell. POSIX operating systems only.



52
53
54
# File 'lib/maatkit-ruby/mk-heartbeat.rb', line 52

def daemonize
  @daemonize
end

#databaseObject

short form: -D; type: string The database to use for the connection.



57
58
59
# File 'lib/maatkit-ruby/mk-heartbeat.rb', line 57

def database
  @database
end

#dbi_driverObject

default: mysql; type: string Specify a driver for the connection; “mysql” and “Pg” are supported.



62
63
64
# File 'lib/maatkit-ruby/mk-heartbeat.rb', line 62

def dbi_driver
  @dbi_driver
end

#defaults_fileObject

short form: -F; type: string Only read mysql options from the given file. You must give an absolute pathname.



67
68
69
# File 'lib/maatkit-ruby/mk-heartbeat.rb', line 67

def defaults_file
  @defaults_file
end

#fileObject

type: string Print latest “–monitor” output to this file. When “–monitor” is given, prints output to the specified file instead of to STDOUT. The file is opened, truncated, and closed every interval, so it will only contain the most recent statistics. Useful when “–daemonize” is given.



75
76
77
# File 'lib/maatkit-ruby/mk-heartbeat.rb', line 75

def file
  @file
end

#framesObject

type: string; default: 1m,5m,15m Timeframes for averages. Specifies the timeframes over which to calculate moving averages when “–monitor” is given. Specify as a comma-separated list of numbers with suffixes. The suffix can be s for seconds, m for minutes, h for hours, or d for days. The size of the largest frame determines the maximum memory usage, as up to the specified number of per-second samples are kept in memory to calculate the averages. You can specify as many timeframes as you like.



85
86
87
# File 'lib/maatkit-ruby/mk-heartbeat.rb', line 85

def frames
  @frames
end

#helpObject

Show help and exit.



89
90
91
# File 'lib/maatkit-ruby/mk-heartbeat.rb', line 89

def help
  @help
end

#hostObject

short form: -h; type: string Connect to host.



94
95
96
# File 'lib/maatkit-ruby/mk-heartbeat.rb', line 94

def host
  @host
end

#intervalObject

type: time; default: 1s Interval between updates and checks. How often to check or update values. The updates and checks will happen when the Unix time (seconds since epoch) is an even multiple of this value. The suffix is similar to “–frames”.



101
102
103
# File 'lib/maatkit-ruby/mk-heartbeat.rb', line 101

def interval
  @interval
end

#logObject

type: string Print all output to this file when daemonized.



106
107
108
# File 'lib/maatkit-ruby/mk-heartbeat.rb', line 106

def log
  @log
end

#monitorObject

Monitor slave delay continuously. Specifies that mk-heartbeat should check the slave’s delay every second and report to STDOUT (or if “–file” is given, to the file instead). The output is the current delay followed by moving averages over the timeframe given in “–frames”. For example,

5s [  0.25s,  0.05s,  0.02s ]


114
115
116
# File 'lib/maatkit-ruby/mk-heartbeat.rb', line 114

def monitor
  @monitor
end

#passwordObject

short form: -p; type: string Password to use when connecting.



119
120
121
# File 'lib/maatkit-ruby/mk-heartbeat.rb', line 119

def password
  @password
end

#path_to_mk_heartbeatObject

Sets the executable path, otherwise the environment path will be used.



238
239
240
# File 'lib/maatkit-ruby/mk-heartbeat.rb', line 238

def path_to_mk_heartbeat
  @path_to_mk_heartbeat
end

#pidObject

type: string Create the given PID file when daemonized. The file contains the process ID of the daemonized instance. The PID file is removed when the daemonized instance exits. The program checks for the existence of the PID file when starting; if it exists and the process with the matching PID exists, the program exits.



127
128
129
# File 'lib/maatkit-ruby/mk-heartbeat.rb', line 127

def pid
  @pid
end

#portObject

short form: -P; type: int Port number to use for connection.



132
133
134
# File 'lib/maatkit-ruby/mk-heartbeat.rb', line 132

def port
  @port
end

#quietObject

short form: -q Suppresses normal output.



137
138
139
# File 'lib/maatkit-ruby/mk-heartbeat.rb', line 137

def quiet
  @quiet
end

#recurseObject

type: int Check slaves recursively to this depth in “–check” mode. Try to discover slave servers recursively, to the specified depth. After discovering servers, run the check on each one of them and print the hostname (if possible), followed by the slave delay. This currently works only with MySQL. See “–recursion-method”.



145
146
147
# File 'lib/maatkit-ruby/mk-heartbeat.rb', line 145

def recurse
  @recurse
end

#recursion_methodObject

type: string Preferred recursion method used to find slaves. Possible methods are:

METHOD #  #  USES
===========  ================
processlist  SHOW PROCESSLIST
hosts #  #   SHOW SLAVE HOSTS

The processlist method is preferred because SHOW SLAVE HOSTS is not reliable. However, the hosts method is required if the server uses a non-standard port (not 3306). Usually mk-heartbeat does the right thing and finds the slaves, but you may give a preferred method and it will be used first. If it doesn’t find any slaves, the other methods will be tried.



159
160
161
# File 'lib/maatkit-ruby/mk-heartbeat.rb', line 159

def recursion_method
  @recursion_method
end

#replaceObject

Use “REPLACE” instead of “UPDATE” for –update. When running in “–update” mode, use “REPLACE” instead of “UPDATE” to set the heartbeat table’s timestamp. The “REPLACE” statement is a MySQL extension to SQL. This option is useful when you don’t know whether the table contains any rows or not.



166
167
168
# File 'lib/maatkit-ruby/mk-heartbeat.rb', line 166

def replace
  @replace
end

#run_timeObject

type: time Time to run before exiting.



171
172
173
# File 'lib/maatkit-ruby/mk-heartbeat.rb', line 171

def run_time
  @run_time
end

#sentinelObject

type: string; default: /tmp/mk-heartbeat-sentinel Exit if this file exists.



176
177
178
# File 'lib/maatkit-ruby/mk-heartbeat.rb', line 176

def sentinel
  @sentinel
end

#set_varsObject

type: string; default: wait_timeout=10000 Set these MySQL variables. Immediately after connecting to MySQL, this string will be appended to SET and executed.



182
183
184
# File 'lib/maatkit-ruby/mk-heartbeat.rb', line 182

def set_vars
  @set_vars
end

#skewObject

type: int; default: 500000 How long to delay checks, in milliseconds. The default is to delay checks one half second. Since the update happens as soon as possible after the beginning of the second on the master, this allows one half second of replication delay before reporting that the slave lags the master by one second. If your clocks are not completely accurate or there is some other reason you’d like to delay the slave more or less, you can tweak this value. Try setting the “MKDEBUG” environment variable to see the effect this has.



192
193
194
# File 'lib/maatkit-ruby/mk-heartbeat.rb', line 192

def skew
  @skew
end

#socketObject

short form: -S; type: string Socket file to use for connection.



197
198
199
# File 'lib/maatkit-ruby/mk-heartbeat.rb', line 197

def socket
  @socket
end

#stopObject

Stop running instances by creating the sentinel file. This should have the effect of stopping all running instances which are watching the same sentinel file. If none of “–update”, “–monitor” or “–check” is specified, “mk-heartbeat” will exit after creating the file. If one of these is specified, “mk-heartbeat” will wait the interval given by “–interval”, then remove the file and continue working. You might find this handy to stop cron jobs gracefully if necessary, or to replace one running instance with another. For example, if you want to stop and restart “mk-heartbeat” every hour (just to make sure that it is restarted every hour, in case of a server crash or some other problem), you could use a “crontab” line like this:

0 * * * * mk-heartbeat --update -D test --stop \
# --sentinel /tmp/mk-heartbeat-hourly

The non-default “–sentinel” will make sure the hourly “cron” job stops only instances previously started with the same options (that is, from the same “cron” job). See also “–sentinel”.



214
215
216
# File 'lib/maatkit-ruby/mk-heartbeat.rb', line 214

def stop
  @stop
end

#tableObject

type: string; default: heartbeat The table to use for the heartbeat. Don’t specify database.table; use “–database” to specify the database.



220
221
222
# File 'lib/maatkit-ruby/mk-heartbeat.rb', line 220

def table
  @table
end

#updateObject

Update a master’s heartbeat.



224
225
226
# File 'lib/maatkit-ruby/mk-heartbeat.rb', line 224

def update
  @update
end

#userObject

short form: -u; type: string User for login if not current user.



229
230
231
# File 'lib/maatkit-ruby/mk-heartbeat.rb', line 229

def user
  @user
end

#versionObject

Show version and exit.



233
234
235
# File 'lib/maatkit-ruby/mk-heartbeat.rb', line 233

def version
  @version
end

Instance Method Details

#start(options = nil) ⇒ Object

Execute the command



249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/maatkit-ruby/mk-heartbeat.rb', line 249

def start(options = nil)
  tmp = Tempfile.new('tmp')
  command = option_string() + options.to_s + " 2> " + tmp.path
  success = system(command)
  if success
    begin
      while (line = tmp.readline)
        line.chomp
        selected_string = line
      end
    rescue EOFError
      tmp.close
    end
    return selected_string
  else
    tmp.close!
    return success
  end
end