Class: Maatkit::LoadAvg

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

Overview

Watch MySQL load and take action when it gets too high.

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLoadAvg

Returns a new LoadAvg Object



256
257
# File 'lib/maatkit-ruby/mk-loadavg.rb', line 256

def initialize()
end

Instance Attribute Details

#andObject

group: Action Trigger the actions only when all “–watch” items exceed their thresholds. The default is to trigger the actions when any one of the watched items exceeds its threshold. This option requires that all watched items exceed their thresholds before any action is triggered.



20
21
22
# File 'lib/maatkit-ruby/mk-loadavg.rb', line 20

def and
  @and
end

#ask_passObject

Prompt for a password when connecting to MySQL.



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

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.



31
32
33
# File 'lib/maatkit-ruby/mk-loadavg.rb', line 31

def charset
  @charset
end

#configObject

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



37
38
39
# File 'lib/maatkit-ruby/mk-loadavg.rb', line 37

def config
  @config
end

#daemonizeObject

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



41
42
43
# File 'lib/maatkit-ruby/mk-loadavg.rb', line 41

def daemonize
  @daemonize
end

#databaseObject

short form: -D; type: string Database to use.



46
47
48
# File 'lib/maatkit-ruby/mk-loadavg.rb', line 46

def database
  @database
end

#defaults_fileObject

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



51
52
53
# File 'lib/maatkit-ruby/mk-loadavg.rb', line 51

def defaults_file
  @defaults_file
end

#execute_commandObject

type: string; group: Action Execute this command when watched items exceed their threshold values This command will be executed every time a “–watch” item (or all items if “–and” is specified) exceeds its threshold. For example, if you specify “–watch ”Server:vmstat:swpd:“:0”>, then this command will be executed when the server begins to swap and it will be executed again at each “–interval” so long as the server is still swapping. After the command is executed, mk-loadavg has no control over it, so it is responsible for its own info gathering, logging, interval, etc. Since the command is spawned from mk-loadavg, its STDOUT, STDERR and STDIN are closed so it doesn’t interfere with mk-loadavg. Therefore, the command must redirect its output to files or some other destination. For example, if you specify “–execute-command ‘echo Hello’”, you will not see “Hello” printed anywhere (neither to screen nor “–log”) because STDOUT is closed for the command. No information from mk-loadavg is passed to the command. See also “–and”.



68
69
70
# File 'lib/maatkit-ruby/mk-loadavg.rb', line 68

def execute_command
  @execute_command
end

#helpObject

Show help and exit.



72
73
74
# File 'lib/maatkit-ruby/mk-loadavg.rb', line 72

def help
  @help
end

#hostObject

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



77
78
79
# File 'lib/maatkit-ruby/mk-loadavg.rb', line 77

def host
  @host
end

#intervalObject

type: time; default: 60s; group: Watch How long to sleep between each check.



82
83
84
# File 'lib/maatkit-ruby/mk-loadavg.rb', line 82

def interval
  @interval
end

#logObject

type: string Print all output to this file when daemonized. Output from “–execute-command” is not printed to this file.



88
89
90
# File 'lib/maatkit-ruby/mk-loadavg.rb', line 88

def log
  @log
end

#passwordObject

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



93
94
95
# File 'lib/maatkit-ruby/mk-loadavg.rb', line 93

def password
  @password
end

#path_to_mk_loadavgObject

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



251
252
253
# File 'lib/maatkit-ruby/mk-loadavg.rb', line 251

def path_to_mk_loadavg
  @path_to_mk_loadavg
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.



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

def pid
  @pid
end

#portObject

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



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

def port
  @port
end

#run_timeObject

type: time Time to run before exiting. Causes “mk-loadavg” to stop after the specified time has elapsed. Optional suffix: s=seconds, m=minutes, h=hours, d=days; if no suffix, s is used.



113
114
115
# File 'lib/maatkit-ruby/mk-loadavg.rb', line 113

def run_time
  @run_time
end

#sentinelObject

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



118
119
120
# File 'lib/maatkit-ruby/mk-loadavg.rb', line 118

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.



124
125
126
# File 'lib/maatkit-ruby/mk-loadavg.rb', line 124

def set_vars
  @set_vars
end

#socketObject

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



129
130
131
# File 'lib/maatkit-ruby/mk-loadavg.rb', line 129

def socket
  @socket
end

#stopObject

Stop running instances by creating the “–sentinel” file.



133
134
135
# File 'lib/maatkit-ruby/mk-loadavg.rb', line 133

def stop
  @stop
end

#userObject

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



138
139
140
# File 'lib/maatkit-ruby/mk-loadavg.rb', line 138

def user
  @user
end

#verboseObject

short form: -v Print information to STDOUT about what is being done. This can be used as a heartbeat to see that mk-loadavg is still properly watching all its values. If “–log” is specified, this information will be printed to that file instead.



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

def verbose
  @verbose
end

#versionObject

Show version and exit.



149
150
151
# File 'lib/maatkit-ruby/mk-loadavg.rb', line 149

def version
  @version
end

#vmstatObject

type: string; default: vmstat 1 2; group: Watch vmstat command for “–watch” Server:vmstat:… The vmstat output should look like:

