mysql_query
index
/home/python/perforce/opensource/maim/maim/mysql_query.py

The purpose of MySQLQuery is to perform queries relating to MySQL internals.
It is not intended to run generic sql queries. 
The two main classes are:
   
CheckForeignKeys -- which checks for foreign key constraints given a table,
   field, and key id.
 
Status -- which returns status information, Master/Slave sync, and
   Efficiency Reports.

 
Modules
       
MySQLdb
re
sys
types

 
Classes
       
MySQLQueryBase
ForeignKeyChecker
Status

 
class ForeignKeyChecker(MySQLQueryBase)
    The class CheckForeignKeys checks to see if you can delete a key in a
table based on foreign key constraints. It checks to see if it the key
exists in any other tables with foreign key constraints.
 
  Methods defined here:
GetForeignkeyCount(self, options=None, return_type=None)
Sets the connection options to MySQL.                       
 
Args:
  options -- standard options.
  return_type -- 'dic' for a dictionary. Anything else
    will return a list. 
  
                                                                         
Returns:                                                               
  Doesn't return anything.
__init__(self, options=None)
Just like the MySQLQueryBase init, except we have autoconnect option.
connect(self, options=None)
Connects to the MySQL server and stores table information for all tables
in a database.                                   
 
Args: Options 
                                                                           
Returns: None

Methods inherited from MySQLQueryBase:
AddHosts(self, hosts=None, clear=None)
Adds hosts the MySQL hosts to analyze.
 
Args:
  # HOST is the name or ip address of a server.
  hosts:
         A list in the format [ HOST, HOST, ...]
         or as a dictionary of dictionaries.
         hosts = {}
         hosts[HOST] = {}
         hosts[HOST]['port'] = 3306
         where each host is a key to its own dictionary.
         A host's keys can be one of the following:
           ['port', 'username', 'password']
  clear: If > 0, removes the other hosts (but not their reports).
         You shouldn't have to remove hosts unless you really want to.
         Their reports will still be there, if any. 
  
Returns:
  The total list of hosts.
DumpRawReports(self)
Returns the internal dictionary self._reports.
  This is intended for debug purposes only. 
Args:
  None
 
Returns:
  A dictionary of hostnames.
  Values for each host is a dictionary with the keys
    ['status', 'errors', 'reports'].
    status is 1 for good, 0 for nothing, -1 = bad.
    errors = A single string describing errors with this
       host. Empty string is good.
    reports = A dictionary whose keywords are the names
       of the reports.
       Each report has the keys
         ['status', 'executed', 'values', 'errors'].
         values = A dictionary of the keyword/values
            returned by the query executed on MySQL.
GetHostErrors(self, host=None)
Returns any errors connecting to the host has had.
   
Args:
  # HOST is the name or ip address of a server.
  host: HOST
 
Returns: None, an empty string or string of errors.
         An empty string means no errors.
GetHosts(self)
Returns the current list of hosts.
GetOptions(self)
Returns a dictionary of current options.
GetQueries(self)
Returns the known defined queries.
GetReportErrors(self, host=None, report=None)
Returns any errors the report has.
 
Args:
  # HOST is the name or ip address of a server.
  host: HOST
  report: Report name.
 
Returns: None, an empty string, or string of errors.
    An empty string means no errors.
GetReportStatus(self, host=None, report=None)
Returns the status of the report.
   Returns -1, 0, 1, or None.
   1 is considered a success.
Args:
  # HOST is the name or ip address of a server.
  host: HOST
  report: Report name.
 
Returns: -1, 0, 1, or None
GetReports(self, hosts=None)
Returns a dictionary of hosts and their reports.
 
Args:
  # HOST is the name or ip address of a server.
  hosts: [ HOST, HOST, ...]
    
Returns:
  Dictionary of hosts whose values are a
  dictionary of report names and their values.
SetOptions(self, options=None)
Sets the connection options to MySQL.
 OPTIONS is a dictionary of the following keys:                     
      username = STRING                                                
      password = STRING                                                
      database = STRING                                                
      Port = STRING                                                    
      ids = LIST of primary ids of a table.                            
      fieldname = STRING of a field in a table.                        
      table = STRING of a table in a database.                         
      host = STRING of the host you are connecting to for single       
             hosts only.                                               
      autoconnect = 0 or 1. 1 means connect as soon as the object      
         is created. Assumes other values are supplied.                
      server_list = FILE which contains a list of hostnames.           
 
Args:
   Key is one of the following: host, database, username, password,
                                table, field, options_file
   options_file is a file which contains 'key value' entries.
     
  options: { Key:Value, Key:Value, .....}
   
Returns:
  Doesn't return anything.

 
class MySQLQueryBase
    A base class for classes that query a mysql database.
It encapsulates the common options for these query subclasses.
 
  Methods defined here:
AddHosts(self, hosts=None, clear=None)
Adds hosts the MySQL hosts to analyze.
 
