From 946be97712a0c805fb86ec6cdbd478d87b413ffe Mon Sep 17 00:00:00 2001 From: John Salter Date: Wed, 9 Jul 2025 14:33:46 +0100 Subject: [PATCH 1/2] Align agent-local mysql script with the snmp version --- agent-local/mysql | 41 ++++++++++++++++++++++++++--------------- snmp/mysql | 8 ++++---- 2 files changed, 30 insertions(+), 19 deletions(-) diff --git a/agent-local/mysql b/agent-local/mysql index c56e4e7e1..c6b1bc7f3 100755 --- a/agent-local/mysql +++ b/agent-local/mysql @@ -28,13 +28,13 @@ if (!array_key_exists('SCRIPT_FILENAME', $_SERVER) # CONFIGURATION # ============================================================================ # Define MySQL connection constants. Instead of defining parameters here, -# you can also define them in another file named the same as this -# file with a .cnf extension. +# you can define them in another file named the same as this +# file, with a .cnf extension. # ============================================================================ $mysql_user = ''; $mysql_pass = ''; -$mysql_host = 'localhost'; +$mysql_host = 'localhost'; # NB the snmp version of this script uses '127.0.0.1' $mysql_port = 3306; $mysql_ssl = FALSE; # Whether to use SSL to connect to MySQL. $mysql_ssl_key = '/etc/pki/tls/certs/mysql/client-key.pem'; @@ -42,12 +42,13 @@ $mysql_ssl_cert = '/etc/pki/tls/certs/mysql/client-cert.pem'; $mysql_ssl_ca = '/etc/pki/tls/certs/mysql/ca-cert.pem'; $mysql_connection_timeout = 5; +$check_mk = TRUE; # FALSE for SNMP extend, TRUE for check_mk agent + $heartbeat = FALSE; # Whether to use pt-heartbeat table for repl. delay calculation. $heartbeat_utc = FALSE; # Whether pt-heartbeat is run with --utc option. $heartbeat_server_id = 0; # Server id to associate with a heartbeat. Leave 0 if no preference. $heartbeat_table = 'percona.heartbeat'; # db.tbl. - $cache_dir = '/var/cache/librenms'; # If set, this uses caching to avoid multiple calls. $timezone = null; # If not set, uses the system default. Example: "UTC" $cache_time = 30; # How long to cache data. @@ -67,16 +68,21 @@ $debug_log = FALSE; # If $debug_log is a filename, it'll be used. # ============================================================================ # You should not need to change anything below this line. # ============================================================================ -$version = "1.1.7"; +$version = "1.1.8"; # ============================================================================ # Include settings from an external config file (issue 39). # ============================================================================ -echo("<<>>\n"); +if ($check_mk) { + echo("<<>>\n"); +} if (file_exists(__FILE__ . '.cnf' ) ) { require(__FILE__ . '.cnf'); debug('Found configuration file ' . __FILE__ . '.cnf'); +} else { + echo("No ".__FILE__ . ".cnf found!\n"); + exit(); } # Make this a happy little script even when there are errors. @@ -152,6 +158,8 @@ if (!isset($called_by_script_server)) { $output[] = $item; } list($short, $val) = explode(":", $item); + # ensure returned values do not exceed max limits in RRD + $val = $val % ( 124999999999 + 1 ); echo(strtolower($short).":".strtolower($val)."\n"); } debug(array("Final result", $output)); @@ -527,8 +535,8 @@ function ss_get_mysql_stats( $options ) { # those to the array too. if ($chk_options['innodb'] && array_key_exists('InnoDB', $engines) - && $engines['InnoDB'] == 'YES' - || $engines['InnoDB'] == 'DEFAULT' + && ( $engines['InnoDB'] == 'YES' + || $engines['InnoDB'] == 'DEFAULT' ) ) { $result = run_query("SHOW /*!50000 ENGINE*/ INNODB STATUS", $conn); $istatus_text = $result[0]['Status']; @@ -1304,6 +1312,7 @@ function to_int ( $str ) { function run_query($sql, $conn) { global $debug; debug($sql); + mysqli_report(MYSQLI_REPORT_OFF); $result = @mysqli_query($conn, $sql); if ( $debug && strpos($sql, 'SHOW SLAVE STATUS ') === false ) { $error = @mysqli_error($conn); @@ -1313,13 +1322,15 @@ function run_query($sql, $conn) { } } $array = array(); - $count = @mysqli_num_rows($result); - if ( $count > 10000 ) { - debug('Abnormal number of rows returned: ' . $count); - } - else { - while ( $row = @mysqli_fetch_array($result) ) { - $array[] = $row; + if ( $result ) { + $count = @mysqli_num_rows($result); + if ( $count > 10000 ) { + debug('Abnormal number of rows returned: ' . $count); + } + else { + while ( $row = @mysqli_fetch_array($result) ) { + $array[] = $row; + } } } debug(array($sql, $array)); diff --git a/snmp/mysql b/snmp/mysql index d7ac4f09a..640a6e95c 100755 --- a/snmp/mysql +++ b/snmp/mysql @@ -27,14 +27,14 @@ if (!array_key_exists('SCRIPT_FILENAME', $_SERVER) # ============================================================================ # CONFIGURATION # ============================================================================ -# Define MySQL connection constants in config.php. Instead of defining -# parameters here, you can define them in another file named the same as this +# Define MySQL connection constants. Instead of defining parameters here, +# you can define them in another file named the same as this # file, with a .cnf extension. # ============================================================================ $mysql_user = ''; $mysql_pass = ''; -$mysql_host = '127.0.0.1'; +$mysql_host = '127.0.0.1'; # NB the agent-local version of this script uses 'localhost' $mysql_port = 3306; $mysql_ssl = FALSE; # Whether to use SSL to connect to MySQL. $mysql_ssl_key = '/etc/pki/tls/certs/mysql/client-key.pem'; @@ -68,7 +68,7 @@ $debug_log = FALSE; # If $debug_log is a filename, it'll be used. # ============================================================================ # You should not need to change anything below this line. # ============================================================================ -$version = "1.1.7"; +$version = "1.1.8"; # ============================================================================ # Include settings from an external config file (issue 39). From 914404ea07b9376bc620224722988268b1d88fa8 Mon Sep 17 00:00:00 2001 From: John Salter Date: Wed, 9 Jul 2025 16:28:30 +0100 Subject: [PATCH 2/2] Add debug option and correct usage statement --- agent-local/mysql | 20 ++++++++++++++++++-- snmp/mysql | 19 +++++++++++++++++-- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/agent-local/mysql b/agent-local/mysql index c6b1bc7f3..6fdfed77e 100755 --- a/agent-local/mysql +++ b/agent-local/mysql @@ -138,6 +138,11 @@ if (!isset($called_by_script_server)) { array_shift($_SERVER["argv"]); # Strip off this script's filename $options = parse_cmdline($_SERVER["argv"]); validate_options($options); + if( isset($options["debug-log"]) ){ + $debug = TRUE; + $debug_log = $options["debug-log"]; + debug("Debug flag set on commandline. This will omit some early debug statements."); + } $result = ss_get_mysql_stats($options); debug($result); @@ -189,7 +194,7 @@ if (!function_exists('array_change_key_case') ) { # ============================================================================ function validate_options($options) { debug($options); - $opts = array('items', 'user', 'pass', 'heartbeat', 'nocache', 'port', 'server-id'); + $opts = array('items', 'user', 'pass', 'heartbeat', 'nocache', 'port', 'server-id', 'debug-log'); # Required command-line options foreach ( array() as $option ) { if (!isset($options[$option]) || !$options[$option] ) { @@ -209,9 +214,10 @@ function validate_options($options) { function usage($message) { global $mysql_host, $mysql_user, $mysql_pass, $mysql_port; + $fn = basename($_SERVER['SCRIPT_FILENAME']); $usage = << --items [OPTION] +Usage: php $fn [--host --items [OPTION]] --host MySQL host --items Comma-separated list of the items whose data you want @@ -223,8 +229,18 @@ Usage: php ss_get_mysql_stats.php --host --items [OPTION] --connection-timeout MySQL connection timeout --server-id Server id to associate with a heartbeat if heartbeat usage is enabled --nocache Do not cache results in a file + --debug-log File to log debug messages to --help Show usage +MySQL params can also be defined in a file '$fn.cfg' in the same directory as this script +containing e.g. + --items [OPTION] +Usage: php $fn [--host --items [OPTION]] --host MySQL host --items Comma-separated list of the items whose data you want @@ -223,8 +228,18 @@ Usage: php ss_get_mysql_stats.php --host --items [OPTION] --connection-timeout MySQL connection timeout --server-id Server id to associate with a heartbeat if heartbeat usage is enabled --nocache Do not cache results in a file + --debug-log File to log debug messages to --help Show usage +MySQL params can also be defined in a file '$fn.cfg' in the same directory as this script +containing e.g. +