Exemplo n.º 1
0
def update_stats(get_innodb=True, get_master=True, get_slave=True):
	"""

	"""
	logging.debug('updating stats')
	global last_update
	global mysql_stats, mysql_stats_last

	cur_time = time.time()
	time_delta = cur_time - last_update
	if time_delta <= 0:
		#we went backward in time.
		logging.debug(" system clock set backwards, probably ntp")

	if cur_time - last_update < MAX_UPDATE_TIME:
		logging.debug(' wait ' + str(int(MAX_UPDATE_TIME - (cur_time - last_update))) + ' seconds')
		return True
	else:
		last_update = cur_time

	logging.debug('refreshing stats')
	mysql_stats = {}

	# Get info from DB
	try:
		conn = MySQLdb.connect(**mysql_conn_opts)

		cursor = conn.cursor(MySQLdb.cursors.DictCursor)
		cursor.execute("SELECT GET_LOCK('gmetric-mysql', 0) as ok")
		lock_stat = cursor.fetchone()
		cursor.close()

		if lock_stat['ok'] == 0:
			return False

		cursor = conn.cursor(MySQLdb.cursors.Cursor)
		cursor.execute("SHOW VARIABLES")
		#variables = dict(((k.lower(), v) for (k,v) in cursor))
		variables = {}
		for (k,v) in cursor:
			variables[k.lower()] = v
		cursor.close()

		cursor = conn.cursor(MySQLdb.cursors.Cursor)
		cursor.execute("SHOW /*!50002 GLOBAL */ STATUS")
		#global_status = dict(((k.lower(), v) for (k,v) in cursor))
		global_status = {}
		for (k,v) in cursor:
			global_status[k.lower()] = v
		cursor.close()
		
		cursor = conn.cursor(MySQLdb.cursors.Cursor)
		cursor.execute("SELECT PLUGIN_STATUS, PLUGIN_VERSION FROM `information_schema`.Plugins WHERE PLUGIN_NAME LIKE '%innodb%' AND PLUGIN_TYPE LIKE 'STORAGE ENGINE';")
		
		have_innodb = False
		innodb_version = 1.0
		row = cursor.fetchone()
		
		if row[0] == "ACTIVE":
			have_innodb = True
			innodb_version = row[1]
		cursor.close()

		# try not to fail ?
		get_innodb = get_innodb and have_innodb
		get_master = get_master and variables['log_bin'].lower() == 'on'

		innodb_status = defaultdict(int)
		if get_innodb:
			cursor = conn.cursor(MySQLdb.cursors.Cursor)
			cursor.execute("SHOW /*!50000 ENGINE*/ INNODB STATUS")
			innodb_status = parse_innodb_status(cursor.fetchone()[2].split('\n'), innodb_version)
			cursor.close()
			logging.debug('innodb_status: ' + str(innodb_status))

		master_logs = tuple
		if get_master:
			cursor = conn.cursor(MySQLdb.cursors.Cursor)
			cursor.execute("SHOW MASTER LOGS")
			master_logs = cursor.fetchall()
			cursor.close()

		slave_status = {}
		if get_slave:
			cursor = conn.cursor(MySQLdb.cursors.DictCursor)
			cursor.execute("SHOW SLAVE STATUS")
			res = cursor.fetchone()
			if res:
				for (k,v) in res.items():
					slave_status[k.lower()] = v
			else:
				get_slave = False
			cursor.close()

		cursor = conn.cursor(MySQLdb.cursors.DictCursor)
		cursor.execute("SELECT RELEASE_LOCK('gmetric-mysql') as ok")
		cursor.close()

		conn.close()
	except MySQLdb.OperationalError, (errno, errmsg):
		logging.error('error updating stats')
		logging.error(errmsg)
		return False
Exemplo n.º 2
0
def update_stats(get_innodb=True, get_master=True, get_slave=True):
    """

	"""
    logging.debug('updating stats')
    global last_update
    global mysql_stats, mysql_stats_last

    cur_time = time.time()
    time_delta = cur_time - last_update
    if time_delta <= 0:
        #we went backward in time.
        logging.debug(" system clock set backwards, probably ntp")

    if cur_time - last_update < MAX_UPDATE_TIME:
        logging.debug(' wait ' +
                      str(int(MAX_UPDATE_TIME -
                              (cur_time - last_update))) + ' seconds')
        return True
    else:
        last_update = cur_time

    logging.debug('refreshing stats')
    mysql_stats = {}

    # Get info from DB
    try:
        conn = MySQLdb.connect(**mysql_conn_opts)

        cursor = conn.cursor(MySQLdb.cursors.DictCursor)
        cursor.execute("SELECT GET_LOCK('gmetric-mysql', 0) as ok")
        lock_stat = cursor.fetchone()
        cursor.close()

        if lock_stat['ok'] == 0:
            return False

        cursor = conn.cursor(MySQLdb.cursors.Cursor)
        cursor.execute("SHOW VARIABLES")
        #variables = dict(((k.lower(), v) for (k,v) in cursor))
        variables = {}
        for (k, v) in cursor:
            variables[k.lower()] = v
        cursor.close()

        cursor = conn.cursor(MySQLdb.cursors.Cursor)
        cursor.execute("SHOW /*!50002 GLOBAL */ STATUS")
        #global_status = dict(((k.lower(), v) for (k,v) in cursor))
        global_status = {}
        for (k, v) in cursor:
            global_status[k.lower()] = v
        cursor.close()

        cursor = conn.cursor(MySQLdb.cursors.Cursor)
        cursor.execute(
            "SELECT PLUGIN_STATUS, PLUGIN_VERSION FROM `information_schema`.Plugins WHERE PLUGIN_NAME LIKE '%innodb%' AND PLUGIN_TYPE LIKE 'STORAGE ENGINE';"
        )

        have_innodb = False
        innodb_version = 1.0
        row = cursor.fetchone()

        if row[0] == "ACTIVE":
            have_innodb = True
            innodb_version = row[1]
        cursor.close()

        # try not to fail ?
        get_innodb = get_innodb and have_innodb
        get_master = get_master and variables['log_bin'].lower() == 'on'

        innodb_status = defaultdict(int)
        if get_innodb:
            cursor = conn.cursor(MySQLdb.cursors.Cursor)
            cursor.execute("SHOW /*!50000 ENGINE*/ INNODB STATUS")
            innodb_status = parse_innodb_status(
                cursor.fetchone()[2].split('\n'), innodb_version)
            cursor.close()
            logging.debug('innodb_status: ' + str(innodb_status))

        master_logs = tuple
        if get_master:
            cursor = conn.cursor(MySQLdb.cursors.Cursor)
            cursor.execute("SHOW MASTER LOGS")
            master_logs = cursor.fetchall()
            cursor.close()

        slave_status = {}
        if get_slave:
            cursor = conn.cursor(MySQLdb.cursors.DictCursor)
            cursor.execute("SHOW SLAVE STATUS")
            res = cursor.fetchone()
            if res:
                for (k, v) in res.items():
                    slave_status[k.lower()] = v
            else:
                get_slave = False
            cursor.close()

        cursor = conn.cursor(MySQLdb.cursors.DictCursor)
        cursor.execute("SELECT RELEASE_LOCK('gmetric-mysql') as ok")
        cursor.close()

        conn.close()
    except MySQLdb.OperationalError, (errno, errmsg):
        logging.error('error updating stats')
        logging.error(errmsg)
        return False