redis 是基於內存的KV數據庫,內存作為存儲介質,關注內存的使用情況是一個重要的指標。解析內存有兩種方法,一個是通過scan遍歷所有key,二是對rdb文件進行分析.
rdb 是rdb-tools工具包其中之一的工具,也是解析dump.rdb文件的工具.
1、生成所有數據庫和鍵中數據的內存報告
2、將轉儲文件轉換為JSON
3、使用標准差異工具比較兩個轉儲文件
rdbtools工具包括了3個可執行文件:
1、rdb 解析整個rdb文件
2、redis-memory-for-key 解析server里的單個key
3、redis-profiler 解析rdb文件成html格式
源碼安裝redis-rdb-tools:
git clone https://github.com/sripathikrishnan/redis-rdb-tools cd redis-rdb-tools python setup.py install
安裝 python-lzf :加快解析速度
pip install python-lzf
PyPI安裝(推薦)
pip install rdbtools python-lzf
參數解析:
[root@hankyoon ~]# rdb --help usage: usage: rdb [options] /path/to/dump.rdb Example : rdb --command json -k "user.*" /var/redis/6379/dump.rdb positional arguments: dump_file RDB Dump file to process optional arguments: -h, --help show this help message and exit -c CMD, --command CMD Command to execute. Valid commands are json, diff, justkeys, justkeyvals, memory and protocol -f FILE, --file FILE Output file -n DBS, --db DBS Database Number. Multiple databases can be provided. If not specified, all databases will be included. -k KEYS, --key KEYS Keys to export. This can be a regular expression -o NOT_KEYS, --not-key NOT_KEYS Keys Not to export. This can be a regular expression -t TYPES, --type TYPES Data types to include. Possible values are string, hash, set, sortedset, list. Multiple typees can be provided. If not specified, all data types will be returned -b BYTES, --bytes BYTES Limit memory output to keys greater to or equal to this value (in bytes) -l LARGEST, --largest LARGEST Limit memory output to only the top N keys (by size) -e {raw,print,utf8,base64}, --escape {raw,print,utf8,base64} Escape strings to encoding: raw (default), print, utf8, or base64. -x, --no-expire With protocol command, remove expiry from all keys -a N, --amend-expire N With protocol command, add N seconds to key expiry time
轉儲的json:
[root@hankyoon]# rdb --command json dump-5001.rdb [{ "name":"yoon", "name1":"hank", "name2":"hankyoon"}]
正則表達式匹配key,並且打印鍵和值:
[root@10-0-10-30 yoon]# rdb --command justkeyvals --key "name.*" dump-5001.rdb name yoon, name1 hank, name2 hankyoon
僅處理數據庫2中hash類型的a開頭的key:
[root@hankyoon ]# rdb -c json --db 2 --type hash --key "a.*" dump-5001.rdb [{},{ "aroma":{"pungent":"vinegar","putrid":"rotten eggs","floral":"roses"}}]
dump文件轉換為JSON:
輸出是UTF-8編碼的JSON。 默認情況下,回調嘗試使用UTF-8解析RDB數據,並使用\U表示符轉義非'ASCII可打印'字符,或使用\x轉義非UTF-8可解析的字節。 嘗試對RDB數據進行解碼可能會導致二進制數據錯誤,可以通過使用--escape raw選項來避免這種情況。 另一種選擇是使用-e base64進行二進制數據的Base64編碼。
解析dump文件並在標准輸出上打印JSON:
[root@hankyoon ]# rdb -c json dump-5001.rdb [{ "name":"yoon", "name1":"hank", "name2":"hankyoon"}]
將dump文件解析為原始字節,並在標准輸出上打印JSON:
[root@hankyoon ]# rdb -c json dump-5001.rdb --escape raw
生成內存報告:
使用-c memory 運行會生成CSV報告,其中包含該鍵使用的近似內存。 --bytes C 和 --largest N 可用於將輸出限制為大於C字節的鍵或N個最大鍵.
[root@hankyoon ]# rdb -c memory 5001dump.rdb --bytes 128 -f memory.csv [root@hankyoon ]# cat memory.csv database,type,key,size_in_bytes,encoding,num_elements,len_largest_element 0,list,lizards,241,quicklist,5,19 0,list,user_list,190,quicklist,3,7 2,hash,baloon,138,ziplist,3,11 2,list,armadillo,231,quicklist,5,20 2,hash,aroma,129,ziplist,3,11
參數:
database: 數據庫編號
type: 數據類型
key: 鍵
size_in_bytes: 使用的內存:包括鍵,值和任何其他開銷
encoding: RDB編碼類型
num_elements: key中的value的個數
len_largest_element: key中的value的長度
expiry: 過期值
注意:內存使用情況是近似的。通常,實際使用的內存將略高於報告的內存。可以按鍵或數據庫編號或數據類型過濾報告。內存報告應有助於檢測由應用程序邏輯引起的內存泄漏。 它還將幫助優化Redis的內存使用。
查找單鍵使用的內存:
查找特定鍵使用的內存(運行整個內存報告非常耗時),使用redis-memory-for-key:
[root@hankyoon ]# redis-memory-for-key name [root@hankyoon ]# redis-memory-for-key -s localhost -p 5001 -a mypassword name Key name Bytes 111 Type hash Encoding ziplist Number of Elements 2 Length of Largest Element 8
比較RDB文件:
使用--command diff選項,並將輸出通過管道傳遞到標准sort:
[root@hankyoon ]# rdb --command diff dump1.rdb | sort > dump1.txt [root@hankyoon ]# rdb --command diff dump2.rdb | sort > dump2.txt [root@hankyoon ]# kdiff3 dump1.txt dump2.txt
要限制文件的大小,可以使用--key選項過濾鍵。
使用Redis協議:
使用protocol命令將RDB文件轉換為redis協議流:
[root@hankyoon ]# rdb -c protocol dump.rdb *4 $4 HSET $9 users:123 $9 firstname $8 Sripathi
Python解析器封裝腳本:
from rdbtools import RdbParser, RdbCallback from rdbtools.encodehelpers import bytes_to_unicode class MyCallback(RdbCallback): ''' Simple example to show how callback works. See RdbCallback for all available callback methods. See JsonCallback for a concrete example ''' def __init__(self): super(MyCallback, self).__init__(string_escape=None) def encode_key(self, key): return bytes_to_unicode(key, self._escape, skip_printable=True) def encode_value(self, val): return bytes_to_unicode(val, self._escape) def set(self, key, value, expiry, info): print('%s = %s' % (self.encode_key(key), self.encode_value(value))) def hset(self, key, field, value): print('%s.%s = %s' % (self.encode_key(key), self.encode_key(field), self.encode_value(value))) def sadd(self, key, member): print('%s has {%s}' % (self.encode_key(key), self.encode_value(member))) def rpush(self, key, value): print('%s has [%s]' % (self.encode_key(key), self.encode_value(value))) def zadd(self, key, score, member): print('%s has {%s : %s}' % (str(key), str(member), str(score))) callback = MyCallback() parser = RdbParser(callback) parser.parse('/home/yoon/dump-5001.rdb')
一、rdb:根據要求分析這個RDB文件
1、按json格式導出rdb:
[root@hankyoon ]# rdb --command json dump.rdb
2、導出rdb中的keys:
[root@hankyoon ]# rdb -c justkeys dump.rdb|uniq
3、導出rdb中的values:
[root@hankyoon ]# rdb -c justkeyvals dump.rdb
4、導出rdb中keys的內存分析:
[root@hankyoon ]# rdb -c memory dump.rdb
5、按RESP協議導出RDB內容:
[root@hankyoon ]# rdb -c protocol dump.rdb 管道導入 [root@hankyoon ]# rdb --command protocol dump.rdb | nc 192.168.1.122 7777
6、分析RDB結果導出到文件:
[root@hankyoon ]# rdb -c memory dump.rdb -f yoon.csv
7、導出指定數據庫的keys:
[root@hankyoon ]# rdb -c justkeyvals dump.rdb -n 0
8、導出匹配(正則)的keys:
[root@hankyoon ]# rdb --command justkeyvals --key ".*set*" dump.rdb
9、不導出匹配(正則)的keys:
[root@hankyoon ]# rdb --command justkeyvals --not-key ".*set*" dump.rdb
10、導出指定類型的keys:
[root@hankyoon ]# rdb --command json --type hash dump.rdb
11、導出大於指定字節的keys:
[root@hankyoon ]# rdb --command memory --bytes 128 dump.rdb
12、導出內存字節排名前3個keys:
[root@hankyoon ]# rdb --command memory --largest 3 dump.rdb
13、導出指定編碼轉義:
[root@hankyoon ]# rdb --command justkeyvals --escape raw dump.rdb
14、導出keys(過期keys除外):
[root@hankyoon ]# rdb --command memory --no-expire dump.rdb
15、導出keys(給過期keys添加時間):
[root@hankyoon ]# rdb --command memory --amend-expire 100 dump.rdb
注意:
以上操作參數可以相互疊加使用,按照實際要求進行組合。並且可以導出成csv文件,導入到數據庫里進行聚合統計和監控。
二、redis-memory-for-key:查看指定key的內存情況
查看指定key的內存分析情況:(查看該服務器上key為hankyoon的內存情況)
[root@hankyoon ]# redis-memory-for-key --server=192.168.163.134 --port=8379 hankyoon Key hankyoon Bytes 56 Type string
三、redis-profiler:RDB分析生成html
RDB分析結果到html文件:
[root@hankyoon ]# redis-profiler dump.rdb -f yoon.html
