Redis大key掃描Python指令碼
指令碼實現掃描redis例項中長度大於10240的key並列印出來
# cat find_bigkey.py
#!/usr/bin/python
import sys
import redis
def check_big_key(r, k):
bigKey = False
length = 0
try:
type = r.type(k)
if type == "string":
length = r.strlen(k)
elif type == "hash":
length = r.hlen(k)
elif type == "list":
length = r.llen(k)
elif type == "set":
length = r.scard(k)
elif type == "zset":
length = r.zcard(k)
except:
return
if length > 10240:
bigKey = True
if bigKey :
print db,k,type,length
def find_big_key_normal(db_host, db_port, db_num):
r = redis.StrictRedis(host=db_host, port=db_port, db=db_num)
for k in r.scan_iter(count=1000):
check_big_key(r, k)
def find_big_key_sharding(db_host, db_port, db_num, nodecount):
r = redis.StrictRedis(host=db_host, port=db_port, db=db_num)
cursor = 0
for node in range(0, nodecount) :
while True:
iscan = r.execute_command("iscan",str(node), str(cursor), "count", "1000")
for k in iscan[1]:
check_big_key(r, k)
cursor = iscan[0]
print cursor, db, node, len(iscan[1])
if cursor == "0":
break;
if __name__ == '__main__':
if len(sys.argv) != 3:
print 'Usage: python ', sys.argv[0], ' host port '
exit(1)
db_host = sys.argv[1]
db_port = sys.argv[2]
r = redis.StrictRedis(host=db_host, port=int(db_port))
#nodecount = r.info()['nodecount']
nodecount = 1
keyspace_info = r.info("keyspace")
for db in keyspace_info:
print 'check ', db, ' ', keyspace_info[db]
if nodecount > 1:
find_big_key_sharding(db_host, db_port, db.replace("db",""), nodecount)
else:
find_big_key_normal(db_host, db_port, db.replace("db", ""))
備註:指令碼21行if length > 10240,可以改成您需要指定的長度
執行例子:
連線本機redis:
# python find_bigkey.py 127.0.0.1 6379
check db1 {'keys': 100000, 'expires': 0, 'avg_ttl': 0}
check db0 {'keys': 3, 'expires': 0, 'avg_ttl': 0}
check db5 {'keys': 500, 'expires': 0, 'avg_ttl': 0}
check db6 {'keys': 1, 'expires': 0, 'avg_ttl': 0}
連線其他機器redis:
# python find_bigkey.py 192.168.1.9 6379 | tee -a bigkey.log
check db15 {'keys': 6, 'expires': 0, 'avg_ttl': 0}
check db0 {'keys': 5, 'expires': 0, 'avg_ttl': 0}
check db10 {'keys': 6, 'expires': 0, 'avg_ttl': 0}
db10 test_ref_169_error hash 892788
db10 test_ref_167_error hash 257735
check db14 {'keys': 18, 'expires': 0, 'avg_ttl': 0}
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/15498/viewspace-2639472/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- redis的bigkey掃描指令碼Redis指令碼
- Python-FTP漏洞掃描指令碼PythonFTP指令碼
- Pythonpyclamad病毒掃描與目錄病毒掃描指令碼(轉載)Python指令碼
- 通過shell指令碼防止埠掃描指令碼
- 2.awd初階_指令碼掃描指令碼
- iOS專案冗餘資源掃描指令碼iOS指令碼
- 小白讀iOS冗餘資源掃描指令碼iOS指令碼
- python掃描埠Python
- python 埠掃描Python
- redis裡顯示key大小的shell指令碼Redis指令碼
- iOS 使用CIDetector掃描相簿二維碼、原生掃描iOSIDE
- Redis熱點key大keyRedis
- stopkey對索引掃描的影響測試TopK索引
- iOS二維碼掃描iOS
- 基於python編寫一個簡單的多執行緒埠掃描指令碼Python執行緒指令碼
- 掃描技術和掃描工具
- Python 實現埠掃描器Python
- iOS中二維碼掃描iOS
- 【Redis】獲取沒有設定ttl的key指令碼Redis指令碼
- Redis刪除大KeyRedis
- 淺析Redis大KeyRedis
- iOS 掃描二維碼/條形碼iOS
- AWVS掃描器掃描web漏洞操作Web
- 文件掃描OCR識別-1(python)Python
- win10系統掃描器提示掃描不到掃描器如何解決Win10
- 掃描二維碼登入思路
- 安卓自定義二維碼掃描安卓
- IOS 使用 ZbarSDK 二維碼掃描iOS
- 掃描王 for Mac專業圖片掃描工具Mac
- Nmap掃描教程之基礎掃描詳解
- Redis 如何處理大 KeyRedis
- Oracle中存取資料掃描Table及索引的方式(全表掃描,索引掃描等)Oracle索引
- MySQL中的全表掃描和索引樹掃描MySql索引
- 利用Python實現自動掃雷小指令碼Python指令碼
- ubuntu安裝zbar二維碼掃描Ubuntu
- Android實現掃描二維碼Android
- Android二維碼生成與掃描Android
- iOS開發之掃描二維碼iOS