Check mysql-sorts

Overview

Checks sort operations in MySQL/MariaDB, including the rate of sort merge passes that required temporary disk files. A high rate of sort merge passes relative to total sorts indicates that sort_buffer_size and/or read_rnd_buffer_size may need to be increased.

Important Notes:

Data Collection:

  • Queries SHOW GLOBAL VARIABLES for read_rnd_buffer_size and sort_buffer_size

  • Queries SHOW GLOBAL STATUS for Sort_merge_passes, Sort_range, and Sort_scan

  • Calculates the total number of sorts and the percentage that required temporary tables

  • Logic is taken from MySQLTuner script:mysql_stats(), v1.9.8

Fact Sheet

Fact

Value

Check Plugin Download

https://github.com/Linuxfabrik/monitoring-plugins/tree/main/check-plugins/mysql-sorts

Nagios/Icinga Check Name

check_mysql_sorts

Check Interval Recommendation

Every 5 minutes

Can be called without parameters

Yes

Runs on

Cross-platform

Compiled for Windows

No

3rd Party Python modules

pymysql

Help

usage: mysql-sorts [-h] [-V] [--always-ok] [--defaults-file DEFAULTS_FILE]
                   [--defaults-group DEFAULTS_GROUP] [--timeout TIMEOUT]

Checks sort operations in MySQL/MariaDB, including the rate of sort merge
passes that required temporary disk files. A high rate indicates that
sort_buffer_size may need to be increased. Alerts when the disk-based sort
rate is too high.

options:
  -h, --help            show this help message and exit
  -V, --version         show program's version number and exit
  --always-ok           Always returns OK.
  --defaults-file DEFAULTS_FILE
                        MySQL/MariaDB cnf file to read user, host and password
                        from. Example: `--defaults-
                        file=/var/spool/icinga2/.my.cnf`. Default:
                        /var/spool/icinga2/.my.cnf
  --defaults-group DEFAULTS_GROUP
                        Group/section to read from in the cnf file. Default:
                        client
  --timeout TIMEOUT     Network timeout in seconds. Default: 3 (seconds)

Usage Examples

./mysql-sorts --defaults-file=/var/spool/icinga2/.my.cnf

Output:

901.6K sorts used a full table scan. Sorts requiring temporary tables: 0% (0.0 temp sorts / 3.1M sorts).

States

  • WARN if more than 10% of sorts required temporary tables (sort merge passes).

  • --always-ok suppresses all alerts and always returns OK.

Perfdata / Metrics

Name

Type

Description

mysql_pct_temp_sort_table

Percentage

sort_merge_passes / total_sorts * 100

mysql_read_rnd_buffer_size

Bytes

Size in bytes of the buffer used when reading rows from a MyISAM table in sorted order after a key sort.

mysql_sort_buffer_size

Bytes

Each session performing a sort allocates a buffer with this amount of memory. Not specific to any storage engine.

mysql_sort_merge_passes

Continuous Counter

Number of merge passes performed by the sort algorithm.

mysql_sort_range

Continuous Counter

Number of sorts which used a range.

mysql_sort_scan

Continuous Counter

Number of sorts which used a full table scan.

mysql_total_sorts

Continuous Counter

sort_scan + sort_range

Credits, License