def get(self, key, all=False, host=None, port=None): """Command: cli config get [key] :param key: redis config keyword :param all: If True, command to all nodes :param host: host :param port: port """ sub_cmd = 'config get "{key}"'.format(key=key) if all: RedisCliUtil.command_all(sub_cmd=sub_cmd, formatter=print_table) else: RedisCliUtil.command(sub_cmd=sub_cmd, host=host, port=port)
def rowcount(self): """Query and show cluster row count""" logger.debug('rowcount') # open-redis-cli-all info Tablespace | grep totalRows | awk -F ', # ' '{print $4}' | awk -F '=' '{sum += $2} END {print sum}' ip_list = config.get_node_ip_list() port_list = config.get_master_port_list() outs, meta = RedisCliUtil.command_raw_all('info Tablespace', ip_list, port_list) lines = outs.splitlines() key = 'totalRows' filtered_lines = (filter(lambda x: key in x, lines)) ld = RedisCliUtil.to_list_of_dict(filtered_lines) # row_count = reduce(lambda x, y: {key: int(x[key]) + int(y[key])}, ld) row_count = reduce(lambda x, y: x + int(y[key]), ld, 0) self._print(row_count)
def slots(self): """Command: cli cluster slots""" def formatter(outs): lines = outs.splitlines() replicas = config.get_replicas() column_count = 2 + 2 * (replicas + 1) row_count = len(lines) / column_count header = [['start', 'end', 'm_ip', 'm_port']] remain = column_count - 4 data = [] for i in range(0, remain / 2): header[0].append('s_ip_%d' % i) header[0].append('s_port_%d' % i) for i in range(0, row_count): data.append(lines[i * column_count:column_count * (i + 1)]) data.sort(key=lambda x: int(x[0])) table = AsciiTable(header + data) print(table.table) RedisCliUtil.command('cluster slots', formatter=formatter)
def __set_maxmemory(self, cluster_id_of_table, cols): key = 'maxmemory' try: sub_cmd = 'config get "{key}"'.format(key=key) raw = RedisCliUtil.command(sub_cmd=sub_cmd, mute=True, cluster_id=cluster_id_of_table) v = int(raw.splitlines()[1]) cols[key] = '{:,} bytes'.format(v) except: cols[key] = 'error'
def __set_ttl(self, cluster_id_of_table, cols): def _filter(raw): seconds = int(raw) return '%s (%s)' % (raw, str(datetime.timedelta(seconds=seconds))) key = 'flash-db-ttl' try: sub_cmd = 'config get "{key}"'.format(key=key) v = RedisCliUtil.command(sub_cmd=sub_cmd, mute=True, cluster_id=cluster_id_of_table) cols[key] = _filter(v.splitlines()[1]) except: cols[key] = 'error'
def __set_tablespace(self, cluster_id_of_table, cols): tablespace_lines = RedisCliUtil.command(sub_cmd='info tablespace', mute=True, cluster_id=cluster_id_of_table) keywords = ['numPartitionColumns', 'partitionColumns', 'partitions'] lines = tablespace_lines.splitlines() for line in lines: for keyword in keywords: if keyword in line: left = line.split(keyword + '=')[1] if ',' in left: value = left.split(',')[0] cols[keyword] = value else: cols[keyword] = left
def _get_ip_port_dict_using_cluster_nodes_cmd(): def mute_formatter(outs): pass outs = RedisCliUtil.command(sub_cmd='cluster nodes', formatter=mute_formatter) lines = outs.splitlines() d = {} for line in lines: rows = line.split(' ') addr = rows[1] if 'connected' in rows: (host, port) = addr.split(':') if host not in d: d[host] = [port] else: d[host].append(port) return d
def set(self, key, value, save, all=False, host=None, port=None): """Command: cli config set [key] [value] :param key: redis config keyword :param value: value :param save: If True, save value to config file :param all: If True, command to all nodes :param host: host :param port: port """ tr = TableReport(['step', 'result']) sub_cmd = 'config set {key} {value}'.format(key=key, value=value) if all: RedisCliUtil.command_all(sub_cmd=sub_cmd, formatter=print_table) else: RedisCliUtil.command(sub_cmd=sub_cmd, host=host, port=port) if save: RedisCliUtil.save_redis_template_config(key, value) cluster_util.rsync_fb_conf() Center().update_redis_conf()
def nodes(self): """Command: cli cluster nodes""" RedisCliUtil.command('cluster nodes')
def info(self): """Command: cli cluster info""" RedisCliUtil.command('cluster info')
def replication(self, host=None, port=None): """Command: cli info replication""" RedisCliUtil.command('info replication', host=host, port=port)
def tablespace(self, host=None, port=None): """Command: cli info tablespace""" RedisCliUtil.command(sub_cmd='info tablespace', host=host, port=port)
def eviction(self, host=None, port=None): """Command: cli info eviction""" RedisCliUtil.command(sub_cmd='info eviction', host=host, port=port)
def memory(self, host=None, port=None): """Command: cli info memory""" RedisCliUtil.command(sub_cmd='info memory', host=host, port=port)
def all(self, host=None, port=None): """Command: cli info all""" RedisCliUtil.command(sub_cmd='info', host=host, port=port)