Skip to content

Commit

Permalink
dded the replication master/slave status returns
Browse files Browse the repository at this point in the history
  • Loading branch information
dkehn committed Nov 29, 2012
1 parent 84fa669 commit 031c0c2
Showing 1 changed file with 143 additions and 1 deletion.
144 changes: 143 additions & 1 deletion salt/modules/mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import time
import logging
import re
import sys

# Import third party libs
try:
Expand Down Expand Up @@ -801,16 +802,157 @@ def processlist():
ret = []
hdr=("Id", "User", "Host", "db", "Command","Time", "State",
"Info", "Rows_sent", "Rows_examined", "Rows_read")

log.debug('processlist')

This comment has been minimized.

Copy link
@SEJeff

SEJeff Nov 29, 2012

Contributor

This doesn't actually seem to do anything and should probably be removed. It doesn't show a processlist or anything meaningful. It would be nice if it did show the processlist or something to that effect if it is useful however. Would changing it to this make it more meaningful?

log.debug(processlist())

or even:

log.debug('MySQL Process List\n' + processlist())

This comment has been minimized.

Copy link
@s0undt3ch

s0undt3ch Dec 28, 2012

Collaborator
log.debug('MySQL Process List:\n{0}'.format(processlist())
db = connect()
cur = db.cursor()
cur.execute("SHOW FULL PROCESSLIST")
for i in range(cur.rowcount):
row = cur.fetchone()
r = {}
for j in range(len(hdr)):
r[hdr[j]] = row[j]
try:
r[hdr[j]] = row[j]
except:

This comment has been minimized.

Copy link
@SEJeff

SEJeff Nov 29, 2012

Contributor

Bare excepts are frowned upon. /me beats you with PEP8. Obviously this is to be taken tongue in cheek. Please catch the errors the mysql module throws if you can.

pass

ret.append(r)

cur.close()
return ret
def __do_query_into_hash(conn, sqlStr):
'''
Perform the query that is passed to it (sqlStr).
Returns:
results in a dict.
'''
mod = sys._getframe().f_code.co_name
log.debug("%s<--(%s)" % (mod, sqlStr))

rtnResults = []

try:
cursor = conn.cursor()
except:
self.__log.error("%s: Can't get cursor for SQL->%s" % (mod, sqlStr))

This comment has been minimized.

Copy link
@s0undt3ch

s0undt3ch Dec 28, 2012

Collaborator

self !?!?!?

Where is that defined?

This comment has been minimized.

Copy link
@s0undt3ch

s0undt3ch Dec 28, 2012

Collaborator

Should this be a regular log call?

log.error("%s: Can't get cursor for SQL->%s" % (mod, sqlStr))

This comment has been minimized.

Copy link
@thatch45

thatch45 Dec 28, 2012

Contributor

Yes, this should be a regular log, and should use a .format string
self, /me is laughing about this one :)

cursor.close()
log.debug(('%s-->' % mod))
return rtnResults

try:
rs = cursor.execute(sqlStr)
except:
log.error("%s: try to execute : SQL->%s" % (mod, sqlStr))
cursor.close()
log.debug(('%s-->' % mod))
return rtnResults

rs = cursor.fetchall()

for rowData in rs:
colCnt = 0
row = {}
for colData in cursor.description:
colName = colData[0]
row[colName] = rowData[colCnt]
colCnt += 1

rtnResults.append(row)

cursor.close()
log.debug(('%s-->' % mod))
return rtnResults

def get_master_status():
'''
Retrieves the master status from the mimion.
Returns:
{'host.domain.com': {'Binlog_Do_DB': '',
'Binlog_Ignore_DB': '',
'File': 'mysql-bin.000021',
'Position': 107}}
CLI Example:
salt '*' mysql.get_master_status
'''
mod = sys._getframe().f_code.co_name
log.debug("%s<--" % (mod))

conn = connect()
rtnv = __do_query_into_hash(conn, "SHOW MASTER STATUS")
conn.close()

# check for if this minion is not a master
if (len(rtnv) == 0):
rtnv.append([])

log.debug("%s-->%d" % (mod, len(rtnv[0])))
return rtnv[0]

def get_slave_status():
'''
Retrieves the slave status from the minion.
Returns:
{'host.domain.com': {'Connect_Retry': 60,
'Exec_Master_Log_Pos': 107,
'Last_Errno': 0,
'Last_Error': '',
'Last_IO_Errno': 0,
'Last_IO_Error': '',
'Last_SQL_Errno': 0,
'Last_SQL_Error': '',
'Master_Host': 'comet.scion-eng.com',
'Master_Log_File': 'mysql-bin.000021',
'Master_Port': 3306,
'Master_SSL_Allowed': 'No',
'Master_SSL_CA_File': '',
'Master_SSL_CA_Path': '',
'Master_SSL_Cert': '',
'Master_SSL_Cipher': '',
'Master_SSL_Key': '',
'Master_SSL_Verify_Server_Cert': 'No',
'Master_Server_Id': 1,
'Master_User': 'replu',
'Read_Master_Log_Pos': 107,
'Relay_Log_File': 'klo-relay-bin.000071',
'Relay_Log_Pos': 253,
'Relay_Log_Space': 553,
'Relay_Master_Log_File': 'mysql-bin.000021',
'Replicate_Do_DB': '',
'Replicate_Do_Table': '',
'Replicate_Ignore_DB': '',
'Replicate_Ignore_Server_Ids': '',
'Replicate_Ignore_Table': '',
'Replicate_Wild_Do_Table': '',
'Replicate_Wild_Ignore_Table': '',
'Seconds_Behind_Master': 0,
'Skip_Counter': 0,
'Slave_IO_Running': 'Yes',
'Slave_IO_State': 'Waiting for master to send event',
'Slave_SQL_Running': 'Yes',
'Until_Condition': 'None',
'Until_Log_File': '',
'Until_Log_Pos': 0}}
CLI Example:
salt '*' mysql.get_slave_status
'''
mod = sys._getframe().f_code.co_name
log.debug("%s<--" % (mod))

conn = connect()
rtnv = __do_query_into_hash(conn, "SHOW SLAVE STATUS")
conn.close()

# check for if this minion is not a slave
if (len(rtnv) == 0):
rtnv.append([])

log.debug("%s-->%d" % (mod, len(rtnv[0])))
return rtnv[0]

3 comments on commit 031c0c2

@thatch45
Copy link
Contributor

Choose a reason for hiding this comment

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

HaHa @SEJeff, thanks for catching the bare exceptions, I will get them :)
better to accept and improve!

@s0undt3ch
Copy link
Collaborator

Choose a reason for hiding this comment

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

I've fixed the issues discussed here.

@thatch45
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks @s0undt3ch !

Please sign in to comment.