Args:
  # HOST is the name or ip address of a server.
  hosts:
         A list in the format [ HOST, HOST, ...]
         or as a dictionary of dictionaries.
         hosts = {}
         hosts[HOST] = {}
         hosts[HOST]['port'] = 3306
         where each host is a key to its own dictionary.
         A host's keys can be one of the following:
           ['port', 'username', 'password']
  clear: If > 0, removes the other hosts (but not their reports).
         You shouldn't have to remove hosts unless you really want to.
         Their reports will still be there, if any. 
  
Returns:
  The total list of hosts.
DumpRawReports(self)
Returns the internal dictionary self._reports.
  This is intended for debug purposes only. 
Args:
  None
 
Returns:
  A dictionary of hostnames.
  Values for each host is a dictionary with the keys
    ['status', 'errors', 'reports'].
    status is 1 for good, 0 for nothing, -1 = bad.
    errors = A single string describing errors with this
       host. Empty string is good.
    reports = A dictionary whose keywords are the names
       of the reports.
       Each report has the keys
         ['status', 'executed', 'values', 'errors'].
         values = A dictionary of the keyword/values
            returned by the query executed on MySQL.
GetHostErrors(self, host=None)
Returns any errors connecting to the host has had.
   
Args:
  # HOST is the name or ip address of a server.
  host: HOST
 
Returns: None, an empty string or string of errors.
         An empty string means no errors.
GetHosts(self)
Returns the current list of hosts.
GetOptions(self)
Returns a dictionary of current options.
GetQueries(self)
Returns the known defined queries.
GetReportErrors(self, host=None, report=None)
Returns any errors the report has.
 
Args:
  # HOST is the name or ip address of a server.
  host: HOST
  report: Report name.
 
Returns: None, an empty string, or string of errors.
    An empty string means no errors.
GetReportStatus(self, host=None, report=None)
Returns the status of the report.
   Returns -1, 0, 1, or None.
   1 is considered a success.
Args:
  # HOST is the name or ip address of a server.
  host: HOST
  report: Report name.
 
Returns: -1, 0, 1, or None
GetReports(self, hosts=None)
Returns a dictionary of hosts and their reports.
 
Args:
  # HOST is the name or ip address of a server.
  hosts: [ HOST, HOST, ...]
    
Returns:
  Dictionary of hosts whose values are a
  dictionary of report names and their values.
SetOptions(self, options=None)
Sets the connection options to MySQL.
 OPTIONS is a dictionary of the following keys:                     
      username = STRING                                                
      password = STRING                                                
      database = STRING                                                
      Port = STRING                                                    
      ids = LIST of primary ids of a table.                            
      fieldname = STRING of a field in a table.                        
      table = STRING of a table in a database.                         
      host = STRING of the host you are connecting to for single       
             hosts only.                                               
      autoconnect = 0 or 1. 1 means connect as soon as the object      
         is created. Assumes other values are supplied.                
      server_list = FILE which contains a list of hostnames.           
 
Args:
   Key is one of the following: host, database, username, password,
                                table, field, options_file
   options_file is a file which contains 'key value' entries.
     
  options: { Key:Value, Key:Value, .....}
   
Returns:
  Doesn't return anything.
__init__(self, options=None)
Base internal variables we use for all classes.

 
class Status(MySQLQueryBase)
    The purpose of this class is to give you information about your MySQL
servers in regards to the health of MySQL. It doesn't analyze any data. It
doesn't analyze any system stats. It only looks at MySQL specific
information given by MySQL.
 
  Methods defined here:
AllQueries(self, hosts=None)
Executes all queries against a list of hosts.
Args: See method AddHosts for details.
  Either as a list of hostnames or as a dictionary of hosts
  whose values are dictionaries of the properties of the host.
              
Returns:                                                               
  A dictionary.
EfficiencyRating(self, hosts=None)
Gets statistics from a list of hosts and analyzes the stats.
 
Args: See method AddHosts for details.
  Either as a list of hostnames or as a dictionary of hosts
  whose values are dictionaries of the properties of the host.
                          
Returns:                                                               
  A dictionary.
ManyQueries(self, query_list=None, host='127.0.0.1')
Executes many queries for a host and returns the result.                
                                                                        
Args:                                                                    
  query: [QUERY, QUERY, ...] # List of valid queries from returnQueries.
  host:  HOST   # A valid computer name or ip address.             
                                                                     
Returns:                                                            
  A dictionary is returned.
MasterSlaveStatus(self, hosts=None)
Checks a list of servers to see if they are in sync in their
master/slave configuration. 
NOTE: This doesn't work with MySQL 5.0.
It should work on MySQL 3.X and 4.X.
 
Args:                                                                    
    # HOST is the name or ip address of a server.
  hosts: See method AddHosts for details.
    Either as a list of hostnames or as a dictionary of hosts
    whose values are dictionaries of the properties of the host.
              
Returns:                                                               
  A dictionary
 
  --- In order to use this method effectively, perform these actions
  --- on the slaves and masters.
  GRANT SUPER ON *.* TO 'USERNAME'@'%' identified by 'PASSWORD';
  GRANT SUPER ON *.* TO 'USERNAME'@'localhost' identified by 'PASSWORD';
 
  GRANT process ON *.* TO 'USERNAME'@'%' identified by 'PASSWORD';
  GRANT process ON *.* TO 'USERNAME'@'localhost' identified by 'PASSWORD';
  --- where USERNAME is the username you want to use for monitoring and
  --- PASSWORD is its password.
  --- You should not grant any other privledges to this account for
  --- security reasons.
