mk-loadavg.1p - Man Page

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

Synopsis

Usage: mk-loadavg [OPTION...] [DSN]

mk-loadavg watches the load on a MySQL server and takes action if it is too high.

Execute my_script.sh when Threads_running exceeds 10:

  mk-loadavg --watch "Status:status:Threads_running:>:10" \
    --execute-command my_script.sh

Risks

The following section is included to inform users about the potential risks, whether known or unknown, of using this tool.  The two main categories of risks are those created by the nature of the tool (e.g. read-only tools vs. read-write tools) and those created by bugs.

mk-loadavg merely reads and prints information by default, and is very low-risk. The "--execute-command" option can execute user-specified commands.

At the time of this release, we know of no bugs that could cause serious harm to users.

The authoritative source for updated information is always the online issue tracking system.  Issues that affect this tool will be marked as such.  You can see a list of such issues at the following URL: <http://www.maatkit.org/bugs/mk-loadavg>.

See also "Bugs" for more information on filing bugs and getting help.

Description

mk-loadavg watches a MySQL server and takes action when a defined threshold is exceeded.  One or more items can be watched including MySQL status values from SHOW STATUS, SHOW INNODB STATUS and SHOW SLAVE STATUS, the three system load averages from uptime, and values from vmstat.  Watched items and their threshold values are specified by "--watch".  Every item is checked at intervals (see "--interval").  By default, if any one item's check returns true (i.e. its threshold is exceeded), then "--execute-command" is executed. Specifying "--and" requires that every item has exceeded its threshold before "--execute-command" is executed.

Output

If you specify "--verbose", mk-loadavg prints information to STDOUT about each check for each watched item.  Else, it prints nothing and "--execute-command" (if specified) is responsible for logging any information you want.

Exit Status

An exit status of 0 (sometimes also called a return value or return code) indicates success.  Any other value represents the exit status of the Perl process itself, or of the last forked process that exited if there were multiple servers to monitor.

Options

This tool accepts additional command-line arguments.  Refer to the "Synopsis" and usage information for details.

--and

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.

--ask-pass

Prompt for a password when connecting to MySQL.

--charset

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.

--config

type: Array

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

--daemonize

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

--database

short form: -D; type: string

Database to use.

--defaults-file

short form: -F; type: string

Only read mysql options from the given file.  You must give an absolute pathname.

--execute-command

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".

--help

Show help and exit.

--host

short form: -h; type: string

Connect to host.

--interval

type: time; default: 60s; group: Watch

How long to sleep between each check.

--log

type: string

Print all output to this file when daemonized.

Output from "--execute-command" is not printed to this file.

--password

short form: -p; type: string

Password to use when connecting.

--pid

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.

--port

short form: -P; type: int

Port number to use for connection.

--run-time

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.

--sentinel

type: string; default: /tmp/mk-loadavg-sentinel

Exit if this file exists.

--set-vars

type: string; default: wait_timeout=10000

Set these MySQL variables.  Immediately after connecting to MySQL, this string will be appended to SET and executed.

--socket

short form: -S; type: string

Socket file to use for connection.

--stop

Stop running instances by creating the "--sentinel" file.

--user

short form: -u; type: string

User for login if not current user.

--verbose

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.

--version

Show version and exit.

--vmstat

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.

--wait

short form: -w; 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.

--watch

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 different).

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 command 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 highest 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".

DSN Options

These DSN options are used to create a DSN.  Each option is given like option=value.  The options are case-sensitive, so P and p are not the same option.  There cannot be whitespace before or after the = and if the value contains whitespace it must be quoted.  DSN options are comma-separated.  See the maatkit manpage for full details.

Downloading

You can download Maatkit from Google Code at <http://code.google.com/p/maatkit/>, or you can get any of the tools easily with a command like the following:

   wget http://www.maatkit.org/get/toolname
   or
   wget http://www.maatkit.org/trunk/toolname

Where toolname can be replaced with the name (or fragment of a name) of any of the Maatkit tools.  Once downloaded, they're ready to run; no installation is needed.  The first URL gets the latest released version of the tool, and the second gets the latest trunk code from Subversion.

Environment

The environment variable MKDEBUG enables verbose debugging output in all of the Maatkit tools:

   MKDEBUG=1 mk-....

System Requirements

You need Perl, DBI, DBD::mysql, and some core packages that ought to be installed in any reasonably new version of Perl.

Bugs

For a list of known bugs see <http://www.maatkit.org/bugs/mk-loadavg>.

Please use Google Code Issues and Groups to report bugs or request support: <http://code.google.com/p/maatkit/>.  You can also join #maatkit on Freenode to discuss Maatkit.

Please include the complete command-line used to reproduce the problem you are seeing, the version of all MySQL servers involved, the complete output of the tool when run with "--version", and if possible, debugging output produced by running with the MKDEBUG=1 environment variable.

Copyright, License and Warranty

This program is copyright 2008-2011 Baron Schwartz. Feedback and improvements are welcome.

THIS PROGRAM IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2; OR the Perl Artistic License.  On UNIX and similar systems, you can issue `man perlgpl' or `man perlartistic' to read these licenses.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.

Author

Baron Schwartz, Daniel Nichter

About Maatkit

This tool is part of Maatkit, a toolkit for power users of MySQL.  Maatkit was created by Baron Schwartz; Baron and Daniel Nichter are the primary code contributors.  Both are employed by Percona.  Financial support for Maatkit development is primarily provided by Percona and its clients.

Version

This manual page documents Ver 0.9.7 Distrib 7540 $Revision: 7460 $.

Info

2024-07-18 perl v5.40.0 User Contributed Perl Documentation