diff --git a/agent-local/mysql b/agent-local/mysql index c56e4e7e1..6fdfed77e 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. @@ -132,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); @@ -152,6 +163,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)); @@ -181,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] ) { @@ -201,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 @@ -215,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. + 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..02d547acb 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). @@ -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] ) { @@ -211,7 +216,7 @@ function usage($message) { $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 +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. +