PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/mysql_info_wrappers.py

https://github.com/m00dawg/Dolphin-Hunter
Python | 172 lines | 141 code | 23 blank | 8 comment | 32 complexity | 05a4e7e0f602d5b9697379ce2cbc5355 MD5 | raw file
  1. import MySQLdb
  2. mysql_conn = None
  3. def connect_to_mysql():
  4. global mysql_conn
  5. if mysql_conn is None:
  6. # Connect to MySQL (this could likely be much improved to, say, read
  7. # the .my.cnf, etc.
  8. try:
  9. mysql_conn = MySQLdb.connect(
  10. read_default_group = 'client')
  11. except MySQLdb.MySQLError, exc:
  12. print "[%d] %s" % exc.args
  13. print "Cannot Connect to MySQL"
  14. print "Is MySQL running? If your .my.cnf correct?"
  15. print "Do you need to configure the script with a username and password?"
  16. sys.exit()
  17. class AttributeAdapter(dict):
  18. def __getattr__(self, key):
  19. return dict.__getitem__(self, key)
  20. def __setattr__(self, key, value):
  21. dict.__setitem__(self, key, value)
  22. # Gather Information From MySQL
  23. def gather_mysql_info():
  24. connect_to_mysql()
  25. def convert(value):
  26. try:
  27. # convert anything that looks like a number to a number
  28. return float(value)
  29. except ValueError:
  30. pass
  31. try:
  32. # convert anything that looks like a boolean to a bool
  33. return bool(['OFF','ON'].index(value))
  34. except ValueError:
  35. pass
  36. try:
  37. # another bool format
  38. return bool(['NO','YES'].index(value))
  39. except ValueError:
  40. pass
  41. # otherwise, remain as normal text
  42. return value
  43. # Grab the STATUS and VARIABLES from MySQL and put them both into dictionaries
  44. try:
  45. cursor = mysql_conn.cursor()
  46. cursor.execute("SHOW GLOBAL STATUS")
  47. status = dict([(key.lower(), convert(value))
  48. for key, value in cursor.fetchall()])
  49. cursor.execute("SHOW GLOBAL VARIABLES")
  50. vars = dict([(key.lower(), convert(value))
  51. for key, value in cursor.fetchall()])
  52. cursor.execute("SHOW ENGINE INNODB STATUS")
  53. fields = [f[0] for f in cursor.description]
  54. innodb_engine_status = dict(zip(fields, cursor.fetchone()))['Status']
  55. cursor.close()
  56. except MySQLdb.MySQLError, exc:
  57. print '[%d] %s' % exc.args
  58. print 'Unable to gather information from MySQL!\nPerhaps the MySQL ' \
  59. 'user we are connecting with doese not have proper permissions?'
  60. sys.exit()
  61. return MySQLInfo(status, vars, innodb_engine_status)
  62. class MySQLInfo(object):
  63. def __init__(self, status, vars, ibstatus):
  64. self.status = AttributeAdapter(status)
  65. self.vars = AttributeAdapter(vars)
  66. self.ibstatus = ibstatus
  67. @property
  68. def key_buffer_used(self):
  69. status = self.status
  70. key_buffer_size = self.vars.key_buffer_size
  71. try:
  72. return (status.key_blocks_used /
  73. status.key_blocks_unused *
  74. 100 * key_buffer_size)
  75. except ZeroDivisionError:
  76. return 0
  77. @property
  78. def key_buffer_used_pct(self):
  79. status = self.status
  80. try:
  81. return (status.key_blocks_used / status.key_blocks_unused) * 100
  82. except ZeroDivisionError:
  83. return 0
  84. @property
  85. def key_buffer_hitrate(self):
  86. status = self.status
  87. try:
  88. return ((1 - status.key_reads) / status.key_read_requests)*100
  89. except ZeroDivisionError:
  90. return 0
  91. @property
  92. def buffer_pool_used(self):
  93. status = self.status
  94. return (status.innodb_buffer_pool_pages_total -
  95. status.innodb_buffer_pool_pages_free) * \
  96. status.innodb_page_size
  97. @property
  98. def buffer_pool_used_pct(self):
  99. status = self.status
  100. return (1 - status.innodb_buffer_pool_pages_free /
  101. status.innodb_buffer_pool_pages_total) * 100
  102. @property
  103. def slow_query_pct(self):
  104. status = self.status
  105. total_queries = sum([status[key] for key in ('com_select',
  106. 'com_insert',
  107. 'com_update',
  108. 'com_delete')])
  109. try:
  110. return (status.slow_queries / total_queries) * 100
  111. except ZeroDivisionError:
  112. return 0
  113. @property
  114. def read_pct(self):
  115. status = self.status
  116. total_queries = sum([status[key] for key in ('com_select',
  117. 'com_insert',
  118. 'com_update',
  119. 'com_delete')])
  120. try:
  121. return (status.com_select / total_queries) * 100
  122. except ZeroDivisionError:
  123. return 0
  124. @property
  125. def tmp_tables_disk_pct(self):
  126. status = self.status
  127. try:
  128. return (status.created_tmp_disk_tables /
  129. status.created_tmp_tables) * 100
  130. except ZeroDivisionError:
  131. return 0
  132. @property
  133. def table_lock_wait_pct(self):
  134. status = self.status
  135. try:
  136. return (status.table_locks_waited /
  137. status.table_locks_waited +
  138. status.table_locks_immediate) * 100
  139. except ZeroDivisionError:
  140. return 0
  141. @property
  142. def query_cache_hitrate(self):
  143. status = self.status
  144. try:
  145. return (status.qcache_hits /
  146. (status.qcache_hits +
  147. status.com_select)) * 100
  148. except ZeroDivisionError:
  149. return 0