Skip to content
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

Allow MySQL check configuration via a mysql config file #590

Merged
merged 1 commit into from
Sep 6, 2013
Merged
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
20 changes: 12 additions & 8 deletions checks.d/mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,36 +41,39 @@ def __init__(self, name, init_config, agentConfig):
self.greater_502 = {}

def check(self, instance):
host, port, user, password, mysql_sock, tags, options = self._get_config(instance)
host, port, user, password, mysql_sock, defaults_file, tags, options = self._get_config(instance)

if not host or not user:
if (not host or not user) and not defaults_file:
raise Exception("Mysql host and user are needed.")

db = self._connect(host, port, mysql_sock, user, password)
db = self._connect(host, port, mysql_sock, user, password, defaults_file)

# Metric collection
self._collect_metrics(host, db, tags, options)
self._collect_system_metrics(host, db, tags)

def _get_config(self, instance):
host = instance['server']
user = instance['user']
host = instance.get('server', '')
user = instance.get('user', '')
port = int(instance.get('port', 0))
password = instance.get('pass', '')
mysql_sock = instance.get('sock', '')
defaults_file = instance.get('defaults_file', '')
tags = instance.get('tags', None)
options = instance.get('options', {})

return host, port, user, password, mysql_sock, tags, options
return host, port, user, password, mysql_sock, defaults_file, tags, options

def _connect(self, host, port, mysql_sock, user, password):
def _connect(self, host, port, mysql_sock, user, password, defaults_file):
try:
import MySQLdb
except ImportError:
raise Exception("Cannot import MySQLdb module. Check the instructions "
"to install this module at https://app.datadoghq.com/account/settings#integrations/mysql")

if mysql_sock != '':
if defaults_file != '':
db = MySQLdb.connect(read_default_file=defaults_file)
elif mysql_sock != '':
db = MySQLdb.connect(unix_socket=mysql_sock,
user=user,
passwd=password)
Expand Down Expand Up @@ -294,6 +297,7 @@ def parse_agent_config(agent_config):
'sock': agent_config.get('mysql_sock',''),
'user': agent_config.get('mysql_user',''),
'pass': agent_config.get('mysql_pass',''),
'defaults_file': agent_config.get('defaults_file',''),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can get rid of this line. This method is for backward compatibility to use old style configuration that was using datadog.conf to configure checks which is now deprecated.
default_files was not supported at that time so there is no need to add it now.

'options': {'replication': True},
}]
}
1 change: 1 addition & 0 deletions conf.d/mysql.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ instances:
# user: my_username
# pass: my_password
# port: 3306 # Optional
# defaults_file: my.cnf # Alternate configuration mechanism
# tags: # Optional
# - optional_tag1
# - optional_tag2
Expand Down