From cf6a8429bb6213037df2e778cb571349f72963c1 Mon Sep 17 00:00:00 2001 From: Trey Palmer Date: Mon, 26 Oct 2015 19:22:27 -0400 Subject: [PATCH 1/2] add config for defaults file option, allow for multiple instances --- README.md | 25 +++++++++++++++++++++++-- mysql.py | 25 +++++++++++++++++++------ 2 files changed, 42 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index bcf609c..bee0919 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,28 @@ You should then configure the MySQL plugin: HeartbeatTable "percona.heartbeat" (if using pt-heartbeat to track slave lag) Verbose false (default: false) - + + +You can use a defaults file instead of the user and password, which will also allow +use of a nonstandard socket location. Then you can collect separate stats for +multiple MySQL instances: + + + Import mysql + + Host "localhost" + Port 3306 + DefaultsFile "/root/.my.cnf-foo" + Instance "foo" + + + Host "localhost" + Port 3306 + DefaultsFile "/root/.my.cnf-bar" + Instance "bar" + + + ## Metrics @@ -323,4 +344,4 @@ For versions of MySQL with support for it and where enabled, `INFORMATION_SCHEMA response_time_count.14 ## License -MIT (http://www.opensource.org/licenses/mit-license.php) \ No newline at end of file +MIT (http://www.opensource.org/licenses/mit-license.php) diff --git a/mysql.py b/mysql.py index c5182a2..57177c4 100644 --- a/mysql.py +++ b/mysql.py @@ -34,6 +34,8 @@ 'Password': '', 'HeartbeatTable': '', 'Verbose': False, + 'DefaultsFile': '', + 'Instance': '' } MYSQL_STATUS_VARS = { @@ -285,12 +287,20 @@ } def get_mysql_conn(): - return MySQLdb.connect( - host=MYSQL_CONFIG['Host'], - port=MYSQL_CONFIG['Port'], - user=MYSQL_CONFIG['User'], - passwd=MYSQL_CONFIG['Password'] - ) + # if defaults file is set, use it vice user and password + if MYSQL_CONFIG['DefaultsFile']: + return MySQLdb.connect( + host=MYSQL_CONFIG['Host'], + port=MYSQL_CONFIG['Port'], + read_default_file=MYSQL_CONFIG['DefaultsFile'] + ) + else: + return MySQLdb.connect( + host=MYSQL_CONFIG['Host'], + port=MYSQL_CONFIG['Port'], + user=MYSQL_CONFIG['User'], + passwd=MYSQL_CONFIG['Password'] + ) def mysql_query(conn, query): cur = conn.cursor(MySQLdb.cursors.DictCursor) @@ -456,6 +466,9 @@ def dispatch_value(prefix, key, value, type, type_instance=None): if not type_instance: type_instance = key + if MYSQL_CONFIG['Instance']: + prefix = prefix + '/' + MYSQL_CONFIG['Instance'] + log_verbose('Sending value: %s/%s=%s' % (prefix, type_instance, value)) if not value: return From 3220351e37bf07de3382f8877a78bcab3ca68cfe Mon Sep 17 00:00:00 2001 From: Trey Palmer Date: Tue, 27 Oct 2015 12:27:39 -0400 Subject: [PATCH 2/2] remove instance config and multi-instance example --- README.md | 11 ++--------- mysql.py | 6 +----- 2 files changed, 3 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index bee0919..09091fd 100644 --- a/README.md +++ b/README.md @@ -38,8 +38,7 @@ You should then configure the MySQL plugin: You can use a defaults file instead of the user and password, which will also allow -use of a nonstandard socket location. Then you can collect separate stats for -multiple MySQL instances: +use of a nonstandard socket location. Import mysql @@ -47,16 +46,10 @@ multiple MySQL instances: Host "localhost" Port 3306 DefaultsFile "/root/.my.cnf-foo" - Instance "foo" - - - Host "localhost" - Port 3306 - DefaultsFile "/root/.my.cnf-bar" - Instance "bar" +The plugin is not configurable for multiple instances per host. ## Metrics diff --git a/mysql.py b/mysql.py index 57177c4..f0349f4 100644 --- a/mysql.py +++ b/mysql.py @@ -34,8 +34,7 @@ 'Password': '', 'HeartbeatTable': '', 'Verbose': False, - 'DefaultsFile': '', - 'Instance': '' + 'DefaultsFile': '' } MYSQL_STATUS_VARS = { @@ -466,9 +465,6 @@ def dispatch_value(prefix, key, value, type, type_instance=None): if not type_instance: type_instance = key - if MYSQL_CONFIG['Instance']: - prefix = prefix + '/' + MYSQL_CONFIG['Instance'] - log_verbose('Sending value: %s/%s=%s' % (prefix, type_instance, value)) if not value: return