Skip to content

Instantly share code, notes, and snippets.

@RPDiep
Last active August 29, 2015 14:01
Show Gist options
  • Save RPDiep/c71187ba6d8df24fc366 to your computer and use it in GitHub Desktop.
Save RPDiep/c71187ba6d8df24fc366 to your computer and use it in GitHub Desktop.
A simple python script that checks for crashed tables. The output file can be read by monitoring solutions such as zabbix, nagios or icinga.
#!/usr/bin/python
#
# Author: Rene Diepstraten <rene@pcextreme.nl>
#
outputfile = "/var/dbcheck/dbcheck.txt"
import MySQLdb, signal, sys
def sigint_handler(signum, frame):
print '\n\nCTRL+C pressed, exiting.\n'
conn.close()
sys.exit(1)
signal.signal(signal.SIGINT, sigint_handler)
print "\nPerforming checks on MySQL tables...\n"
conn = MySQLdb.connect ( read_default_file="~/.my.cnf" )
cur = conn.cursor()
crashedtables=0
cur.execute("SHOW DATABASES WHERE `Database` NOT IN ('information_schema','performance_schema')")
databases = [ db[0] for db in cur.fetchall() ]
for database in databases:
cur.execute("SHOW FULL TABLES FROM `%s` WHERE Table_Type = 'BASE TABLE'" % database)
tables = [ table[0] for table in cur.fetchall() ]
for table in tables:
if database + "." + table not in ( "mysql.general_log","mysql.slow_log" ):
cur.execute("CHECK TABLE `%s`.`%s`" % ( database,table ))
result = cur.fetchone()[3]
print "\033[1A" + "Checking " + database + "." + table + " " * 30
if result != 'OK':
print "\033[1A" + "%s.%s has crashed!\n" % (database,table)
crashedtables += 1
print "\033[1A" + "Found %i crashed tables" % crashedtables + " " * 25 + "\n"
file = open(outputfile,"w")
file.write(str(crashedtables))
file.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment