예제 #1
0
 def retrieve_node_info_stat(self, params):
     if not params:
         raise UserVisiableException('params are not given')
     _dict = {}
     conn = self.get_mysql_connection()
     if conn is None:
         raise UserVisiableException("Can\'t connect to mysql server")
     try:
         for item in params:
             getattr(self, 'retrieve_' + item)(conn, item, params[item],
                                               _dict)
         return _dict
     except AttributeError:
         raise UserVisiableException('%s param given is wrong!' % str(item))
     finally:
         conn.close()
예제 #2
0
 def retrieve_stat_database_size_command(self, conn, key, value, _dict):
     cursor = conn.cursor()
     cursor.execute(
         'select (sum(DATA_LENGTH)+sum(INDEX_LENGTH))/1024 FROM information_schema.TABLES where TABLE_SCHEMA="{0}"'
         .format(value))
     rows = cursor.fetchall()
     if rows[0][0] is None:
         raise UserVisiableException('%s param given is wrong!' % key)
     _dict.setdefault(key, str(rows[0][0]))
예제 #3
0
 def show_user_max_conn(self, conn, username, host):
     cursor = conn.cursor()
     cursor.execute(
         "select max_user_connections from mysql.user where user='******' and host='{1}';"
         .format(username, host))
     rows = cursor.fetchall()
     if not rows:
         raise UserVisiableException(
             'the %s user and %s host_ip is not exists' % (username, host))
     return rows[0][0]
예제 #4
0
    def run(self):
        isLock, lock = self.zkOpers.lock_backup_action()
        if not isLock:
            logging.info('zk is not lock')
            return

        try:
            _password = retrieve_monitor_password()
            conn = self.dba_opers.get_mysql_connection(user="******",
                                                       passwd=_password)
            if None == conn:
                raise UserVisiableException("Can\'t connect to mysql server")

            db_status = self.dba_opers.show_status(conn)
            if 'Synced' != db_status[-14][1]:
                self.backup_record[
                    'error: '] = 'Mcluster is not start %s' % datetime.datetime.now(
                    ).strftime(TIME_FORMAT)
                self.backupOpers._write_info_to_local(
                    self.backupOpers.path, self.backupOpers.file_name,
                    self.backup_record)
                self.zkOpers.write_backup_backup_info(self.backup_record)
                return

            if '0' == self.__run_comm(CHECK_DMP_DATA_CMD):
                self.backup_record[
                    'error: '] = 'No have /data partition %s' % datetime.datetime.now(
                    ).strftime(TIME_FORMAT)
                self.backupOpers._write_info_to_local(
                    self.backupOpers.path, self.backupOpers.file_name,
                    self.backup_record)
                self.zkOpers.write_backup_backup_info(self.backup_record)
                return

            self.backupOpers.create_backup_directory()
            self.backupOpers.remove_expired_backup_file()

            self.backupOpers.backup_action(self.zkOpers)
            self.backupOpers.trans_backup_file(self.zkOpers)

            record = {
                "recently_backup_ip: ": str(get_localhost_ip()),
                'time: ': datetime.datetime.now().strftime(TIME_FORMAT),
                'backup_type: ': self._backup_mode
            }
            self.zkOpers.write_backup_backup_info(record)

        except Exception, e:
            record = {
                "error: ": 'backup is wrong, please check it!',
                'time:': datetime.datetime.now().strftime(TIME_FORMAT),
                'backup_type: ': self._backup_mode
            }
            self.zkOpers.write_backup_backup_info(record)
            logging.error(e, exc_info=True)
