def connect(): """Connect to MySQL server""" try: #Create connection to database db = MySQLdb.connect(host=config.get('dbAlerter', 'mysql_hostname'), port=int(config.get('dbAlerter', 'mysql_port')), user=config.get('dbAlerter', 'mysql_username'), passwd=config.get('dbAlerter', 'mysql_password')) except MySQLdb.Error, (error, description): #Access denied error if (error == 1045): notify.notify( 'Error', '(' + str(error) + ') - ' + description, 'Error (' + str(error) + ') - ' + description + "\n\n\nDid you remember to grant the correct privileges?\n\nGRANT PROCESS, SELECT, REPLICATION CLIENT, SHOW DATABASES, SUPER ON *.* TO 'mysqluser'@'localhost' IDENTIFIED BY 'mysqluser_password';\n\nGRANT CREATE, INSERT, DELETE, DROP ON dbAlerter.* TO 'mysqluser'@'localhost' IDENTIFIED BY 'mysqluser_password';" ) notify.log_notify('Shutdown') sys.exit(1) #No database selected error elif (error == 1046): pass else: notify.notify( 'Error', '(' + str(error) + ') - ' + description, 'Error (' + str(error) + ') - ' + description + "\n\ndbAlerter Shutdown") notify.log_notify('Shutdown') sys.exit(1)
def check(stats=False): """Perform MySQL checks. Keyword arguments: stats -- produce statistics report post check """ global db, statistics try: db.ping() #OS Checks if (checkos.supported): check_cpu_usage() check_disk_usage() #MySQL Checks update_variables() check_status() check_auto_increment() check_anonymous_accounts() check_empty_passwords() check_functionality() check_error_log() check_process_list() check_slow_query_log() check_slave_status() #Produce statistics report? if (stats): statistics_report() except MySQLdb.Error, (error, description): if error == 2006: notify.notify('Error', '(' + str(error) + ') ' + description, 'Error (' + str(error) + ') - ' + description) retry_count = 0 while 1: try: #Create connection to database db = MySQLdb.connect( host=config.get('dbAlerter', 'mysql_hostname'), port=int(config.get('dbAlerter', 'mysql_port')), user=config.get('dbAlerter', 'mysql_username'), passwd=config.get('dbAlerter', 'mysql_password')) #Update MySQL pid variables['PID'] = int( file(variables['PID_FILE'], 'r').read().strip()) notify.notify('Info', 'MySQL Server Back Online', 'MySQL Server Back Online') statistics['INFO'] += 1 break except MySQLdb.Error, (error, description): if (error == 2003 or error == 2002): if (retry_count == 5): notify.log_notify( 'Error', '(' + str(error) + ') ' + description) retry_count = 0 else: retry_count += 1 time.sleep( int(config.get('dbAlerter', 'check_interval')))
def shutdown(): """Perform any cleanup code before we cease to exist""" notify.log_notify('Shutdown') #Remove pidfile if exists if (config.has_option('dbAlerter', 'pid_file')): pidfile = config.get('dbAlerter', 'pid_file') if os.path.exists(pidfile): os.remove(pidfile) sys.exit(0)
def check(stats=False): """Perform MySQL checks. Keyword arguments: stats -- produce statistics report post check """ global db, statistics try: db.ping() #OS Checks if (checkos.supported): check_cpu_usage() check_disk_usage() #MySQL Checks update_variables() check_status() check_auto_increment() check_anonymous_accounts() check_empty_passwords() check_functionality() check_error_log() check_process_list() check_slow_query_log() check_slave_status() #Produce statistics report? if (stats): statistics_report() except MySQLdb.Error, (error,description): if error == 2006: notify.notify('Error','(' + str(error) + ') ' + description, 'Error (' + str(error) + ') - ' + description) retry_count=0 while 1: try: #Create connection to database db=MySQLdb.connect(host=config.get('dbAlerter', 'mysql_hostname'), port=int(config.get('dbAlerter', 'mysql_port')), user=config.get('dbAlerter', 'mysql_username'), passwd=config.get('dbAlerter', 'mysql_password')) #Update MySQL pid variables['PID'] = int(file(variables['PID_FILE'],'r').read().strip()) notify.notify('Info', 'MySQL Server Back Online', 'MySQL Server Back Online') statistics['INFO'] += 1 break except MySQLdb.Error, (error,description): if (error == 2003 or error == 2002): if (retry_count == 5): notify.log_notify('Error', '(' + str(error) + ') ' + description) retry_count=0 else: retry_count+=1 time.sleep(int(config.get('dbAlerter','check_interval')))
def connect(): """Connect to MySQL server""" try: #Create connection to database db=MySQLdb.connect(host=config.get('dbAlerter', 'mysql_hostname'), port=int(config.get('dbAlerter', 'mysql_port')), user=config.get('dbAlerter', 'mysql_username'), passwd=config.get('dbAlerter', 'mysql_password')) except MySQLdb.Error, (error,description): #Access denied error if (error==1045): notify.notify('Error','(' + str(error) + ') - ' + description, 'Error (' + str(error) + ') - ' + description + "\n\n\nDid you remember to grant the correct privileges?\n\nGRANT PROCESS, SELECT, REPLICATION CLIENT, SHOW DATABASES, SUPER ON *.* TO 'mysqluser'@'localhost' IDENTIFIED BY 'mysqluser_password';\n\nGRANT CREATE, INSERT, DELETE, DROP ON dbAlerter.* TO 'mysqluser'@'localhost' IDENTIFIED BY 'mysqluser_password';") notify.log_notify('Shutdown') sys.exit(1) #No database selected error elif (error==1046): pass else: notify.notify('Error','(' + str(error) + ') - ' + description, 'Error (' + str(error) + ') - ' + description + "\n\ndbAlerter Shutdown") notify.log_notify('Shutdown') sys.exit(1)
def main (): """This is the main function run by the daemon.""" #Import Global Vars global alivestamp global laststats #Filter Warnings warnings.filterwarnings('ignore') #Initialise notification module notify.initialise() #Initialise mysql module checkmysql.initialise() notify.log_notify('Startup') while 1: #Write to logfile once an hour if (time.time() - alivestamp) > 3600: alivestamp = time.time() notify.log_notify('Info', 'Alive') #Send Statistics once a day if (time.strftime("%d", time.localtime()) != laststats): #Process MySQL Checks checkmysql.check(True) checkmysql.reset_statistics() laststats = time.strftime("%d", time.localtime()) else: #Process OS Checks checkos.check() #Process MySQL Checks checkmysql.check() time.sleep(int(config.get('dbAlerter','check_interval'))) #Cleanup checkmysql.cleanup()
#Update MySQL pid variables['PID'] = int(file(variables['PID_FILE'],'r').read().strip()) notify.notify('Info', 'MySQL Server Back Online', 'MySQL Server Back Online') statistics['INFO'] += 1 break except MySQLdb.Error, (error,description): if (error == 2003 or error == 2002): if (retry_count == 5): notify.log_notify('Error', '(' + str(error) + ') ' + description) retry_count=0 else: retry_count+=1 time.sleep(int(config.get('dbAlerter','check_interval'))) else: notify.notify('Error','(' + str(error) + ') ' + description, 'Error (' + str(error) + ') - ' + description + "\n\ndbAlerter Shutdown") notify.log_notify('Shutdown') time.sleep(5) sys.exit(1) except: notify.log_notify('Shutdown') raise time.sleep(5) sys.exit(1) def connect(): """Connect to MySQL server""" try: #Create connection to database
break except MySQLdb.Error, (error, description): if (error == 2003 or error == 2002): if (retry_count == 5): notify.log_notify( 'Error', '(' + str(error) + ') ' + description) retry_count = 0 else: retry_count += 1 time.sleep( int(config.get('dbAlerter', 'check_interval'))) else: notify.notify( 'Error', '(' + str(error) + ') ' + description, 'Error (' + str(error) + ') - ' + description + "\n\ndbAlerter Shutdown") notify.log_notify('Shutdown') time.sleep(5) sys.exit(1) except: notify.log_notify('Shutdown') raise time.sleep(5) sys.exit(1) def connect(): """Connect to MySQL server""" try: #Create connection to database db = MySQLdb.connect(host=config.get('dbAlerter', 'mysql_hostname'),