HEX
Server: Apache
System: Linux vpshost0650.publiccloud.com.br 4.4.79-grsec-1.lc.x86_64 #1 SMP Wed Aug 2 14:18:21 -03 2017 x86_64
User: bandeirantesbomb3 (10068)
PHP: 8.0.7
Disabled: apache_child_terminate,dl,escapeshellarg,escapeshellcmd,exec,link,mail,openlog,passthru,pcntl_alarm,pcntl_exec,pcntl_fork,pcntl_get_last_error,pcntl_getpriority,pcntl_setpriority,pcntl_signal,pcntl_signal_dispatch,pcntl_sigprocmask,pcntl_sigtimedwait,pcntl_sigwaitinfo,pcntl_strerror,pcntl_wait,pcntl_waitpid,pcntl_wexitstatus,pcntl_wifexited,pcntl_wifsignaled,pcntl_wifstopped,pcntl_wstopsig,pcntl_wtermsig,php_check_syntax,php_strip_whitespace,popen,proc_close,proc_open,shell_exec,symlink,system
Upload Files
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)