예제 #5
0
    def run(self):
        url_path = "/inner/backup"
        if self.incr_basedir:
            self.data['incr_basedir'] = self.incr_basedir
        self.data['backup_type'] = self.backup_type
        try:
            if self.backup_type == BACKUP_TYPE.FULL:
                action_ips = self._get_usable_ips()
                if not action_ips:
                    raise UserVisiableException('no available node, usually disk is not enough!')
                self._dispatch_request([action_ips[0]], 'POST', url_path, self.data)
            elif self.backup_type == BACKUP_TYPE.INCR:
                key_value = self.zkOper.retrieve_type_backup_status_info('full')
                action_ips = key_value['full_backup_ip:']
                if not action_ips:
                    raise UserVisiableException('no available full-backup node')
                self._dispatch_request([action_ips], 'POST', url_path, self.data)

        except Exception, e:
            logging.info(e)
    def _get_usable_ips(self):
        online_node_list = self.zkOper.retrieve_started_nodes()
        if not online_node_list:
            raise UserVisiableException('not started node, please check zk node!')

        url_system_path = "/node/stat/workload"
        url_disk_path = "/node/stat/disk/available"
        url_memory_path = "/node/stat/memory/available"
        url_diskenough_path = "/node/stat/disk/enough"

        try:
            system_loads = self._retrieve_nodes_load(online_node_list, url_system_path)
            available_spaces = self._retrieve_nodes_load(online_node_list, url_disk_path)
            available_memory = self._retrieve_nodes_load(online_node_list, url_memory_path)
            disk_enough = self._retrieve_nodes_load(online_node_list, url_diskenough_path)

            usable_ips = self._analysis_usable_backup_node(system_loads, available_spaces, available_memory, disk_enough)

        except Exception, e:
            self.zkOper.write_backup_backup_info({'error': traceback.format_exc()})
            raise UserVisiableException(e)
예제 #7
0
    def stat_binlog_eng_log_pos(self, params):
        if not params:
            raise UserVisiableException('params are not given')

        conn = self.dba_opers.get_mysql_connection()
        if None == conn:
            raise UserVisiableException("Can\'t connect to mysql server")

        log_pos_info = ''
        master_log_file = ''
        end_log_pos = ''
        try:
            cursor = conn.cursor()
            cursor.execute('show binary logs')
            rows_bin_logs = cursor.fetchall()
            assert rows_bin_logs
            invokecommand = InvokeCommand()
            for i in range(len(rows_bin_logs)):
                master_log_file = rows_bin_logs[-i - 1][-2]
                ret_str = invokecommand._runSysCmd(
                    '''mysql -uroot -pMcluster -e "show binlog events IN '%s'"|grep 'xid=%s' '''
                    % (master_log_file, params['xid']))
                assert ret_str
                log_pos_info = ret_str[0]
                if log_pos_info:
                    break

            end_log_pos = log_pos_info.strip('\n').split('\t')[-2]

        finally:
            conn.close()

        result = {}
        result.setdefault('Master_Log_File', master_log_file)
        result.setdefault('End_Log_Pos', end_log_pos)
        return result
예제 #8
0
 def retrieve_stat_table_space_analyze_command(self, conn, key, value,
                                               _dict):
     cursor = conn.cursor()
     cursor.execute("set names 'utf8'")
     cursor.execute(
         'select table_name, table_comment, (data_length+index_length)/1024 as total_kb from information_schema.tables where table_schema="{0}"'
         .format(value))
     rows = cursor.fetchall()
     row_dict = {}
     if rows == ():
         raise UserVisiableException('%s param given is wrong!' %
                                     str(key + '=' + value))
     for row in rows:
         __dict = {}
         __dict.setdefault('table_comment', row[1])
         __dict.setdefault('total_kb', str(row[2]))
         row_dict.setdefault(row[0], __dict)
     _dict.setdefault(key, row_dict)
예제 #9
0
    def bin_log_node_stat(self):
        conn = self.dba_opers.get_mysql_connection()
        if None == conn:
            raise UserVisiableException("Can\'t connect to mysql server")
        try:
            cursor = conn.cursor()
            cursor.execute("show variables like 'log_bin'")
            rows_stat_log_bin = cursor.fetchall()
            stat_log_bin = rows_stat_log_bin[0][1]
        finally:
            conn.close()

        zkOper = self.retrieve_zkOper()
        started_node_list = zkOper.retrieve_started_nodes()
        local_ip = get_localhost_ip()
        if local_ip in started_node_list:
            started_node_list.remove(local_ip)

        result = {}
        result.setdefault('node_list', started_node_list)
        result.setdefault('stat_log_bin', stat_log_bin)
        return result