redis的幾個實用工具安裝測試

hotdog04發表於2015-06-29

#####redis-audit####
統計key分佈,是否配置過期時間,是否長時間沒有操作:
1、安裝:https://github.com/snmaynard/redis-audit下載
2、依賴包:rubygems  ruby bundler-1.10.5.gem  redis-3.2.1.gem
3、進入到目錄下: bundle exec ruby  redis-audit.rb  127.0.0.1 40000 0 1000
   (bundle exec ruby redis-audit.rb [host] [port] [dbnum] [(optional)sample_size])
4、需要auth的可以在連線redis的時候配置password
5、dbsize獲取資料庫大小; 使用randomkey獲取隨機key;debug object 獲取key大小;
   type獲取key型別;ttl獲取key的過期資訊;匹配超過key長度1/3的放到一個組

示例輸出:
[root@bjm6-24-20 redis-audit-master]# bundle exec ruby  /work/soft/redis-audit-master/redis-audit.rb  127.0.0.1 40000 0 10
Auditing 127.0.0.1:40000 dbnum:0 sampling 10 keys
Sampling 10 keys...
1 keys sampled - 10% complete - Sun Jun 28 17:50:55 +0800 2015
2 keys sampled - 20% complete - Sun Jun 28 17:50:55 +0800 2015
3 keys sampled - 30% complete - Sun Jun 28 17:50:55 +0800 2015
4 keys sampled - 40% complete - Sun Jun 28 17:50:55 +0800 2015
5 keys sampled - 50% complete - Sun Jun 28 17:50:55 +0800 2015
6 keys sampled - 60% complete - Sun Jun 28 17:50:55 +0800 2015
7 keys sampled - 70% complete - Sun Jun 28 17:50:55 +0800 2015
8 keys sampled - 80% complete - Sun Jun 28 17:50:55 +0800 2015
9 keys sampled - 90% complete - Sun Jun 28 17:50:55 +0800 2015
10 keys sampled - 100% complete - Sun Jun 28 17:50:55 +0800 2015
DB has 39844482 keys
Sampled 50 bytes of Redis memory

Found 3 key groups

==============================================================================
Found 1 keys containing strings, like:
c69820248

These keys use 10.0% of the total sampled memory (5 bytes)
None of these keys expire
Average last accessed time: 7 hours, 12 minutes, 37 seconds - (Max: 7 hours, 12 minutes, 37 seconds Min:7 hours, 12 minutes, 37 seconds)

==============================================================================
Found 2 keys containing strings, like:
a96563681, a96026184

These keys use 20.0% of the total sampled memory (10 bytes)
None of these keys expire
Average last accessed time: 4 hours, 28 minutes, 5 seconds - (Max: 4 hours, 37 minutes, 1 seconds Min:4 hours, 19 minutes, 9 seconds)

==============================================================================
Found 7 keys containing strings, like:
b74872995, b73310193, b64434574, b64319909, b71458564, b66514883, b71289887

These keys use 70.0% of the total sampled memory (35 bytes)
None of these keys expire
Average last accessed time: 7 hours, 31 minutes, 56 seconds - (Max: 10 hours, 31 minutes, 19 seconds Min:4 hours, 22 minutes, 49 seconds)

==============================================================================
Summary

---------------------------------------------------+--------------+-------------------+---------------------------------------------------
Key                                                | Memory Usage | Expiry Proportion | Last Access Time                                 
---------------------------------------------------+--------------+-------------------+---------------------------------------------------
b74872995                                          | 70.0%        | 0.0%              | 4 hours, 22 minutes, 49 seconds                  
a96563681                                          | 20.0%        | 0.0%              | 4 hours, 19 minutes, 9 seconds                   
c69820248                                          | 10.0%        | 0.0%              | 7 hours, 12 minutes, 37 seconds                  
---------------------------------------------------+--------------+-------------------+---------------------------------------------------
[root@bjm6-24-20 redis-audit-master]#

 

