File: //usr/lib/python2.7/site-packages/lap/mysql_56.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")
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
# is innodb working? It's crucial to normal operation
if params.get('innodb',True):
cursor.execute("""select engine from information_schema.engines where Engine='Innodb' and support='default'""")
ret = cursor.fetchone()
if ret[0] != 'InnoDB':
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)