Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 44 additions & 17 deletions agent-local/mysql
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,27 @@ 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';
$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.
Expand All @@ -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("<<<mysql>>>\n");
if ($check_mk) {
echo("<<<mysql>>>\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.
Expand Down Expand Up @@ -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);
Expand All @@ -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));
Expand Down Expand Up @@ -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] ) {
Expand All @@ -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 = <<<EOF
$message
Usage: php ss_get_mysql_stats.php --host <host> --items <item,...> [OPTION]
Usage: php $fn [--host <host> --items <item,...> [OPTION]]

--host MySQL host
--items Comma-separated list of the items whose data you want
Expand All @@ -215,8 +229,18 @@ Usage: php ss_get_mysql_stats.php --host <host> --items <item,...> [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.
<?php
\$mysql_user = 'a_user';
\$mysql_pass = 'a_password';
\$mysql_host = 'localhost';
\$mysql_port = 3306;


EOF;
die($usage);
}
Expand Down Expand Up @@ -527,8 +551,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'];
Expand Down Expand Up @@ -1304,6 +1328,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);
Expand All @@ -1313,13 +1338,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));
Expand Down
27 changes: 21 additions & 6 deletions snmp/mysql
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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).
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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] ) {
Expand All @@ -211,7 +216,7 @@ function usage($message) {

$usage = <<<EOF
$message
Usage: php ss_get_mysql_stats.php --host <host> --items <item,...> [OPTION]
Usage: php $fn [--host <host> --items <item,...> [OPTION]]

--host MySQL host
--items Comma-separated list of the items whose data you want
Expand All @@ -223,8 +228,18 @@ Usage: php ss_get_mysql_stats.php --host <host> --items <item,...> [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.
<?php
\$mysql_user = 'a_user';
\$mysql_pass = 'a_password';
\$mysql_host = 'localhost';
\$mysql_port = 3306;


EOF;
die($usage);
}
Expand Down