####redis-sampler####
統計key型別分佈,各個型別的資料情況
1、安裝:https://github.com/antirez/redis-sampler下載
2、使用:./redis-sampler.rb 127.0.0.1 6379 0 6
  (redis-sampler.rb
3、需要auth的可以在連線redis的時候配置password

示例輸出:
[root@db redis-sampler-master]# ./redis-sampler.rb 127.0.0.1 6379 0 6
Sampling 127.0.0.1:6379 DB:0 with 6 RANDOMKEYS

TYPES
=====
 string: 5 (83.33%)       list: 1 (16.67%)       

EXPIRES
=======
 unknown: 6 (100.00%)   
 Average: 0.00 Standard Deviation: 0.00
 Min: 0 Max: 0

Powers of two distribution: (NOTE <= p means: p/2 < x <= p)

Note: 'unknown' expire means keys with no expire

STRINGS, SIZE OF VALUES
=======================
 2: 3 (60.00%)            4: 2 (40.00%)          
 Average: 2.80 Standard Deviation: 0.98
 Min: 2 Max: 4

Powers of two distribution: (NOTE <= p means: p/2 < x <= p)
 <= 2: 3 (60.00%)         <= 4: 2 (40.00%)       

LISTS, NUMBER OF ELEMENTS
=========================
 3: 1 (100.00%)         
 Average: 3.00 Standard Deviation: 0.00
 Min: 3 Max: 3

Powers of two distribution: (NOTE <= p means: p/2 < x <= p)
 <= 4: 1 (100.00%)      

LISTS, SIZE OF ELEMENTS
=======================
 1: 1 (100.00%)         
 Average: 1.00 Standard Deviation: 0.00
 Min: 1 Max: 1

Powers of two distribution: (NOTE <= p means: p/2 < x <= p)
 <= 1: 1 (100.00%)      


####redis-stat####
實時輸出redis當前狀態:cpu,記憶體,命中率,key總數等資訊,並且還包括了web展示功能
1、安裝:下載jar包;或者ruby包(ruby好多依賴包,還是jar比較好除錯)
  https://github.com/junegunn/redis-stat
2、使用:
java -jar redis-stat-0.4.12.jar 127.0.0.1:40000 --server=8380

usage: redis-stat [HOST[:PORT] ...] [INTERVAL [COUNT]]

    -a, --auth=PASSWORD              Password
    -v, --verbose                    Show more info
        --style=STYLE                Output style: unicode|ascii
        --no-color                   Suppress ANSI color codes
        --csv[=CSV_FILE]             Print or save the result in CSV
        --es=ELASTICSEARCH_URL       Send results to ElasticSearch: [http://]HOST[:PORT][/INDEX]

        --server[=PORT]              Launch redis-stat web server (default port: 63790)
        --daemon                     Daemonize redis-stat. Must be used with --server option.

        --version                    Show version
        --help                       Show this message


示例輸出,也可以訪問http://xxx.xxx.xxx.xxx:8380檢視

[root@bjm6-24-20 soft]# java -jar redis-stat-0.4.12.jar  127.0.0.1:40000  --server=8380
┌────────────────────────┬─────────────────┐
│                        │ 127.0.0.1:40000 │
├────────────────────────┼─────────────────┤
│          redis_version │           3.0.2 │
│             redis_mode │      standalone │
│             process_id │           34392 │
│      uptime_in_seconds │          262423 │
│         uptime_in_days │               3 │
│                   role │          master │
│       connected_slaves │               0 │
│            aof_enabled │               0 │
│ rdb_bgsave_in_progress │               1 │
│     rdb_last_save_time │      1435485239 │
└────────────────────────┴─────────────────┘

┌────────┬──┬──┬──┬───┬──────┬──────┬─────┬─────┬─────┬─────┬──────┬─────┬─────┬─

────┐
     time us sy cl bcl    mem    rss  keys cmd/s exp/s evt/s hit%/s hit/s mis/s aofcs
├────────┼──┼──┼──┼───┼──────┼──────┼─────┼─────┼─────┼─────┼──────┼─────┼─────┼─

────┤
 17:55:08  -  -  2   0 4.66GB 5.76GB 39.9M     -     -     -      -     -     -    0B
 17:55:10  4 10  1   0 4.66GB 5.76GB 39.9M 1.40k     0   322   33.3   723 1.45k    0B
 17:55:12  4 10  1   0 4.66GB 5.76GB 39.9M 1.38k     0   475   33.3   711 1.42k    0B
 17:55:14  5 10  1   0 4.66GB 5.76GB 39.9M 1.37k     0   477   33.3   709 1.42k    0B
 17:55:16  3 11  1   0 4.66GB 5.76GB 39.9M 1.35k     0   463   33.3   697 1.39k    0B
 17:55:18  5  9  2   0 4.66GB 5.76GB 39.9M 1.35k     0   464   33.3   698 1.40k    0B
 17:55:20  5 10  1   0 4.66GB 5.76GB 39.9M 1.33k     0   456   33.3   689 1.38k    0B
 17:55:22  4  9  1   0 4.66GB 5.76GB 39.9M 1.31k     0   432   33.3   703 1.41k    0B
 17:55:24  4 10  1   0 4.66GB 5.76GB 39.9M 1.30k     0   424   33.3   703 1.41k    0B
 17:55:26  5 10  1   0 4.66GB 5.76GB 39.9M 1.30k     0   429   33.3   701 1.40k    0B
└────────┴──┴──┴──┴───┴──────┴──────┴─────┴─────┴─────┴─────┴──────┴─────┴─────┴─

────┘

####redis-rdb-tools#######
功能:rdb解析,生成json,protocol,diff等解析格式
1、下載:https://github.com/sripathikrishnan/redis-rdb-tools
2、確保python環境(需要安裝setuptools, redis-py)
3、python setup.py install
多出幾個命令:rdb redis-profiler   redis-memory-for-key
使用:
rdb:
1)rdb --command json dump.rdb:解析成json串
2)rdb --command json --key "user.*" dump.rdb:解析rdb檔案,找出包含user關鍵字的key
  雷同:rdb --command json --db 2 --type list --key "a.*" dump.rdb
3)rdb -c memory /var/redis/6379/dump.rdb > memory.csv:到csv檔案
4)對比:
rdb --command diff  dump1.rdb | sort > dump1.txt
rdb --command diff  dump2.rdb | sort > dump2.txt
kdiff3 dump1.txt dump2.txt

5)解析成redis協議的流:
 rdb --command protocol /var/redis/6379/dump.rdb
 rdb  -c protocol dump.rdb  | redis-cli -d 1  -p 40002 --pipe (匯入到指定資料庫)

 

redis-memory-for-key
  找出一個key佔用記憶體的大小:
  redis-memory-for-key -s 127.0.0.1 -p 6379 -a password person

redis-profiler:解析成html格式:
  redis-profiler  dump40000.rdb  >1.html
 

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/20625855/viewspace-1715597/,如需轉載,請註明出處,否則將追究法律責任。

相關文章