OneQuery(self, query='status', host='127.0.0.1')
Executes one query for a host and returns the result.
 
Args:                                                                    
  query: QUERY  # One of the valid queries listed in returnQueries.
  host:  HOST   # A valid computer name or ip address.
                                                                         
Returns:                                                               
  A dictionary is returned.
QueryHost(self, hosts=None, query_list=None, refresh=0, simple=1)
Executes a list of queries against a list of hosts.
Used internally mostly.                       
 
Args:                                                                    
  query_list: [QUERY, QUERY, ...] # List of valid queries
                                    # from returnQueries.
  hosts: See method AddHosts for details.
         Either as a list of hostnames or as a dictionary of hosts
         whose values are dictionaries of the properties of the host.
  options: { key:Value, key:Value, .....}                              
  refresh: 1 or 0             # 0 means use cache, 1 means refresh.  
                                                                         
Returns:                                                               
  A dictionary

Methods inherited from MySQLQueryBase:
AddHosts(self, hosts=None, clear=None)
Adds hosts the MySQL hosts to analyze.
 
Args:
  # HOST is the name or ip address of a server.
  hosts:
         A list in the format [ HOST, HOST, ...]
         or as a dictionary of dictionaries.
         hosts = {}
         hosts[HOST] = {}
         hosts[HOST]['port'] = 3306
         where each host is a key to its own dictionary.
         A host's keys can be one of the following:
           ['port', 'username', 'password']
  clear: If > 0, removes the other hosts (but not their reports).
         You shouldn't have to remove hosts unless you really want to.
         Their reports will still be there, if any. 
  
Returns:
  The total list of hosts.
DumpRawReports(self)
Returns the internal dictionary self._reports.
  This is intended for debug purposes only. 
Args:
  None
 
Returns:
  A dictionary of hostnames.
  Values for each host is a dictionary with the keys
    ['status', 'errors', 'reports'].
    status is 1 for good, 0 for nothing, -1 = bad.
    errors = A single string describing errors with this
       host. Empty string is good.
    reports = A dictionary whose keywords are the names
       of the reports.
       Each report has the keys
         ['status', 'executed', 'values', 'errors'].
         values = A dictionary of the keyword/values
            returned by the query executed on MySQL.
GetHostErrors(self, host=None)
Returns any errors connecting to the host has had.
   
Args:
  # HOST is the name or ip address of a server.
  host: HOST
 
Returns: None, an empty string or string of errors.
         An empty string means no errors.
GetHosts(self)
Returns the current list of hosts.
GetOptions(self)
Returns a dictionary of current options.
GetQueries(self)
Returns the known defined queries.
GetReportErrors(self, host=None, report=None)
Returns any errors the report has.
 
Args:
  # HOST is the name or ip address of a server.
  host: HOST
  report: Report name.
 
Returns: None, an empty string, or string of errors.
    An empty string means no errors.
GetReportStatus(self, host=None, report=None)
Returns the status of the report.
   Returns -1, 0, 1, or None.
   1 is considered a success.
Args:
  # HOST is the name or ip address of a server.
  host: HOST
  report: Report name.
 
Returns: -1, 0, 1, or None
GetReports(self, hosts=None)
Returns a dictionary of hosts and their reports.
 
Args:
  # HOST is the name or ip address of a server.
  hosts: [ HOST, HOST, ...]
    
Returns:
  Dictionary of hosts whose values are a
  dictionary of report names and their values.
SetOptions(self, options=None)
Sets the connection options to MySQL.
 OPTIONS is a dictionary of the following keys:                     
      username = STRING                                                
      password = STRING                                                
      database = STRING                                                
      Port = STRING                                                    
      ids = LIST of primary ids of a table.                            
      fieldname = STRING of a field in a table.                        
      table = STRING of a table in a database.                         
      host = STRING of the host you are connecting to for single       
             hosts only.                                               
      autoconnect = 0 or 1. 1 means connect as soon as the object      
         is created. Assumes other values are supplied.                
      server_list = FILE which contains a list of hostnames.           
 
Args:
   Key is one of the following: host, database, username, password,
                                table, field, options_file
   options_file is a file which contains 'key value' entries.
     
  options: { Key:Value, Key:Value, .....}
   
Returns:
  Doesn't return anything.
__init__(self, options=None)
Base internal variables we use for all classes.

 
Functions
       
Except()
Generic except method used by all subclasses.

 
Data
        QUERIES = {'databases': 'SHOW DATABASES', 'engines': 'show engines', 'errors': 'show errors', 'innodb': 'show innodb status', 'logs': 'show logs', 'master': 'show master status', 'privs': 'show privledges', 'processlist': 'SHOW FULL PROCESSLIST', 'slave': 'show slave status', 'status': 'show status', ...}