procs -----------memory---------- ---swap-- -----io---- -system-- ----cpu----
r  b # swpd # free # buff  cache # si # so #  bi #  bo # in # cs us sy id wa
0  0 #  # 0 590380 143756 571852 #  0 #  0 #   6 #   9  228  340  4  1 94  1
0  0 #  # 0 590400 143764 571852 #  0 #  0 #   0 #  28  751  818  4  2 90  3

The second line from the top needs to be column headers for subsequent lines. Values are taken from the last line. The default, “vmstat 1 2”, gets current values. Running just “vmstat” would get average values since last reboot.



163
164
165
# File 'lib/maatkit-ruby/mk-loadavg.rb', line 163

def vmstat
  @vmstat
end

#waitObject

type: time; default: 60s Wait this long to reconnect to MySQL. If the MySQL server goes away between “–interval” checks, mk-loadavg will attempt to reconnect to MySQL forever, sleeping this amount of time in between attempts.



170
171
172
# File 'lib/maatkit-ruby/mk-loadavg.rb', line 170

def wait
  @wait
end

#watchObject

type: string; group: Watch A comma-separated list of watched items and their thresholds (required). Each watched item is string of arguments separated by colons (like arg:arg). Each argument defines the watch item: what particular value is watched and how to compare the current value to a threshold value (N). Multiple watched items can be given by separating them with a comma, and the same watched item can be given multiple times (but, of course, it only makes sense to do this if the comparison and/or threshold values are differnt). The first argument is the most important and is case-sensitive. It defines the module responsible for watching the value. For example,

--watch Status:...

causes the WatchStatus module to be loaded. The second and subsequent arguments are passed to the WatchStatus module which parses them. Each watch module requires different arguments. The watch modules included in mk-loadavg and what arguments they require are listed below. This is a common error when specifying “–watch” on the commnad line:

# mk-loadavg --watch Server:vmstat:swpd:>:0
# Failed to load --watch WatchServer: Error parsing parameters vmstat:swpd:: No comparison parameter; expected >, < or = at ./mk-loadavg line 3100.

The “–watch” values need to be quoted:

# mk-loadavg --watch "Server:vmstat:swpd:>:0"

Status

#  Watch SHOW STATUS, SHOW INNODB STATUS, and SHOW SLAVE STATUS values.  The value argument is case-
#  sensitive.
#  # --watch Status:[status|innodb|slave]:value:[><=]:N
#  Examples:
#  # --watch "Status:status:Threads_connected:>:16"
#  # --watch "Status:innodb:Innodb_buffer_pool_hit_rate:<:0.98"
#  # --watch "Status:slave:Seconds_behind_master:>:300"
#  You can easily see what values are available for SHOW STATUS and SHOW SLAVE STATUS, but the values
#  for SHOW INNODB STATUS are not apparent.  Some common values are:
#  # Innodb_buffer_pool_hit_rate
#  # Innodb_buffer_pool_pages_created_sec
#  # Innodb_buffer_pool_pages_dirty
#  # Innodb_buffer_pool_pages_read_sec
#  # Innodb_buffer_pool_pages_written_sec
#  # Innodb_buffer_pool_pending_data_writes
#  # Innodb_buffer_pool_pending_dirty_writes
#  # Innodb_buffer_pool_pending_fsyncs
#  # Innodb_buffer_pool_pending_reads
#  # Innodb_buffer_pool_pending_single_writes
#  # Innodb_common_memory_allocated
#  # Innodb_data_fsyncs_sec
#  # Innodb_data_pending_fsyncs
#  # Innodb_data_pending_preads
#  # Innodb_data_pending_pwrites
#  # Innodb_data_reads_sec
#  # Innodb_data_writes_sec
#  # Innodb_insert_buffer_pending_reads
#  # Innodb_rows_read_sec
#  # Innodb_rows_updated_sec
#  # lock_wait_time
#  # mysql_tables_locked
#  # mysql_tables_used
#  # row_locks
#  # io_avg_wait
#  # io_wait
#  # max_io_wait
#  Several of those values can appear multiple times in the SHOW INNODB STATUS output.  The value used
#  for comparison is always the higest value.  So the value for io_wait is the highest io_wait value
#  for all the IO threads.

Processlist

#  Watch aggregated SHOW PROCESSLIST values.
#  #  --watch Processlist:[db|user|host|state|command]:value:[count|time]:[><=]:N
#  Examples:
#  # --watch "Processlist:state:Locked:count:>:5"
#  # --watch "Processlist:command:Query:time:<:1"

Server

#  Watch server values.
#  #  --watch Server:loadavg:[1|5|15]:[><=]:N
#  #  --watch Server:vmstat:[r|b|swpd|free|buff|cache|si|so|bi|bo|in|cs|us|sy|id|wa]:[><=]:N
#  Examples:
#  # --watch "Server:loadavg:5:>:4.00"
#  # --watch "Server:vmstat:swpd:>:0"
#  # --watch "Server:vmstat:free:=:0"
#  See "--vmstat".


246
247
248
# File 'lib/maatkit-ruby/mk-loadavg.rb', line 246

def watch
  @watch
end

Instance Method Details

#start(options = nil) ⇒ Object

Execute the command



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/maatkit-ruby/mk-loadavg.rb', line 262

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