File: //usr/lib/python2.7/site-packages/lap/mysql.py
import MySQLdb
import yaml
import warnings
def perfdata(max_conn, in_use):
return "max_connections=%s|in_use=%s" % (max_conn, in_use)
def run(params):
conn = None
try:
f = params.get("defaults-file", "/etc/check_mk/mysql.cfg")
cmd = params.get('check_command')
thresolds = yaml.load(params.get('thresholds'))
conn = MySQLdb.connect(read_default_file=f)
cursor = conn.cursor()
# we connected, so mysql is working, now some additional tests
# Verify user specified command
if cmd is not None:
cursor.execute(cmd);
result = cursor.fetchone()
if result is not None:
return [0, 'OK: %s is ok' % cmd ]
else:
return [2, 'CRITICAL: %s is EMPTY' % cmd]
# Check mysql version
cursor.execute("SELECT VERSION()")
version = cursor.fetchone()[0]
# is innodb working? It's crucial to normal operation
if params.get('innodb',True):
if "5.0" in version:
cursor.execute("SHOW VARIABLES LIKE 'have_innodb'")
rows = cursor.fetchone()[1]
if rows <> 'YES':
return [2, 'CRITICAL: InnoDB is not loaded']
else:
rows = cursor.execute("select 1 from information_schema.engines where engine = 'InnoDB' and support in ('YES', 'DEFAULT')")
if rows == 0:
return [2, 'CRITICAL: InnoDB is not loaded']
# get number of connections
cursor.execute("select count(id) from information_schema.processlist")
current_connections = cursor.fetchone()[0]
cursor.execute("show variables like 'max_connections'")
max_connections = cursor.fetchone()[1]
percent = float(current_connections) / float(max_connections)
percent = percent * 100
if percent > thresolds['critical']:
return [2, 'CRITICAL: {0}% of connections in use'.format(percent), perfdata(max_connections, current_connections)]
elif percent > thresolds['warning']:
return [1, 'WARNING: {0}% of connections in use'.format(percent), perfdata(max_connections, current_connections)]
else:
return [0, 'OK: {0}% of connections in use'.format(percent), perfdata(max_connections, current_connections)]
except Exception, e:
return [2, 'CRITICAL: {0}'.format(str(e))]
finally:
try:
if conn:
conn.close()
except:
# finally clause, don't bother about errors here
pass
def __run__(params):
with warnings.catch_warnings():
warnings.simplefilter("ignore")
return run(params)