开源项目 分享一个 Ruby 写的小工具 - RedisScanner

vincent · 2016年07月10日 · 最后由 linuxgit 回复于 2016年07月10日 · 3293 次阅读

RedisScanner 是我用 Ruby 写的一个小工具,用于了解 redis 中 key 的分布情况,可以根据 pattern 统计 key 的数量,类型和长度信息。

薄荷的系统中大量使用 redis,有做存储也有做缓存,但是其实对这些 redis 实例中的 key 分布情况缺乏了解。在 MySQL 或 Postgress 中可以通过 schema 了解数据库概况,在 redis 中只能通过 info 命令了解有限情况,没法简便获取 redis 实际 schema 信息,RedisScanner 就是用来解决这个问题的。

它主要有以下特性:

  1. 通过 redis scan 命令进行 key 扫描,避免 keys 命令阻塞实例;
  2. 通过 patten 方式分组统计 key 个数;
  3. 根据 key 的 type 统计更详细的信息,比如 list,hash 和 sorted set 的 size 信息。

安装: gem install redis_scanner

运行: redis_scanner

默认扫描本机的 redis 实例,输出如下所示:

+------------------------------------+-------+
| Key                                | Count |
+------------------------------------+-------+
| demo:user:<id>:counter             | 10000 |
| u:<uuid>:pf                        |    52 |
| sidekiq_demo:stat:failed:<date>    |     4 |
| sidekiq_demo:stat:processed:<date> |     4 |
| _sp_one:queue:default              |     1 |
| bh:queues                          |     1 |
...
+------------------------------------+-------+

通过增加 -d 参数获取更丰富的信息:

redis_scanner -d -l 10

输出如下:

+------------------------------------+--------+-------+--------+---------+
| Key                                | Type   | Count | Size   | AvgSize |
+------------------------------------+--------+-------+--------+---------+
| demo:user:<id>:counter             | string | 10000 | 927510 |   92.75 |
| u:<uuid>:pf                        | hash   |    52 |    108 |    2.08 |
| sidekiq_demo:stat:failed:<date>    | string |     4 |      5 |    1.25 |
| sidekiq_demo:stat:processed:<date> | string |     4 |      6 |     1.5 |
| _sp_one:queue:default              | list   |     1 |      1 |     1.0 |
| bh:queues                          | set    |     1 |      1 |     1.0 |
| bh:retry                           | zset   |     1 |      1 |     1.0 |
| bh:stat:failed                     | string |     1 |      1 |     1.0 |
| bh:stat:processed                  | string |     1 |      1 |     1.0 |
| bus_app:app_two                    | hash   |     1 |      1 |     1.0 |
+------------------------------------+--------+-------+--------+---------+

redis_scanner 完整参数如下所示:

redis_scanner --help

Usage: redis_scanner [options]
    -f, --file FILE                  Output to file
    -m, --match MATCH                Only scan the pattern
    -l, --limit LIMIT                Only show top <limit> keys
    -d, --detail                     Show detail info(type & size)
    -t, --format FORMAT              Format(simple or talbe. default is table)
    -h, --host HOST                  Server hostname (default: 127.0.0.1)
    -p, --port PORT                  Server port (default: 6379)
    -s, --socket SOCKET              Server socket (overrides hostname and port)
    -a, --password PASSWORD          Password to use when connecting to the server.
    -n, --db DB                      Database number

项目代码在 https://github.com/xiewenwei/redis_scanner ,欢迎使用或者提交 issue.

不错的工具。想了解大家线上是否用到了 redis 集群?用的什么架构。类似 Redis cluster,Twemproxy 或 codis。

需要 登录 后方可回复, 如果你还没有账号请 注册新账号