Skip to content

add config for defaults file option, allow for multiple instances #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,21 @@ You should then configure the MySQL plugin:
HeartbeatTable "percona.heartbeat" (if using pt-heartbeat to track slave lag)
Verbose false (default: false)
</Module>
</Python>
</Plugin>

You can use a defaults file instead of the user and password, which will also allow
use of a nonstandard socket location.

<Plugin python>
Import mysql
<Module mysql>
Host "localhost"
Port 3306
DefaultsFile "/root/.my.cnf-foo"
</Module>
</Plugin>

The plugin is not configurable for multiple instances per host.

## Metrics

Expand Down Expand Up @@ -323,4 +337,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)
MIT (http://www.opensource.org/licenses/mit-license.php)
21 changes: 15 additions & 6 deletions mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
'Password': '',
'HeartbeatTable': '',
'Verbose': False,
'DefaultsFile': ''
}

MYSQL_STATUS_VARS = {
Expand Down Expand Up @@ -285,12 +286,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)
Expand Down